WDPayController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\WDPayLogic;
  4. use App\Http\logic\api\WDPayCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\WDPay;
  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 WDPayController 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 WDPayLogic();
  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_WDPay", 1);
  25. Redis::expire("PayErro_WDPay", 600);
  26. Util::WriteLog('WDPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  27. TelegramBot::getDefault()->sendProgramNotify("WDPay 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_WDPay", 1);
  42. Redis::expire("PayErro_WDPay", 600);
  43. TelegramBot::getDefault()->sendProgramNotify("WDPay 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. // WDPay官方回调格式(POST JSON):
  55. // {
  56. // "amount": "4.99",
  57. // "currency": "USD",
  58. // "customerOrderNo": "xxxxxx",
  59. // "orderNo": "c66uNp01892LvA",
  60. // "sign": "2D9AB732083EF62AED9990C96A2BBA17",
  61. // "signType": "MD5",
  62. // "status": "succeeded" // succeeded=成功, closed=失败
  63. // }
  64. $post = $request->all();
  65. $payload = json_encode($post, JSON_UNESCAPED_UNICODE);
  66. Util::WriteLog('WDPay', "WDPay支付回调\n" . $payload);
  67. // 验证签名
  68. $service = new WDPay();
  69. try {
  70. $verify = $service->verifySign($post);
  71. } catch (\Exception $e) {
  72. Util::WriteLog('WDPay', '验签失败:' . $e->getMessage());
  73. return response()->json(['success' => false, 'message' => 'Sign verify failed']);
  74. }
  75. if (!$verify) {
  76. Util::WriteLog('WDPay', '签名错误');
  77. return response()->json(['success' => false, 'message' => 'Invalid sign']);
  78. }
  79. // 提取商户订单号
  80. $order_sn = $post['customerOrderNo'] ?? '';
  81. if (empty($order_sn)) {
  82. Util::WriteLog('WDPay', '缺少订单号');
  83. return response()->json(['success' => false, 'message' => 'Missing customerOrderNo']);
  84. }
  85. // 代收回调加锁,防止并发重复处理
  86. $lockKey = 'pay_notify_WDPay_' . $order_sn;
  87. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  88. Util::WriteLog('WDPay', '代收回调并发,订单已处理或处理中: ' . $order_sn);
  89. return '{"msg":"success","code":200}';
  90. }
  91. $logic = new WDPayLogic();
  92. try {
  93. $redis = Redis::connection();
  94. $ret = $logic->notify($post);
  95. if ($ret == '{"msg":"success","code":200}') {
  96. $redis->set("wdpay_notify_" . $order_sn, $order_sn, 3600 * 24);
  97. }
  98. return $ret;
  99. } catch (\Exception $exception) {
  100. Redis::set("PayErro_WDPay", 1);
  101. Redis::expire("PayErro_WDPay", 600);
  102. TelegramBot::getDefault()->sendProgramNotify("WDPay 订单回调执行 异常 ", json_encode($post), $exception);
  103. Util::WriteLog("WDPay_error", $exception->getMessage());
  104. return response()->json(['success' => false, 'message' => 'Process failed']);
  105. } finally {
  106. SetNXLock::release($lockKey);
  107. }
  108. }
  109. public function sync_notify(Request $request)
  110. {
  111. Log::info('WDPay同步回调');
  112. echo 'WDPay同步回调';
  113. }
  114. // 提现异步回调
  115. public function cash_notify(Request $request)
  116. {
  117. $post = $request->all();
  118. $payload = json_encode($post);
  119. Util::WriteLog('WDPay', "WDPay cash 异步回调\n" . $payload);
  120. // ✅ 使用 WDPayOut 配置进行验签
  121. $payConfigService = new PayConfig();
  122. $config = $payConfigService->getConfig('WDPayOut');
  123. $apiKey = $config['key'];
  124. try {
  125. $verify = PayUtils::verifySign($post, $apiKey);
  126. } catch (\Exception $e) {
  127. Util::WriteLog('WDPay cash', '验签失败:' . $e->getMessage());
  128. return 'fail';
  129. }
  130. if (!$verify) {
  131. Util::WriteLog('WDPay cash', '签名错误');
  132. return 'fail';
  133. }
  134. $logic = new WDPayCashierLogic();
  135. try {
  136. return $logic->notify($post);
  137. } catch (\Exception $exception) {
  138. TelegramBot::getDefault()->sendProgramNotify("WDPay 提现异步回调执行 异常 ", json_encode($post), $exception);
  139. Util::WriteLog("WDPay_error", $post);
  140. return '{"success":false,"message":"商户自定义出错信息"}';
  141. }
  142. }
  143. }