SafePayController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. $url = $res['data']['order_data'] ?? '';
  42. if (empty($url)) {
  43. TelegramBot::getDefault()->sendProgramNotify("SafePay返回值异常 url 为空 ", json_encode($res));
  44. return apiReturnFail('Payment failed: missing payment URL');
  45. }
  46. $data = [
  47. 'content' => $res['data']['order_data'] ?? '',
  48. 'money' => $payAmt,
  49. 'prdOrdNo' => $res['data']['order_no'] ?? '',
  50. ];
  51. return apiReturnSuc($data);
  52. }
  53. if ($res == false) {
  54. return apiReturnFail($logic->getError() ?: 'Payment failed');
  55. }
  56. // 非200的响应,记录错误
  57. Redis::set("PayErro_SafePay", 1);
  58. Redis::expire("PayErro_SafePay", 600);
  59. $errMsg = $res['message'] ?? 'Unknown error';
  60. TelegramBot::getDefault()->sendProgramNotify("SafePay ReturnFail ", $errMsg . " | " . json_encode($res)."$userId");
  61. return apiReturnFail(['web.payment.paytype_error', $errMsg]);
  62. }
  63. /**
  64. * 代收异步回调通知
  65. *
  66. * SafePay回调格式(POST JSON):
  67. * {
  68. * "mer_no": 600000,
  69. * "order_no": "xxx",
  70. * "order_amount": "1000.00",
  71. * "order_reality_amount": "1000.00",
  72. * "pay_type_code": 1301,
  73. * "status": "success",
  74. * "ref_no": "xxx",
  75. * "sign": "xxx"
  76. * }
  77. */
  78. public function notify(Request $request)
  79. {
  80. $post = $request->all();
  81. $payload = json_encode($post);
  82. Util::WriteLog('SafePay', "SafePay回调订单\n" . $payload);
  83. $service = new SafePay();
  84. // 验签(平台公钥)
  85. try {
  86. $verify = $service->verifySign($post);
  87. } catch (\Exception $e) {
  88. Util::WriteLog('SafePay', '验签失败:' . $e->getMessage());
  89. return 'fail';
  90. }
  91. if (!$verify) {
  92. Util::WriteLog('SafePay', '签名错误');
  93. return 'fail';
  94. }
  95. $order_sn = $post['order_no'] ?? '';
  96. if (empty($order_sn)) {
  97. Util::WriteLog('SafePay', '缺少订单号');
  98. return 'fail';
  99. }
  100. // 代收回调加锁,防止并发重复处理
  101. $lockKey = 'pay_notify_SafePay_' . $order_sn;
  102. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  103. Util::WriteLog('SafePay', '代收回调并发,订单已处理或处理中: ' . $order_sn);
  104. return 'ok';
  105. }
  106. $logic = new SafePayLogic();
  107. try {
  108. $ret = $logic->notify($post);
  109. return $ret;
  110. } catch (\Exception $exception) {
  111. Redis::set("PayErro_SafePay", 1);
  112. Redis::expire("PayErro_SafePay", 600);
  113. TelegramBot::getDefault()->sendProgramNotify("SafePay 订单回调执行异常 ", json_encode($post), $exception);
  114. Util::WriteLog("SafePay_error", $post);
  115. return 'fail';
  116. } finally {
  117. SetNXLock::release($lockKey);
  118. }
  119. }
  120. /**
  121. * 代付异步回调通知
  122. *
  123. * SafePay 代付回调格式(POST JSON):
  124. * {
  125. * "mer_no": 600000,
  126. * "order_no": "xxx",
  127. * "order_amount": "10.00",
  128. * "order_reality_amount": "10.00",
  129. * "currency": "USD",
  130. * "result": "success",
  131. * "sys_no": "212073",
  132. * "sign": "xxx"
  133. * }
  134. */
  135. public function cash_notify(Request $request)
  136. {
  137. $post = $request->all();
  138. $payload = json_encode($post);
  139. Util::WriteLog('SafePay', "SafePay 提现回调\n" . $payload);
  140. $service = new SafePay();
  141. // 验签(平台公钥)
  142. try {
  143. $verify = $service->verifySign($post);
  144. } catch (\Exception $e) {
  145. Util::WriteLog('SafePay', '提现回调验签失败:' . $e->getMessage());
  146. return 'FAIL';
  147. }
  148. if (!$verify) {
  149. Util::WriteLog('SafePay', '提现回调签名错误');
  150. return 'FAIL';
  151. }
  152. $logic = new SafePayCashierLogic();
  153. try {
  154. return $logic->notify($post);
  155. } catch (\Exception $exception) {
  156. TelegramBot::getDefault()->sendProgramNotify(
  157. "SafePay 提现异步回调执行异常 ",
  158. json_encode($post),
  159. $exception
  160. );
  161. Util::WriteLog("SafePay_error", $post);
  162. return 'SUCCESS';
  163. }
  164. }
  165. }