RechargeController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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) {
  93. Log::error("充值记录异常");
  94. return json_success('支付状态异常');
  95. // return json_fail("支付状态异常");
  96. }
  97. $extendJson = [];
  98. if (!empty($payDetail->pay_extend_json)) {
  99. $extendJson = json_decode($payDetail->pay_extend_json, true);
  100. if (isset($extendJson['notify']) && $extendJson['notify'] == 'success') {
  101. return json_success('处理完成');
  102. }
  103. }
  104. try {
  105. $updateUserId = JwtToken::getCurrentId();
  106. } catch (\Exception $e) {
  107. $updateUserId = 1001;
  108. }
  109. Db::beginTransaction();
  110. try {
  111. $extendJson['notify'] = 'success';
  112. $payDetail->pay_extend_json = json_encode($extendJson);
  113. $payDetail->save();
  114. // 赠送比例
  115. $objectJson = json_decode($payDetail->join_pay_object_json, true);
  116. $goodsAttributeJson = [];
  117. $couponParams = [];
  118. if (isset($objectJson['order_id'])) {
  119. $couponParams['order_id'] = $objectJson['order_id'];
  120. // 对应订单设置已完成
  121. Order::where('order_groupby', $payDetail->join_pay_order_id)->update(['order_status_system' => 'DONE', 'order_is_complete' => 'Y', 'order_status_storage' => 'DONE']);
  122. $goodsAttributeJson = RechargeService::getGoodsAttributeJson($objectJson['order_id']);
  123. }
  124. // 赠送金额累加到 账户表 member_account_added
  125. $memberAccount = MemberAccount::where('join_account_member_id', $payDetail->join_pay_member_id)
  126. ->where('member_account_classify', 'CASH')
  127. ->first();
  128. $addedNbr = !empty($goodsAttributeJson['added']) && !empty($goodsAttributeJson['added']['nbr']) ? $goodsAttributeJson['added']['nbr'] : 0;
  129. $payAmount = floatval($payDetail->pay_amount);
  130. $addedAmount = round($payAmount * $addedNbr, 2);
  131. $added = $memberAccount->member_account_added + $addedAmount;
  132. $income = $memberAccount->member_account_income + $payAmount;
  133. // 保留原数据
  134. RechargeService::saveOriginData($memberAccount, $payAmount, $addedNbr, $addedAmount);
  135. $memberAccount->member_account_added = $added;
  136. $memberAccount->member_account_income = $income;
  137. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $payAmount;
  138. $memberAccount->member_account_update_user_id = $updateUserId ?? 1001;
  139. $memberAccount->member_account_updatetimes = time();
  140. $memberAccount->save();
  141. if (!empty($goodsAttributeJson)) {
  142. // 根据最新的数据,更新用户等级
  143. RechargeService::disposeRole($payDetail->join_pay_member_id, $payDetail->pay_amount);
  144. // 发券
  145. RechargeService::disposeRoleCoupon($goodsAttributeJson, $couponParams, $payDetail->join_pay_member_id);
  146. }
  147. // 计算充值提成
  148. if (!empty($payDetail->join_pay_object_json)) {
  149. $payObjectJson = json_decode($payDetail->join_pay_object_json, true);
  150. if (isset($payObjectJson['order_id'])) {
  151. // 没有提成过
  152. if (!MemberAccountList::whereJsonContains('member_account_list_json->order_id', $payObjectJson['order_id'])->exists()) {
  153. // 上级提成
  154. Event::dispatch('commission.order', ['orderId' => $payObjectJson['order_id'], 'member_account_list_category' => '充值']);
  155. }
  156. // 入收支明细表
  157. $params['orderId'] = $payObjectJson['order_id'];
  158. $params['inout_category'] = '会员充值';
  159. $params['pay_id'] = $payId;
  160. Event::dispatch('statistics.inout.in', $params);
  161. }
  162. }
  163. Log::info("充值成功", ['pay_id' => $payId]);
  164. Db::commit();
  165. return json_success('success');
  166. } catch (BusinessException $e) {
  167. Db::rollBack();
  168. Log::error("充值失败", ['msg' => $e->getMessage()]);
  169. throw new BusinessException("处理失败");
  170. } catch (\Exception $e) {
  171. Db::rollBack();
  172. Log::error("充值失败", ['msg' => $e->getMessage()]);
  173. throw new BusinessException("处理失败");
  174. }
  175. }
  176. }