2
0
laowu vor 1 Woche
Ursprung
Commit
b9d8c1b4a4

+ 16 - 0
app/Constant/Payment.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Constant;
+
+class Payment
+{
+    const STATUS_SUCCESS = 1;
+
+    const STATUS_FAIL = 2;
+
+    const STATUS_IN_PROGRESS = 3;
+
+    const STATUS_REFUND = 4;
+
+    const STATUS_UNKNOWN = 5;
+}

+ 129 - 0
app/Http/Controllers/Api/StarPayController.php

@@ -0,0 +1,129 @@
+<?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;
+    }
+}

+ 289 - 0
app/Http/logic/api/StarPayCashierLogic.php

@@ -0,0 +1,289 @@
+<?php
+
+namespace App\Http\logic\api;
+
+use App\Constant\Payment;
+use App\dao\Estatisticas\RechargeWithDraw;
+use App\Inter\CashierInterFace;
+use App\Models\PrivateMail;
+use App\Models\RecordUserDataStatistics;
+use App\Services\PayConfig;
+use App\Services\StarPayService;
+use App\Services\StoredProcedure;
+use App\Util;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class StarPayCashierLogic implements CashierInterFace
+{
+    /**
+     * 代付渠道标识,对应 agent.dbo.admin_configs.config_value(type=cash)。
+     * 使用前请在后台配置同名渠道值。
+     */
+    const AGENT = 107;
+
+    /**
+     * 创建 StarPay 代付订单。
+     */
+    public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
+    {
+        // 查询提现订单
+        $query = DB::connection('write')
+            ->table('QPAccountsDB.dbo.OrderWithDraw')
+            ->where('RecordID', $RecordID)
+            ->first();
+
+        if (!$query) {
+            Util::WriteLog('StarPay', 'withdraw order not found: '.$RecordID);
+            return 'fail';
+        }
+        $config = (new PayConfig())->getConfig('StarPayOut');
+        $service = new StarPayService($config);
+
+        // 账户号:优先 PixNum,其次 BankNO
+        $account = $PixNum ?: $BankNO;
+        if (!$account) {
+            Util::WriteLog('StarPay_error', 'missing account for withdraw: '.$OrderId);
+            return 'fail';
+        }
+
+        // 银行编号:优先 BankNO(如果看作 bankId),否则使用配置默认
+        $bankId = $BranchBank;
+
+        if ($bankId === '') {
+            Util::WriteLog('StarPay_error', 'missing bankId for withdraw: '.$OrderId);
+            return 'fail';
+        }
+
+        // 提现金额是以分存储,转成两位小数金额
+        $orderAmount = number_format($amount / 100, 2, '.', '');
+        $bankList = config('games.mex_bank_list');
+
+        try {
+            $data = $service->cash($OrderId, $orderAmount, $bankId, $bankList[$bankId] ?? '', $account, $accountName);
+            if ($data === false) {
+                return 'fail';
+            }
+        } catch (\Throwable $e) {
+            Util::WriteLog('StarPay_error', 'payout request exception: '.$e->getMessage());
+            return '';
+        }
+
+
+
+        // 文档示例:code=200 且 data.transactionStatus = "00" 表示下单成功
+        if (isset($data['code']) && (string)$data['code'] === '0') {
+            $transactionStatus = $data['data']['orderStatus'] ?? -99;
+            if ($transactionStatus >= 0) {
+                return $data;
+            }
+            if ($transactionStatus == -4 || $transactionStatus == -99) {
+                return $data;
+            }
+        }
+
+        // 同步下单失败:如果订单在处理中(State=5),退回资金并标记失败
+        if ((int)$query->State === 5) {
+            $msg = $data['msg'] ?? 'StarPay payout failed';
+            $WithDraw = $query->WithDraw + $query->ServiceFee;
+            $bonus = '30000,'.$WithDraw;
+
+            PrivateMail::failMail($query->UserID, $OrderId, $WithDraw, $msg, $bonus);
+
+            $withdraw_data = [
+                'State'      => 6,
+                'agent'      => self::AGENT,
+                'finishDate' => now(),
+                'remark'     => json_encode($data),
+            ];
+
+            DB::connection('write')
+                ->table('QPAccountsDB.dbo.OrderWithDraw')
+                ->where('OrderId', $query->OrderId)
+                ->update($withdraw_data);
+
+            $RecordData = ['after_state' => 6, 'update_at' => now()];
+
+            DB::connection('write')
+                ->table('QPAccountsDB.dbo.AccountsRecord')
+                ->where('type', 1)
+                ->where('RecordID', $RecordID)
+                ->update($RecordData);
+        }
+
+        return 'fail';
+    }
+
+    /**
+     * StarPay代付回调。
+     *
+     * @param array<string,mixed>|string $post
+     */
+    public function notify($post)
+    {
+        if (!is_array($post)) {
+            $post = \GuzzleHttp\json_decode($post, true);
+        }
+
+        Util::WriteLog('StarPay', 'payout notify: '.json_encode($post, JSON_UNESCAPED_UNICODE));
+
+        $config = (new PayConfig())->getConfig('StarPayOut');
+        $service = new StarPayService($config);
+
+        // 验签
+        if (!$service->verifySign($post)) {
+            Util::WriteLog('StarPay_error', 'notify sign invalid');
+            return 'fail';
+        }
+
+        $res = $service->cashNotify($post);
+        if (in_array($res->status, [Payment::STATUS_UNKNOWN, Payment::STATUS_IN_PROGRESS])) {
+            Util::WriteLog('StarPay', 'ignore non-payout notify');
+            return 'success';
+        }
+
+        $OrderId = $post['merOrderNo'] ?? '';
+        if ($OrderId === '') {
+            Util::WriteLog('StarPay_error', 'notify missing merOrderId');
+            return 'fail';
+        }
+
+        $query = DB::connection('write')
+            ->table('QPAccountsDB.dbo.OrderWithDraw')
+            ->where('OrderId', $OrderId)
+            ->first();
+
+        if (!$query) {
+            Util::WriteLog('StarPay_error', 'withdraw order not found in notify: '.$OrderId);
+            return 'success';
+        }
+
+        // 只处理 State=5 或 7 的订单,避免重复
+        if (!in_array((int)$query->State, [5, 7], true)) {
+            Util::WriteLog('StarPay', 'withdraw already handled: '.$OrderId);
+            return 'success';
+        }
+
+        $agentID = DB::connection('write')
+            ->table('agent.dbo.admin_configs')
+            ->where('config_value', self::AGENT)
+            ->where('type', 'cash')
+            ->value('id') ?? '';
+
+        $now = now();
+
+        $UserID = $query->UserID;
+        $TakeMoney = $query->WithDraw + $query->ServiceFee;
+
+        // Supefina 回调里带有 amount/fee 和 realityAmount/realityFee
+        // 提醒:这里仍以我们订单金额为准做账,仅将真实金额用于日志,可根据需要扩展到统计侧。
+        $realityAmount = isset($post['amount']) ? (float)$post['amount'] : null;
+        $realityFee = isset($post['tradeCharge']) ? (float)$post['tradeCharge'] : null;
+        Util::WriteLog('StarPay', 'payout reality: amount='.$realityAmount.', fee='.$realityFee.', orderId='.$OrderId);
+
+        $withdraw_data = [];
+
+        switch ($res->status) {
+            case Payment::STATUS_SUCCESS: // 提现成功
+                $withdraw_data = [
+                    'State'      => 2,
+                    'agent'      => $agentID,
+                    'finishDate' => $now,
+                ];
+
+                // 增加提现记录
+                $first = DB::connection('write')
+                    ->table('QPAccountsDB.dbo.UserTabData')
+                    ->where('UserID', $UserID)
+                    ->first();
+
+                if ($first) {
+                    DB::connection('write')
+                        ->table('QPAccountsDB.dbo.UserTabData')
+                        ->where('UserID', $UserID)
+                        ->increment('TakeMoney', $TakeMoney);
+                } else {
+                    DB::connection('write')
+                        ->table('QPAccountsDB.dbo.UserTabData')
+                        ->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
+                    try {
+                        PrivateMail::praiseSendMail($UserID);
+                    } catch (\Throwable $e) {
+                        // 忽略邮件发送失败
+                    }
+                }
+
+                // 免审记录
+                $withdrawal_position_log = DB::connection('write')
+                    ->table('agent.dbo.withdrawal_position_log')
+                    ->where('order_sn', $OrderId)
+                    ->first();
+
+                if ($withdrawal_position_log) {
+                    DB::connection('write')
+                        ->table('agent.dbo.withdrawal_position_log')
+                        ->where('order_sn', $OrderId)
+                        ->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
+                }
+
+                try {
+                    StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
+                } catch (\Throwable $exception) {
+                    Util::WriteLog('StoredProcedure', $exception->getMessage());
+                }
+
+                $ServiceFee = $query->ServiceFee;
+                RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
+
+                (new RechargeWithDraw())->withDraw($UserID, $TakeMoney);
+
+                $redis = Redis::connection();
+                $redis->incr('draw_'.date('Ymd').$UserID);
+                break;
+
+            case Payment::STATUS_FAIL: // 提现失败
+                $msg = $post['msg'] ?? 'Withdraw rejected';
+                $bonus = '30000,'.$TakeMoney;
+                PrivateMail::failMail($query->UserID, $OrderId, $TakeMoney, $msg, $bonus);
+
+                Util::WriteLog('SupefinaSpeiEmail', [$query->UserID, $OrderId, $TakeMoney, $msg, $bonus]);
+
+                $withdraw_data = [
+                    'State'  => 6,
+                    'agent'  => $agentID,
+                    'remark' => $msg,
+                ];
+                break;
+            case Payment::STATUS_REFUND:
+                Log::error('代付订单退款', ['data' => $post]);
+                return 'success';
+        }
+
+        $RecordData = [
+            'before_state' => $query->State,
+            'after_state'  => $withdraw_data['State'] ?? 0,
+            'RecordID'     => $query->RecordID,
+            'update_at'    => date('Y-m-d H:i:s'),
+        ];
+
+        DB::connection('write')
+            ->table('QPAccountsDB.dbo.AccountsRecord')
+            ->updateOrInsert(
+                ['RecordID' => $query->RecordID, 'type' => 1],
+                $RecordData
+            );
+
+        DB::connection('write')
+            ->table('QPAccountsDB.dbo.OrderWithDraw')
+            ->where('OrderId', $OrderId)
+            ->update($withdraw_data);
+
+        if (isset($withdraw_data['State']) && (int)$withdraw_data['State'] === 2) {
+            StoredProcedure::user_label($UserID, 2, $TakeMoney);
+        }
+
+        return 'success';
+    }
+}
+

+ 174 - 0
app/Http/logic/api/StarPayLogic.php

@@ -0,0 +1,174 @@
+<?php
+
+namespace App\Http\logic\api;
+
+use App\Constant\Payment;
+use App\dao\Pay\PayController;
+use App\Http\helper\CreateOrder;
+use App\Http\helper\NumConfig;
+use App\Jobs\Order;
+use App\Services\OrderServices;
+use App\Services\PayConfig;
+use App\Services\StarPayService;
+use App\Services\SupefinaSpei;
+use App\Services\CreateLog;
+use App\Util;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * Supefina SPEI 墨西哥代收(充值)逻辑
+ * 文档:https://docs.supefina.net/huan-ying-shi-yong-supefina-de-api-wen-dang/dai-shou/mo-xi-ge/spei
+ * 代收回调以 realityAmount 为准入账(处理用户实转金额与订单金额不一致)
+ */
+class StarPayLogic extends BaseApiLogic
+{
+    public $result;
+
+    public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
+    {
+        $PayVerify = new PayController();
+        $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
+        if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
+            $this->error = $PayVerify->getError();
+            return false;
+        }
+        if ($pay_amount < 0) {
+            $this->error = 'Payment error_4';
+            return false;
+        }
+        $payConfigService = new PayConfig();
+        $config = $payConfigService->getConfig('StarPay') ?? [];
+        if (empty($config)) {
+            $this->error = 'Payment config error';
+            return false;
+        }
+        $service = new StarPayService($config);
+        $order_sn = CreateOrder::order_sn($userId);
+
+        $logic = new OrderLogic();
+        $amount = round($pay_amount, 2);
+        $logic->orderCreate($order_sn, $amount * NumConfig::NUM_VALUE, 'StarPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
+
+        try {
+            $data = $service->create($order_sn, $amount, ['userId' => $userId]);
+        } catch (\Throwable $e) {
+            Util::WriteLog('StarPay_error', 'payin request exception: ' . $e->getMessage());
+            $this->error = 'Payment processing error';
+            return false;
+        }
+
+        if (!isset($data['code']) || $data['code'] !== 0) {
+            $this->error = $data['msg'] ?? 'Payment request failed';
+            return false;
+        }
+
+        $this->result = $data;
+        return $data;
+    }
+
+    /**
+     * 代收回调:仅代收存在“实际金额与订单金额不一致”,以 realityAmount 入账
+     *
+     * @param array<string,mixed> $post
+     */
+    public function notify($post)
+    {
+        $config = (new PayConfig())->getConfig('StarPay') ?? [];
+        $starPayService = new StarPayService($config);
+        $r = $starPayService->notify($post);
+        if ($r->status === Payment::STATUS_UNKNOWN) {
+            Util::WriteLog('StarPay', "unknown status {$r->orderSn}");
+            return $starPayService->notifySuccess();
+        }
+        if ($r->status === Payment::STATUS_IN_PROGRESS) {
+            return $starPayService->notifySuccess();
+        }
+        if ($r->status == Payment::STATUS_REFUND) {
+            Log::error('订单退款' . $r->orderSn);
+            return $starPayService->notifySuccess();
+        }
+
+        try {
+            $order = DB::connection('write')->table('agent.dbo.order')
+                ->where('order_sn', $r->orderSn)
+                ->first();
+            if (!$order) {
+                Util::WriteLog('StarPay', 'payin order not found: ' . $r->orderSn);
+                return 'success';
+            }
+
+            if (!empty($order->pay_at) || !empty($order->finished_at)) {
+                if ($order->payment_sn != $r->orderNo) {
+                    $logic = new OrderLogic();
+                    $amount = 100;
+                    $order_sn = $r->orderSn.'#'.time();
+                    $logic->orderCreate($order_sn, $amount, 'StarPay', $order->user_id);
+                    $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)
+                        ->first();
+                } else {
+                    return $starPayService->notifySuccess();
+                }
+            }
+
+            if ($r->status == Payment::STATUS_FAIL) {
+                $body = ['pay_status' => 2, 'updated_at' => date('Y-m-d H:i:s')];
+                DB::connection('write')->table('agent.dbo.order')->where('order_sn', $r->orderSn)
+                    ->update($body);
+                Util::WriteLog('StarPay', 'payin notify status not success: ' . $r->orderSn);
+                return $starPayService->notifySuccess();
+            }
+
+
+            $GiftsID = $order->GiftsID ?: '';
+            $userID = $order->user_id ?: '';
+            $AdId = $order->AdId ?: '';
+            $eventType = $order->eventType ?: '';
+
+            $realityAmount = isset($post['paidAmount']) ? (float)$post['paidAmount'] : (float)($post['amount'] ?? 0);
+            if ($realityAmount <= 0) {
+                Util::WriteLog('StarPay_error', 'payin notify invalid realityAmount: ' . json_encode($post));
+                return $starPayService->notifyFail();
+            }
+
+            $payAmt = round($realityAmount, 2);
+            $amountInScore = (int) round($payAmt * NumConfig::NUM_VALUE);
+
+            $body = [
+                'payment_sn'   => $r->orderNo,
+                'pay_status'   => 1,
+                'pay_at'       => date('Y-m-d H:i:s'),
+                'finished_at'  => date('Y-m-d H:i:s'),
+                'amount'       => $amountInScore,
+                'updated_at'   => date('Y-m-d H:i:s'),
+            ];
+
+            $config = (new PayConfig())->getConfig('StarPay');
+            $body['payment_fee'] = isset($config['payin_fee']) ? $amountInScore * $config['payin_fee'] : 0;
+
+            $service = new OrderServices();
+            if ((int)$order->amount != $amountInScore) {
+                $body['GiftsID'] = 0;
+                $body['amount'] = $amountInScore;
+                $Recharge = $payAmt;
+                $give = 0;
+                $favorable_price = $Recharge + $give;
+                $czReason = 1;
+                $cjReason = 45;
+            } else {
+                [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
+            }
+
+            [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $r->orderSn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
+            Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $r->orderSn]);
+
+            DB::connection('write')->table('agent.dbo.order')->where('order_sn', $r->orderSn)->update($body);
+
+            Util::WriteLog('StarPay', 'payin success, order_sn=' . $r->orderSn . ', realityAmount=' . $realityAmount);
+            return $starPayService->notifySuccess();
+        } catch (\Throwable $exception) {
+            Util::WriteLog('StarPay_error', $exception->getMessage() . "\n" . $exception->getTraceAsString());
+            throw $exception;
+        }
+    }
+}

+ 4 - 1
app/Services/CashService.php

@@ -3,6 +3,7 @@
 
 namespace App\Services;
 
+use App\Http\logic\api\StarPayCashierLogic;
 use App\Http\logic\api\WiwiPayCashierLogic;
 use App\Http\logic\api\WDPayCashierLogic;
 use App\Http\logic\api\CoinPayCashierLogic;
@@ -18,7 +19,6 @@ class CashService
     {
         switch ($val) {
 
-
             case WiwiPayCashierLogic::AGENT:
                 return new WiwiPayCashierLogic();
 
@@ -40,6 +40,9 @@ class CashService
             case SupefinaSpeiCashierLogic::AGENT:
                 return new SupefinaSpeiCashierLogic();
 
+            case StarPayCashierLogic::AGENT:
+                return new StarPayCashierLogic();
         }
+        throw new \RuntimeException('unknown cash method');
     }
 }

+ 3 - 0
app/Services/PayMentService.php

@@ -9,6 +9,7 @@ use App\Http\Controllers\Api\CryptoController;
 use App\Http\Controllers\Api\GooglePayController;
 use App\Http\Controllers\Api\GoopagoController;
 use App\Http\Controllers\Api\SitoBankController;
+use App\Http\Controllers\Api\StarPayController;
 use App\Http\Controllers\Api\WiwiPayController;
 use App\Http\Controllers\Api\WDPayController;
 use App\Http\Controllers\Api\CoinPayController;
@@ -47,6 +48,8 @@ class PayMentService
 
             case 'SupefinaSpei':
                 return new SupefinaSpeiController();
+            case 'StarPay':
+                return new StarPayController();
 
             case 'apple':
                 return new AppleStorePayController();

+ 208 - 0
app/Services/StarPayService.php

@@ -0,0 +1,208 @@
+<?php
+
+namespace App\Services;
+
+use App\Constant\Payment;
+use App\Util;
+use GuzzleHttp\Client;
+use Illuminate\Support\Facades\Log;
+
+class StarPayService
+{
+    protected $code = 'StarPay';
+
+    protected $config;
+
+    const STATUS_MAP = [
+        0 => Payment::STATUS_IN_PROGRESS,
+        1 => Payment::STATUS_IN_PROGRESS,
+        2 => Payment::STATUS_SUCCESS,
+        3 => Payment::STATUS_SUCCESS,
+        -1 => Payment::STATUS_FAIL,
+        -2 => Payment::STATUS_FAIL,
+        -3 => Payment::STATUS_REFUND,
+        -4 => Payment::STATUS_IN_PROGRESS,
+
+    ];
+
+    public function __construct($config = [])
+    {
+        if (!$config) {
+            $config = config('pay.StarPay');
+        }
+        $this->config = $config;
+    }
+
+    public function create($orderSn, $amount, $options = [])
+    {
+        $data = [];
+        $data['appId'] = $this->config['appID'];
+        $data['merOrderNo'] = $orderSn;
+        $data['currency'] = 'MXN';
+        $data['amount'] = strval($amount);
+        $data['extra'] = [
+            'single' => false,
+            'minAmount' => $this->config['minOrderAmount'] ?? 20,
+            'maxAmount' => $this->config['maxOrderAmount'] ?? 50000,
+        ];
+
+        $data['returnUrl'] = $this->config['syncNotify'];
+        $data['notifyUrl'] = $this->config['notify'];
+        $data['attach'] = strval($options['userId'] ?? '0');
+        $data['sign'] = $this->sign($this->config['secretKey'], $data);
+        $client = new Client(['verify' => false, 'timeout' => 10]);
+        $url = $this->config['baseUrl'] . '/api/v1/payment/order/create';
+
+        $request_extra = \GuzzleHttp\json_encode($data);
+        CreateLog::pay_request('', $request_extra, $orderSn, '', '', '');
+
+        Util::WriteLog('StarPay', 'payin request: ' . $url . ' | ' . $request_extra);
+        $resp = $client->post($url, [
+            'json' => $data,
+        ]);
+        if ($resp->getStatusCode() !== 200) {
+            return $resp->getReasonPhrase();
+        }
+        $content = $resp->getBody()->getContents();
+        Util::WriteLog('StarPay', 'payin response: ' . $url . ' | ' . $request_extra . '|' . $content);
+        $json = json_decode($content, true);
+        if (json_last_error() !== JSON_ERROR_NONE) {
+            Util::WriteLog('StarPay_error', [
+                'result' => $content,
+                'url' => $url,
+            ]);
+        }
+
+        return $json;
+    }
+
+    public function cash($OrderId, $amount, $bankCode, $bankName, $accountNo, $accountName)
+    {
+        $data = [
+            'appId' => $this->config['appID'],
+            'merOrderNo' => $OrderId,
+            'currency' => 'MXN',
+            'amount' => strval(round($amount, 2)),
+            'notifyUrl' => $this->config['notify'],
+            'extra' => [
+                'bankCode' => $bankCode,
+                'bankName' => $bankName,
+                'accountNo' => $accountNo,
+                'accountName' => $accountName,
+                'accountType' => strlen($accountNo) == 16 ? 3 : 40,
+            ],
+        ];
+        $data['sign'] = $this->sign($this->config['secretKey'], $data);
+        $url = $this->config['baseUrl'] . '/api/v1/payout/order/create';
+        Util::WriteLog('StarPay', 'payout request: ' . $url . ' | ' . json_encode($data, JSON_UNESCAPED_UNICODE));
+        $client = new Client(['verify' => false, 'timeout' => 10]);
+        $resp = $client->post($url, [
+            'json' => $data,
+        ]);
+        $content = $resp->getBody()->getContents();
+        Util::WriteLog('StarPay', 'payout response: ' . $content);
+        $json = json_decode($content, true);
+        if (json_last_error() !== JSON_ERROR_NONE) {
+            Util::WriteLog('StarPay_error', ['decode fail', json_last_error_msg()]);
+            return false;
+        }
+        return $json;
+    }
+
+    public function verifySign($data, $headers = []): bool
+    {
+        $sign = $this->sign($this->config['secretKey'], $data);
+        return $sign == $data['sign'];
+    }
+
+    public function notifyFail()
+    {
+        return 'fail';
+    }
+
+    public function notifySuccess()
+    {
+        return 'success';
+    }
+
+    public function notify($data)
+    {
+        if (!isset($data['orderStatus'])) {
+            throw new \RuntimeException('invalid notify data');
+        }
+        $r = new \stdClass();
+        $r->status = self::STATUS_MAP[$data['orderStatus']] ?? Payment::STATUS_UNKNOWN;
+        $r->orderNo = $data['orderNo'];
+        $r->orderSn = $data['merOrderNo'];
+        $r->amount = $data['paidAmount'];
+        $r->buyerId = $data['attach'];
+
+        return $r;
+    }
+
+    public function cashNotify($data)
+    {
+        $r = new \stdClass();
+        $r->status = self::STATUS_MAP[$data['orderStatus']] ?? Payment::STATUS_UNKNOWN;
+        $r->orderNo = $data['orderNo'];
+        $r->orderSn = $data['merOrderNo'];
+        $r->amount = $data['amount'];
+        return $r;
+    }
+
+    public function search($orderNo, $orderSn)
+    {
+        $data = [];
+        $data['appId'] = $this->config['appID'];
+        $data['orderNo'] = $orderNo;
+        $data['sign'] = $this->sign($this->config['secretKey'], $data);
+        $client = new Client(['verify' => false, 'timeout' => 10]);
+        $url = $this->config['baseUrl'] . '/api/v1/payment/order/query';
+        Log::info('StarPay查询参数', [
+            'data' => $data,
+        ]);
+        $resp = $client->get($url . '?' . http_build_query($data));
+        $r = new \stdClass();
+        $r->status = false;
+        if ($resp->getStatusCode() !== 200) {
+            $this->error = $resp->getReasonPhrase();
+            return $r;
+        }
+        $content = $resp->getBody()->getContents();
+        Log::info('StarPay查询返回结果.', ['res' => $content, 'url' => $url]);
+        $json = json_decode($content, true);
+        if (json_last_error() !== JSON_ERROR_NONE) {
+            Log::error('StarPay json_decode error', [
+                'result' => $content,
+                'url' => $url,
+            ]);
+            return $r;
+        }
+        $r->status = self::STATUS_MAP[$json['orderStatus']] ?? Payment::STATUS_UNKNOWN;
+        return $r;
+    }
+
+    public function sign($secretKey, $data)
+    {
+        $copy = $data;
+        unset($copy['sign']);
+
+        $str = self::joinMap($copy);
+        $str .= "&key=$secretKey";
+        return hash('sha256', $str);
+    }
+
+    protected static function joinMap($arr)
+    {
+        ksort($arr);
+        $pair = [];
+        foreach ($arr as $k => $v) {
+            if (is_array($v)) {
+                $pair[] = "$k=" . self::joinMap($v);
+            } else {
+                $pair[] = "$k=" . (is_bool($v) ? json_encode($v) : $v);
+            }
+        }
+        return implode('&', $pair);
+    }
+}

+ 16 - 1
config/payTest.php

@@ -42,5 +42,20 @@ return [
         'defaultBankId' => env('SUPEFINA_DEFAULT_BANK_ID', ''),
         'callbackUrl'   => env('APP_URL', '') . '/api/supefina/notify',
     ],
-
+    'StarPay' => [
+        'baseUrl' => env('STAR_PAY_BASE_URL', 'https://api.starpaybank.com'),
+        'appID' => env('STAR_PAY_APP_ID', '3e03bab78ed755c8352a2a099d88b43a'),
+        'secretKey' => env('STAR_PAY_SECRET_KEY', '373ad0afd59be13cf77931a401aa7558'),
+        'notify' => env('APP_URL', '') . '/api/star_pay/notify',
+        'syncNotify' => env('APP_URL', '') . '/api/star_pay/syncNotify',
+        'minOrderAmount' => 20,
+        'maxOrderAmount' => 50000,
+        'payin_fee' => 0.002,
+    ],
+    'StarPayOut' => [
+        'baseUrl' => env('STAR_PAY_BASE_URL', 'https://api.starpaybank.com'),
+        'appID' => env('STAR_PAY_CASH_APP_ID', '0cf78c8e28ba5882e89557410645d5b3'),
+        'secretKey' => env('STAR_PAY_CASH_SECRET_KEY', '92d91beee9a8ee3767194bdfbfe4048f'),
+        'notify' => env('APP_URL', '') . '/api/star_pay/payout_notify',  // 提现异步回调
+    ]
 ];

+ 6 - 0
routes/api.php

@@ -400,6 +400,12 @@ Route::any('/karopay/cash_notify', 'Api\KaroPayController@cash_notify');
 Route::any('/supefina/notify', 'Api\SupefinaSpeiController@notify');
 Route::any('/supefina/payout_notify', 'Api\SupefinaSpeiController@cash_notify');
 
+// StarPay
+Route::any('/star_pay/notify', 'Api\StarPayController@notify');
+Route::any('/star_pay/sync_notify', function ($r) {
+    return 'success';
+});
+Route::any('/star_pay/payout_notify', 'Api\StarPayController@cash_notify');
 
 Route::any('/clear_cache', function () {
     $path = app()->basePath('/public/cache/');