PagYeepPayController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\PagYeepPayLogic;
  4. use App\Http\logic\api\PagYeepPayCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\PagYeepPay;
  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 PagYeepPayController 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 PagYeepPayLogic();
  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_PagYeepPay", 1);
  25. Redis::expire("PayErro_PagYeepPay", 600);
  26. Util::WriteLog('PagYeepPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  27. TelegramBot::getDefault()->sendProgramNotify("PagYeepPay 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_PagYeepPay", 1);
  42. Redis::expire("PayErro_PagYeepPay", 600);
  43. TelegramBot::getDefault()->sendProgramNotify("PagYeepPay 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. // PagYeepPay回调格式(POST form表单):
  55. // version, orgNo, custId, custOrderNo, prdOrdNo, payAmt, ordStatus, sign
  56. $post = $request->all();
  57. $payload = json_encode($post, JSON_UNESCAPED_UNICODE);
  58. Util::WriteLog('PagYeepPay', "PagYeepPay支付回调\n" . $payload);
  59. // 验证签名
  60. $service = new PagYeepPay();
  61. try {
  62. $verify = $service->verifySign($post);
  63. } catch (\Exception $e) {
  64. Util::WriteLog('PagYeepPay', '验签失败:' . $e->getMessage());
  65. return 'SC000000'; // 仍然返回成功,避免重复回调
  66. }
  67. if (!$verify) {
  68. Util::WriteLog('PagYeepPay', '签名错误');
  69. return 'SC000000'; // 仍然返回成功,避免重复回调
  70. }
  71. // 提取商户订单号
  72. $order_sn = $post['custOrderNo'] ?? '';
  73. if (empty($order_sn)) {
  74. Util::WriteLog('PagYeepPay', '缺少订单号');
  75. return 'SC000000';
  76. }
  77. // 代收回调加锁,防止并发重复处理
  78. $lockKey = 'pay_notify_PagYeepPay_' . $order_sn;
  79. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  80. Util::WriteLog('PagYeepPay', '代收回调并发,订单已处理或处理中: ' . $order_sn);
  81. return 'SC000000';
  82. }
  83. $logic = new PagYeepPayLogic();
  84. try {
  85. $redis = Redis::connection();
  86. $ret = $logic->notify($post);
  87. if ($ret == 'SC000000') {
  88. $redis->set("pagyeepay_notify_" . $order_sn, $order_sn, 3600 * 24);
  89. }
  90. return $ret;
  91. } catch (\Exception $exception) {
  92. Redis::set("PayErro_PagYeepPay", 1);
  93. Redis::expire("PayErro_PagYeepPay", 600);
  94. TelegramBot::getDefault()->sendProgramNotify("PagYeepPay 订单回调执行 异常 ", json_encode($post), $exception);
  95. Util::WriteLog("PagYeepPay_error", $exception->getMessage());
  96. return 'SC000000';
  97. } finally {
  98. SetNXLock::release($lockKey);
  99. }
  100. }
  101. public function sync_notify(Request $request)
  102. {
  103. echo "Success";
  104. }
  105. // 提现异步回调
  106. public function cash_notify(Request $request)
  107. {
  108. $post = $request->all();
  109. $payload = json_encode($post, JSON_UNESCAPED_UNICODE);
  110. Util::WriteLog('PagYeepPay', "PagYeepPay cash 异步回调\n" . $payload);
  111. // 使用 PagYeepPayOut 配置进行验签
  112. $payConfigService = new PayConfig();
  113. $config = $payConfigService->getConfig('PagYeepPayOut');
  114. $service = new PagYeepPay();
  115. $service->config = $config;
  116. $service->key = $config['key'] ?? '';
  117. try {
  118. $verify = $service->verifySign($post);
  119. } catch (\Exception $e) {
  120. Util::WriteLog('PagYeepPay cash', '验签失败:' . $e->getMessage());
  121. return 'SC000000';
  122. }
  123. if (!$verify) {
  124. Util::WriteLog('PagYeepPay cash', '签名错误');
  125. return 'SC000000';
  126. }
  127. $logic = new PagYeepPayCashierLogic();
  128. try {
  129. return $logic->notify($post);
  130. } catch (\Exception $exception) {
  131. TelegramBot::getDefault()->sendProgramNotify("PagYeepPay 提现异步回调执行 异常 ", json_encode($post), $exception);
  132. Util::WriteLog("PagYeepPay_error", $post);
  133. return 'SC000000';
  134. }
  135. }
  136. }