RechargeController.php 5.8 KB

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