PaySuccess.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\api\service\sharing\order;
  3. use think\Hook;
  4. use app\api\service\Basics;
  5. use app\api\model\User as UserModel;
  6. use app\api\model\sharing\Order as OrderModel;
  7. use app\api\model\sharing\Goods as GoodsModel;
  8. use app\api\model\sharing\Active as ActiveModel;
  9. use app\api\model\user\BalanceLog as BalanceLogModel;
  10. use app\api\model\WxappPrepayId as WxappPrepayIdModel;
  11. use app\common\enum\user\balanceLog\Scene as SceneEnum;
  12. use app\common\enum\order\PayType as PayTypeEnum;
  13. use app\common\enum\OrderType as OrderTypeEnum;
  14. /**
  15. * 订单支付成功服务类
  16. * Class PaySuccess
  17. * @package app\api\service\sharing\order
  18. */
  19. class PaySuccess extends Basics
  20. {
  21. // 订单模型
  22. public $model;
  23. // 当前用户信息
  24. private $user;
  25. /**
  26. * 构造函数
  27. * PaySuccess constructor.
  28. * @param $orderNo
  29. * @throws \think\exception\DbException
  30. */
  31. public function __construct($orderNo)
  32. {
  33. // 实例化订单模型
  34. $this->model = OrderModel::getPayDetail($orderNo);
  35. if (!empty($this->model)) {
  36. $this->wxappId = $this->model['wxapp_id'];
  37. }
  38. // 获取用户信息
  39. $this->user = UserModel::detail($this->model['user_id']);
  40. }
  41. /**
  42. * 获取订单详情
  43. * @return OrderModel|null
  44. */
  45. public function getOrderInfo()
  46. {
  47. return $this->model;
  48. }
  49. /**
  50. * 订单支付成功业务处理
  51. * @param $payType
  52. * @param array $payData
  53. * @return bool
  54. */
  55. public function onPaySuccess($payType, $payData = [])
  56. {
  57. if (empty($this->model)) {
  58. $this->error = '未找到该订单信息';
  59. return false;
  60. }
  61. // 更新付款状态
  62. $status = $this->updatePayStatus($payType, $payData);
  63. // 订单支付成功行为
  64. if ($status == true) {
  65. Hook::listen('order_pay_success', $this->model, OrderTypeEnum::SHARING);
  66. }
  67. return $status;
  68. }
  69. /**
  70. * 更新付款状态
  71. * @param $payType
  72. * @param $payData
  73. * @return bool
  74. */
  75. private function updatePayStatus($payType, $payData)
  76. {
  77. // 验证余额支付时用户余额是否满足
  78. if ($payType == PayTypeEnum::BALANCE) {
  79. if ($this->user['balance'] < $this->model['pay_price']) {
  80. $this->error = '用户余额不足,无法使用余额支付';
  81. return false;
  82. }
  83. }
  84. $this->model->transaction(function () use ($payType, $payData) {
  85. // 更新商品库存、销量
  86. (new GoodsModel)->updateStockSales($this->model['goods']);
  87. // 更新拼单记录
  88. $this->saveSharingActive($this->model['goods'][0]);
  89. // 整理订单信息
  90. $order = ['pay_type' => $payType, 'pay_status' => 20, 'pay_time' => time()];
  91. if ($payType == PayTypeEnum::WECHAT) {
  92. $order['transaction_id'] = $payData['transaction_id'];
  93. }
  94. // 更新订单状态
  95. $this->model->save($order);
  96. // 累积用户总消费金额
  97. $this->user->setIncPayMoney($this->model['pay_price']);
  98. // 余额支付
  99. if ($payType == PayTypeEnum::BALANCE) {
  100. // 更新用户余额
  101. $this->user->setDec('balance', $this->model['pay_price']);
  102. BalanceLogModel::add(SceneEnum::CONSUME, [
  103. 'user_id' => $this->user['user_id'],
  104. 'money' => -$this->model['pay_price'],
  105. ], ['order_no' => $this->model['order_no']]);
  106. }
  107. // 微信支付
  108. if ($payType == PayTypeEnum::WECHAT) {
  109. // 更新prepay_id记录
  110. WxappPrepayIdModel::updatePayStatus($this->model['order_id'], OrderTypeEnum::SHARING);
  111. }
  112. });
  113. return true;
  114. }
  115. /**
  116. * 更新拼单记录
  117. * @param $goods
  118. * @return bool
  119. * @throws \app\common\exception\BaseException
  120. * @throws \think\exception\DbException
  121. */
  122. private function saveSharingActive($goods)
  123. {
  124. // 新增/更新拼单记录
  125. if ($this->model['order_type']['value'] != 20) {
  126. return false;
  127. }
  128. // 拼单模型
  129. $ActiveModel = new ActiveModel;
  130. // 参与他人的拼单, 更新拼单记录
  131. if ($this->model['active_id'] > 0) {
  132. $ActiveModel = $ActiveModel::detail($this->model['active_id']);
  133. return $ActiveModel->onUpdate($this->model['user_id'], $this->model['order_id']);
  134. }
  135. // 自己发起的拼单, 新增拼单记录
  136. $ActiveModel->onCreate($this->model['user_id'], $this->model['order_id'], $goods);
  137. // 记录拼单id
  138. $this->model['active_id'] = $ActiveModel['active_id'];
  139. return true;
  140. }
  141. }