2
0

PagYeepPayController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Redis;
  14. class PagYeepPayController implements PayMentInterFace
  15. {
  16. private $retryTimes = 0;
  17. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  18. {
  19. $logic = new PagYeepPayLogic();
  20. try {
  21. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  22. } catch (\Exception $exception) {
  23. Redis::set("PayErro_PagYeepPay", 1);
  24. Redis::expire("PayErro_PagYeepPay", 600);
  25. Util::WriteLog('PagYeepPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  26. TelegramBot::getDefault()->sendProgramNotify("PagYeepPay Except ", $exception->getMessage(), $exception);
  27. return apiReturnFail($logic->getError());
  28. }
  29. if (!empty($res) && isset($res['code']) && $res['code'] == 0) {
  30. $data = [
  31. 'content' => $res['data']['cashierUrl'],
  32. 'money' => $payAmt,
  33. 'prdOrdNo' => $res['data']['mchOrderNo'],
  34. ];
  35. return apiReturnSuc($data);
  36. } else if ($res == false) {
  37. return apiReturnFail($logic->getError());
  38. } else {
  39. if ($this->retryTimes > 0) {
  40. Redis::set("PayErro_PagYeepPay", 1);
  41. Redis::expire("PayErro_PagYeepPay", 600);
  42. TelegramBot::getDefault()->sendProgramNotify("PagYeepPay RetrunFail ", $logic->getError() . " | " . json_encode($res));
  43. return apiReturnFail($logic->getError());
  44. } else {
  45. $this->retryTimes++;
  46. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  47. }
  48. }
  49. }
  50. // 支付异步回调
  51. public function notify(Request $request)
  52. {
  53. // PagYeepPay回调格式(POST form表单):
  54. // version, orgNo, custId, custOrderNo, prdOrdNo, payAmt, ordStatus, sign
  55. $post = $request->all();
  56. $payload = json_encode($post, JSON_UNESCAPED_UNICODE);
  57. Util::WriteLog('PagYeepPay', "PagYeepPay支付回调\n" . $payload);
  58. // 验证签名
  59. $service = new PagYeepPay();
  60. try {
  61. $verify = $service->verifySign($post);
  62. } catch (\Exception $e) {
  63. Util::WriteLog('PagYeepPay', '验签失败:' . $e->getMessage());
  64. return 'SC000000'; // 仍然返回成功,避免重复回调
  65. }
  66. if (!$verify) {
  67. Util::WriteLog('PagYeepPay', '签名错误');
  68. return 'SC000000'; // 仍然返回成功,避免重复回调
  69. }
  70. // 提取商户订单号
  71. $order_sn = $post['custOrderNo'] ?? '';
  72. if (empty($order_sn)) {
  73. Util::WriteLog('PagYeepPay', '缺少订单号');
  74. return 'SC000000';
  75. }
  76. $logic = new PagYeepPayLogic();
  77. try {
  78. $redis = Redis::connection();
  79. $ret = $logic->notify($post);
  80. if ($ret == 'SC000000') {
  81. $redis->set("pagyeepay_notify_" . $order_sn, $order_sn, 3600 * 24);
  82. }
  83. return $ret;
  84. } catch (\Exception $exception) {
  85. Redis::set("PayErro_PagYeepPay", 1);
  86. Redis::expire("PayErro_PagYeepPay", 600);
  87. TelegramBot::getDefault()->sendProgramNotify("PagYeepPay 订单回调执行 异常 ", json_encode($post), $exception);
  88. Util::WriteLog("PagYeepPay_error", $exception->getMessage());
  89. return 'SC000000';
  90. }
  91. }
  92. public function sync_notify(Request $request)
  93. {
  94. echo "Success";
  95. }
  96. // 提现异步回调
  97. public function cash_notify(Request $request)
  98. {
  99. $post = $request->all();
  100. $payload = json_encode($post, JSON_UNESCAPED_UNICODE);
  101. Util::WriteLog('PagYeepPay', "PagYeepPay cash 异步回调\n" . $payload);
  102. // 使用 PagYeepPayOut 配置进行验签
  103. $payConfigService = new PayConfig();
  104. $config = $payConfigService->getConfig('PagYeepPayOut');
  105. $service = new PagYeepPay();
  106. $service->config = $config;
  107. $service->key = $config['key'] ?? '';
  108. try {
  109. $verify = $service->verifySign($post);
  110. } catch (\Exception $e) {
  111. Util::WriteLog('PagYeepPay cash', '验签失败:' . $e->getMessage());
  112. return 'SC000000';
  113. }
  114. if (!$verify) {
  115. Util::WriteLog('PagYeepPay cash', '签名错误');
  116. return 'SC000000';
  117. }
  118. $logic = new PagYeepPayCashierLogic();
  119. try {
  120. return $logic->notify($post);
  121. } catch (\Exception $exception) {
  122. TelegramBot::getDefault()->sendProgramNotify("PagYeepPay 提现异步回调执行 异常 ", json_encode($post), $exception);
  123. Util::WriteLog("PagYeepPay_error", $post);
  124. return 'SC000000';
  125. }
  126. }
  127. }