SupefinaSpeiController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 'clabe' => $data['identifier'] ?? '',
  39. 'bank' => $data['channelName'] ?? ''
  40. ]);
  41. }
  42. if ($res === false) {
  43. return apiReturnFail($logic->getError());
  44. }
  45. if ($this->retryTimes > 0) {
  46. Redis::set('PayErro_SupefinaSpei', 1);
  47. Redis::expire('PayErro_SupefinaSpei', 600);
  48. TelegramBot::getDefault()->sendProgramNotify('SupefinaSpei ReturnFail ', $logic->getError() . ' | ' . json_encode($res));
  49. return apiReturnFail($logic->getError());
  50. }
  51. $this->retryTimes++;
  52. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  53. }
  54. /**
  55. * 统一回调入口:代收(01) + 代付(02),验签后按 transactionType 分发
  56. */
  57. public function notify(Request $request)
  58. {
  59. $payload = $request->getContent();
  60. $post = json_decode($payload, true);
  61. if (!is_array($post)) {
  62. $post = $request->all();
  63. }
  64. Util::WriteLog('SupefinaSpei', "raw notify\n" . $payload);
  65. $transactionType = $post['transactionType'] ?? '';
  66. $orderId = $post['merOrderId'] ?? $post['merOrderNo'] ?? '';
  67. $configKey = $transactionType === '02' ? 'SupefinaSpeiOut' : 'SupefinaSpei';
  68. $service = new SupefinaSpei($configKey);
  69. if (!$service->verify($post)) {
  70. Util::WriteLog('SupefinaSpei_error', 'notify sign invalid');
  71. return 'fail';
  72. }
  73. $lockKey = '';
  74. if ($orderId) {
  75. $lockKey = 'pay_notify_SupefinaSpei_' . $transactionType . '_' . $orderId;
  76. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  77. Util::WriteLog('SupefinaSpei', 'notify concurrent, ignore: ' . $orderId);
  78. return 'SUCCESS';
  79. }
  80. }
  81. try {
  82. if ($transactionType === '01') {
  83. $logic = new SupefinaSpeiLogic();
  84. $ret = $logic->notify($post);
  85. } elseif ($transactionType === '02') {
  86. $logic = new SupefinaSpeiCashierLogic();
  87. $ret = $logic->notify($post);
  88. } else {
  89. Util::WriteLog('SupefinaSpei', 'ignore transactionType: ' . $transactionType);
  90. $ret = 'SUCCESS';
  91. }
  92. } finally {
  93. if ($lockKey !== '') {
  94. SetNXLock::release($lockKey);
  95. }
  96. }
  97. return $ret;
  98. }
  99. /**
  100. * 代付回调(保留兼容,建议统一用 notify)
  101. */
  102. public function cash_notify(Request $request)
  103. {
  104. $payload = $request->getContent();
  105. $post = json_decode($payload, true);
  106. if (!is_array($post)) {
  107. $post = $request->all();
  108. }
  109. Util::WriteLog('SupefinaSpei', "raw payout notify\n" . $payload);
  110. $orderId = $post['merOrderId'] ?? $post['merOrderNo'] ?? '';
  111. $lockKey = '';
  112. if ($orderId) {
  113. $lockKey = 'pay_notify_SupefinaSpei_02_' . $orderId;
  114. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  115. Util::WriteLog('SupefinaSpei', 'payout notify concurrent, ignore: ' . $orderId);
  116. return 'SUCCESS';
  117. }
  118. }
  119. try {
  120. $logic = new SupefinaSpeiCashierLogic();
  121. $ret = $logic->notify($post);
  122. } finally {
  123. if ($lockKey !== '') {
  124. SetNXLock::release($lockKey);
  125. }
  126. }
  127. return $ret;
  128. }
  129. }