PaySuccess.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\order;
  13. use think\facade\Event;
  14. use app\common\service\BaseService;
  15. use app\api\model\User as UserModel;
  16. use app\api\model\Order as OrderModel;
  17. use app\api\model\user\BalanceLog as BalanceLogModel;
  18. use app\common\service\goods\source\Factory as StockFactory;
  19. use app\common\enum\OrderType as OrderTypeEnum;
  20. use app\common\enum\order\PayStatus as PayStatusEnum;
  21. use app\common\enum\order\PayType as OrderPayTypeEnum;
  22. use app\common\enum\user\balanceLog\Scene as SceneEnum;
  23. /**
  24. * 订单支付成功服务类
  25. * Class PaySuccess
  26. * @package app\api\service\order
  27. */
  28. class PaySuccess extends BaseService
  29. {
  30. // 订单模型
  31. public $model;
  32. // 当前用户信息
  33. private $user;
  34. /**
  35. * 构造函数
  36. * PaySuccess constructor.
  37. * @param $orderNo
  38. */
  39. public function __construct($orderNo)
  40. {
  41. parent::__construct();
  42. // 实例化订单模型
  43. $this->model = OrderModel::getPayDetail($orderNo);
  44. // 获取用户信息
  45. $this->user = UserModel::detail($this->model['user_id']);
  46. }
  47. /**
  48. * 获取订单详情
  49. * @return OrderModel|null
  50. */
  51. public function getOrderInfo()
  52. {
  53. return $this->model;
  54. }
  55. /**
  56. * 订单支付成功业务处理
  57. * @param $payType
  58. * @param array $payData
  59. * @return bool
  60. */
  61. public function onPaySuccess($payType, $payData = [])
  62. {
  63. if (empty($this->model)) {
  64. $this->error = '未找到该订单信息';
  65. return false;
  66. }
  67. // 更新付款状态
  68. $status = $this->updatePayStatus($payType, $payData);
  69. // 订单支付成功事件
  70. if ($status == true) {
  71. Event::trigger('OrderPaySuccess', ['order' => $this->model, 'orderType' => OrderTypeEnum::ORDER]);
  72. }
  73. return $status;
  74. }
  75. /**
  76. * 更新付款状态
  77. * @param $payType
  78. * @param array $payData
  79. * @return bool
  80. */
  81. private function updatePayStatus($payType, $payData = [])
  82. {
  83. // 验证余额支付时用户余额是否满足
  84. if ($payType == OrderPayTypeEnum::BALANCE) {
  85. if ($this->user['balance'] < $this->model['pay_price']) {
  86. $this->error = '用户余额不足,无法使用余额支付';
  87. return false;
  88. }
  89. }
  90. // 事务处理
  91. $this->model->transaction(function () use ($payType, $payData) {
  92. // 更新订单状态
  93. $this->updateOrderInfo($payType, $payData);
  94. // 累积用户总消费金额
  95. UserModel::setIncPayMoney($this->user['user_id'], (float)$this->model['pay_price']);
  96. // 记录订单支付信息
  97. $this->updatePayInfo($payType);
  98. });
  99. return true;
  100. }
  101. /**
  102. * 更新订单记录
  103. * @param int $payType
  104. * @param array $payData
  105. * @return false|int
  106. * @throws \Exception
  107. */
  108. private function updateOrderInfo(int $payType, array $payData)
  109. {
  110. // 更新商品库存、销量
  111. StockFactory::getFactory($this->model['order_source'])->updateStockSales($this->model['goods']);
  112. // 整理订单信息
  113. $order = [
  114. 'pay_type' => $payType,
  115. 'pay_status' => PayStatusEnum::SUCCESS,
  116. 'pay_time' => time()
  117. ];
  118. if ($payType == OrderPayTypeEnum::WECHAT) {
  119. $order['transaction_id'] = $payData['transaction_id'];
  120. }
  121. // 更新订单状态
  122. return $this->model->save($order);
  123. }
  124. /**
  125. * 记录订单支付信息
  126. * @param int $payType
  127. */
  128. private function updatePayInfo(int $payType)
  129. {
  130. // 余额支付
  131. if ($payType == OrderPayTypeEnum::BALANCE) {
  132. // 更新用户余额
  133. UserModel::setDecBalance((int)$this->user['user_id'], (float)$this->model['pay_price']);
  134. // 新增余额变动记录
  135. BalanceLogModel::add(SceneEnum::CONSUME, [
  136. 'user_id' => (int)$this->user['user_id'],
  137. 'money' => -$this->model['pay_price'],
  138. ], ['order_no' => $this->model['order_no']]);
  139. }
  140. // 微信支付
  141. if ($payType == OrderPayTypeEnum::WECHAT) {
  142. }
  143. }
  144. }