NewSupefinaSpeiLogic.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Estatisticas\RechargeWithDraw;
  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\Models\PrivateMail;
  9. use App\Models\RecordUserDataStatistics;
  10. use App\Services\Aes;
  11. use App\Services\OrderServices;
  12. use App\Services\PayConfig;
  13. use App\Services\StoredProcedure;
  14. use App\Services\SupefinaSpei;
  15. use App\Services\CreateLog;
  16. use App\Util;
  17. use GuzzleHttp\Client;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. use Illuminate\Support\Facades\Redis;
  21. /**
  22. * Supefina SPEI
  23. *
  24. */
  25. class NewSupefinaSpeiLogic extends BaseApiLogic
  26. {
  27. const AGENT = 108;
  28. public $result;
  29. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  30. {
  31. $PayVerify = new PayController();
  32. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  33. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  34. $this->error = $PayVerify->getError();
  35. return false;
  36. }
  37. if ($pay_amount < 0) {
  38. $this->error = 'Payment error_4';
  39. return false;
  40. }
  41. $service = new SupefinaSpei('NewSupefinaSpei');
  42. $config = $service->config;
  43. $merId = $config['merId'] ?? '';
  44. $baseUrl = $config['baseUrl'] ?? '';
  45. $payinPath = $config['payinPath'] ?? '/api/supefina/transactions/collection';
  46. $callbackUrl = $config['callbackUrl'] ?? '';
  47. $countryId = $config['countryId'] ?? 'MX';
  48. $currency = $config['currency'] ?? 'MXN';
  49. $aesKey = $config['aesKey'] ?? '';
  50. $privateKey = $config['privateKey'] ?? '';
  51. $productNo = $config['productNo'] ?? '';
  52. if ($merId === '' || $baseUrl === '' || empty($privateKey) || $callbackUrl === '') {
  53. $this->error = 'Payment config error';
  54. return false;
  55. }
  56. $order_sn = CreateOrder::order_sn($userId);
  57. $logic = new OrderLogic();
  58. $amount = (int)round($pay_amount * NumConfig::NUM_VALUE);
  59. $logic->orderCreate($order_sn, $amount, 'NewSupefinaSpei', $userId, $pay_method, $GiftsID, $AdId, $eventType);
  60. $orderAmount = (string)(int)$pay_amount;
  61. if ($pay_amount != (int)$pay_amount) {
  62. $orderAmount = number_format($pay_amount, 2, '.', '');
  63. }
  64. $params = [
  65. 'merchantOrderNo' => $order_sn,
  66. 'amount' => $orderAmount,
  67. 'country' => $countryId,
  68. 'currency' => $currency,
  69. 'notifyUrl' => $callbackUrl,
  70. ];
  71. $headers = [
  72. 'version' => '1.0.0',
  73. 'merchantNo' => $merId,
  74. 'requestId' => mt_rand(100000, 999999) . time(),
  75. 'requestTime' => time() * 1000,
  76. ];
  77. $body = [
  78. 'productNo' => $productNo,
  79. 'data' => Aes::encrypt(json_encode($params), base64_decode($aesKey)),
  80. ];
  81. $sign = $this->sign($headers, $body, $privateKey);
  82. $headers['sign'] = $sign;
  83. $request_extra = \GuzzleHttp\json_encode(array_merge($headers, $params));
  84. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  85. $url = rtrim($baseUrl, '/') . $payinPath;
  86. Util::WriteLog('NewSupefinaSpei', 'payin request: ' . $url . ' | ' . $request_extra);
  87. try {
  88. $client = new Client();
  89. $result = $client->post($url, [
  90. 'headers' => $headers,
  91. 'json' => $body,
  92. ])->getBody()->getContents();
  93. } catch (\Throwable $e) {
  94. Util::WriteLog('NewSupefinaSpei_error', 'payin request exception: ' . $e->getMessage());
  95. $this->error = 'Payment processing error';
  96. return false;
  97. }
  98. Util::WriteLog('NewSupefinaSpei', 'payin response: ' . $result);
  99. try {
  100. $data = \GuzzleHttp\json_decode($result, true);
  101. } catch (\Throwable $e) {
  102. Util::WriteLog('NewSupefinaSpei_error', [$result, $e->getMessage()]);
  103. $this->error = 'Payment processing error';
  104. return false;
  105. }
  106. if (!isset($data['success']) || $data['success'] !== true) {
  107. $this->error = $data['message'] ?? 'Payment request failed';
  108. return false;
  109. }
  110. $decryptData = Aes::decrypt($data['data'], base64_decode($aesKey));
  111. Util::WriteLog('NewSupefinaSpei', 'payin response1: ' . $decryptData);
  112. $this->result = json_decode($decryptData, true);
  113. return $this->result;
  114. }
  115. /**
  116. * 代收回调:仅代收存在“实际金额与订单金额不一致”,以 realityAmount 入账
  117. *
  118. * @param array<string,mixed> $post
  119. */
  120. public function notify($post)
  121. {
  122. $order_sn = $post['merchantOrderNo'];
  123. if ($order_sn === '') {
  124. Util::WriteLog('NewSupefinaSpei_error', 'payin notify missing merchantOrderNo');
  125. return 'fail';
  126. }
  127. try {
  128. $order = DB::connection('write')->table('agent.dbo.order')
  129. ->lock('with(nolock)')
  130. ->where('order_sn', $order_sn)->first();
  131. if (!$order) {
  132. $order1 = DB::connection('write')->table('agent.dbo.order')
  133. ->lock('with(nolock)')
  134. ->where('payment_sn', $post['platformOrderNo'])
  135. ->first();
  136. if ($order1) {
  137. Util::WriteLog('NewSupefinaSpei', 'repeat call by platformOrderNo: ' . $post['platformOrderNo']);
  138. return '{"success":true}';
  139. }
  140. $orderArr = explode('-', $order_sn);
  141. if (count($orderArr) > 1) {
  142. $order_sn1 = $orderArr[0];
  143. $order2 = DB::connection('write')->table('agent.dbo.order')
  144. ->lock('with(nolock)')
  145. ->where('order_sn', $order_sn1)
  146. ->first();
  147. if (!$order2) {
  148. Util::WriteLog('NewSupefinaSpei_error', 'payin notify order not found: ' . $order_sn);
  149. Log::error('订单无法对应用户:' . $order_sn);
  150. }
  151. $logic = new OrderLogic();
  152. $amount = 100;
  153. $logic->orderCreate($order_sn, $amount, 'NewSupefinaSpei', $order1->user_id);
  154. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)
  155. ->first();
  156. }
  157. }
  158. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  159. if ($order->payment_sn != $post['platformOrderNo']) {
  160. $logic = new OrderLogic();
  161. $amount = 100;
  162. $order_sn = $order_sn . '#' . time();
  163. $logic->orderCreate($order_sn, $amount, 'NewSupefinaSpei', $order->user_id);
  164. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)
  165. ->first();
  166. } else {
  167. return '{"success":true}';
  168. }
  169. }
  170. $status = (string)($post['transStatus'] ?? '');
  171. if (in_array($status, ['FAILED', 'CLOSED', 'CANCELED'])) {
  172. $body = ['pay_status' => 2, 'updated_at' => date('Y-m-d H:i:s')];
  173. DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->update($body);
  174. return '{"success":true}';
  175. }
  176. if (in_array($status, ['REFUNDING', 'PARTIAL_REFUND', 'REFUNDED'])) {
  177. Util::WriteLog('NewSupefinaSpei', 'payin notify status refund: ' . $order_sn);
  178. return '{"success":true}';
  179. }
  180. if ($status !== 'SUCCESS') {
  181. Util::WriteLog('NewSupefinaSpei', 'payin notify status not success: ' . $order_sn);
  182. return '{"success":true}';
  183. }
  184. $GiftsID = $order->GiftsID ?: '';
  185. $userID = $order->user_id ?: '';
  186. $AdId = $order->AdId ?: '';
  187. $eventType = $order->eventType ?: '';
  188. $realityAmount = isset($post['amount']) ? (float)$post['amount'] : (float)($post['amount'] ?? 0);
  189. if ($realityAmount <= 0) {
  190. Util::WriteLog('NewSupefinaSpei_error', 'payin notify invalid realityAmount: ' . json_encode($post));
  191. return 'fail';
  192. }
  193. $payAmt = round($realityAmount, 2);
  194. $amountInScore = (int)round($payAmt * NumConfig::NUM_VALUE);
  195. $body = [
  196. 'payment_sn' => $post['platformOrderNo'] ?? '',
  197. 'pay_status' => 1,
  198. 'pay_at' => date('Y-m-d H:i:s'),
  199. 'finished_at' => date('Y-m-d H:i:s'),
  200. 'amount' => $amountInScore,
  201. 'updated_at' => date('Y-m-d H:i:s'),
  202. ];
  203. $body['payment_fee'] = $post['fee'] ?? 0;
  204. $service = new OrderServices();
  205. if ((int)$order->amount != $amountInScore) {
  206. $body['GiftsID'] = 0;
  207. $body['amount'] = $amountInScore;
  208. $Recharge = $payAmt;
  209. $give = 0;
  210. $favorable_price = $Recharge + $give;
  211. $czReason = 1;
  212. $cjReason = 45;
  213. } else {
  214. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  215. }
  216. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType,
  217. $body['payment_fee'] ?? 0);
  218. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_sn]);
  219. DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->update($body);
  220. Util::WriteLog('NewSupefinaSpei', 'payin success, order_sn=' . $order_sn . ', realityAmount=' . $realityAmount);
  221. return '{"success":true}';
  222. } catch (\Throwable $exception) {
  223. Util::WriteLog('NewSupefinaSpei_error', $exception->getMessage() . "\n" . $exception->getTraceAsString());
  224. throw $exception;
  225. }
  226. }
  227. public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
  228. {
  229. // 查询提现订单
  230. $query = DB::connection('write')
  231. ->table('QPAccountsDB.dbo.OrderWithDraw')
  232. ->where('RecordID', $RecordID)
  233. ->first();
  234. if (!$query) {
  235. Util::WriteLog('NewSupefinaSpei', 'withdraw order not found: ' . $RecordID);
  236. return 'fail';
  237. }
  238. $config = (new PayConfig())->getConfig('NewSupefinaSpei');
  239. // 账户号:优先 PixNum,其次 BankNO
  240. $account = $PixNum ?: $BankNO;
  241. if (!$account) {
  242. Util::WriteLog('NewSupefinaSpei_error', 'missing account for withdraw: ' . $OrderId);
  243. return 'fail';
  244. }
  245. // 银行编号:优先 BankNO(如果看作 bankId),否则使用配置默认
  246. $bankId = $BranchBank;
  247. if ($bankId === '') {
  248. Util::WriteLog('NewSupefinaSpei_error', 'missing bankId for withdraw: ' . $OrderId);
  249. return 'fail';
  250. }
  251. // 提现金额是以分存储,转成两位小数金额
  252. $orderAmount = number_format($amount / 100, 2, '.', '');
  253. $bankList = config('games.mex_bank_list');
  254. try {
  255. $now = time() * 1000;
  256. $data = [
  257. 'transTime' => $now,
  258. 'merchantOrderNo' => $OrderId,
  259. 'amount' => (string)$orderAmount,
  260. 'country' => $config['country'] ?? 'MX',
  261. 'currency' => $config['currency'] ?? 'MXN',
  262. 'payeeInfo' => [
  263. 'accountInfo' => [
  264. 'accountNo' => $account,
  265. 'accountType' => mb_strlen($account) == 16 ? '20' : '10',
  266. ],
  267. 'bankInfo' => [
  268. 'bankCode' => $bankId,
  269. 'bankName' => $bankList[$bankId] ?? 'bank',
  270. ],
  271. 'payeeName' => $accountName ?: 'slot777',
  272. ],
  273. 'notifyUrl' => $config['cashCallbackUrl'],
  274. ];
  275. $url = $config['baseUrl'] . $config['transferPath'];
  276. $result = $this->sendRequest($url, 'TRANSFER', $data);
  277. if ($result === false) {
  278. return 'fail';
  279. }
  280. } catch (\Throwable $e) {
  281. Util::WriteLog('NewSupefinaSpei_error', 'payout request exception: ' . $e->getMessage());
  282. return '';
  283. }
  284. try {
  285. $responseData = \GuzzleHttp\json_decode($result, true);
  286. } catch (\Throwable $e) {
  287. Util::WriteLog('NewSupefinaSpei_error', [$result, $e->getMessage()]);
  288. $this->error = 'Payment processing error';
  289. return false;
  290. }
  291. if (!isset($responseData['success']) || $responseData['success'] !== true) {
  292. $this->error = $responseData['message'] ?? 'request failed';
  293. // 同步下单失败:如果订单在处理中(State=5),退回资金并标记失败
  294. if ((int)$query->State === 5) {
  295. $msg = $data['msg'] ?? 'NewSupefinaSpei payout failed';
  296. $WithDraw = $query->WithDraw + $query->ServiceFee;
  297. $bonus = '30000,' . $WithDraw;
  298. PrivateMail::failMail($query->UserID, $OrderId, $WithDraw, $msg, $bonus);
  299. $withdraw_data = [
  300. 'State' => 6,
  301. 'agent' => self::AGENT,
  302. 'finishDate' => now(),
  303. 'remark' => json_encode($data),
  304. ];
  305. DB::connection('write')
  306. ->table('QPAccountsDB.dbo.OrderWithDraw')
  307. ->where('OrderId', $query->OrderId)
  308. ->update($withdraw_data);
  309. $RecordData = ['after_state' => 6, 'update_at' => now()];
  310. DB::connection('write')
  311. ->table('QPAccountsDB.dbo.AccountsRecord')
  312. ->where('type', 1)
  313. ->where('RecordID', $RecordID)
  314. ->update($RecordData);
  315. }
  316. return 'fail';
  317. }
  318. $decryptData = Aes::decrypt($responseData['data'], base64_decode($config['aesKey']));
  319. Util::WriteLog('NewSupefinaSpei', 'transfer decrypt response: ' . $decryptData);
  320. $this->result = json_decode($decryptData, true);
  321. return $this->result;
  322. }
  323. public function cashNotify($post)
  324. {
  325. $OrderId = $post['merchantOrderNo'];
  326. if ($OrderId === '') {
  327. Util::WriteLog('NewSupefinaSpei_error', 'payout notify missing merchantOrderNo');
  328. return 'fail';
  329. }
  330. $query = DB::connection('write')
  331. ->table('QPAccountsDB.dbo.OrderWithDraw')
  332. ->where('OrderId', $OrderId)
  333. ->first();
  334. if (!$query) {
  335. Util::WriteLog('NewSupefinaSpei_error', 'withdraw order not found in notify: '.$OrderId);
  336. return '{"success":true}';
  337. }
  338. // 只处理 State=5 或 7 的订单,避免重复
  339. if (!in_array((int)$query->State, [5, 7], true)) {
  340. Util::WriteLog('NewSupefinaSpei', 'withdraw already handled: '.$OrderId);
  341. return '{"success":true}';
  342. }
  343. $status = $post['transStatus'] ?? '';
  344. $orderStatus = 0;
  345. if (in_array($status, ['SUCCESS'], true)) {
  346. $orderStatus = 1; // 成功
  347. } elseif (in_array($status, ['FAILED', 'CANCELED', 'REFUNDED'])) {
  348. $orderStatus = 2; // 失败
  349. }
  350. if ($orderStatus === 0) {
  351. Util::WriteLog('NewSupefinaSpei', 'payout processing: '.$OrderId.' status='.$status);
  352. return '{"success":true}';
  353. }
  354. $agentID = DB::connection('write')
  355. ->table('agent.dbo.admin_configs')
  356. ->where('config_value', self::AGENT)
  357. ->where('type', 'cash')
  358. ->value('id') ?? '';
  359. $now = now();
  360. $UserID = $query->UserID;
  361. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  362. // Supefina 回调里带有 amount/fee 和 realityAmount/realityFee
  363. // 提醒:这里仍以我们订单金额为准做账,仅将真实金额用于日志,可根据需要扩展到统计侧。
  364. $realityAmount = isset($post['amount']) ? (float)$post['amount'] : null;
  365. $realityFee = isset($post['fee']) ? (float)$post['fee'] : null;
  366. Util::WriteLog('NewSupefinaSpei', 'payout reality: amount='.$realityAmount.', fee='.$realityFee.', orderId='.$OrderId);
  367. $withdraw_data = [];
  368. switch ($orderStatus) {
  369. case 1: // 提现成功
  370. $withdraw_data = [
  371. 'State' => 2,
  372. 'agent' => $agentID,
  373. 'finishDate' => $now,
  374. 'withdraw_fee' => $realityFee * NumConfig::NUM_VALUE,
  375. ];
  376. // 增加提现记录
  377. $first = DB::connection('write')
  378. ->table('QPAccountsDB.dbo.UserTabData')
  379. ->where('UserID', $UserID)
  380. ->first();
  381. if ($first) {
  382. DB::connection('write')
  383. ->table('QPAccountsDB.dbo.UserTabData')
  384. ->where('UserID', $UserID)
  385. ->increment('TakeMoney', $TakeMoney);
  386. } else {
  387. DB::connection('write')
  388. ->table('QPAccountsDB.dbo.UserTabData')
  389. ->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  390. try {
  391. PrivateMail::praiseSendMail($UserID);
  392. } catch (\Throwable $e) {
  393. // 忽略邮件发送失败
  394. }
  395. }
  396. // 免审记录
  397. $withdrawal_position_log = DB::connection('write')
  398. ->table('agent.dbo.withdrawal_position_log')
  399. ->where('order_sn', $OrderId)
  400. ->first();
  401. if ($withdrawal_position_log) {
  402. DB::connection('write')
  403. ->table('agent.dbo.withdrawal_position_log')
  404. ->where('order_sn', $OrderId)
  405. ->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
  406. }
  407. try {
  408. StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
  409. } catch (\Throwable $exception) {
  410. Util::WriteLog('StoredProcedure', $exception->getMessage());
  411. }
  412. $ServiceFee = $query->ServiceFee;
  413. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
  414. $fee = DB::table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $OrderId)
  415. ->value('withdraw_fee');
  416. (new RechargeWithDraw())->withDraw($UserID, $TakeMoney, $realityFee * NumConfig::NUM_VALUE, $ServiceFee);
  417. $redis = Redis::connection();
  418. $redis->incr('draw_'.date('Ymd').$UserID);
  419. break;
  420. case 2: // 提现失败
  421. $msg = $post['msg'] ?? 'Withdraw rejected';
  422. $bonus = '30000,'.$TakeMoney;
  423. PrivateMail::failMail($query->UserID, $OrderId, $TakeMoney, $msg, $bonus);
  424. Util::WriteLog('SupefinaSpeiEmail', [$query->UserID, $OrderId, $TakeMoney, $msg, $bonus]);
  425. $withdraw_data = [
  426. 'State' => 6,
  427. 'agent' => $agentID,
  428. 'remark' => $msg,
  429. ];
  430. break;
  431. }
  432. $RecordData = [
  433. 'before_state' => $query->State,
  434. 'after_state' => $withdraw_data['State'] ?? 0,
  435. 'RecordID' => $query->RecordID,
  436. 'update_at' => date('Y-m-d H:i:s'),
  437. ];
  438. DB::connection('write')
  439. ->table('QPAccountsDB.dbo.AccountsRecord')
  440. ->updateOrInsert(
  441. ['RecordID' => $query->RecordID, 'type' => 1],
  442. $RecordData
  443. );
  444. DB::connection('write')
  445. ->table('QPAccountsDB.dbo.OrderWithDraw')
  446. ->where('OrderId', $OrderId)
  447. ->update($withdraw_data);
  448. if (isset($withdraw_data['State']) && (int)$withdraw_data['State'] === 2) {
  449. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  450. }
  451. return '{"success":true}';
  452. }
  453. public function getSignString($headers, $data)
  454. {
  455. $params = array_merge($headers, $data);
  456. $filtered = [];
  457. foreach ($params as $k => $v) {
  458. if (strtolower($k) === 'sign') {
  459. continue;
  460. }
  461. if ($v === null) {
  462. continue;
  463. }
  464. $filtered[$k] = $v;
  465. }
  466. ksort($filtered);
  467. $parts = [];
  468. foreach ($filtered as $k => $v) {
  469. $parts[] = $k . '=' . $v;
  470. }
  471. $signString = implode('&', $parts);
  472. return $signString;
  473. }
  474. public function sign($headers, $data, $key)
  475. {
  476. $signString = $this->getSignString($headers, $data);
  477. $res = openssl_pkey_get_private("-----BEGIN PRIVATE KEY-----\n" . $key . "\n-----END PRIVATE KEY-----");
  478. openssl_sign(
  479. $signString,
  480. $signature,
  481. $res,
  482. OPENSSL_ALGO_SHA256 // RSA2 核心
  483. );
  484. return base64_encode($signature);
  485. }
  486. public function verifySign($data, $sign, $key)
  487. {
  488. $res = openssl_pkey_get_public("-----BEGIN PUBLIC KEY-----\n" . $key . "\n-----END PUBLIC KEY-----");
  489. $result = openssl_verify(
  490. $data,
  491. base64_decode($sign),
  492. $res,
  493. OPENSSL_ALGO_SHA256
  494. );
  495. return $result === 1;
  496. }
  497. private function sendRequest($url, $productNo, $data)
  498. {
  499. $config = (new PayConfig())->getConfig('NewSupefinaSpei');
  500. $headers = [
  501. 'version' => '1.0.0',
  502. 'merchantNo' => $config['merId'] ?? '',
  503. 'requestId' => mt_rand(100000, 999999) . time(),
  504. 'requestTime' => time() * 1000,
  505. ];
  506. $body = [
  507. 'productNo' => $productNo,
  508. 'data' => Aes::encrypt(json_encode($data), base64_decode($config['aesKey'])),
  509. ];
  510. $sign = $this->sign($headers, $body, $config['privateKey']);
  511. $headers['sign'] = $sign;
  512. try {
  513. $client = new Client();
  514. Util::WriteLog('NewSupefinaSpei', $productNo . ' request: ' . $url . ' | ' . json_encode($data) . '|' . json_encode($headers));
  515. $result = $client->post($url, [
  516. 'headers' => $headers,
  517. 'json' => $body,
  518. ])->getBody()->getContents();
  519. Util::WriteLog('NewSupefinaSpei', $productNo . ' response: ' . $result);
  520. return $result;
  521. } catch (\Throwable $e) {
  522. Util::WriteLog('NewSupefinaSpei_error', $productNo . ' request exception: ' . $e->getMessage());
  523. $this->error = 'Payment processing error';
  524. return false;
  525. }
  526. }
  527. }