CoinPayController.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\CoinPayCashierLogic;
  4. use App\Http\logic\api\CoinPayLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\CoinPay;
  8. use App\Util;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Redis;
  11. class CoinPayController 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 CoinPayLogic();
  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_CoinPay', 1, 'EX', 600);
  21. Util::WriteLog('CoinPay_error', $exception->getMessage());
  22. TelegramBot::getDefault()->sendProgramNotify('CoinPay pay error', $exception->getMessage(), $exception);
  23. return apiReturnFail($logic->getError());
  24. }
  25. if (isset($res['code']) && (int)$res['code'] === 0) {
  26. $data = [
  27. 'content' => $res['data']['url'] ?? '',
  28. 'money' => $payAmt,
  29. 'prdOrdNo' => $res['data']['orderNo'] ?? '',
  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_CoinPay', 1, 'EX', 600);
  38. return apiReturnFail($logic->getError());
  39. }
  40. $this->retryTimes++;
  41. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  42. }
  43. public function notify(Request $request)
  44. {
  45. $post = $request->all();
  46. Util::WriteLog('CoinPay', 'pay notify: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  47. $service = new CoinPay();
  48. if (!$service->verify($post)) {
  49. Util::WriteLog('CoinPay', 'pay notify verify failed');
  50. return 'fail';
  51. }
  52. $logic = new CoinPayLogic();
  53. try {
  54. return $logic->notify($post);
  55. } catch (\Throwable $exception) {
  56. Redis::set('PayErro_CoinPay', 1, 'EX', 600);
  57. return '{"success":false,"message":"internal error"}';
  58. }
  59. }
  60. public function sync_notify(Request $request)
  61. {
  62. Util::WriteLog('CoinPay', 'sync callback: ' . json_encode($request->all(), JSON_UNESCAPED_UNICODE));
  63. return 'success';
  64. }
  65. public function cash_notify(Request $request)
  66. {
  67. $post = $request->all();
  68. Util::WriteLog('CoinPay', 'cash notify: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  69. $service = new CoinPay('CoinPayOut');
  70. if (!$service->verify($post)) {
  71. Util::WriteLog('CoinPay', 'cash notify verify failed');
  72. return 'fail';
  73. }
  74. $logic = new CoinPayCashierLogic();
  75. try {
  76. return $logic->notify($post);
  77. } catch (\Throwable $exception) {
  78. return '{"success":false,"message":"internal error"}';
  79. }
  80. }
  81. }