RechargeController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\admin\controller\notify;
  3. use app\admin\service\notify\RechargeService;
  4. use app\model\Member;
  5. use app\model\MemberAccount;
  6. use app\model\MemberAccountList;
  7. use app\model\Order;
  8. use app\model\PayDetail;
  9. use support\Db;
  10. use support\exception\BusinessException;
  11. use support\Log;
  12. use support\Request;
  13. use Tinywan\Jwt\JwtToken;
  14. use Webman\Event\Event;
  15. class RechargeController
  16. {
  17. /**
  18. * @Desc 计划任务
  19. * @Author Gorden
  20. * @Date 2024/5/29 11:31
  21. *
  22. * @return void
  23. */
  24. // public function disposePaySuccess()
  25. // {
  26. //// $unix = strtotime(date("Y-m-d H:i",strtotime("-1 minute")).':00');
  27. // $payDetails = PayDetail::where('pay_category', 'RECHARGE')
  28. // ->where('pay_status', 'SUCCESS')
  29. // ->where(function ($query) {
  30. // $query->whereRaw("JSON_EXTRACT(`pay_extend_json`, '$.notify') IS NULL")
  31. // ->orWhereJsonDoesntContain('pay_extend_json->notify', 'success');
  32. // })
  33. // ->get()
  34. // ->toArray();
  35. // if (!$payDetails) {
  36. // return;
  37. // }
  38. //
  39. // foreach ($payDetails as $payDetail) {
  40. // $this->paySuccess($payDetail['pay_id']);
  41. // }
  42. // }
  43. /**
  44. * @Desc
  45. * @Author Gorden
  46. * @Date 2024/8/19 13:33
  47. *
  48. * @param Request $request
  49. * @return \support\Response
  50. */
  51. public function paySuccess(Request $request)
  52. {
  53. $data = $request->post();
  54. if (is_json($data)) {
  55. $data = json_decode($data, true);
  56. }
  57. _syslog("APP充值", "APP充值回调开始", $data, $data, 1001);
  58. if (!$data['pay_id']) {
  59. return json_fail('参数异常');
  60. }
  61. try {
  62. $result = $this->disposePaySuccess($data['pay_id']);
  63. _syslog("APP充值", "APP处理完成", $data, $data, 1001);
  64. return $result;
  65. } catch (BusinessException $e) {
  66. _syslog("APP充值", "APP充值失败", $data, $data, 1001);
  67. return json_fail("处理失败");
  68. } catch (\Exception $e) {
  69. _syslog("APP充值", "APP充值失败", $data, $data, 1001);
  70. return json_fail("处理失败");
  71. }
  72. // public function paySuccess($id)
  73. // {
  74. // if (!$id) {
  75. // return json_fail('参数异常');
  76. // }
  77. //
  78. // $payDetail = PayDetail::find($id);
  79. // if ($payDetail->pay_status != 'SUCCESS') {
  80. // return json_fail("支付状态异常");
  81. // }
  82. }
  83. /**
  84. * @param $payId
  85. * @return \support\Response
  86. * @throws BusinessException
  87. */
  88. public function disposePaySuccess($payId)
  89. {
  90. Log::info("开始充值", ['pay_id' => $payId]);
  91. $payDetail = PayDetail::find($payId);
  92. if (!$payDetail || $payDetail->pay_status != 'SUCCESS') {
  93. throw new BusinessException("支付状态异常");
  94. // return json_fail("支付状态异常");
  95. }
  96. $extendJson = [];
  97. if (!empty($payDetail->pay_extend_json)) {
  98. $extendJson = json_decode($payDetail->pay_extend_json, true);
  99. if (isset($extendJson['notify']) && $extendJson['notify'] == 'success') {
  100. return json_success('处理完成');
  101. }
  102. }
  103. try {
  104. $updateUserId = JwtToken::getCurrentId();
  105. }catch (\Exception $e){
  106. $updateUserId = 1001;
  107. }
  108. Db::beginTransaction();
  109. try {
  110. $extendJson['notify'] = 'success';
  111. $payDetail->pay_extend_json = json_encode($extendJson);
  112. $payDetail->save();
  113. // 赠送比例
  114. $objectJson = json_decode($payDetail->join_pay_object_json, true);
  115. $goodsAttributeJson = [];
  116. $couponParams = [];
  117. if (isset($objectJson['order_id'])) {
  118. $couponParams['order_id'] = $objectJson['order_id'];
  119. // 对应订单设置已完成
  120. Order::where('order_groupby', $payDetail->join_pay_order_id)->update(['order_status_system' => 'DONE', 'order_is_complete' => 'Y', 'order_status_storage' => 'DONE']);
  121. $goodsAttributeJson = RechargeService::getGoodsAttributeJson($objectJson['order_id']);
  122. }
  123. // 赠送金额累加到 账户表 member_account_added
  124. $memberAccount = MemberAccount::where('join_account_member_id', $payDetail->join_pay_member_id)
  125. ->where('member_account_classify', 'CASH')
  126. ->first();
  127. $addedNbr = !empty($goodsAttributeJson['added']) && !empty($goodsAttributeJson['added']['nbr']) ? $goodsAttributeJson['added']['nbr'] : 0;
  128. $payAmount = floatval($payDetail->pay_amount);
  129. $addedAmount = round($payAmount * $addedNbr, 2);
  130. $added = $memberAccount->member_account_added + $addedAmount;
  131. $income = $memberAccount->member_account_income + $payAmount;
  132. // 保留原数据
  133. RechargeService::saveOriginData($memberAccount, $payAmount, $addedNbr, $addedAmount);
  134. $memberAccount->member_account_added = $added;
  135. $memberAccount->member_account_income = $income;
  136. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $payAmount;
  137. $memberAccount->member_account_update_user_id = $updateUserId ?? 1001;
  138. $memberAccount->member_account_updatetimes = time();
  139. $memberAccount->save();
  140. if (!empty($goodsAttributeJson)){
  141. // 根据最新的数据,更新用户等级
  142. RechargeService::disposeRole($payDetail->join_pay_member_id, $payDetail->pay_amount);
  143. // 发券
  144. RechargeService::disposeRoleCoupon($goodsAttributeJson, $couponParams, $payDetail->join_pay_member_id);
  145. }
  146. // 计算充值提成
  147. if (!empty($payDetail->join_pay_object_json)) {
  148. $payObjectJson = json_decode($payDetail->join_pay_object_json, true);
  149. if (isset($payObjectJson['order_id'])) {
  150. // 没有提成过
  151. if (!MemberAccountList::whereJsonContains('member_account_list_json->order_id', $payObjectJson['order_id'])->exists()) {
  152. // 上级提成
  153. Event::dispatch('commission.order', ['orderId' => $payObjectJson['order_id'], 'member_account_list_category' => '充值']);
  154. }
  155. // 入收支明细表
  156. $params['orderId'] = $payObjectJson['order_id'];
  157. $params['inout_category'] = '会员充值';
  158. Event::dispatch('statistics.inout.in', $params);
  159. }
  160. }
  161. Log::info("充值成功", ['pay_id' => $payId]);
  162. Db::commit();
  163. return json_success('success');
  164. } catch (BusinessException $e) {
  165. Db::rollBack();
  166. Log::error("充值失败", ['msg' => $e->getMessage()]);
  167. throw new BusinessException("处理失败");
  168. } catch (\Exception $e) {
  169. Db::rollBack();
  170. Log::error("充值失败", ['msg' => $e->getMessage()]);
  171. throw new BusinessException("处理失败");
  172. }
  173. }
  174. }