WDPayController.php 5.6 KB

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