SupefinaSpeiLogic.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if ($order->payment_sn != $post['supefinaOrderId']) {
  115. $logic = new OrderLogic();
  116. $amount = 100;
  117. $order_sn = $order_sn.'#'.time();
  118. $logic->orderCreate($order_sn, $amount, 'SupefinaSpei', $order->user_id);
  119. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)
  120. ->first();
  121. } else {
  122. return 'SUCCESS';
  123. }
  124. }
  125. $status = (string)($post['status'] ?? '');
  126. if ($status === '03') {
  127. $body = ['pay_status' => 2, 'updated_at' => date('Y-m-d H:i:s')];
  128. DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->update($body);
  129. return 'SUCCESS';
  130. }
  131. if ($status !== '01') {
  132. Util::WriteLog('SupefinaSpei', 'payin notify status not success: ' . $status);
  133. return 'SUCCESS';
  134. }
  135. $GiftsID = $order->GiftsID ?: '';
  136. $userID = $order->user_id ?: '';
  137. $AdId = $order->AdId ?: '';
  138. $eventType = $order->eventType ?: '';
  139. $realityAmount = isset($post['realityAmount']) ? (float)$post['realityAmount'] : (float)($post['amount'] ?? 0);
  140. if ($realityAmount <= 0) {
  141. Util::WriteLog('SupefinaSpei_error', 'payin notify invalid realityAmount: ' . json_encode($post));
  142. return 'fail';
  143. }
  144. $payAmt = round($realityAmount, 2);
  145. $amountInScore = (int) round($payAmt * NumConfig::NUM_VALUE);
  146. $body = [
  147. 'payment_sn' => $post['supefinaOrderId'] ?? $post['reference'] ?? '',
  148. 'pay_status' => 1,
  149. 'pay_at' => date('Y-m-d H:i:s'),
  150. 'finished_at' => date('Y-m-d H:i:s'),
  151. 'amount' => $amountInScore,
  152. 'updated_at' => date('Y-m-d H:i:s'),
  153. ];
  154. $config = (new PayConfig())->getConfig('SupefinaSpei');
  155. $body['payment_fee'] = isset($config['payin_fee']) ? $amountInScore * $config['payin_fee'] : 0;
  156. $service = new OrderServices();
  157. if ((int)$order->amount != $amountInScore) {
  158. $body['GiftsID'] = 0;
  159. $body['amount'] = $amountInScore;
  160. $Recharge = $payAmt;
  161. $give = 0;
  162. $favorable_price = $Recharge + $give;
  163. $czReason = 1;
  164. $cjReason = 45;
  165. } else {
  166. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  167. }
  168. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
  169. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_sn]);
  170. DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->update($body);
  171. Util::WriteLog('SupefinaSpei', 'payin success, order_sn=' . $order_sn . ', realityAmount=' . $realityAmount);
  172. return 'SUCCESS';
  173. } catch (\Throwable $exception) {
  174. Util::WriteLog('SupefinaSpei_error', $exception->getMessage() . "\n" . $exception->getTraceAsString());
  175. throw $exception;
  176. }
  177. }
  178. }