WiwiPayController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\WiwiPayLogic;
  4. use App\Http\logic\api\WiwiPayCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\WiwiPay;
  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 WiwiPayController 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 WiwiPayLogic();
  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_WiwiPay", 1);
  24. Redis::expire("PayErro_WiwiPay", 600);
  25. Util::WriteLog('WiwiPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  26. TelegramBot::getDefault()->sendProgramNotify("WiwiPay 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_WiwiPay", 1);
  41. Redis::expire("PayErro_WiwiPay", 600);
  42. TelegramBot::getDefault()->sendProgramNotify("WiwiPay 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. $post = $request->all();
  54. $payload = json_encode($post);
  55. Util::WriteLog('WiwiPay', "WiwiPay回调订单\n" . $payload);
  56. $service = new WiwiPay();
  57. try {
  58. $verify = $service->verifySign($post);
  59. } catch (\Exception $e) {
  60. Util::WriteLog('WiwiPay', '验签失败:' . $e->getMessage());
  61. return 'fail';
  62. }
  63. if (!$verify) {
  64. Util::WriteLog('WiwiPay', '签名错误');
  65. return 'fail';
  66. }
  67. $order_sn = @$post['mchOrderNo'];
  68. $logic = new WiwiPayLogic();
  69. try {
  70. $redis = Redis::connection();
  71. $ret = $logic->notify($post);
  72. if ($ret == 'success') $redis->set($order_sn, $order_sn, 3600 * 24);
  73. return $ret;
  74. } catch (\Exception $exception) {
  75. Redis::set("PayErro_WiwiPay", 1);
  76. Redis::expire("PayErro_WiwiPay", 600);
  77. TelegramBot::getDefault()->sendProgramNotify("WiwiPay 订单回调执行 异常 ", json_encode($post), $exception);
  78. Util::WriteLog("WiwiPay_error", $post);
  79. return '{"success":false,"message":"商户自定义出错信息"}';
  80. }
  81. }
  82. public function sync_notify(Request $request)
  83. {
  84. Log::info('WiwiPay同步回调');
  85. echo 'WiwiPay同步回调';
  86. }
  87. // 提现异步回调
  88. public function cash_notify(Request $request)
  89. {
  90. $post = $request->all();
  91. $payload = json_encode($post);
  92. Util::WriteLog('WiwiPay', "WiwiPay cash 异步回调\n" . $payload);
  93. // ✅ 使用 WiwiPayOut 配置进行验签
  94. $payConfigService = new PayConfig();
  95. $config = $payConfigService->getConfig('WiwiPayOut');
  96. $apiKey = $config['key'];
  97. try {
  98. $verify = PayUtils::verifySign($post, $apiKey);
  99. } catch (\Exception $e) {
  100. Util::WriteLog('WiwiPay cash', '验签失败:' . $e->getMessage());
  101. return 'fail';
  102. }
  103. if (!$verify) {
  104. Util::WriteLog('WiwiPay cash', '签名错误');
  105. return 'fail';
  106. }
  107. $logic = new WiwiPayCashierLogic();
  108. try {
  109. return $logic->notify($post);
  110. } catch (\Exception $exception) {
  111. TelegramBot::getDefault()->sendProgramNotify("WiwiPay 提现异步回调执行 异常 ", json_encode($post), $exception);
  112. Util::WriteLog("WiwiPay_error", $post);
  113. return '{"success":false,"message":"商户自定义出错信息"}';
  114. }
  115. }
  116. }