WiwiPayController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Util;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Redis;
  12. class WiwiPayController implements PayMentInterFace
  13. {
  14. private $retryTimes = 0;
  15. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  16. {
  17. $logic = new WiwiPayLogic();
  18. try {
  19. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  20. } catch (\Exception $exception) {
  21. Redis::set("PayErro_WiwiPay", 1);
  22. Redis::expire("PayErro_WiwiPay", 600);
  23. Util::WriteLog('WiwiPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  24. TelegramBot::getDefault()->sendProgramNotify("WiwiPay Except ", $exception->getMessage(), $exception);
  25. return apiReturnFail($logic->getError());
  26. }
  27. if (!empty($res) && isset($res['status']) && $res['status'] > 0) {
  28. $data = [
  29. 'content' => urldecode($res['payUrl'] ?? $res['url'] ?? ''),
  30. 'money' => $payAmt,
  31. 'prdOrdNo' => $res['mchOrderNo'],
  32. ];
  33. return apiReturnSuc($data);
  34. } else if ($res == false) {
  35. return apiReturnFail($logic->getError());
  36. } else {
  37. if ($this->retryTimes > 0) {
  38. Redis::set("PayErro_WiwiPay", 1);
  39. Redis::expire("PayErro_WiwiPay", 600);
  40. TelegramBot::getDefault()->sendProgramNotify("WiwiPay RetrunFail ", $logic->getError() . " | " . json_encode($res));
  41. return apiReturnFail($logic->getError());
  42. } else {
  43. $this->retryTimes++;
  44. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  45. }
  46. }
  47. }
  48. // 支付异步回调
  49. public function notify(Request $request)
  50. {
  51. $post = $request->all();
  52. $payload = json_encode($post);
  53. Util::WriteLog('WiwiPay', "WiwiPay回调订单\n" . $payload);
  54. $service = new WiwiPay();
  55. try {
  56. $verify = $service->verifySign($post);
  57. } catch (\Exception $e) {
  58. Util::WriteLog('WiwiPay', '验签失败:' . $e->getMessage());
  59. return 'fail';
  60. }
  61. if (!$verify) {
  62. Util::WriteLog('WiwiPay', '签名错误');
  63. return 'fail';
  64. }
  65. $order_sn = @$post['mchOrderNo'];
  66. $logic = new WiwiPayLogic();
  67. try {
  68. $redis = Redis::connection();
  69. $ret = $logic->notify($post);
  70. if ($ret == 'SUCCESS') $redis->set($order_sn, $order_sn, 3600 * 24);
  71. return $ret;
  72. } catch (\Exception $exception) {
  73. Redis::set("PayErro_WiwiPay", 1);
  74. Redis::expire("PayErro_WiwiPay", 600);
  75. TelegramBot::getDefault()->sendProgramNotify("WiwiPay 订单回调执行 异常 ", json_encode($post), $exception);
  76. Util::WriteLog("WiwiPay_error", $post);
  77. return '{"success":false,"message":"商户自定义出错信息"}';
  78. }
  79. }
  80. public function sync_notify(Request $request)
  81. {
  82. Log::info('WiwiPay同步回调');
  83. echo 'WiwiPay同步回调';
  84. }
  85. // 提现异步回调
  86. public function cash_notify(Request $request)
  87. {
  88. $post = $request->all();
  89. $payload = json_encode($post);
  90. Util::WriteLog('WiwiPay', "WiwiPay cash 异步回调\n" . $payload);
  91. $service = new WiwiPay();
  92. try {
  93. $verify = $service->verifySign($post);
  94. } catch (\Exception $e) {
  95. Util::WriteLog('WiwiPay cash', '验签失败:' . $e->getMessage());
  96. return 'fail';
  97. }
  98. if (!$verify) {
  99. Util::WriteLog('WiwiPay cash', '签名错误');
  100. return 'fail';
  101. }
  102. $logic = new WiwiPayCashierLogic();
  103. try {
  104. return $logic->notify($post);
  105. } catch (\Exception $exception) {
  106. TelegramBot::getDefault()->sendProgramNotify("WiwiPay 提现异步回调执行 异常 ", json_encode($post), $exception);
  107. Util::WriteLog("WiwiPay_error", $post);
  108. return '{"success":false,"message":"商户自定义出错信息"}';
  109. }
  110. }
  111. }