- 新增 AlinPay 网关核心类:签名/验签/代收/代付/查询/账户接口 - 新增 alinPay trait:充值发起 + 代收回调结算(事务+行锁) - 修改 admin/agent Report:提现审批自动路由 ALIN_VN 发起代付 - 新增 Pcapi:银行卡提现申请 + 越南银行列表接口 - 修改 Crypto:代付回调/订单查询/账户查询入口 - 新增越南区 area_list(id=3) + alinPay pay_list 配置 - DDL: user_withdraw 表增加 pay_channel/ext 字段
295 lines
12 KiB
PHP
295 lines
12 KiB
PHP
<?php
|
|
namespace app\onlinechip\controller\traits;
|
|
|
|
use think\Db;
|
|
use think\Log;
|
|
use think\Request;
|
|
|
|
/**
|
|
* AlinPay 越南支付 Trait
|
|
* 仅包含代收(充值)相关方法
|
|
* 代付回调在 Crypto.php 中处理
|
|
*/
|
|
trait alinPay
|
|
{
|
|
/**
|
|
* 越南代收 - 发起充值
|
|
*/
|
|
public function alin_online_order()
|
|
{
|
|
$user_id = intval(Request::instance()->post('user_id'));
|
|
$money = intval(Request::instance()->post('price'));
|
|
$client = intval(Request::instance()->post('client'));
|
|
$username = trim(Request::instance()->post('username'));
|
|
|
|
$user = Db::name('user')->where([
|
|
'id' => $user_id,
|
|
'status' => 1,
|
|
'is_delete' => 0,
|
|
'agent' => 0,
|
|
])->find();
|
|
|
|
if (!$user || $user_id <= 0 || $money <= 0 || $client <= 0) {
|
|
return json(['status' => 0, 'message' => '参数错误,请稍后再试']);
|
|
}
|
|
|
|
// 判断地区支线权限
|
|
$zdlIdArr = explode(',', $user['agent_parent_id_path']);
|
|
$area_list = config('area_list');
|
|
|
|
if (!isset($area_list[$user['area_id']])) {
|
|
return json(['status' => 0, 'message' => '所在地区暂不支持充值']);
|
|
}
|
|
|
|
$isAllow = false;
|
|
foreach ($area_list[$user['area_id']]['limit_agent'] as $allowAgentId) {
|
|
if (in_array($allowAgentId, $zdlIdArr)) {
|
|
$isAllow = true;
|
|
}
|
|
}
|
|
if (!$isAllow) {
|
|
return json(['status' => 0, 'message' => '该账号暂不支持在线充值']);
|
|
}
|
|
|
|
// 验证总代理余额
|
|
$zdlId = $zdlIdArr[0];
|
|
$zdl = Db::name('user')->where([
|
|
'id' => $zdlId,
|
|
'status' => 1,
|
|
'is_delete' => 0,
|
|
'agent' => 1,
|
|
])->find();
|
|
|
|
if (!$zdl) {
|
|
return json(['status' => 0, 'message' => '所属代理账号异常,暂不能充值']);
|
|
}
|
|
if ($zdl['money'] < $money) {
|
|
return json(['status' => 0, 'message' => '所属代理余额不足,暂不能充值']);
|
|
}
|
|
|
|
$pay_list = config('pay_list');
|
|
$pay_orderid = 'E' . date("YmdHis") . rand(100000, 999999);
|
|
$notify_url = $pay_list['alinPay']['notify_url'];
|
|
|
|
try {
|
|
$alinPay = new \pay\AlinPay();
|
|
$result = $alinPay->createOrder($pay_orderid, sprintf('%.2f', $money), $notify_url, 'uid:' . $user_id);
|
|
|
|
if (is_array($result) && isset($result['code']) && $result['code'] == 200) {
|
|
$returnUrl = $result['data']['url'] ?? '';
|
|
|
|
if (!$returnUrl) {
|
|
return json(['status' => 0, 'message' => '支付地址获取失败']);
|
|
}
|
|
|
|
// 记录订单
|
|
$order_record = [
|
|
'pay_channel_id' => 7,
|
|
'pay_channel_name' => 'alinPay',
|
|
'user_id' => $user_id,
|
|
'order_sn' => $pay_orderid,
|
|
'appid' => $alinPay->getConfigInfo()['app_id'],
|
|
'api_key' => '',
|
|
'money' => $money,
|
|
'sign' => '',
|
|
'create_time' => time(),
|
|
'back_order_sn' => '',
|
|
'client' => $client,
|
|
];
|
|
$order_id = Db::name('order_record')->insertGetId($order_record);
|
|
|
|
// 记录日志
|
|
Db::name('order_log')->insert([
|
|
'pay_channel_id' => 7,
|
|
'pay_channel_name' => 'alinPay',
|
|
'money' => $money,
|
|
'create_time' => time(),
|
|
'remake' => 'user_id:' . $user_id . ',用户名:' . $username . ',发起充值:' . $money . ',充值时间:' . date('Y-m-d H:i:s'),
|
|
'order_id' => $order_id,
|
|
'client' => $client,
|
|
]);
|
|
|
|
return json(['status' => 1, 'message' => '发起支付成功,即将跳转...', 'url' => $returnUrl]);
|
|
} else {
|
|
$errMsg = $result['message'] ?? '未知错误';
|
|
return json(['status' => 0, 'message' => '支付发起失败:' . $errMsg]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
addLogToFile('代收下单异常:' . $e->getMessage(), 'alin_error', 'alin');
|
|
return json(['status' => 0, 'message' => '支付发起失败,请稍后再试']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 越南代收 - 回调处理
|
|
*/
|
|
public function alin_recharge_callback()
|
|
{
|
|
$post = Request::instance()->post();
|
|
$json = json_encode($post, JSON_UNESCAPED_UNICODE);
|
|
|
|
addLogToFile('代收回调原始数据:' . $json, 'alin_callback', 'alin');
|
|
|
|
if (!is_array($post) || empty($post['out_trade_no']) || empty($post['sign'])) {
|
|
Log::record($post, 'alin_error_callback');
|
|
exit('fail');
|
|
}
|
|
|
|
// 记录回调日志
|
|
$logId = Db::name('pay_callback_log')->insertGetId([
|
|
'pay_channel' => 'ALIN_RECHARGE',
|
|
'type' => 'recharge',
|
|
'json' => $json,
|
|
'create_time' => date('Y-m-d H:i:s'),
|
|
]);
|
|
|
|
try {
|
|
$alinPay = new \pay\AlinPay();
|
|
$result = $alinPay->verifyRechargeCallback($post, $logId);
|
|
|
|
if (!$result['verified']) {
|
|
exit('fail');
|
|
}
|
|
|
|
$tradeStatus = $result['trade_status'];
|
|
|
|
// trade_status: 1=成功, -1=处理中, 2=失败
|
|
if ($tradeStatus === 1) {
|
|
$this->alin_recharge_settle($post);
|
|
exit('success');
|
|
} elseif ($tradeStatus === 2) {
|
|
// 失败,终态,确认收到
|
|
addLogToFile('代收回调失败:order=' . $post['out_trade_no'], 'alin_callback', 'alin');
|
|
exit('success');
|
|
} else {
|
|
// 处理中(-1),不返回success让网关重试
|
|
exit('fail');
|
|
}
|
|
} catch (\Exception $e) {
|
|
addLogToFile('代收回调处理异常:' . $e->getMessage(), 'alin_callback_error', 'alin');
|
|
exit('fail');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 代收成功后结算(事务保护:扣代理、写recharge表、加用户余额)
|
|
*/
|
|
private function alin_recharge_settle($data)
|
|
{
|
|
$orderNo = $data['out_trade_no'];
|
|
|
|
Db::startTrans();
|
|
try {
|
|
// 加锁读取订单,防止重复处理
|
|
$order = Db::name('order_record')->where('order_sn', $orderNo)->lock(true)->find();
|
|
if (!$order || $order['status'] == 3) {
|
|
Db::rollback();
|
|
return;
|
|
}
|
|
|
|
// 加锁读取用户
|
|
$user = Db::name('user')->where([
|
|
'id' => $order['user_id'],
|
|
'status' => 1,
|
|
'is_delete' => 0,
|
|
'agent' => 0,
|
|
])->lock(true)->find();
|
|
if (!$user) {
|
|
Db::rollback();
|
|
return;
|
|
}
|
|
|
|
$zdlIdArr = explode(',', $user['agent_parent_id_path']);
|
|
$zdlId = $zdlIdArr[0];
|
|
|
|
// 加锁读取总代理
|
|
$zdl = Db::name('user')->where([
|
|
'id' => $zdlId,
|
|
'status' => 1,
|
|
'is_delete' => 0,
|
|
'agent' => 1,
|
|
])->lock(true)->find();
|
|
|
|
if (!$zdl || $zdl['money'] < $order['money']) {
|
|
Db::rollback();
|
|
addLogToFile('代收结算失败:代理余额不足 order=' . $orderNo, 'alin_callback_error', 'alin');
|
|
return;
|
|
}
|
|
|
|
// 1. 更新订单状态为已完成
|
|
Db::name('order_record')->where('id', $order['id'])->update([
|
|
'back_money' => $data['real_amount'] ?: $data['amount'],
|
|
'back_time' => time(),
|
|
'status' => 3,
|
|
'agent_parent_id' => $user['agent_parent_id'],
|
|
'agent_parent_username' => $user['agent_parent_username'],
|
|
'zdl_id' => $zdl['id'],
|
|
'zdl_username' => $zdl['username'],
|
|
'zdl_money_before' => $zdl['money'],
|
|
'zdl_money_after' => $zdl['money'] - $order['money'],
|
|
'money_before' => $user['money'],
|
|
'money_after' => $user['money'] + $order['money'],
|
|
]);
|
|
|
|
// 2. 记录日志
|
|
Db::name('order_log')->insert([
|
|
'pay_channel_id' => 7,
|
|
'pay_channel_name' => 'alinPay',
|
|
'money' => $order['money'],
|
|
'create_time' => time(),
|
|
'remake' => '回调支付成功',
|
|
'order_id' => $order['id'],
|
|
]);
|
|
|
|
// 3. 写recharge流水表
|
|
Db::name('recharge')->insert([
|
|
'type' => 4,
|
|
'amount' => $order['money'],
|
|
'mode' => 1,
|
|
'agent_or_admin' => 3,
|
|
'controller_id' => $zdl['id'],
|
|
'controller_username' => $zdl['username'],
|
|
'controller_nickname' => $zdl['nickname'],
|
|
'controller_type' => '越南第三方充值平台充值,扣取总代理余分',
|
|
'controller_old_money' => $zdl['money'],
|
|
'controller_new_money' => $zdl['money'] - $order['money'],
|
|
'user_id' => $order['user_id'],
|
|
'user_type' => $user['agent'],
|
|
'user_agent_level' => 0,
|
|
'username_for' => $user['username'],
|
|
'nickname_for' => $user['nickname'],
|
|
'user_parent_id' => $user['agent_parent_id'],
|
|
'create_time' => time(),
|
|
'old_money' => $user['money'],
|
|
'new_money' => $user['money'] + $order['money'],
|
|
'controller_system' => 3,
|
|
'remake' => '越南AlinPay第三方充值成功',
|
|
]);
|
|
|
|
// 4. 扣代理余额
|
|
Db::name('user')->where('id', $zdl['id'])->update([
|
|
'money' => $zdl['money'] - $order['money'],
|
|
'last_recharge_out' => $order['money'],
|
|
'last_recharge_out_time' => time(),
|
|
'recharge_out_count' => $zdl['recharge_out_count'] + 1,
|
|
'recharge_out_total_amount' => $zdl['recharge_out_total_amount'] + $order['money'],
|
|
]);
|
|
|
|
// 5. 给用户加余额
|
|
Db::name('user')->where('id', $order['user_id'])->update([
|
|
'money' => $user['money'] + $order['money'],
|
|
'last_recharge' => $order['money'],
|
|
'last_recharge_time' => time(),
|
|
'recharge_count' => $user['recharge_count'] + 1,
|
|
'recharge_total_amount' => $user['recharge_total_amount'] + $order['money'],
|
|
]);
|
|
|
|
Db::commit();
|
|
addLogToFile('代收结算成功:order=' . $orderNo . ' money=' . $order['money'], 'alin_callback', 'alin');
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
addLogToFile('代收结算异常:' . $e->getMessage(), 'alin_callback_error', 'alin');
|
|
}
|
|
}
|
|
}
|