SfPayLogic.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\SfPay;
  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 SfPayLogic extends BaseApiLogic
  15. {
  16. public $result;
  17. /**
  18. * 支付方式位掩码 → SfPay payProduct 映射
  19. *
  20. * 1=cashapp, 2=paypal, 4=applepay, 16=btc(CASH_BTC), 1024=usdt
  21. */
  22. protected $payProductMap = [
  23. 1 => 'USA102', // CashApp
  24. 2 => 'USA107', // PayPal
  25. 4 => 'USA103', // APPLE_PAY
  26. 16 => 'USA106', // CASH_BTC
  27. 1024 => 'USA105', // USDT
  28. ];
  29. /**
  30. * 代收下单
  31. */
  32. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  33. {
  34. $dao = new AccountPayInfo();
  35. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  36. // 礼包类型验证
  37. $PayVerify = new PayController();
  38. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  39. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  40. $this->error = $PayVerify->getError();
  41. return false;
  42. }
  43. if ($pay_amount < 0) {
  44. $this->error = 'Payment error_4';
  45. return false;
  46. }
  47. $service = new SfPay();
  48. $config = $service->config;
  49. $order_sn = CreateOrder::order_sn($userId);
  50. // 生成订单信息
  51. $logic = new OrderLogic();
  52. $amount = (int) round($pay_amount * NumConfig::NUM_VALUE);
  53. $logic->orderCreate($order_sn, $amount, 'SfPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
  54. // 确定 payProduct
  55. $payProduct = $this->payProductMap[$pay_method] ?? ($config['pay_product'] ?? 'USA102');
  56. // 构建支付请求参数(明文,后续AES加密)
  57. // 必传: merchantOrderNo, amount, payProduct, gameId
  58. $params = [
  59. 'merchantOrderNo' => $order_sn,
  60. 'amount' => (float)$pay_amount,
  61. 'payProduct' => $payProduct,
  62. 'gameId' => (int) ($config['game_id'] ?? 0),
  63. ];
  64. // 可选参数(传了收银台会预填,不传则需要用户手动输入)
  65. if (!empty($userName)) $params['customerName'] = $userName;
  66. if (!empty($userEmail)) $params['customerEmail'] = $userEmail;
  67. if (!empty($userPhone)) $params['customerPhone'] = $userPhone;
  68. if (!empty($config['notify_url'])) $params['notifyUrl'] = $config['notify_url'];
  69. if (!empty($config['return_url'])) $params['returnUrl'] = $config['return_url'];
  70. // 可选辅助参数
  71. $params['countryId'] = 'USA'; // 货币单位依据countryId
  72. $params['userAgent'] = $_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0';
  73. $params['ip'] = $buyIP ?: ($_SERVER['REMOTE_ADDR'] ?? '');
  74. // 生成用户请求信息日志
  75. $request_extra = \GuzzleHttp\json_encode($params);
  76. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  77. $url = $service->apiUrl . '/gateway/payment/init';
  78. Util::WriteLog('SfPay', 'SfPay支付请求: ' . $url . ' | ' . $request_extra);
  79. $result = $service->curlPost($url, $params);
  80. Util::WriteLog('SfPay', 'SfPay支付结果: ' . ($result ?: '空结果'));
  81. if ($result === false) {
  82. $this->error = 'Payment processing error';
  83. return false;
  84. }
  85. try {
  86. $data = \GuzzleHttp\json_decode($result, true);
  87. } catch (\Exception $e) {
  88. Util::WriteLog("SfPay_error", [$result, $e->getMessage()]);
  89. $this->error = 'Payment processing error';
  90. return false;
  91. }
  92. return $data;
  93. }
  94. /**
  95. * 代收异步回调处理
  96. *
  97. * SfPay回调格式(POST JSON,不加密):
  98. * {
  99. * "data": {
  100. * "amount": "20.00",
  101. * "orderNo": "...",
  102. * "message": "Success",
  103. * "type": "payment",
  104. * "merchantOrderNo": "...",
  105. * "processedTime": "...",
  106. * "transferMode": "PIX",
  107. * "status": "SUCCESS"
  108. * },
  109. * "signature_n": "..."
  110. * }
  111. */
  112. public function notify($post)
  113. {
  114. $callbackData = $post['data'] ?? [];
  115. $signatureN = $post['signature_n'] ?? '';
  116. if (empty($callbackData) || empty($signatureN)) {
  117. Util::WriteLog('SfPay', '回调数据不完整');
  118. return 'ok';
  119. }
  120. // 验签
  121. $service = new SfPay();
  122. if (!$service->verifySign($callbackData, $signatureN)) {
  123. Util::WriteLog('SfPay', '回调签名验证失败');
  124. return 'ok';
  125. }
  126. $order_no = $callbackData['merchantOrderNo'] ?? '';
  127. // 查询订单信息
  128. $order = DB::connection('write')->table('agent.dbo.order')
  129. ->where('order_sn', $order_no)
  130. ->first();
  131. if (!$order) {
  132. Util::WriteLog('SfPay', '订单不存在: ' . $order_no);
  133. return 'ok';
  134. }
  135. // 已处理过则直接返回
  136. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  137. return 'ok';
  138. }
  139. $status = $callbackData['status'] ?? '';
  140. $body = [
  141. 'payment_sn' => $callbackData['orderNo'] ?? '',
  142. 'updated_at' => date('Y-m-d H:i:s'),
  143. ];
  144. // status: SUCCESS=成功, FAILURE=失败, PENDING=处理中
  145. if ($status !== 'SUCCESS') {
  146. Util::WriteLog('SfPay', "支付未完成订单: {$order_no}, status: {$status}");
  147. return 'ok';
  148. }
  149. // 支付成功
  150. $GiftsID = $order->GiftsID ?: '';
  151. $userID = $order->user_id ?: '';
  152. $AdId = $order->AdId ?: '';
  153. $eventType = $order->eventType ?: '';
  154. $payAmt = (float)($callbackData['amount'] ?? 0);
  155. $body['pay_status'] = 1;
  156. $body['pay_at'] = date('Y-m-d H:i:s');
  157. $body['finished_at'] = date('Y-m-d H:i:s');
  158. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  159. try {
  160. $orderService = new OrderServices();
  161. if (intval($order->amount) != $body['amount']) {
  162. $body['GiftsID'] = 0;
  163. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  164. $Recharge = $payAmt;
  165. $give = 0;
  166. $favorable_price = $Recharge + $give;
  167. $czReason = 1;
  168. $cjReason = 45;
  169. } else {
  170. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $orderService->getPayInfo(
  171. $GiftsID, $userID, $payAmt
  172. );
  173. }
  174. // 增加充值记录
  175. [$Score] = $orderService->addRecord(
  176. $userID, $payAmt, $favorable_price, $order_no, $GiftsID,
  177. $Recharge, $czReason, $give, $cjReason, $AdId, $eventType,
  178. 0
  179. );
  180. // 异步处理后续任务
  181. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_no]);
  182. } catch (\Exception $exception) {
  183. Util::WriteLog("SfPay_error", $exception->getMessage());
  184. }
  185. // 更新订单状态
  186. $order_up = DB::connection('write')->table('agent.dbo.order')
  187. ->where('order_sn', $order_no)
  188. ->update($body);
  189. if (!$order_up) {
  190. Util::WriteLog('SfPay', '订单更新失败: ' . $order_no);
  191. return 'ok';
  192. }
  193. Util::WriteLog("SfPay", 'success: ' . $order_no);
  194. return 'ok';
  195. }
  196. /**
  197. * 代收订单查询
  198. *
  199. * POST /gateway/payment/orderQuery
  200. * 请求: {"merchantOrderNo": "xxx"}(AES加密)
  201. * 响应: code=0, data={status, amount, orderNo, ...}
  202. *
  203. * @param string $merchantOrderNo 商户订单号
  204. * @return array|false
  205. */
  206. public function orderQuery($merchantOrderNo)
  207. {
  208. $service = new SfPay();
  209. $params = ['merchantOrderNo' => $merchantOrderNo];
  210. $url = $service->apiUrl . '/gateway/payment/orderQuery';
  211. Util::WriteLog('SfPay', 'SfPay订单查询: ' . $url . ' | ' . $merchantOrderNo);
  212. $result = $service->curlPost($url, $params);
  213. if ($result === false) {
  214. $this->error = 'Query processing error';
  215. return false;
  216. }
  217. try {
  218. $data = \GuzzleHttp\json_decode($result, true);
  219. } catch (\Exception $e) {
  220. Util::WriteLog("SfPay_error", [$result, $e->getMessage()]);
  221. $this->error = 'Query processing error';
  222. return false;
  223. }
  224. return $data;
  225. }
  226. }