NewSupefinaSpeiController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\NewSupefinaSpeiLogic;
  4. use App\Http\logic\api\NewSupefinaSpeiCashierLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\Aes;
  8. use App\Services\PayConfig;
  9. use App\Util;
  10. use App\Utility\SetNXLock;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Redis;
  14. class NewSupefinaSpeiController implements PayMentInterFace
  15. {
  16. private $retryTimes = 0;
  17. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  18. {
  19. $logic = new NewSupefinaSpeiLogic();
  20. try {
  21. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  22. } catch (\Exception $exception) {
  23. Redis::set('PayErro_NewSupefinaSpei', 1);
  24. Redis::expire('PayErro_NewSupefinaSpei', 600);
  25. Util::WriteLog('NewSupefinaSpei_error', $exception->getMessage() . json_encode($logic->result ?? []));
  26. TelegramBot::getDefault()->sendProgramNotify('SupefinaSpei Except ', $exception->getMessage(), $exception);
  27. return apiReturnFail($logic->getError());
  28. }
  29. if (!empty($res) && isset($res['barcodeUrl'])) {
  30. $data = $res['data'];
  31. $content = $data['barcodeUrl'] ?? '';
  32. if (empty($content) && !empty($data['receiveCode'])) {
  33. $content = 'CLABE:' . $data['receiveCode'];
  34. }
  35. return apiReturnSuc([
  36. 'content' => $content,
  37. 'money' => $payAmt,
  38. 'prdOrdNo' => $data['merchantOrderNo'] ?? '',
  39. 'identifier' => $data['receiveCode'] ?? '',
  40. ]);
  41. }
  42. if ($res === false) {
  43. return apiReturnFail($logic->getError());
  44. }
  45. if ($this->retryTimes > 0) {
  46. Redis::set('PayErro_NewSupefinaSpei', 1);
  47. Redis::expire('PayErro_NewSupefinaSpei', 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. $headers = $request->headers->all();
  60. $payload = $request->getContent();
  61. $post = json_decode($payload, true);
  62. if (!is_array($post)) {
  63. $post = $request->all();
  64. }
  65. Util::WriteLog('NewSupefinaSpei', "raw notify\n" . $payload . ' ' . json_encode($headers));
  66. if (!isset($post['data'])) {
  67. Util::WriteLog('NewSupefinaSpei_error', 'empty data');
  68. return 'fail';
  69. }
  70. $sign = $request->header('sign');
  71. $logic = new NewSupefinaSpeiLogic();
  72. $signHeaders = [
  73. 'version' => $request->header('version'),
  74. 'merchantNo' => $request->header('merchantno'),
  75. 'requestId' => $request->header('requestid'),
  76. 'requestTime' => $request->header('requesttime'),
  77. ];
  78. $config = (new PayConfig())->getConfig('NewSupefinaSpei');
  79. $string = $logic->getSignString($signHeaders, $post);
  80. $res = $logic->verifySign($string, $sign, $config['publickey']);
  81. if ($res !== true) {
  82. Util::WriteLog('NewSupefinaSpei_error', 'notify sign invalid' . $string);
  83. return 'fail';
  84. }
  85. $data = Aes::decrypt($post['data'], base64_decode($config['aesKey']));
  86. Util::WriteLog('NewSupefinaSpei', 'decoded data'.$data);
  87. if (empty($data)) {
  88. Util::WriteLog('NewSupefinaSpei_error', 'des decrypt fail ' . $post['data']);
  89. return 'fail';
  90. }
  91. $data = json_decode($data, true);
  92. $lockKey = '';
  93. $orderId = $data['merchantOrderNo'];
  94. if ($orderId) {
  95. $lockKey = 'pay_notify_NewSupefinaSpei_' . $orderId;
  96. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  97. Util::WriteLog('NewSupefinaSpei', 'notify concurrent, ignore: ' . $orderId);
  98. return 'SUCCESS';
  99. }
  100. }
  101. try {
  102. $res = $logic->notify($data);
  103. } finally {
  104. if ($lockKey !== '') {
  105. SetNXLock::release($lockKey);
  106. }
  107. }
  108. return $res;
  109. }
  110. /**
  111. * 代付回调(保留兼容,建议统一用 notify)
  112. */
  113. public function cash_notify(Request $request)
  114. {
  115. $headers = $request->headers->all();
  116. $payload = $request->getContent();
  117. $post = json_decode($payload, true);
  118. if (!is_array($post)) {
  119. $post = $request->all();
  120. }
  121. Util::WriteLog('NewSupefinaSpei', "raw payout notify\n" . $payload . json_encode($headers));
  122. $sign = $request->header('sign');
  123. $logic = new NewSupefinaSpeiLogic();
  124. $signHeaders = [
  125. 'version' => $request->header('version'),
  126. 'merchantNo' => $request->header('merchantno'),
  127. 'requestId' => $request->header('requestid'),
  128. 'requestTime' => $request->header('requesttime'),
  129. ];
  130. $config = (new PayConfig())->getConfig('NewSupefinaSpei');
  131. $string = $logic->getSignString($signHeaders, $post);
  132. $res = $logic->verifySign($string, $sign, $config['publickey']);
  133. if ($res !== true) {
  134. Util::WriteLog('NewSupefinaSpei_error', 'payout notify sign invalid' . $string);
  135. return 'fail';
  136. }
  137. $data = Aes::decrypt($post['data'], base64_decode($config['aesKey']));
  138. Util::WriteLog('NewSupefinaSpei', 'decoded data'.$data);
  139. if (empty($data)) {
  140. Util::WriteLog('NewSupefinaSpei_error', 'des decrypt fail ' . $post['data']);
  141. return 'fail';
  142. }
  143. $data = json_decode($data, true);
  144. $lockKey = '';
  145. $orderId = $data['merchantOrderNo'];
  146. if ($orderId) {
  147. $lockKey = 'payout_notify_NewSupefinaSpei_' . $orderId;
  148. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  149. Util::WriteLog('NewSupefinaSpei', 'payout notify concurrent, ignore: ' . $orderId);
  150. return 'SUCCESS';
  151. }
  152. }
  153. try {
  154. $logic = new NewSupefinaSpeiLogic();
  155. $ret = $logic->cashNotify($data);
  156. } finally {
  157. if ($lockKey !== '') {
  158. SetNXLock::release($lockKey);
  159. }
  160. }
  161. return $ret;
  162. }
  163. }