PaySuccess.php 5.0 KB

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