SupefinaSpeiLogic.php 7.8 KB

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