RechargeController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. public function paySuccess(Request $request)
  40. {
  41. $data = $request->post();
  42. if (is_json($data)) {
  43. $data = json_decode($data, true);
  44. }
  45. if (!$data['pay_id']) {
  46. return json_fail('参数异常');
  47. }
  48. try {
  49. _syslog("APP充值","APP充值成功");
  50. return $this->disposePaySuccess($data['pay_id']);
  51. }catch (\Exception $e){
  52. _syslog("APP充值","APP充值失败");
  53. return json_fail("处理失败");
  54. }
  55. // public function paySuccess($id)
  56. // {
  57. // if (!$id) {
  58. // return json_fail('参数异常');
  59. // }
  60. //
  61. // $payDetail = PayDetail::find($id);
  62. // if ($payDetail->pay_status != 'SUCCESS') {
  63. // return json_fail("支付状态异常");
  64. // }
  65. }
  66. /**
  67. * @param $payId
  68. * @return \support\Response
  69. * @throws BusinessException
  70. */
  71. public function disposePaySuccess($payId){
  72. $payDetail = PayDetail::find($payId);
  73. if (!$payDetail || $payDetail->pay_status != 'SUCCESS') {
  74. throw new BusinessException("支付状态异常");
  75. // return json_fail("支付状态异常");
  76. }
  77. $extendJson = [];
  78. if (!empty($payDetail->pay_extend_json)) {
  79. $extendJson = json_decode($payDetail->pay_extend_json, true);
  80. if (isset($extendJson['notify']) && $extendJson['notify'] == 'success') {
  81. return json_success('处理完成');
  82. }
  83. }
  84. try {
  85. Db::beginTransaction();
  86. $extendJson['notify'] = 'success';
  87. $payDetail->pay_extend_json = json_encode($extendJson);
  88. $payDetail->save();
  89. // 赠送比例
  90. $objectJson = json_decode($payDetail->join_pay_object_json, true);
  91. $addedNbr = 0;
  92. if (isset($objectJson['order_id'])) {
  93. // 对应订单设置已完成
  94. Order::where('order_groupby',$payDetail->join_pay_order_id)->update(['order_status_system'=>'DONE','order_is_complete'=>'Y']);
  95. $addedNbr = RechargeService::disposeOrder($objectJson['order_id']);
  96. // throw new BusinessException("支付数据异常");
  97. // return json_fail("");
  98. }
  99. // 赠送金额累加到 账户表 member_account_added
  100. $memberAccount = MemberAccount::where('join_account_member_id', $payDetail->join_pay_member_id)
  101. ->where('member_account_classify', 'CASH')
  102. ->first();
  103. $payAmount = floatval($payDetail->pay_amount);
  104. $added = $memberAccount->member_account_added + $payAmount * $addedNbr;
  105. $income = $memberAccount->member_account_income + $payAmount;
  106. $memberAccount->member_account_added = $added;
  107. $memberAccount->member_account_income = $income;
  108. $memberAccount->member_account_surplus = $income - $memberAccount->member_account_expend;
  109. $memberAccount->save();
  110. // 根据最新的数据,更新用户等级
  111. $member = Member::find($payDetail->join_pay_member_id);
  112. $roleId = RechargeService::disposeRole($payDetail->join_pay_member_id, $payDetail->pay_amount);
  113. if ($roleId && $member->join_member_role_id != $roleId) {
  114. $member->join_member_role_id = $roleId;
  115. $member->save();
  116. // 处理增值套包、组件
  117. RechargeService::disposeAdded($roleId, $payDetail->join_pay_member_id);
  118. }
  119. Db::commit();
  120. return json_success('success');
  121. } catch (BusinessException $e) {
  122. dump($e->getMessage());
  123. Db::rollBack();
  124. throw new BusinessException("处理失败");
  125. // return json_fail('处理失败');
  126. } catch (\Exception $e) {
  127. dump($e->getMessage());
  128. Db::rollBack();
  129. throw new BusinessException("处理失败");
  130. // return json_fail('处理失败');
  131. }
  132. }
  133. }