Files
yuyin-php/application/common/pay/Callback.php
2025-08-08 14:45:40 +08:00

201 lines
9.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\pay;
use app\common\traits\ReturnTrait;
use app\common\yun\service\Des3Service;
use app\common\yun\util\RsaUtil;
use think\Db;
use think\Exception;
use think\facade\Log;
class Callback
{
use ReturnTrait;
public static function order()
{
$config = Conf::config();
$result = new RsaUtil($config);
$verify = $result->verify($_REQUEST);
if ($verify == false) {
Log::write('签名失败','yun_notify');
echo 'fail';
die;
}
// Log::info($_REQUEST);
// Log::info($_REQUEST['data']);
// Log::write($_REQUEST,'yun_notify');
// Log::write($_REQUEST['data'],'yun_notify');
$data_info = Des3Service::decode($_REQUEST['data'], $config->des3_key); #对业务数据进行解密
if (!isset($data_info['data'])) {
echo 'fail';
die;
}
$data = $data_info['data'];
Log::write($data,'yun_notify');
// Log::write($_REQUEST['data'],'yun_notify');
if (!isset($data['status'])) {
echo 'fail';
die;
}
// Log::info('xxxxxxx');
// Log::info($data);
//var_dump($data);
//根据回调数据中的status做一下订单状态的判断和业务逻辑处理
//若有用户钱包体系,则在下单同步返回成功时,将用户钱包进行相应金额的扣减冻结
$status = $data['status'];
switch ($status) {
case "1":
// 支付成功(对于支付宝和微信支付是最终状态,对于银行卡大部分情况是终态,小概率会出现"退汇现象",状态由"成功"变为"退汇"
// TODO 更新业务订单状态,提示用户提现成功
$redis = connectionRedis();
$key_name = "api:user:widthdraw:order_sn".$data['order_id'];
$redis_order_sn = redis_lock_exits($key_name,1,5);
if($redis_order_sn) {
$update_arr = [
'status' => 6,
'update_time' => time(),
'pay_time' => time(),
'pay_message' => $data['status_message'],
// 'pay_platform' => 1,
];
Db::name('user_withdrawal')->where(['order_sn' => $data['order_id'], 'status' => 4])->update($update_arr);
}
echo "success";
break;
case "2":
// 支付失败(最终状态)
// TODO 更新业务订单状态,提示用户提现失败,若有用户钱包体系,则需将提现金额退回至用户钱包
$redis = connectionRedis();
$key_name = "api:user:widthdraw:order_sn".$data['order_id'];
$redis_order_sn = redis_lock_exits($key_name,1,5);
$update_arr = [
'status' => 5,
'update_time' => time(),
'pay_message' => $data['status_message'],
'pay_time' => time(),
// 'pay_platform' => 1,
];
if($redis_order_sn) {
Db::startTrans();
try {
// $info = Db::name('user_withdrawal')->where(['order_sn' => $data['order_id'], 'status' => 4])->find();
$info = Db::name('user_withdrawal')->where(['order_sn' => $data['order_id']])->find();
if ($info) {
Db::name('user_withdrawal')->where(['order_sn' => $data['order_id']])->update($update_arr);
$reslut=model('admin/User')->change_user_money_by_uid($info['uid'], $info['money'], 1, 15, "代付失败,余额提现驳回:" . $info['order_sn'], $info['uid'], $info['wid']);
if ($reslut['code'] !=200 ) {
Db::rollback();
Log::write(['msg'=>'支付失败1!','msg1'=>$reslut['msg']],'yun_notify');
}
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
Log::write(['msg'=>'支付失败2!','msg1'=>$e->getMessage()],'yun_notify');
// Log::info($e->getMessage());
}
}
echo "success";
break;
case "4":
// 订单挂单(中间状态,挂单原因会在订单详细状态信息返回)
// TODO 提示用户提现中其他逻辑如若因余额不足导致的挂单可通知财务及时充值72小时内补足余额后可自动继续支付
echo "success";
break;
case "9":
// 退汇,(最终状态,银行卡渠道特有现象,会先收到"成功"回调,然后再收到"退汇"的回调一般以成功状态间隔24小时以上
// TODO 更新业务订单状态为“退汇”(失败),通知用户提现失败,建议用户更换其他银行卡提现,若有用户钱包体系,则需将提现金额退回至用户钱包
$update_arr = [
'status' => 5,
'update_time' => time(),
'pay_message' => $data['status_message'],
// 'pay_platform' => 1,
'pay_time' => time()
];
$redis = connectionRedis();
$key_name = "api:user:widthdraw:order_sn".$data['order_id'];
$redis_order_sn = redis_lock_exits($key_name,1,5);
if($redis_order_sn) {
Db::startTrans();
try {
// $info = Db::name('user_withdrawal')->where(['order_sn' => $data['order_id'], 'status' => 4])->find();
$info = Db::name('user_withdrawal')->where(['order_sn' => $data['order_id']])->find();
if ($info) {
Db::name('user_withdrawal')->where(['order_sn' => $data['order_id']])->update($update_arr);
$reslut=model('admin/User')->change_user_money_by_uid($info['uid'], $info['money'], 1, 15, "代付失败,余额提现驳回:" . $info['order_sn'], $info['uid'], $info['wid']);
if ($reslut['code'] !=200 ) {
Db::rollback();
// return ['code' => 201, 'msg' => "处理失败", 'data' => null];
}
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
Log::write(['msg'=>'退汇!','msg1'=>$e->getMessage()],'yun_notify');
// Log::info($e->getMessage());
}
}
echo "success";
break;
case "15":
// 订单取消,(最终状态,只有挂单的订单才可以取消)
// TODO 更新业务订单状态为“取消”(失败),通知用户提现失败,若有用户钱包体系,则需将提现金额退回至用户钱包
$update_arr = [
'status' => 5,
'update_time' => time(),
'pay_message' => $data['status_message'],
// 'pay_platform' => 1,
'pay_time' => time()
];
$redis = connectionRedis();
$key_name = "api:user:widthdraw:order_sn".$data['order_id'];
$redis_order_sn = redis_lock_exits($key_name,1,5);
if($redis_order_sn) {
Db::startTrans();
try {
// $info = Db::name('user_withdrawal')->where(['order_sn' => $data['order_id'], 'status' => 4])->find();
$info = Db::name('user_withdrawal')->where(['order_sn' => $data['order_id']])->find();
if ($info) {
Db::name('user_withdrawal')->where(['order_sn' => $data['order_id']])->update($update_arr);
$reslut=model('admin/User')->change_user_money_by_uid($info['uid'], $info['money'], 1, 15, "代付失败,余额提现驳回:" . $info['order_sn'], $info['uid'], $info['wid']);
if ($reslut['code'] !=200 ) {
Db::rollback();
}
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
Log::write(['msg'=>'订单取消!','msg1'=>$e->getMessage()],'yun_notify');
// Log::info($e->getMessage());
}
}
echo "success";
break;
}
// echo "success"; //回写success终止本次回调
}
}