AiPayController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\AiPayCashierLogic;
  4. use App\Http\logic\api\AiPayLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\AiPay;
  8. use App\Util;
  9. use App\Utility\SetNXLock;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Redis;
  12. class AiPayController implements PayMentInterFace
  13. {
  14. private $retryTimes = 0;
  15. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  16. {
  17. $logic = new AiPayLogic();
  18. try {
  19. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  20. } catch (\Throwable $exception) {
  21. Redis::set('PayErro_AiPay', 1, 'EX', 600);
  22. Util::WriteLog('AiPay_error', $exception->getMessage());
  23. TelegramBot::getDefault()->sendProgramNotify('AiPay pay_order exception', $exception->getMessage(), $exception);
  24. return apiReturnFail($logic->getError());
  25. }
  26. if (isset($res['code']) && (int)$res['code'] === 0) {
  27. $data = [
  28. 'content' => $res['data']['payUrl'] ?? '',
  29. 'money' => $payAmt,
  30. 'prdOrdNo' => $res['data']['sysOrderNo'] ?? '',
  31. ];
  32. return apiReturnSuc($data);
  33. }
  34. if ($res === false) {
  35. return apiReturnFail($logic->getError());
  36. }
  37. if ($this->retryTimes > 0) {
  38. Redis::set('PayErro_AiPay', 1, 'EX', 600);
  39. TelegramBot::getDefault()->sendProgramNotify('AiPay pay_order failed', json_encode($res));
  40. return apiReturnFail($logic->getError());
  41. }
  42. $this->retryTimes++;
  43. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  44. }
  45. public function notify(Request $request)
  46. {
  47. $post = $request->all();
  48. Util::WriteLog('AiPay', 'pay notify: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  49. $service = new AiPay();
  50. if (!$service->verify($post)) {
  51. Util::WriteLog('AiPay', 'pay notify verify failed');
  52. return 'fail';
  53. }
  54. $order_sn = $post['mchOrderNo'] ?? '';
  55. if (empty($order_sn)) {
  56. Util::WriteLog('AiPay', '缺少订单号');
  57. return 'fail';
  58. }
  59. // 代收回调加锁,防止并发重复处理
  60. $lockKey = 'pay_notify_AiPay_' . $order_sn;
  61. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  62. Util::WriteLog('AiPay', '代收回调并发,订单已处理或处理中: ' . $order_sn);
  63. return 'success';
  64. }
  65. $logic = new AiPayLogic();
  66. try {
  67. return $logic->notify($post);
  68. } catch (\Throwable $exception) {
  69. Redis::set('PayErro_AiPay', 1, 'EX', 600);
  70. //TelegramBot::getDefault()->sendProgramNotify('AiPay notify exception', json_encode($post), $exception);
  71. return '{"success":false,"message":"internal error"}';
  72. } finally {
  73. SetNXLock::release($lockKey);
  74. }
  75. }
  76. public function sync_notify(Request $request)
  77. {
  78. Util::WriteLog('AiPay', 'sync callback: ' . json_encode($request->all(), JSON_UNESCAPED_UNICODE));
  79. return 'success';
  80. }
  81. public function cash_notify(Request $request)
  82. {
  83. $post = $request->all();
  84. Util::WriteLog('AiPay', 'cash notify: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  85. $service = new AiPay('AiPayOut');
  86. if($post['amount']){
  87. $post['amount'] = number_format($post['amount'], 2, '.', '');
  88. }
  89. if (!$service->verify($post)) {
  90. Util::WriteLog('AiPay', 'cash notify verify failed');
  91. return 'fail';
  92. }
  93. $logic = new AiPayCashierLogic();
  94. try {
  95. return $logic->notify($post);
  96. } catch (\Throwable $exception) {
  97. TelegramBot::getDefault()->sendProgramNotify('AiPay cash notify exception', json_encode($post), $exception);
  98. return '{"success":false,"message":"internal error"}';
  99. }
  100. }
  101. }