SafePayLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Pay\AccountPayInfo;
  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\SafePay;
  9. use App\Services\CreateLog;
  10. use App\Services\OrderServices;
  11. use App\Services\PayConfig;
  12. use App\Util;
  13. use Illuminate\Support\Facades\DB;
  14. class SafePayLogic extends BaseApiLogic
  15. {
  16. public $result;
  17. /**
  18. * 支付方式到 pay_code 的映射
  19. * 1=cashapp, 2=paypal, 4=applepay, 8=googlepay
  20. */
  21. protected $payCodeMap = [
  22. 1 => '1301', // 美国 CashApp
  23. 2 => '1305', // 美国 PayPal
  24. 4 => '1302', // 美国 ApplePay
  25. 8 => '1303', // 美国 GooglePay
  26. ];
  27. /**
  28. * 代收下单
  29. */
  30. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  31. {
  32. $dao = new AccountPayInfo();
  33. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  34. // 礼包类型验证
  35. $PayVerify = new PayController();
  36. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  37. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  38. $this->error = $PayVerify->getError();
  39. return false;
  40. }
  41. if ($pay_amount < 0) {
  42. $this->error = 'Payment error_4';
  43. return false;
  44. }
  45. $service = new SafePay();
  46. $config = $service->config;
  47. $order_sn = CreateOrder::order_sn($userId);
  48. // 生成订单信息
  49. $logic = new OrderLogic();
  50. $amount = (int) round($pay_amount * NumConfig::NUM_VALUE);
  51. $logic->orderCreate($order_sn, $amount, 'SafePay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
  52. // 确定 pay_code
  53. $payCode = $this->payCodeMap[$pay_method] ?? ($config['pay_code'] ?? '1301');
  54. // 构建支付请求参数
  55. $params = [
  56. 'mer_no' => $service->merNo,
  57. 'order_no' => $order_sn,
  58. 'amount' => (string)$pay_amount,
  59. 'name' => $userName ?: 'user',
  60. 'email' => $userEmail ?: ($userId . '@unknown.com'),
  61. 'phone' => $userPhone ?: '0000000000',
  62. 'currency' => $config['currency'] ?? 'USD',
  63. 'pay_code' => $payCode,
  64. 'notify_url' => $config['notify_url'] ?? '',
  65. 'extra' => json_encode([
  66. 'clientIP' => $buyIP,
  67. 'firstName' => $userName ?: '',
  68. 'lastName' => '',
  69. ]),
  70. ];
  71. // RSA签名
  72. $signedParams = $service->sign($params);
  73. // 生成用户请求信息日志
  74. $request_extra = \GuzzleHttp\json_encode($signedParams);
  75. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  76. $url = $service->apiUrl . '/open/api/order/in';
  77. $result = $service->curlPost($url, $signedParams);
  78. $rresult = compact('result', 'signedParams', 'url');
  79. Util::WriteLog('SafePay', 'SafePay支付请求' . $url . " | " . $request_extra);
  80. Util::WriteLog('SafePay', 'SafePay支付结果' . json_encode($rresult));
  81. try {
  82. $data = \GuzzleHttp\json_decode($result, true);
  83. } catch (\Exception $e) {
  84. Util::WriteLog("SafePay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  85. $this->error = 'Payment processing error';
  86. return false;
  87. }
  88. return $data;
  89. }
  90. /**
  91. * 代收异步回调处理
  92. */
  93. public function notify($post)
  94. {
  95. $order_no = $post['order_no'] ?? '';
  96. // 查询订单信息
  97. $order = DB::connection('write')->table('agent.dbo.order')
  98. ->where('order_sn', $order_no)
  99. ->first();
  100. if (!$order) {
  101. Util::WriteLog('SafePay', '订单不存在: ' . $order_no);
  102. return 'ok';
  103. }
  104. // 已处理过则直接返回
  105. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  106. return 'ok';
  107. }
  108. $status = $post['status'] ?? '';
  109. $body = [
  110. 'payment_sn' => $post['ref_no'] ?? '',
  111. 'updated_at' => date('Y-m-d H:i:s'),
  112. ];
  113. // status: success=交易成功, 其他=失败/待处理
  114. if ($status !== 'success') {
  115. // 非成功状态(如 pending/waiting),不处理,等待后续通知
  116. Util::WriteLog('SafePay', "支付未完成订单: {$order_no}, status: {$status}");
  117. return 'ok';
  118. }
  119. // 支付成功
  120. $GiftsID = $order->GiftsID ?: '';
  121. $userID = $order->user_id ?: '';
  122. $AdId = $order->AdId ?: '';
  123. $eventType = $order->eventType ?: '';
  124. $orderAmount = (float)($post['order_reality_amount'] ?? $post['order_amount'] ?? 0);
  125. $payAmt = $orderAmount;
  126. $body['pay_status'] = 1;
  127. $body['pay_at'] = date('Y-m-d H:i:s');
  128. $body['finished_at'] = date('Y-m-d H:i:s');
  129. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  130. $config = (new PayConfig())->getConfig('SafePay');
  131. $body['payment_fee'] = (int)($body['amount'] * ($config['payin_fee'] ?? 0));
  132. try {
  133. $service = new OrderServices();
  134. if (intval($order->amount) != $body['amount']) {
  135. $body['GiftsID'] = 0;
  136. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  137. $Recharge = $payAmt;
  138. $give = 0;
  139. $favorable_price = $Recharge + $give;
  140. $czReason = 1;
  141. $cjReason = 45;
  142. } else {
  143. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo(
  144. $GiftsID, $userID, $payAmt
  145. );
  146. }
  147. // 增加充值记录
  148. [$Score] = $service->addRecord(
  149. $userID, $payAmt, $favorable_price, $order_no, $GiftsID,
  150. $Recharge, $czReason, $give, $cjReason, $AdId, $eventType,
  151. $body['payment_fee'] ?? 0
  152. );
  153. // 异步处理后续任务
  154. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_no]);
  155. } catch (\Exception $exception) {
  156. Util::WriteLog("SafePay_error", $exception->getMessage());
  157. }
  158. // 更新订单状态
  159. $order_up = DB::connection('write')->table('agent.dbo.order')
  160. ->where('order_sn', $order_no)
  161. ->update($body);
  162. if (!$order_up) {
  163. Util::WriteLog('SafePay', '订单更新失败: ' . $order_no);
  164. return 'ok';
  165. }
  166. Util::WriteLog("SafePay", 'success: ' . $order_no);
  167. return 'ok';
  168. }
  169. }