RechargeController.php 6.8 KB

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