|
|
@@ -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';
|
|
|
+ }
|
|
|
+}
|
|
|
+
|