1.配置新版支付模块-菜单和接口都已重构
2.优化充值提现页面 3.菜单翻译问题 4.备份数据库
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace app\admin\controller\order;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\finance\DDPayGateway;
|
||||
use support\think\Db;
|
||||
use support\Response;
|
||||
use Throwable;
|
||||
@@ -251,6 +252,264 @@ class WithdrawOrder extends Backend
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
|
||||
// 审核通过后自动发起 DDPAY 出金(并在失败时回冲余额)
|
||||
try {
|
||||
$fresh = Db::name('withdraw_order')->where('id', $id)->find();
|
||||
if (!is_array($fresh)) {
|
||||
// 理论上不会发生:只写失败备注
|
||||
Db::name('withdraw_order')->where('id', $id)->update([
|
||||
'remark' => '[ddpay] payout order missing',
|
||||
'update_time' => time(),
|
||||
]);
|
||||
} else {
|
||||
$orderNo = is_string($fresh['order_no'] ?? null) ? trim($fresh['order_no'] ?? '') : strval($fresh['order_no'] ?? '');
|
||||
$receiveType = is_string($fresh['receive_type'] ?? null) ? strtolower(trim($fresh['receive_type'] ?? '')) : '';
|
||||
|
||||
// 当前仅接入 bank 类型出金(与移动端 withdrawCreate 校验一致)
|
||||
if ($orderNo !== '' && $receiveType === 'bank') {
|
||||
$base = \app\common\library\finance\DDPayGateway::publicBaseUrlForCallbacks($request);
|
||||
if ($base === '') {
|
||||
$base = 'https://' . strval($request->host());
|
||||
}
|
||||
$callbackUrl = rtrim($base, '/') . '/api/finance/ddpayPayoutNotify';
|
||||
$payoutAmount = is_string($fresh['actual_amount'] ?? null) && $fresh['actual_amount'] !== '' ? trim($fresh['actual_amount']) : $newActual;
|
||||
|
||||
$receiverName = is_string($fresh['ddpay_receiver_name'] ?? null) ? trim($fresh['ddpay_receiver_name'] ?? '') : '';
|
||||
$receiverAccount = is_string($fresh['receive_account'] ?? null) ? trim($fresh['receive_account'] ?? '') : '';
|
||||
$bankName = is_string($fresh['ddpay_bank_name'] ?? null) ? trim($fresh['ddpay_bank_name'] ?? '') : '';
|
||||
$bankBranch = is_string($fresh['ddpay_bank_branch'] ?? null) ? trim($fresh['ddpay_bank_branch'] ?? '') : 'N/A';
|
||||
|
||||
if ($receiverName === '' || $receiverAccount === '' || $bankName === '') {
|
||||
// 缺少 DDPAY 出金字段:回冲并置为失败
|
||||
Db::startTrans();
|
||||
try {
|
||||
$amountRefund = bcadd(strval($fresh['amount'] ?? '0'), '0', 2);
|
||||
$updated = Db::name('withdraw_order')
|
||||
->where('id', $id)
|
||||
->where('status', 1)
|
||||
->update([
|
||||
'status' => 2,
|
||||
'remark' => '[ddpay] missing payout fields',
|
||||
'update_time' => time(),
|
||||
]);
|
||||
if ($updated > 0 && bccomp($amountRefund, '0', 2) > 0) {
|
||||
$userIdRefund = intval(strval($fresh['user_id'] ?? 0));
|
||||
$userRow = Db::name('user')->where('id', $userIdRefund)->find();
|
||||
$beforeCoin = bcadd(strval($userRow['coin'] ?? '0'), '0', 2);
|
||||
$afterCoin = bcadd($beforeCoin, $amountRefund, 2);
|
||||
Db::name('user')->where('id', $userIdRefund)->update([
|
||||
'coin' => $afterCoin,
|
||||
'total_withdraw_coin' => Db::raw('total_withdraw_coin - ' . $amountRefund),
|
||||
'update_time' => time(),
|
||||
]);
|
||||
$idempotencyKey = 'wd_ddpay_failed_' . strval($orderNo);
|
||||
$exists = Db::name('user_wallet_record')->where('idempotency_key', $idempotencyKey)->find();
|
||||
if (!$exists) {
|
||||
$channelId = null;
|
||||
if (isset($fresh['channel_id']) && is_numeric(strval($fresh['channel_id']))) {
|
||||
$channelId = intval(strval($fresh['channel_id']));
|
||||
}
|
||||
Db::name('user_wallet_record')->insert([
|
||||
'user_id' => $userIdRefund,
|
||||
'channel_id' => $channelId,
|
||||
'biz_type' => 'withdraw_refund',
|
||||
'direction' => 1,
|
||||
'amount' => $amountRefund,
|
||||
'balance_before' => $beforeCoin,
|
||||
'balance_after' => $afterCoin,
|
||||
'ref_type' => 'withdraw_order',
|
||||
'ref_id' => intval(strval($id)),
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'operator_admin_id' => null,
|
||||
'remark' => '[ddpay] missing payout fields refund',
|
||||
'create_time' => time(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
} catch (Throwable $e2) {
|
||||
Db::rollback();
|
||||
}
|
||||
} else {
|
||||
$clientId = config('app.ddpay_client_id', '');
|
||||
$identifier = config('app.ddpay_identifier', '');
|
||||
|
||||
$ddReq = [
|
||||
'client_id' => $clientId,
|
||||
'identifier' => $identifier,
|
||||
'bill_number' => $orderNo,
|
||||
'amount' => $payoutAmount,
|
||||
'receiver_name' => $receiverName,
|
||||
'receiver_account' => $receiverAccount,
|
||||
'bank[name]' => $bankName,
|
||||
'bank_branch' => $bankBranch,
|
||||
'callback_url' => $callbackUrl,
|
||||
];
|
||||
|
||||
$ddResp = [];
|
||||
$ts = '';
|
||||
try {
|
||||
$ddResp = DDPayGateway::payoutInitiation($ddReq);
|
||||
$ts = is_string($ddResp['transaction_status'] ?? null) ? strtolower(trim($ddResp['transaction_status'] ?? '')) : '';
|
||||
} catch (Throwable $e) {
|
||||
// initiation 异常:同“failed”处理,回冲并置失败
|
||||
$ts = 'failed';
|
||||
$ddResp = ['error' => (string) $e->getMessage()];
|
||||
}
|
||||
|
||||
if (is_array($ddResp)) {
|
||||
Db::name('withdraw_order')
|
||||
->where('id', $id)
|
||||
->update([
|
||||
'ddpay_payout_snapshot' => json_encode([
|
||||
'init_request' => $ddReq,
|
||||
'init_response' => $ddResp,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($ts === 'completed') {
|
||||
Db::name('withdraw_order')
|
||||
->where('id', $id)
|
||||
->where('status', 1)
|
||||
->update([
|
||||
'status' => 3,
|
||||
'remark' => '[ddpay] payout completed',
|
||||
'update_time' => time(),
|
||||
]);
|
||||
} elseif ($ts === 'failed') {
|
||||
$amountRefund = bcadd(strval($fresh['amount'] ?? '0'), '0', 2);
|
||||
Db::startTrans();
|
||||
try {
|
||||
$updated = Db::name('withdraw_order')
|
||||
->where('id', $id)
|
||||
->where('status', 1)
|
||||
->update([
|
||||
'status' => 2,
|
||||
'remark' => '[ddpay] payout failed',
|
||||
'update_time' => time(),
|
||||
]);
|
||||
if ($updated > 0 && bccomp($amountRefund, '0', 2) > 0) {
|
||||
$userIdRefund = intval(strval($fresh['user_id'] ?? 0));
|
||||
$userRow = Db::name('user')->where('id', $userIdRefund)->find();
|
||||
$beforeCoin = bcadd(strval($userRow['coin'] ?? '0'), '0', 2);
|
||||
$afterCoin = bcadd($beforeCoin, $amountRefund, 2);
|
||||
Db::name('user')->where('id', $userIdRefund)->update([
|
||||
'coin' => $afterCoin,
|
||||
'total_withdraw_coin' => Db::raw('total_withdraw_coin - ' . $amountRefund),
|
||||
'update_time' => time(),
|
||||
]);
|
||||
$idempotencyKey = 'wd_ddpay_failed_' . strval($orderNo);
|
||||
$exists = Db::name('user_wallet_record')->where('idempotency_key', $idempotencyKey)->find();
|
||||
if (!$exists) {
|
||||
$channelId = null;
|
||||
if (isset($fresh['channel_id']) && is_numeric(strval($fresh['channel_id']))) {
|
||||
$channelId = intval(strval($fresh['channel_id']));
|
||||
}
|
||||
Db::name('user_wallet_record')->insert([
|
||||
'user_id' => $userIdRefund,
|
||||
'channel_id' => $channelId,
|
||||
'biz_type' => 'withdraw_refund',
|
||||
'direction' => 1,
|
||||
'amount' => $amountRefund,
|
||||
'balance_before' => $beforeCoin,
|
||||
'balance_after' => $afterCoin,
|
||||
'ref_type' => 'withdraw_order',
|
||||
'ref_id' => intval(strval($id)),
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'operator_admin_id' => null,
|
||||
'remark' => '[ddpay] payout failed refund',
|
||||
'create_time' => time(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
} catch (Throwable $e3) {
|
||||
Db::rollback();
|
||||
}
|
||||
} else {
|
||||
// pending:按文档做一次 status inquiry 兜底(Webhook 仍会最终落账)
|
||||
try {
|
||||
$inq = DDPayGateway::payoutStatusInquiry([
|
||||
'client_id' => $clientId,
|
||||
'bill_number' => $orderNo,
|
||||
]);
|
||||
$ts2 = is_string($inq['transaction_status'] ?? null) ? strtolower(trim($inq['transaction_status'] ?? '')) : '';
|
||||
if ($ts2 === 'completed') {
|
||||
Db::name('withdraw_order')
|
||||
->where('id', $id)
|
||||
->where('status', 1)
|
||||
->update([
|
||||
'status' => 3,
|
||||
'remark' => '[ddpay] payout completed (after status inquiry)',
|
||||
'update_time' => time(),
|
||||
]);
|
||||
} elseif ($ts2 === 'failed') {
|
||||
$amountRefund = bcadd(strval($fresh['amount'] ?? '0'), '0', 2);
|
||||
Db::startTrans();
|
||||
try {
|
||||
$updated = Db::name('withdraw_order')
|
||||
->where('id', $id)
|
||||
->where('status', 1)
|
||||
->update([
|
||||
'status' => 2,
|
||||
'remark' => '[ddpay] payout failed (after status inquiry)',
|
||||
'update_time' => time(),
|
||||
]);
|
||||
if ($updated > 0 && bccomp($amountRefund, '0', 2) > 0) {
|
||||
$userIdRefund = intval(strval($fresh['user_id'] ?? 0));
|
||||
$userRow = Db::name('user')->where('id', $userIdRefund)->find();
|
||||
$beforeCoin = bcadd(strval($userRow['coin'] ?? '0'), '0', 2);
|
||||
$afterCoin = bcadd($beforeCoin, $amountRefund, 2);
|
||||
Db::name('user')->where('id', $userIdRefund)->update([
|
||||
'coin' => $afterCoin,
|
||||
'total_withdraw_coin' => Db::raw('total_withdraw_coin - ' . $amountRefund),
|
||||
'update_time' => time(),
|
||||
]);
|
||||
$idempotencyKey = 'wd_ddpay_failed_' . strval($orderNo);
|
||||
$exists = Db::name('user_wallet_record')->where('idempotency_key', $idempotencyKey)->find();
|
||||
if (!$exists) {
|
||||
$channelId = null;
|
||||
if (isset($fresh['channel_id']) && is_numeric(strval($fresh['channel_id']))) {
|
||||
$channelId = intval(strval($fresh['channel_id']));
|
||||
}
|
||||
Db::name('user_wallet_record')->insert([
|
||||
'user_id' => $userIdRefund,
|
||||
'channel_id' => $channelId,
|
||||
'biz_type' => 'withdraw_refund',
|
||||
'direction' => 1,
|
||||
'amount' => $amountRefund,
|
||||
'balance_before' => $beforeCoin,
|
||||
'balance_after' => $afterCoin,
|
||||
'ref_type' => 'withdraw_order',
|
||||
'ref_id' => intval(strval($id)),
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'operator_admin_id' => null,
|
||||
'remark' => '[ddpay] payout failed refund (after status inquiry)',
|
||||
'create_time' => time(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
} catch (Throwable $e4) {
|
||||
Db::rollback();
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e5) {
|
||||
// ignore:Webhook 会兜底
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
// 外部出金调用失败不阻断审核流:只记录 remark(避免阻塞用户提现)
|
||||
Db::name('withdraw_order')->where('id', $id)->update([
|
||||
'remark' => '[ddpay] payout flow exception: ' . substr((string) $e->getMessage(), 0, 200),
|
||||
'update_time' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->success(__('Approved'), [
|
||||
'id' => $id,
|
||||
'amount' => $newAmount,
|
||||
|
||||
Reference in New Issue
Block a user