AiNewPayController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\AiNewPayLogic;
  4. use App\Http\logic\api\AiNewPayCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\AiNewPay;
  8. use App\Services\PayConfig;
  9. use App\Services\PayUtils;
  10. use App\Util;
  11. use App\Utility\SetNXLock;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Redis;
  15. class AiNewPayController implements PayMentInterFace
  16. {
  17. private $retryTimes = 0;
  18. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  19. {
  20. $logic = new AiNewPayLogic();
  21. try {
  22. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  23. } catch (\Exception $exception) {
  24. Redis::set("PayErro_AiNewPay", 1);
  25. Redis::expire("PayErro_AiNewPay", 600);
  26. Util::WriteLog('AiNewPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  27. TelegramBot::getDefault()->sendProgramNotify("AiNewPay Except ", $exception->getMessage(), $exception);
  28. return apiReturnFail($logic->getError());
  29. }
  30. if (!empty($res) && isset($res['code']) && $res['code'] == 0) {
  31. $data = [
  32. 'content' => $res['data']['cashierUrl'],
  33. 'money' => $payAmt,
  34. 'prdOrdNo' => $res['data']['mchOrderNo'],
  35. ];
  36. return apiReturnSuc($data);
  37. } else if ($res == false) {
  38. return apiReturnFail($logic->getError());
  39. } else {
  40. if ($this->retryTimes > 0) {
  41. Redis::set("PayErro_AiNewPay", 1);
  42. Redis::expire("PayErro_AiNewPay", 600);
  43. TelegramBot::getDefault()->sendProgramNotify("AiNewPay RetrunFail ", $logic->getError() . " | " . json_encode($res));
  44. return apiReturnFail($logic->getError());
  45. } else {
  46. $this->retryTimes++;
  47. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  48. }
  49. }
  50. }
  51. // 支付异步回调
  52. public function notify(Request $request)
  53. {
  54. $post = $request->all();
  55. $payload = json_encode($post);
  56. Util::WriteLog('AiNewPay', "AiNewPay回调订单\n" . $payload);
  57. $service = new AiNewPay();
  58. try {
  59. $verify = $service->verifySign($post);
  60. } catch (\Exception $e) {
  61. Util::WriteLog('AiNewPay', '验签失败:' . $e->getMessage());
  62. return 'fail';
  63. }
  64. if (!$verify) {
  65. Util::WriteLog('AiNewPay', '签名错误');
  66. return 'fail';
  67. }
  68. $order_sn = @$post['mchOrderNo'];
  69. if (empty($order_sn)) {
  70. Util::WriteLog('AiNewPay', '缺少订单号');
  71. return 'fail';
  72. }
  73. // 代收回调加锁,防止并发重复处理
  74. $lockKey = 'pay_notify_AiNewPay_' . $order_sn;
  75. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  76. Util::WriteLog('AiNewPay', '代收回调并发,订单已处理或处理中: ' . $order_sn);
  77. return 'success';
  78. }
  79. $logic = new AiNewPayLogic();
  80. try {
  81. $redis = Redis::connection();
  82. $ret = $logic->notify($post);
  83. if ($ret == 'success') $redis->set($order_sn, $order_sn, 3600 * 24);
  84. return $ret;
  85. } catch (\Exception $exception) {
  86. Redis::set("PayErro_AiNewPay", 1);
  87. Redis::expire("PayErro_AiNewPay", 600);
  88. TelegramBot::getDefault()->sendProgramNotify("AiNewPay 订单回调执行 异常 ", json_encode($post), $exception);
  89. Util::WriteLog("AiNewPay_error", $post);
  90. return '{"success":false,"message":"商户自定义出错信息"}';
  91. } finally {
  92. SetNXLock::release($lockKey);
  93. }
  94. }
  95. public function sync_notify(Request $request)
  96. {
  97. Log::info('AiNewPay同步回调');
  98. echo 'AiNewPay同步回调';
  99. }
  100. // 提现异步回调
  101. public function cash_notify(Request $request)
  102. {
  103. $post = $request->all();
  104. $payload = json_encode($post);
  105. Util::WriteLog('AiNewPay', "AiNewPay cash 异步回调\n" . $payload);
  106. $payConfigService = new PayConfig();
  107. $config = $payConfigService->getConfig('AiNewPayOut');
  108. $apiKey = $config['key'];
  109. try {
  110. $verify = PayUtils::verifySign($post, $apiKey);
  111. } catch (\Exception $e) {
  112. Util::WriteLog('AiNewPay cash', '验签失败:' . $e->getMessage());
  113. return 'fail';
  114. }
  115. if (!$verify) {
  116. Util::WriteLog('AiNewPay cash', '签名错误');
  117. return 'fail';
  118. }
  119. $logic = new AiNewPayCashierLogic();
  120. try {
  121. return $logic->notify($post);
  122. } catch (\Exception $exception) {
  123. TelegramBot::getDefault()->sendProgramNotify("AiNewPay 提现异步回调执行 异常 ", json_encode($post), $exception);
  124. Util::WriteLog("AiNewPay_error", $post);
  125. return '{"success":false,"message":"商户自定义出错信息"}';
  126. }
  127. }
  128. }