控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Order.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 订单模型
  13. // +----------------------------------------------------------------------
  14. namespace app\cms\model;
  15. use app\member\model\Member;
  16. use app\member\service\User;
  17. use think\Db;
  18. use think\Exception;
  19. use think\Model;
  20. class Order extends Model
  21. {
  22. protected $name = "cms_order";
  23. // 自动写入时间戳
  24. protected $autoWriteTimestamp = true;
  25. //提交订单
  26. public static function submitOrder($catid, $id, $pay_type = 'wechat', $method = 'web', $notifyurl = '', $returnurl = '')
  27. {
  28. //获取栏目数据
  29. $category = getCategory($catid);
  30. if (empty($category)) {
  31. throw new Exception('栏目不存在!');
  32. }
  33. $modelid = $category['modelid'];
  34. $modelInfo = cache('Model')[$modelid];
  35. if (empty($modelInfo)) {
  36. throw new Exception('模型不存在!');
  37. }
  38. $info = (new Cms)->getContent($modelid, ['catid' => $catid, 'id' => $id], false, '*');
  39. if (!$info) {
  40. throw new Exception('支付内容不存在或未审核!');
  41. }
  42. $paytype = $info['paytype'] ?? 1; // 1钱 2积分
  43. $readpoint = $info['readpoint'] ?? 0; //金额
  44. if ($paytype == 2 && $pay_type != 'balance') {
  45. //TODO 暂不支持积分使用微信支付宝
  46. throw new Exception('积分付费只能选择余额支付!');
  47. }
  48. $order = self::where(['catid' => $catid, 'contentid' => $id])->order('id', 'desc')->find();
  49. if ($order && $order['status'] == 'succ') {
  50. throw new Exception('订单已支付');
  51. }
  52. $auth = User::instance();
  53. $trade_sn = date("Ymdhis") . sprintf("%08d", $auth->id) . mt_rand(1000, 9999);
  54. if (!$order) {
  55. //无订单创建
  56. $data = [
  57. 'trade_sn' => $trade_sn,
  58. 'catid' => $catid,
  59. 'contentid' => $id,
  60. 'user_id' => $auth->id,
  61. 'title' => "付费阅读:" . substr($info['title'], 0, 60),
  62. 'total_price' => $readpoint,
  63. 'pay_price' => 0,
  64. 'pay_type' => $pay_type,
  65. 'method' => $method,
  66. 'ip' => request()->ip(),
  67. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  68. 'status' => 'unpay',
  69. 'remark' => '',
  70. ];
  71. $order = self::create($data);
  72. } else {
  73. //已存在未支付订单更新
  74. $order->trade_sn = $trade_sn;
  75. $order->total_price = $readpoint;
  76. $order->pay_type = $pay_type;
  77. $order->method = $method;
  78. $order->save();
  79. }
  80. //使用余额支付
  81. if ($pay_type == 'balance') {
  82. $paytype = $paytype == 1 ? "amount" : "point";
  83. if ($auth->{$paytype} < $readpoint) {
  84. throw new Exception(($paytype == "amount" ? "余额" : "积分") . '不足,请先充值!');
  85. }
  86. Db::startTrans();
  87. try {
  88. Member::{$paytype}(-$readpoint, $auth->id, '购买付费文档:' . $info['title']);
  89. self::settle($order->trade_sn, $readpoint);
  90. Db::commit();
  91. } catch (Exception $e) {
  92. Db::rollback();
  93. throw new Exception($e->getMessage());
  94. }
  95. return true;
  96. }
  97. $info = get_addon_info('pay');
  98. if ($info && $info['status'] > 0) {
  99. $notifyurl = $notifyurl ? $notifyurl : request()->root(true) . '/cms/order/epay/type/notify/pay_type/' . $pay_type;
  100. $returnurl = $returnurl ? $returnurl : request()->root(true) . '/cms/order/epay/type/return/pay_type/' . $pay_type . '/trade_sn/' . $order->trade_sn;
  101. $total_price = sprintf("%.2f", $order->total_price);
  102. try {
  103. //扫码支付后跳转链接
  104. \think\facade\Session::set('jumpUrl', buildContentUrl($catid, $id, '', true, true));
  105. //调用支付插件支付接口
  106. \addons\pay\library\Service::submitOrder($total_price, $order->trade_sn, $pay_type, "支付{$total_price}元", $notifyurl, $returnurl, $method);
  107. } catch (Exception $e) {
  108. throw new Exception($e->getMessage());
  109. }
  110. } else {
  111. throw new Exception("请先在后台安装支付插件");
  112. }
  113. }
  114. //订单是否支付
  115. public static function check_payment($info)
  116. {
  117. $auth = User::instance();
  118. //判断前台添加,并且作者是自己才免费
  119. if ($info['sysadd'] == 0 && $auth->id && $auth->id == $info['uid']) {
  120. return true;
  121. }
  122. return self::where([
  123. 'catid' => $info['catid'],
  124. 'contentid' => $info['id'],
  125. 'status' => 'succ',
  126. ])->find() ? true : false;
  127. }
  128. //订单结算
  129. public static function settle($trade_sn, $pay_price)
  130. {
  131. $order = self::getByTradeSn($trade_sn);
  132. if (!$order) {
  133. return false;
  134. }
  135. if ($order['status'] != 'paid') {
  136. if ($pay_price != $order->total_price) {
  137. throw new Exception("订单金额异常");
  138. return false;
  139. }
  140. try {
  141. $order->pay_time = time();
  142. $order->pay_price = $pay_price;
  143. $order->status = 'succ';
  144. $order->save();
  145. } catch (Exception $e) {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. }