AiPayController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Redis;
  11. class AiPayController implements PayMentInterFace
  12. {
  13. private $retryTimes = 0;
  14. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  15. {
  16. $logic = new AiPayLogic();
  17. try {
  18. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  19. } catch (\Throwable $exception) {
  20. Redis::set('PayErro_AiPay', 1, 'EX', 600);
  21. Util::WriteLog('AiPay_error', $exception->getMessage());
  22. TelegramBot::getDefault()->sendProgramNotify('AiPay pay_order exception', $exception->getMessage(), $exception);
  23. return apiReturnFail($logic->getError());
  24. }
  25. if (isset($res['code']) && (int)$res['code'] === 0) {
  26. $data = [
  27. 'content' => $res['data']['payUrl'] ?? '',
  28. 'money' => $payAmt,
  29. 'prdOrdNo' => $res['data']['sysOrderNo'] ?? '',
  30. ];
  31. return apiReturnSuc($data);
  32. }
  33. if ($res === false) {
  34. return apiReturnFail($logic->getError());
  35. }
  36. if ($this->retryTimes > 0) {
  37. Redis::set('PayErro_AiPay', 1, 'EX', 600);
  38. TelegramBot::getDefault()->sendProgramNotify('AiPay pay_order failed', json_encode($res));
  39. return apiReturnFail($logic->getError());
  40. }
  41. $this->retryTimes++;
  42. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  43. }
  44. public function notify(Request $request)
  45. {
  46. $post = $request->all();
  47. Util::WriteLog('AiPay', 'pay notify: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  48. $service = new AiPay();
  49. if (!$service->verify($post)) {
  50. Util::WriteLog('AiPay', 'pay notify verify failed');
  51. return 'fail';
  52. }
  53. $logic = new AiPayLogic();
  54. try {
  55. return $logic->notify($post);
  56. } catch (\Throwable $exception) {
  57. Redis::set('PayErro_AiPay', 1, 'EX', 600);
  58. //TelegramBot::getDefault()->sendProgramNotify('AiPay notify exception', json_encode($post), $exception);
  59. return '{"success":false,"message":"internal error"}';
  60. }
  61. }
  62. public function sync_notify(Request $request)
  63. {
  64. Util::WriteLog('AiPay', 'sync callback: ' . json_encode($request->all(), JSON_UNESCAPED_UNICODE));
  65. return 'success';
  66. }
  67. public function cash_notify(Request $request)
  68. {
  69. $post = $request->all();
  70. Util::WriteLog('AiPay', 'cash notify: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  71. $service = new AiPay('AiPayOut');
  72. if($post['amount']){
  73. $post['amount'] = number_format($post['amount'], 2, '.', '');
  74. }
  75. if (!$service->verify($post)) {
  76. Util::WriteLog('AiPay', 'cash notify verify failed');
  77. return 'fail';
  78. }
  79. $logic = new AiPayCashierLogic();
  80. try {
  81. return $logic->notify($post);
  82. } catch (\Throwable $exception) {
  83. TelegramBot::getDefault()->sendProgramNotify('AiPay cash notify exception', json_encode($post), $exception);
  84. return '{"success":false,"message":"internal error"}';
  85. }
  86. }
  87. }