AiPayLogic.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Pay\AccountPayInfo;
  4. use App\dao\Pay\PayController;
  5. use App\Facade\TableName;
  6. use App\Http\helper\CreateOrder;
  7. use App\Http\helper\NumConfig;
  8. use App\Jobs\Order as OrderJob;
  9. use App\Notification\TelegramBot;
  10. use App\Services\AiPay;
  11. use App\Services\OrderServices;
  12. use App\Services\PayConfig;
  13. use App\Services\CreateLog;
  14. use App\Util;
  15. use Illuminate\Support\Facades\DB;
  16. class AiPayLogic extends BaseApiLogic
  17. {
  18. public $result;
  19. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  20. {
  21. $dao = new AccountPayInfo();
  22. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  23. $PayVerify = new PayController();
  24. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  25. $this->error = $PayVerify->getError();
  26. return false;
  27. }
  28. if ($pay_amount < 0) {
  29. $this->error = 'Payment error_4';
  30. return false;
  31. }
  32. $service = new AiPay();
  33. $config = $service->getConfig();
  34. $order_sn = CreateOrder::order_sn($userId);
  35. $orderLogic = new OrderLogic();
  36. $amountInt = (int) round($pay_amount * NumConfig::NUM_VALUE);
  37. $orderLogic->orderCreate($order_sn, $amountInt, 'AiPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
  38. $methodMap = [
  39. 1 => '1101',
  40. 2 => '1104',
  41. 4 => '1108',
  42. 8 => '1109',
  43. ];
  44. $paymentMethod = $methodMap[$pay_method] ?? ($config['paymentMethod'] ?? '1101');
  45. $params = [
  46. 'mchNo' => $config['mchNo'] ?? '',
  47. 'mchOrderNo' => $order_sn,
  48. 'paymentMethod' => (string)$paymentMethod,
  49. 'amount' => number_format($pay_amount, 2, '.', ''),
  50. 'notifyUrl' => $config['notify'] ?? '',
  51. 'returnUrl' => $config['return'] ?? '',
  52. 'clientId' => (string)$userId,
  53. 'clientIp' => $buyIP,
  54. 'timestamp' => (string)round(microtime(true) * 1000),
  55. ];
  56. // if (!empty($config['accountData']) && is_array($config['accountData'])) {
  57. // $params['accountData'] = $config['accountData'];
  58. // }
  59. $signedParams = $service->sign($params);
  60. CreateLog::pay_request($userPhone, json_encode($signedParams), $order_sn, $userEmail, $userId, $userName);
  61. $response = $service->post('/api/payment', $signedParams);
  62. Util::WriteLog('AiPay', 'pay request => ' . json_encode($signedParams, JSON_UNESCAPED_UNICODE));
  63. Util::WriteLog('AiPay', 'pay response => ' . $response);
  64. try {
  65. $data = \GuzzleHttp\json_decode($response, true);
  66. } catch (\Throwable $e) {
  67. Util::WriteLog('AiPay_error', $e->getMessage());
  68. $this->error = 'Payment processing error';
  69. return false;
  70. }
  71. if (!isset($data['code']) || (int)$data['code'] !== 0) {
  72. $this->error = $data['msg'] ?? 'Payment request failed';
  73. }
  74. return $data;
  75. }
  76. public function notify(array $post)
  77. {
  78. $order_sn = $post['mchOrderNo'] ?? '';
  79. if (!$order_sn) {
  80. return '{"success":false,"message":"missing order"}';
  81. }
  82. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
  83. if (!$order) {
  84. Util::WriteLog('AiPay', 'order not found: ' . $order_sn);
  85. return '{"success":false,"message":"order not found"}';
  86. }
  87. if (!empty($order->pay_at) && !empty($order->finished_at)) {
  88. return 'success';
  89. }
  90. $status = (int)($post['status'] ?? 0);
  91. $payAmt = (float)($post['actualAmount'] ?? $post['amount'] ?? 0);
  92. $body = [
  93. 'payment_sn' => $post['sysOrderNo'] ?? '',
  94. 'updated_at' => date('Y-m-d H:i:s'),
  95. ];
  96. $GiftsID = $order->GiftsID ?: '';
  97. $userID = $order->user_id ?: '';
  98. $AdId = $order->AdId ?: '';
  99. $eventType = $order->eventType ?: '';
  100. switch ($status) {
  101. case 3:
  102. $body['pay_status'] = 1;
  103. $body['pay_at'] = date('Y-m-d H:i:s');
  104. $body['finished_at'] = date('Y-m-d H:i:s');
  105. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  106. $config = (new PayConfig())->getConfig('AiPay');
  107. if (!empty($config['payin_fee'])) {
  108. $body['payment_fee'] = $body['amount'] * (float)$config['payin_fee'];
  109. }
  110. try {
  111. $service = new OrderServices();
  112. if ((int)$order->amount !== $body['amount']) {
  113. $body['GiftsID'] = 0;
  114. $Recharge = $payAmt;
  115. $give = 0;
  116. $favorable_price = $Recharge;
  117. $czReason = 1;
  118. $cjReason = 45;
  119. } else {
  120. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  121. }
  122. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
  123. OrderJob::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_sn]);
  124. } catch (\Throwable $exception) {
  125. Util::WriteLog('AiPay_error', $exception->getMessage());
  126. TelegramBot::getDefault()->sendProgramNotify('AiPay notify error', $exception->getMessage(), $exception);
  127. }
  128. break;
  129. case 4:
  130. $body['pay_status'] = 2;
  131. break;
  132. default:
  133. return 'fail';
  134. }
  135. DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->update($body);
  136. return 'success';
  137. }
  138. }