StarPayLogic.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\Constant\Payment;
  4. use App\dao\Pay\PayController;
  5. use App\Http\helper\CreateOrder;
  6. use App\Http\helper\NumConfig;
  7. use App\Jobs\Order;
  8. use App\Services\OrderServices;
  9. use App\Services\PayConfig;
  10. use App\Services\StarPayService;
  11. use App\Services\SupefinaSpei;
  12. use App\Services\CreateLog;
  13. use App\Util;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. /**
  17. * Supefina SPEI 墨西哥代收(充值)逻辑
  18. * 文档:https://docs.supefina.net/huan-ying-shi-yong-supefina-de-api-wen-dang/dai-shou/mo-xi-ge/spei
  19. * 代收回调以 realityAmount 为准入账(处理用户实转金额与订单金额不一致)
  20. */
  21. class StarPayLogic extends BaseApiLogic
  22. {
  23. public $result;
  24. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  25. {
  26. $PayVerify = new PayController();
  27. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  28. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  29. $this->error = $PayVerify->getError();
  30. return false;
  31. }
  32. if ($pay_amount < 0) {
  33. $this->error = 'Payment error_4';
  34. return false;
  35. }
  36. $payConfigService = new PayConfig();
  37. $config = $payConfigService->getConfig('StarPay') ?? [];
  38. if (empty($config)) {
  39. $this->error = 'Payment config error';
  40. return false;
  41. }
  42. $service = new StarPayService($config);
  43. $order_sn = CreateOrder::order_sn($userId);
  44. $logic = new OrderLogic();
  45. $amount = round($pay_amount, 2);
  46. $logic->orderCreate($order_sn, $amount * NumConfig::NUM_VALUE, 'StarPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
  47. try {
  48. $data = $service->create($order_sn, $amount, ['userId' => $userId]);
  49. } catch (\Throwable $e) {
  50. Util::WriteLog('StarPay_error', 'payin request exception: ' . $e->getMessage());
  51. $this->error = 'Payment processing error';
  52. return false;
  53. }
  54. if (!isset($data['code']) || $data['code'] !== 0) {
  55. $this->error = $data['msg'] ?? 'Payment request failed';
  56. return false;
  57. }
  58. $this->result = $data;
  59. return $data;
  60. }
  61. /**
  62. * 代收回调:仅代收存在“实际金额与订单金额不一致”,以 realityAmount 入账
  63. *
  64. * @param array<string,mixed> $post
  65. */
  66. public function notify($post)
  67. {
  68. $config = (new PayConfig())->getConfig('StarPay') ?? [];
  69. $starPayService = new StarPayService($config);
  70. $r = $starPayService->notify($post);
  71. if ($r->status === Payment::STATUS_UNKNOWN) {
  72. Util::WriteLog('StarPay', "unknown status {$r->orderSn}");
  73. return $starPayService->notifySuccess();
  74. }
  75. if ($r->status === Payment::STATUS_IN_PROGRESS) {
  76. return $starPayService->notifySuccess();
  77. }
  78. if ($r->status == Payment::STATUS_REFUND) {
  79. Log::error('订单退款' . $r->orderSn);
  80. return $starPayService->notifySuccess();
  81. }
  82. try {
  83. $order = DB::connection('write')->table('agent.dbo.order')
  84. ->where('order_sn', $r->orderSn)
  85. ->first();
  86. if (!$order) {
  87. Util::WriteLog('StarPay', 'payin order not found: ' . $r->orderSn);
  88. return 'success';
  89. }
  90. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  91. if ($order->payment_sn != $r->orderNo) {
  92. $logic = new OrderLogic();
  93. $amount = 100;
  94. $order_sn = $r->orderSn.'#'.time();
  95. $logic->orderCreate($order_sn, $amount, 'StarPay', $order->user_id);
  96. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)
  97. ->first();
  98. } else {
  99. return $starPayService->notifySuccess();
  100. }
  101. }
  102. if ($r->status == Payment::STATUS_FAIL) {
  103. $body = ['pay_status' => 2, 'updated_at' => date('Y-m-d H:i:s')];
  104. DB::connection('write')->table('agent.dbo.order')->where('order_sn', $r->orderSn)
  105. ->update($body);
  106. Util::WriteLog('StarPay', 'payin notify status not success: ' . $r->orderSn);
  107. return $starPayService->notifySuccess();
  108. }
  109. $GiftsID = $order->GiftsID ?: '';
  110. $userID = $order->user_id ?: '';
  111. $AdId = $order->AdId ?: '';
  112. $eventType = $order->eventType ?: '';
  113. $realityAmount = isset($post['paidAmount']) ? (float)$post['paidAmount'] : (float)($post['amount'] ?? 0);
  114. if ($realityAmount <= 0) {
  115. Util::WriteLog('StarPay_error', 'payin notify invalid realityAmount: ' . json_encode($post));
  116. return $starPayService->notifyFail();
  117. }
  118. $payAmt = round($realityAmount, 2);
  119. $amountInScore = (int) round($payAmt * NumConfig::NUM_VALUE);
  120. $body = [
  121. 'payment_sn' => $r->orderNo,
  122. 'pay_status' => 1,
  123. 'pay_at' => date('Y-m-d H:i:s'),
  124. 'finished_at' => date('Y-m-d H:i:s'),
  125. 'amount' => $amountInScore,
  126. 'updated_at' => date('Y-m-d H:i:s'),
  127. ];
  128. $config = (new PayConfig())->getConfig('StarPay');
  129. $body['payment_fee'] = isset($config['payin_fee']) ? $amountInScore * $config['payin_fee'] : 0;
  130. $service = new OrderServices();
  131. if ((int)$order->amount != $amountInScore) {
  132. $body['GiftsID'] = 0;
  133. $body['amount'] = $amountInScore;
  134. $Recharge = $payAmt;
  135. $give = 0;
  136. $favorable_price = $Recharge + $give;
  137. $czReason = 1;
  138. $cjReason = 45;
  139. } else {
  140. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  141. }
  142. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $r->orderSn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType, $body['payment_fee'] ?? 0);
  143. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $r->orderSn]);
  144. DB::connection('write')->table('agent.dbo.order')->where('order_sn', $r->orderSn)->update($body);
  145. Util::WriteLog('StarPay', 'payin success, order_sn=' . $r->orderSn . ', realityAmount=' . $realityAmount);
  146. return $starPayService->notifySuccess();
  147. } catch (\Throwable $exception) {
  148. Util::WriteLog('StarPay_error', $exception->getMessage() . "\n" . $exception->getTraceAsString());
  149. throw $exception;
  150. }
  151. }
  152. }