BotImPayController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\BotImPayLogic;
  4. use App\Http\logic\api\BotImPayCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\BotImPayService;
  8. use App\Util;
  9. use App\Utility\SetNXLock;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Redis;
  12. class BotImPayController implements PayMentInterFace
  13. {
  14. /**
  15. * 代收下单
  16. */
  17. public function pay_order(
  18. $userId, $payAmt, $userName, $userEmail, $userPhone,
  19. $GiftsID, $buyIP, $AdId, $eventType, $pay_method = ''
  20. ) {
  21. $logic = new BotImPayLogic();
  22. try {
  23. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  24. } catch (\Exception $exception) {
  25. Redis::set("PayErro_BotImPay", 1);
  26. Redis::expire("PayErro_BotImPay", 600);
  27. Util::WriteLog('BotImPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  28. TelegramBot::getDefault()->sendProgramNotify("BotImPay Except ", $exception->getMessage(), $exception);
  29. return apiReturnFail($logic->getError());
  30. }
  31. if (!empty($res) && isset($res['code']) && $res['code'] == 200) {
  32. $data = [
  33. 'content' => $res['data']['order_data'] ?? '',
  34. 'money' => $payAmt,
  35. 'prdOrdNo' => $res['data']['order_no'] ?? '',
  36. ];
  37. return apiReturnSuc($data);
  38. }
  39. if ($res == false) {
  40. return apiReturnFail($logic->getError() ?: 'Payment failed');
  41. }
  42. Redis::set("PayErro_BotImPay", 1);
  43. Redis::expire("PayErro_BotImPay", 600);
  44. $errMsg = $res['message'] ?? 'Unknown error';
  45. TelegramBot::getDefault()->sendProgramNotify("BotImPay ReturnFail ", $errMsg . " | " . json_encode($res));
  46. return apiReturnFail(['web.payment.paytype_error', $errMsg]);
  47. }
  48. /**
  49. * 代收异步回调通知
  50. */
  51. public function notify(Request $request)
  52. {
  53. $post = $request->all();
  54. Util::WriteLog('BotImPay', "BotImPay回调订单\n" . json_encode($post));
  55. $service = new BotImPayService('BotImPay');
  56. try {
  57. $verify = $service->verifySign($post);
  58. } catch (\Exception $e) {
  59. Util::WriteLog('BotImPay', '验签失败:' . $e->getMessage());
  60. return 'fail';
  61. }
  62. if (!$verify) {
  63. Util::WriteLog('BotImPay', '签名错误');
  64. return 'fail';
  65. }
  66. $order_sn = $post['order_no'] ?? '';
  67. if (empty($order_sn)) {
  68. Util::WriteLog('BotImPay', '缺少订单号');
  69. return 'fail';
  70. }
  71. $lockKey = 'pay_notify_BotImPay_' . $order_sn;
  72. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  73. Util::WriteLog('BotImPay', '代收回调并发,订单已处理或处理中: ' . $order_sn);
  74. return 'ok';
  75. }
  76. $logic = new BotImPayLogic();
  77. try {
  78. $ret = $logic->notify($post);
  79. return $ret;
  80. } catch (\Exception $exception) {
  81. Redis::set("PayErro_BotImPay", 1);
  82. Redis::expire("PayErro_BotImPay", 600);
  83. TelegramBot::getDefault()->sendProgramNotify("BotImPay 订单回调执行异常 ", json_encode($post), $exception);
  84. Util::WriteLog("BotImPay_error", $post);
  85. return 'fail';
  86. } finally {
  87. SetNXLock::release($lockKey);
  88. }
  89. }
  90. /**
  91. * 代付异步回调通知
  92. */
  93. public function cash_notify(Request $request)
  94. {
  95. $post = $request->all();
  96. Util::WriteLog('BotImPay', "BotImPay 提现回调\n" . json_encode($post));
  97. $service = new BotImPayService('BotImPayOut');
  98. try {
  99. $verify = $service->verifySign($post);
  100. } catch (\Exception $e) {
  101. Util::WriteLog('BotImPay', '提现回调验签失败:' . $e->getMessage());
  102. return 'FAIL';
  103. }
  104. if (!$verify) {
  105. Util::WriteLog('BotImPay', '提现回调签名错误');
  106. return 'FAIL';
  107. }
  108. $logic = new BotImPayCashierLogic();
  109. try {
  110. return $logic->notify($post);
  111. } catch (\Exception $exception) {
  112. TelegramBot::getDefault()->sendProgramNotify("BotImPay 提现异步回调执行异常 ", json_encode($post), $exception);
  113. Util::WriteLog("BotImPay_error", $post);
  114. return 'SUCCESS';
  115. }
  116. }
  117. }