2
0

SupefinaSpeiController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\SupefinaSpeiLogic;
  4. use App\Http\logic\api\SupefinaSpeiCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\SupefinaSpei;
  8. use App\Util;
  9. use App\Utility\SetNXLock;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Redis;
  12. class SupefinaSpeiController 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 SupefinaSpeiLogic();
  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_SupefinaSpei', 1);
  22. Redis::expire('PayErro_SupefinaSpei', 600);
  23. Util::WriteLog('SupefinaSpei_error', $exception->getMessage() . json_encode($logic->result ?? []));
  24. TelegramBot::getDefault()->sendProgramNotify('SupefinaSpei Except ', $exception->getMessage(), $exception);
  25. return apiReturnFail($logic->getError());
  26. }
  27. if (!empty($res) && isset($res['code']) && (string)$res['code'] === '200' && !empty($res['data'])) {
  28. $data = $res['data'];
  29. $content = $data['url'] ?? '';
  30. if (empty($content) && !empty($data['identifier'])) {
  31. $content = 'CLABE:' . $data['identifier'];
  32. }
  33. return apiReturnSuc([
  34. 'content' => $content,
  35. 'money' => $payAmt,
  36. 'prdOrdNo' => $data['merOrderNo'] ?? '',
  37. 'identifier' => $data['identifier'] ?? '',
  38. ]);
  39. }
  40. if ($res === false) {
  41. return apiReturnFail($logic->getError());
  42. }
  43. if ($this->retryTimes > 0) {
  44. Redis::set('PayErro_SupefinaSpei', 1);
  45. Redis::expire('PayErro_SupefinaSpei', 600);
  46. TelegramBot::getDefault()->sendProgramNotify('SupefinaSpei ReturnFail ', $logic->getError() . ' | ' . json_encode($res));
  47. return apiReturnFail($logic->getError());
  48. }
  49. $this->retryTimes++;
  50. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  51. }
  52. /**
  53. * 统一回调入口:代收(01) + 代付(02),验签后按 transactionType 分发
  54. */
  55. public function notify(Request $request)
  56. {
  57. $payload = $request->getContent();
  58. $post = json_decode($payload, true);
  59. if (!is_array($post)) {
  60. $post = $request->all();
  61. }
  62. Util::WriteLog('SupefinaSpei', "raw notify\n" . $payload);
  63. $transactionType = $post['transactionType'] ?? '';
  64. $orderId = $post['merOrderId'] ?? $post['merOrderNo'] ?? '';
  65. $configKey = $transactionType === '02' ? 'SupefinaSpeiOut' : 'SupefinaSpei';
  66. $service = new SupefinaSpei($configKey);
  67. if (!$service->verify($post)) {
  68. Util::WriteLog('SupefinaSpei_error', 'notify sign invalid');
  69. return 'fail';
  70. }
  71. $lockKey = '';
  72. if ($orderId) {
  73. $lockKey = 'pay_notify_SupefinaSpei_' . $transactionType . '_' . $orderId;
  74. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  75. Util::WriteLog('SupefinaSpei', 'notify concurrent, ignore: ' . $orderId);
  76. return 'SUCCESS';
  77. }
  78. }
  79. try {
  80. if ($transactionType === '01') {
  81. $logic = new SupefinaSpeiLogic();
  82. $ret = $logic->notify($post);
  83. } elseif ($transactionType === '02') {
  84. $logic = new SupefinaSpeiCashierLogic();
  85. $ret = $logic->notify($post);
  86. } else {
  87. Util::WriteLog('SupefinaSpei', 'ignore transactionType: ' . $transactionType);
  88. $ret = 'SUCCESS';
  89. }
  90. } finally {
  91. if ($lockKey !== '') {
  92. SetNXLock::release($lockKey);
  93. }
  94. }
  95. return $ret;
  96. }
  97. /**
  98. * 代付回调(保留兼容,建议统一用 notify)
  99. */
  100. public function cash_notify(Request $request)
  101. {
  102. $payload = $request->getContent();
  103. $post = json_decode($payload, true);
  104. if (!is_array($post)) {
  105. $post = $request->all();
  106. }
  107. Util::WriteLog('SupefinaSpei', "raw payout notify\n" . $payload);
  108. $orderId = $post['merOrderId'] ?? $post['merOrderNo'] ?? '';
  109. $lockKey = '';
  110. if ($orderId) {
  111. $lockKey = 'pay_notify_SupefinaSpei_02_' . $orderId;
  112. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  113. Util::WriteLog('SupefinaSpei', 'payout notify concurrent, ignore: ' . $orderId);
  114. return 'SUCCESS';
  115. }
  116. }
  117. try {
  118. $logic = new SupefinaSpeiCashierLogic();
  119. $ret = $logic->notify($post);
  120. } finally {
  121. if ($lockKey !== '') {
  122. SetNXLock::release($lockKey);
  123. }
  124. }
  125. return $ret;
  126. }
  127. }