RechargeController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. {
  86. $payDetail = PayDetail::find($payId);
  87. if (!$payDetail || $payDetail->pay_status != 'SUCCESS') {
  88. throw new BusinessException("支付状态异常");
  89. // return json_fail("支付状态异常");
  90. }
  91. $extendJson = [];
  92. if (!empty($payDetail->pay_extend_json)) {
  93. $extendJson = json_decode($payDetail->pay_extend_json, true);
  94. if (isset($extendJson['notify']) && $extendJson['notify'] == 'success') {
  95. return json_success('处理完成');
  96. }
  97. }
  98. Db::beginTransaction();
  99. try {
  100. $extendJson['notify'] = 'success';
  101. $payDetail->pay_extend_json = json_encode($extendJson);
  102. $payDetail->save();
  103. // 赠送比例
  104. $objectJson = json_decode($payDetail->join_pay_object_json, true);
  105. $addedNbr = 0;
  106. if (isset($objectJson['order_id'])) {
  107. // 对应订单设置已完成
  108. Order::where('order_groupby', $payDetail->join_pay_order_id)->update(['order_status_system' => 'DONE', 'order_is_complete' => 'Y', 'order_status_storage' => 'DONE']);
  109. $addedNbr = RechargeService::disposeOrder($objectJson['order_id']);
  110. // throw new BusinessException("支付数据异常");
  111. // return json_fail("");
  112. }
  113. // 赠送金额累加到 账户表 member_account_added
  114. $memberAccount = MemberAccount::where('join_account_member_id', $payDetail->join_pay_member_id)
  115. ->where('member_account_classify', 'CASH')
  116. ->first();
  117. $payAmount = floatval($payDetail->pay_amount);
  118. $added = $memberAccount->member_account_added + $payAmount * $addedNbr;
  119. $income = $memberAccount->member_account_income + $payAmount;
  120. $memberAccount->member_account_added = $added;
  121. $memberAccount->member_account_income = $income;
  122. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $payAmount;
  123. $memberAccount->save();
  124. // 根据最新的数据,更新用户等级
  125. // 临时屏蔽该会员升级
  126. // if (!in_array($payDetail->join_pay_member_id, ['MR2409132254Z9VW'])) {
  127. // $member = Member::find($payDetail->join_pay_member_id);
  128. // $roleId = RechargeService::disposeRole($payDetail->join_pay_member_id, $payDetail->pay_amount);
  129. // if ($roleId && $member->join_member_role_id != $roleId) {
  130. // $member->join_member_role_id = $roleId;
  131. // $member->save();
  132. // // 处理增值套包、组件
  133. // RechargeService::disposeAdded($roleId, $payDetail->join_pay_member_id);
  134. // }
  135. // }
  136. Db::commit();
  137. return json_success('success');
  138. } catch (BusinessException $e) {
  139. dump($e->getMessage());
  140. Db::rollBack();
  141. throw new BusinessException("处理失败");
  142. // return json_fail('处理失败');
  143. } catch (\Exception $e) {
  144. dump($e->getMessage());
  145. Db::rollBack();
  146. throw new BusinessException("处理失败");
  147. // return json_fail('处理失败');
  148. }
  149. }
  150. }