| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\logic\api\StarPayLogic;
- use App\Http\logic\api\StarPayCashierLogic;
- use App\Inter\PayMentInterFace;
- use App\Notification\TelegramBot;
- use App\Services\PayConfig;
- use App\Services\StarPayService;
- use App\Util;
- use App\Utility\SetNXLock;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Redis;
- class StarPayController implements PayMentInterFace
- {
- private $retryTimes = 0;
- public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
- {
- $logic = new StarPayLogic();
- try {
- $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
- } catch (\Exception $exception) {
- Redis::set('PayErro_StarPay', 1);
- Redis::expire('PayErro_StarPay', 600);
- Util::WriteLog('StarPay_error', $exception->getMessage() . json_encode($logic->result ?? []));
- TelegramBot::getDefault()->sendProgramNotify('StarPay Except ', $exception->getMessage(), $exception);
- return apiReturnFail($logic->getError());
- }
- if (!empty($res) && isset($res['code']) && (string)$res['code'] === '0' && !empty($res['data'])) {
- $data = $res['data'];
- $content = $data['params']['url'] ?? '';
- if (empty($content) && !empty($data['params']['clabe'])) {
- $content = 'CLABE:' . $data['params']['clabe'];
- }
- return apiReturnSuc([
- 'content' => $content,
- 'money' => $payAmt,
- 'prdOrdNo' => $data['merOrderNo'] ?? '',
- 'identifier' => $data['params']['clabe'] ?? '',
- ]);
- }
- if ($res === false) {
- return apiReturnFail($logic->getError());
- }
- if ($this->retryTimes > 0) {
- Redis::set('PayErro_StarPay', 1);
- Redis::expire('PayErro_StarPay', 600);
- TelegramBot::getDefault()->sendProgramNotify('StarPay ReturnFail ', $logic->getError() . ' | ' . json_encode($res));
- return apiReturnFail($logic->getError());
- }
- $this->retryTimes++;
- return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
- }
- /**
- * 统一回调入口:代收(01) + 代付(02),验签后按 transactionType 分发
- */
- public function notify(Request $request)
- {
- $post = $request->all();
- Util::WriteLog('StarPay_notify', $post);
- $configKey = 'StarPay';
- $config = (new PayConfig())->getConfig($configKey);
- $service = new StarPayService($config);
- if (!$service->verifySign($post)) {
- Util::WriteLog('StarPay_error', 'notify sign invalid');
- return 'fail';
- }
- $orderId = $post['orderNo'] ?? '';
- $lockKey = '';
- if ($orderId) {
- $lockKey = 'pay_notify_StarPay_pay_' . $orderId;
- if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
- Util::WriteLog('StarPay', 'notify concurrent, ignore: ' . $orderId);
- return 'success';
- }
- }
- try {
- $logic = new StarPayLogic();
- $ret = $logic->notify($post);
- } finally {
- if ($lockKey !== '') {
- SetNXLock::release($lockKey);
- }
- }
- return $ret;
- }
- /**
- * 代付回调
- */
- public function cash_notify(Request $request)
- {
- $post = $request->all();
- Util::WriteLog('StarPay_notify', $post);
- $orderId = $post['merOrderNo'] ?? '';
- $lockKey = '';
- if ($orderId) {
- $lockKey = 'pay_notify_StarPay_02_' . $orderId;
- if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
- Util::WriteLog('StarPay', 'payout notify concurrent, ignore: ' . $orderId);
- return 'SUCCESS';
- }
- }
- try {
- $logic = new StarPayCashierLogic();
- $ret = $logic->notify($post);
- } finally {
- if ($lockKey !== '') {
- SetNXLock::release($lockKey);
- }
- }
- return $ret;
- }
- }
|