CoinPayLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 as OrderJob;
  8. use App\Notification\TelegramBot;
  9. use App\Services\CoinPay;
  10. use App\Services\OrderServices;
  11. use App\Services\PayConfig;
  12. use App\Services\CreateLog;
  13. use App\Util;
  14. use Illuminate\Support\Facades\DB;
  15. class CoinPayLogic extends BaseApiLogic
  16. {
  17. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  18. {
  19. $dao = new AccountPayInfo();
  20. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  21. $PayVerify = new PayController();
  22. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  23. $this->error = $PayVerify->getError();
  24. return false;
  25. }
  26. if ($pay_amount < 0) {
  27. $this->error = 'Payment error_4';
  28. return false;
  29. }
  30. $service = new CoinPay();
  31. $config = $service->getConfig();
  32. $order_sn = CreateOrder::order_sn($userId);
  33. $orderLogic = new OrderLogic();
  34. $amountInt = (int) round($pay_amount * NumConfig::NUM_VALUE);
  35. $orderLogic->orderCreate($order_sn, $amountInt, 'CoinPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
  36. $language = strtoupper($config['language'] ?? 'en');
  37. $coin = strtoupper($config['coin'] ?? 'USDT');
  38. $protocol = strtoupper($config['protocol'] ?? 'TRC20');
  39. $rateType = (int)($config['rateType'] ?? 2);
  40. $currency = strtoupper($config['currency'] ?? 'USD');
  41. $coinbase = [
  42. 64 => [
  43. 'coin' => 'USDT',
  44. 'protocol' => 'TRC20',
  45. ],
  46. 128 =>[
  47. 'coin' => 'USDT',
  48. 'protocol' => 'ERC20',
  49. ],
  50. 256 => [
  51. 'coin' => 'USDC',
  52. 'protocol' => 'TRC20',
  53. ],
  54. 512 =>[
  55. 'coin' => 'USDC',
  56. 'protocol' => 'ERC20',
  57. ]
  58. ];
  59. $select = $coinbase[$pay_method] ?? $coinbase[64];
  60. $params = [
  61. 'merchantMemberNo' => (string)$userId,
  62. 'merchantOrderNo' => $order_sn,
  63. // 'amount' => $pay_amount,
  64. 'language' => $language,
  65. 'coin' => $select['coin'],
  66. 'rateType' => $rateType,
  67. 'protocol' => $select['protocol'],
  68. 'notifyUrl' => $config['notify'] ?? '',
  69. 'timestamp' => time(),
  70. ];
  71. if ($rateType === 1) {
  72. $params['amount'] = number_format($pay_amount, 8, '.', '');
  73. $params['rate'] = $config['rate'] ?? '1';
  74. } else {
  75. $params['currencyAmount'] = number_format($pay_amount, 2, '.', '');
  76. $params['currency'] = $currency;
  77. }
  78. if (!empty($config['extra'])) {
  79. $params = array_merge($params, $config['extra']);
  80. }
  81. $signedParams = $service->sign($params);
  82. CreateLog::pay_request($userPhone, json_encode($signedParams), $order_sn, $userEmail, $userId, $userName);
  83. $response = $service->post('/order/depositOrderCoinCreate', $signedParams);
  84. Util::WriteLog('CoinPay', 'pay request => ' . json_encode($signedParams, JSON_UNESCAPED_UNICODE));
  85. Util::WriteLog('CoinPay', 'pay response => ' . $response);
  86. try {
  87. $data = \GuzzleHttp\json_decode($response, true);
  88. } catch (\Throwable $e) {
  89. Util::WriteLog('CoinPay_error', $e->getMessage());
  90. $this->error = 'Payment processing error';
  91. return false;
  92. }
  93. if (!isset($data['code']) || (int)$data['code'] !== 0) {
  94. $this->error = $data['msg'] ?? 'Payment request failed';
  95. }
  96. return $data;
  97. }
  98. public function notify(array $post)
  99. {
  100. $order_sn = $post['merchantOrderNo'] ?? '';
  101. if (!$order_sn) {
  102. return '{"success":false,"message":"missing order"}';
  103. }
  104. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
  105. if (!$order) {
  106. Util::WriteLog('CoinPay', 'order not found: ' . $order_sn);
  107. return '{"success":false,"message":"order not found"}';
  108. }
  109. if (!empty($order->pay_at) && !empty($order->finished_at)) {
  110. return 'SUCCESS';
  111. }
  112. $status = (int)($post['state'] ?? 0);
  113. $payAmt = (float)($post['currencyAmount'] ?? $post['amount'] ?? 0);
  114. $body = [
  115. 'payment_sn' => $post['orderNo'] ?? '',
  116. 'updated_at' => date('Y-m-d H:i:s'),
  117. ];
  118. $GiftsID = $order->GiftsID ?: '';
  119. $userID = $order->user_id ?: '';
  120. $AdId = $order->AdId ?: '';
  121. $eventType = $order->eventType ?: '';
  122. switch ($status) {
  123. case 3:
  124. $body['pay_status'] = 1;
  125. $body['pay_at'] = date('Y-m-d H:i:s');
  126. $body['finished_at'] = date('Y-m-d H:i:s');
  127. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  128. $config = (new PayConfig())->getConfig('CoinPay');
  129. if (!empty($config['payin_fee'])) {
  130. $body['payment_fee'] = $body['amount'] * (float)$config['payin_fee'];
  131. }
  132. try {
  133. $service = new OrderServices();
  134. if ((int)$order->amount !== $body['amount']) {
  135. $body['GiftsID'] = 0;
  136. $Recharge = $payAmt;
  137. $give = 0;
  138. $favorable_price = $Recharge;
  139. $czReason = 1;
  140. $cjReason = 45;
  141. } else {
  142. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  143. }
  144. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
  145. OrderJob::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_sn]);
  146. } catch (\Throwable $exception) {
  147. Util::WriteLog('CoinPay_error', $exception->getMessage());
  148. TelegramBot::getDefault()->sendProgramNotify('CoinPay notify error', $exception->getMessage(), $exception);
  149. }
  150. break;
  151. case 4:
  152. case 5:
  153. $body['pay_status'] = 2;
  154. break;
  155. default:
  156. return 'fail';
  157. }
  158. DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->update($body);
  159. return 'SUCCESS';
  160. }
  161. }