StarPayController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\StarPayLogic;
  4. use App\Http\logic\api\StarPayCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\PayConfig;
  8. use App\Services\StarPayService;
  9. use App\Util;
  10. use App\Utility\SetNXLock;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Redis;
  13. class StarPayController implements PayMentInterFace
  14. {
  15. private $retryTimes = 0;
  16. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  17. {
  18. $logic = new StarPayLogic();
  19. try {
  20. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  21. } catch (\Exception $exception) {
  22. Redis::set('PayErro_StarPay', 1);
  23. Redis::expire('PayErro_StarPay', 600);
  24. Util::WriteLog('StarPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
  25. TelegramBot::getDefault()->sendProgramNotify('StarPay Except ', $exception->getMessage(), $exception);
  26. return apiReturnFail($logic->getError());
  27. }
  28. if (!empty($res) && isset($res['code']) && (string)$res['code'] === '0' && !empty($res['data'])) {
  29. $data = $res['data'];
  30. $content = $data['params']['url'] ?? '';
  31. if (empty($content) && !empty($data['params']['clabe'])) {
  32. $content = 'CLABE:' . $data['params']['clabe'];
  33. }
  34. return apiReturnSuc([
  35. 'content' => $content,
  36. 'money' => $payAmt,
  37. 'prdOrdNo' => $data['merOrderNo'] ?? '',
  38. 'identifier' => $data['params']['clabe'] ?? '',
  39. ]);
  40. }
  41. if ($res === false) {
  42. return apiReturnFail($logic->getError());
  43. }
  44. if ($this->retryTimes > 0) {
  45. Redis::set('PayErro_StarPay', 1);
  46. Redis::expire('PayErro_StarPay', 600);
  47. TelegramBot::getDefault()->sendProgramNotify('StarPay ReturnFail ', $logic->getError() . ' | ' . json_encode($res));
  48. return apiReturnFail($logic->getError());
  49. }
  50. $this->retryTimes++;
  51. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  52. }
  53. /**
  54. * 统一回调入口:代收(01) + 代付(02),验签后按 transactionType 分发
  55. */
  56. public function notify(Request $request)
  57. {
  58. $post = $request->all();
  59. Util::WriteLog('StarPay_notify', $post);
  60. $configKey = 'StarPay';
  61. $config = (new PayConfig())->getConfig($configKey);
  62. $service = new StarPayService($config);
  63. if (!$service->verifySign($post)) {
  64. Util::WriteLog('StarPay_error', 'notify sign invalid');
  65. return 'fail';
  66. }
  67. $orderId = $post['orderNo'] ?? '';
  68. $lockKey = '';
  69. if ($orderId) {
  70. $lockKey = 'pay_notify_StarPay_pay_' . $orderId;
  71. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  72. Util::WriteLog('StarPay', 'notify concurrent, ignore: ' . $orderId);
  73. return 'success';
  74. }
  75. }
  76. try {
  77. $logic = new StarPayLogic();
  78. $ret = $logic->notify($post);
  79. } finally {
  80. if ($lockKey !== '') {
  81. SetNXLock::release($lockKey);
  82. }
  83. }
  84. return $ret;
  85. }
  86. /**
  87. * 代付回调
  88. */
  89. public function cash_notify(Request $request)
  90. {
  91. $post = $request->all();
  92. Util::WriteLog('StarPay_notify', $post);
  93. $orderId = $post['merOrderNo'] ?? '';
  94. $lockKey = '';
  95. if ($orderId) {
  96. $lockKey = 'pay_notify_StarPay_02_' . $orderId;
  97. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  98. Util::WriteLog('StarPay', 'payout notify concurrent, ignore: ' . $orderId);
  99. return 'SUCCESS';
  100. }
  101. }
  102. try {
  103. $logic = new StarPayCashierLogic();
  104. $ret = $logic->notify($post);
  105. } finally {
  106. if ($lockKey !== '') {
  107. SetNXLock::release($lockKey);
  108. }
  109. }
  110. return $ret;
  111. }
  112. }