SafePayController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\SafePayLogic;
  4. use App\Http\logic\api\SafePayCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\SafePay;
  8. use App\Util;
  9. use App\Utility\SetNXLock;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Redis;
  12. class SafePayController implements PayMentInterFace
  13. {
  14. /**
  15. * 代收下单
  16. */
  17. public function pay_order(
  18. $userId,
  19. $payAmt,
  20. $userName,
  21. $userEmail,
  22. $userPhone,
  23. $GiftsID,
  24. $buyIP,
  25. $AdId,
  26. $eventType,
  27. $pay_method = ''
  28. ) {
  29. $logic = new SafePayLogic();
  30. try {
  31. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  32. } catch (\Exception $exception) {
  33. Redis::set("PayErro_SafePay", 1);
  34. Redis::expire("PayErro_SafePay", 600);
  35. Util::WriteLog('SafePay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  36. TelegramBot::getDefault()->sendProgramNotify("SafePay Except ", $exception->getMessage(), $exception);
  37. return apiReturnFail($logic->getError());
  38. }
  39. if (!empty($res) && isset($res['code']) && $res['code'] == 200) {
  40. // SafePay 成功返回: data.order_data 是支付链接
  41. $data = [
  42. 'content' => $res['data']['order_data'] ?? '',
  43. 'money' => $payAmt,
  44. 'prdOrdNo' => $res['data']['order_no'] ?? '',
  45. ];
  46. return apiReturnSuc($data);
  47. }
  48. if ($res == false) {
  49. return apiReturnFail($logic->getError() ?: 'Payment failed');
  50. }
  51. // 非200的响应,记录错误
  52. Redis::set("PayErro_SafePay", 1);
  53. Redis::expire("PayErro_SafePay", 600);
  54. $errMsg = $res['message'] ?? 'Unknown error';
  55. TelegramBot::getDefault()->sendProgramNotify("SafePay ReturnFail ", $errMsg . " | " . json_encode($res)."$userId");
  56. return apiReturnFail(['web.payment.paytype_error', $errMsg]);
  57. }
  58. /**
  59. * 代收异步回调通知
  60. *
  61. * SafePay回调格式(POST JSON):
  62. * {
  63. * "mer_no": 600000,
  64. * "order_no": "xxx",
  65. * "order_amount": "1000.00",
  66. * "order_reality_amount": "1000.00",
  67. * "pay_type_code": 1301,
  68. * "status": "success",
  69. * "ref_no": "xxx",
  70. * "sign": "xxx"
  71. * }
  72. */
  73. public function notify(Request $request)
  74. {
  75. $post = $request->all();
  76. $payload = json_encode($post);
  77. Util::WriteLog('SafePay', "SafePay回调订单\n" . $payload);
  78. $service = new SafePay();
  79. // 验签(平台公钥)
  80. try {
  81. $verify = $service->verifySign($post);
  82. } catch (\Exception $e) {
  83. Util::WriteLog('SafePay', '验签失败:' . $e->getMessage());
  84. return 'fail';
  85. }
  86. if (!$verify) {
  87. Util::WriteLog('SafePay', '签名错误');
  88. return 'fail';
  89. }
  90. $order_sn = $post['order_no'] ?? '';
  91. if (empty($order_sn)) {
  92. Util::WriteLog('SafePay', '缺少订单号');
  93. return 'fail';
  94. }
  95. // 代收回调加锁,防止并发重复处理
  96. $lockKey = 'pay_notify_SafePay_' . $order_sn;
  97. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  98. Util::WriteLog('SafePay', '代收回调并发,订单已处理或处理中: ' . $order_sn);
  99. return 'ok';
  100. }
  101. $logic = new SafePayLogic();
  102. try {
  103. $ret = $logic->notify($post);
  104. return $ret;
  105. } catch (\Exception $exception) {
  106. Redis::set("PayErro_SafePay", 1);
  107. Redis::expire("PayErro_SafePay", 600);
  108. TelegramBot::getDefault()->sendProgramNotify("SafePay 订单回调执行异常 ", json_encode($post), $exception);
  109. Util::WriteLog("SafePay_error", $post);
  110. return 'fail';
  111. } finally {
  112. SetNXLock::release($lockKey);
  113. }
  114. }
  115. /**
  116. * 代付异步回调通知
  117. *
  118. * SafePay 代付回调格式(POST JSON):
  119. * {
  120. * "mer_no": 600000,
  121. * "order_no": "xxx",
  122. * "order_amount": "10.00",
  123. * "order_reality_amount": "10.00",
  124. * "currency": "USD",
  125. * "result": "success",
  126. * "sys_no": "212073",
  127. * "sign": "xxx"
  128. * }
  129. */
  130. public function cash_notify(Request $request)
  131. {
  132. $post = $request->all();
  133. $payload = json_encode($post);
  134. Util::WriteLog('SafePay', "SafePay 提现回调\n" . $payload);
  135. $service = new SafePay();
  136. // 验签(平台公钥)
  137. try {
  138. $verify = $service->verifySign($post);
  139. } catch (\Exception $e) {
  140. Util::WriteLog('SafePay', '提现回调验签失败:' . $e->getMessage());
  141. return 'FAIL';
  142. }
  143. if (!$verify) {
  144. Util::WriteLog('SafePay', '提现回调签名错误');
  145. return 'FAIL';
  146. }
  147. $logic = new SafePayCashierLogic();
  148. try {
  149. return $logic->notify($post);
  150. } catch (\Exception $exception) {
  151. TelegramBot::getDefault()->sendProgramNotify(
  152. "SafePay 提现异步回调执行异常 ",
  153. json_encode($post),
  154. $exception
  155. );
  156. Util::WriteLog("SafePay_error", $post);
  157. return 'SUCCESS';
  158. }
  159. }
  160. }