2
0

StarPayController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. 'clabe' => $data['params']['clabe'] ?? '',
  40. 'bank' => 'TRANSFER',
  41. ]);
  42. }
  43. if ($res === false) {
  44. return apiReturnFail($logic->getError());
  45. }
  46. if ($this->retryTimes > 0) {
  47. Redis::set('PayErro_StarPay', 1);
  48. Redis::expire('PayErro_StarPay', 600);
  49. TelegramBot::getDefault()->sendProgramNotify('StarPay ReturnFail ', $logic->getError() . ' | ' . json_encode($res));
  50. return apiReturnFail($logic->getError());
  51. }
  52. $this->retryTimes++;
  53. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  54. }
  55. /**
  56. * 统一回调入口:代收(01) + 代付(02),验签后按 transactionType 分发
  57. */
  58. public function notify(Request $request)
  59. {
  60. $post = $request->all();
  61. Util::WriteLog('StarPay_notify', $post);
  62. $configKey = 'StarPay';
  63. $config = (new PayConfig())->getConfig($configKey);
  64. $service = new StarPayService($config);
  65. if (!$service->verifySign($post)) {
  66. Util::WriteLog('StarPay_error', 'notify sign invalid');
  67. return 'fail';
  68. }
  69. $orderId = $post['orderNo'] ?? '';
  70. $lockKey = '';
  71. if ($orderId) {
  72. $lockKey = 'pay_notify_StarPay_pay_' . $orderId;
  73. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  74. Util::WriteLog('StarPay', 'notify concurrent, ignore: ' . $orderId);
  75. return 'success';
  76. }
  77. }
  78. try {
  79. $logic = new StarPayLogic();
  80. $ret = $logic->notify($post);
  81. } finally {
  82. if ($lockKey !== '') {
  83. SetNXLock::release($lockKey);
  84. }
  85. }
  86. return $ret;
  87. }
  88. /**
  89. * 代付回调
  90. */
  91. public function cash_notify(Request $request)
  92. {
  93. $post = $request->all();
  94. Util::WriteLog('StarPay_notify', $post);
  95. $orderId = $post['merOrderNo'] ?? '';
  96. $lockKey = '';
  97. if ($orderId) {
  98. $lockKey = 'pay_notify_StarPay_02_' . $orderId;
  99. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  100. Util::WriteLog('StarPay', 'payout notify concurrent, ignore: ' . $orderId);
  101. return 'SUCCESS';
  102. }
  103. }
  104. try {
  105. $logic = new StarPayCashierLogic();
  106. $ret = $logic->notify($post);
  107. } finally {
  108. if ($lockKey !== '') {
  109. SetNXLock::release($lockKey);
  110. }
  111. }
  112. return $ret;
  113. }
  114. }