123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\cms\model;
-
- use app\member\model\Member;
- use app\member\service\User;
- use think\Db;
- use think\Exception;
- use think\Model;
-
- class Order extends Model
- {
- protected $name = "cms_order";
-
- protected $autoWriteTimestamp = true;
-
-
- public static function submitOrder($catid, $id, $pay_type = 'wechat', $method = 'web', $notifyurl = '', $returnurl = '')
- {
-
- $category = getCategory($catid);
- if (empty($category)) {
- throw new Exception('栏目不存在!');
- }
- $modelid = $category['modelid'];
- $modelInfo = cache('Model')[$modelid];
- if (empty($modelInfo)) {
- throw new Exception('模型不存在!');
- }
-
- $info = (new Cms)->getContent($modelid, ['catid' => $catid, 'id' => $id], false, '*');
- if (!$info) {
- throw new Exception('支付内容不存在或未审核!');
- }
- $paytype = $info['paytype'] ?? 1;
- $readpoint = $info['readpoint'] ?? 0;
- if ($paytype == 2 && $pay_type != 'balance') {
-
- throw new Exception('积分付费只能选择余额支付!');
- }
-
- $order = self::where(['catid' => $catid, 'contentid' => $id])->order('id', 'desc')->find();
- if ($order && $order['status'] == 'succ') {
- throw new Exception('订单已支付');
- }
- $auth = User::instance();
- $trade_sn = date("Ymdhis") . sprintf("%08d", $auth->id) . mt_rand(1000, 9999);
-
- if (!$order) {
-
- $data = [
- 'trade_sn' => $trade_sn,
- 'catid' => $catid,
- 'contentid' => $id,
- 'user_id' => $auth->id,
- 'title' => "付费阅读:" . substr($info['title'], 0, 60),
- 'total_price' => $readpoint,
- 'pay_price' => 0,
- 'pay_type' => $pay_type,
- 'method' => $method,
- 'ip' => request()->ip(),
- 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
- 'status' => 'unpay',
- 'remark' => '',
- ];
- $order = self::create($data);
- } else {
-
- $order->trade_sn = $trade_sn;
- $order->total_price = $readpoint;
- $order->pay_type = $pay_type;
- $order->method = $method;
- $order->save();
- }
-
-
- if ($pay_type == 'balance') {
- $paytype = $paytype == 1 ? "amount" : "point";
- if ($auth->{$paytype} < $readpoint) {
- throw new Exception(($paytype == "amount" ? "余额" : "积分") . '不足,请先充值!');
- }
- Db::startTrans();
- try {
- Member::{$paytype}(-$readpoint, $auth->id, '购买付费文档:' . $info['title']);
- self::settle($order->trade_sn, $readpoint);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- throw new Exception($e->getMessage());
- }
- return true;
- }
-
- $info = get_addon_info('pay');
- if ($info && $info['status'] > 0) {
- $notifyurl = $notifyurl ? $notifyurl : request()->root(true) . '/cms/order/epay/type/notify/pay_type/' . $pay_type;
- $returnurl = $returnurl ? $returnurl : request()->root(true) . '/cms/order/epay/type/return/pay_type/' . $pay_type . '/trade_sn/' . $order->trade_sn;
-
- $total_price = sprintf("%.2f", $order->total_price);
- try {
-
- \think\facade\Session::set('jumpUrl', buildContentUrl($catid, $id, '', true, true));
-
- \addons\pay\library\Service::submitOrder($total_price, $order->trade_sn, $pay_type, "支付{$total_price}元", $notifyurl, $returnurl, $method);
- } catch (Exception $e) {
- throw new Exception($e->getMessage());
- }
- } else {
- throw new Exception("请先在后台安装支付插件");
- }
- }
-
-
- public static function check_payment($info)
- {
- $auth = User::instance();
-
- if ($info['sysadd'] == 0 && $auth->id && $auth->id == $info['uid']) {
- return true;
- }
- return self::where([
- 'catid' => $info['catid'],
- 'contentid' => $info['id'],
- 'status' => 'succ',
- ])->find() ? true : false;
- }
-
-
- public static function settle($trade_sn, $pay_price)
- {
- $order = self::getByTradeSn($trade_sn);
- if (!$order) {
- return false;
- }
- if ($order['status'] != 'paid') {
- if ($pay_price != $order->total_price) {
- throw new Exception("订单金额异常");
- return false;
- }
- try {
- $order->pay_time = time();
- $order->pay_price = $pay_price;
- $order->status = 'succ';
- $order->save();
- } catch (Exception $e) {
- return false;
- }
- }
- return true;
- }
- }
|