256 lines
12 KiB
PHP
256 lines
12 KiB
PHP
<?php
|
|
namespace app\onlinechip\controller\traits;
|
|
use think\Db;
|
|
use think\Log;
|
|
use think\Request;
|
|
trait hengfuPay{
|
|
public function hengfu_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'));
|
|
$pay_type = intval(Request::instance()->post('pay_type'));
|
|
$user = Db::name('user')->where(array('id' => $user_id, 'status' => 1, 'is_delete' => 0, 'agent' => 0))->find();
|
|
$zdlIdArr = explode(',',$user['agent_parent_id_path']);
|
|
$area_list = config('area_list');
|
|
//判断是否金流支线,如果不是的话,不允许充值
|
|
$isAllow = false;
|
|
foreach($area_list[$user['area_id']]['limit_agent'] as $allowAgentId){
|
|
if(in_array($allowAgentId,$zdlIdArr)){
|
|
$isAllow = true;
|
|
}
|
|
}
|
|
if(!$isAllow){
|
|
return json(array('status' => 0, 'message' => '该账号暂不支持在线充值'));
|
|
}
|
|
$zdlId = $zdlIdArr[0];
|
|
$zdl = Db::name('user')->where(array('id' => $zdlId, 'status' => 1, 'is_delete' => 0, 'agent' => 1))->find();
|
|
if(!$zdl){
|
|
return json(array('status' => 0, 'message' => '所属代理账号异常,暂不能充值'));
|
|
}
|
|
if($zdl['money'] < $money){
|
|
return json(array('status' => 0, 'message' => '所属代理余额不足,暂不能充值'));
|
|
}
|
|
$pay_list = config('pay_list');
|
|
if($user && $user_id > 0 && $money > 0 && $client > 0){
|
|
$sn = '1965312301';
|
|
$Authorization = 'aE86s4HKxKghfXLOmAPBKMIJymFedk6W99RJe4L9QOOwNvWFlKZgNyqfycP4lXVS';
|
|
$PaymentType = $pay_type;
|
|
$VendorSerialNumber = 'E'.date("YmdHis").rand(100000,999999); //订单号
|
|
$SourceName = $user['username']; //来源名称
|
|
$TransactionAmount = $money; //交易金额
|
|
$CallBack = $pay_list['hengfuPay']['notify_url']; //回调返回地址
|
|
$RedirectUrl = $pay_list['hengfuPay']['callback_url']; //页面跳转返回地址
|
|
$curl = curl_init();
|
|
curl_setopt_array($curl, array(
|
|
CURLOPT_URL => "https://hengfu.cool/api/2.0.0/recharge/order/",
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => "",
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 0,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => "POST",
|
|
CURLOPT_POSTFIELDS => "sn=".$sn."&PaymentType=".$PaymentType."&TransactionAmount=".$TransactionAmount."&VendorSerialNumber=".$VendorSerialNumber."&CallBack=".$CallBack."&RedirectUrl=".$RedirectUrl."&SourceName=".$SourceName,
|
|
CURLOPT_HTTPHEADER => array(
|
|
"Authorization: ".$Authorization,
|
|
"Content-Type: application/x-www-form-urlencoded"
|
|
),
|
|
));
|
|
$response = curl_exec($curl);
|
|
curl_close($curl);
|
|
$response = json_decode($response,true);
|
|
//echo "<pre>";
|
|
//print_r($response);
|
|
//echo "</pre>";
|
|
//exit();
|
|
if($response['api_status'] == true){
|
|
$orderUrl = $response['data']['path'];
|
|
$back_order_sn = $response['data']['serial_number'];
|
|
//创建订单
|
|
$order_record = array();
|
|
$order_record['pay_channel_id'] = 6;
|
|
$order_record['pay_channel_name'] = 'hengfuPay';
|
|
$order_record['user_id'] = $user_id;
|
|
$order_record['order_sn'] = $VendorSerialNumber;
|
|
$order_record['appid'] = $sn;
|
|
$order_record['api_key'] = $Authorization;
|
|
$order_record['money'] = $TransactionAmount;
|
|
$order_record['create_time'] = time();
|
|
$order_record['back_order_sn'] = $back_order_sn ;
|
|
$order_record['client'] = $client;
|
|
$order_id = Db::name('order_record')->insertGetId($order_record);
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 6;
|
|
$order_log['pay_channel_name'] = 'hengfuPay';
|
|
$order_log['money'] = $TransactionAmount;
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = 'user_id:'.$user_id.',用户名:'.$username.',发起充值:'.$TransactionAmount.',充值时间:'.date('Y-m-d H:i:s',time());
|
|
$order_log['order_id'] = $order_id;
|
|
$order_log['client'] = $client;
|
|
Db::name('order_log')->insert($order_log);
|
|
return json(array('status' => 1, 'message' => '发起支付成功,即将跳转...', 'url' => $orderUrl));
|
|
}else{
|
|
return json(array('status' => 0, 'message' => $response['code']));
|
|
}
|
|
}else{
|
|
return json(array('status' => 0, 'message' => '支付发起失败,请稍后再试'));
|
|
}
|
|
}
|
|
public function hengfu_call_back(){
|
|
if(Request::instance()->isPost()){
|
|
$serializeData = Request::instance()->post('data');
|
|
$post = json_decode(Request::instance()->post('data'), true);
|
|
if(isset($post['Status']) && intval($post['Status']) == 3){
|
|
$order = Db::name('order_record')->where(['order_sn' => $post['VendorSerialNumber']])->find();
|
|
if(!$order){
|
|
//订单不存在,回调失败
|
|
$order_log['pay_channel_id'] = 6;
|
|
$order_log['pay_channel_name'] = 'hengfuPay';
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "收到回调,但是查询不到订单号,回调内容:".$serializeData;
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('OK');
|
|
}
|
|
$user = Db::name('user')->where(array('id' => $order['user_id'], 'status' => 1, 'is_delete' => 0, 'agent' => 0, 'username' => $post['SourceName']))->find();
|
|
if(!$user){
|
|
//用户不存在,回调失败
|
|
$update['status'] = 1;
|
|
$update['remake'] = '收到回调,无法查询到会员';
|
|
Db::name('order_record')->where('id',$order['id'])->update($update);
|
|
$order_log['pay_channel_id'] = 6;
|
|
$order_log['pay_channel_name'] = 'hengfuPay';
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "收到回调,但是查询不到会员信息,会员可能已经删除或者停用,回调内容:".$serializeData;
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('OK');
|
|
}
|
|
//验证签名
|
|
$Md5key = "WYPJVZInmafDxRFCEc6Nn9Ss8FRG0U2FHaZ4sT8buxx0LCncIz9nM12dokbiJjWr";
|
|
$postData = $post;
|
|
unset($postData['Sign']);
|
|
ksort($postData);
|
|
$md5str = "";
|
|
foreach ($postData as $key => $val) {
|
|
$md5str = $md5str . $key . "=" . $val . "&";
|
|
}
|
|
$sign = md5($md5str . "Key=" . $Md5key);
|
|
if ($sign != $post['Sign']) {
|
|
//签名不对,回调失败
|
|
$update['status'] = 1;
|
|
$update['remake'] = '签名不对,交易暂停';
|
|
Db::name('order_record')->where('id',$order['id'])->update($update);
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 6;
|
|
$order_log['pay_channel_name'] = 'hengfuPay';
|
|
$order_log['money'] = $order['money'];
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "收到回调,但是签名不对,回调内容:".$serializeData;
|
|
$order_log['order_id'] = $order['id'];
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('OK');
|
|
}
|
|
//返回金额
|
|
if(round($post['TransactionAmount'],2) != $order['money']){
|
|
//金额不对,回调失败
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 6;
|
|
$order_log['pay_channel_name'] = 'hengfuPay';
|
|
$order_log['money'] = $order['money'];
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "收到回调,但是金额不对,回调内容:".$serializeData;
|
|
$order_log['order_id'] = $order['id'];
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('success');
|
|
}
|
|
|
|
//总代理金额
|
|
$zdlIdArr = explode(',',$user['agent_parent_id_path']);
|
|
$zdlId = $zdlIdArr[0];
|
|
$zdl = Db::name('user')->where(array('id' => $zdlId, 'status' => 1, 'is_delete' => 0, 'agent' => 1))->find();
|
|
if(!$zdl || $zdl['money'] < $order['money']){
|
|
//找不到总代理,回调失败
|
|
$update['status'] = 1;
|
|
$update['remake'] = '无法找到总代理,或者总代理账号余分不足,交易暂停';
|
|
Db::name('order_record')->where('id',$order['id'])->update($update);
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 6;
|
|
$order_log['pay_channel_name'] = 'hengfuPay';
|
|
$order_log['money'] = $order['money'];
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "无法找到总代理,或者总代理账号余分不足,回调内容:".$serializeData;
|
|
$order_log['order_id'] = $order['id'];
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('success');
|
|
}
|
|
//更新订单表
|
|
$call_back_data['back_money'] = $post['TransactionAmount'];
|
|
$call_back_data['back_sign'] = $post['Sign'];
|
|
$call_back_data['back_time'] = time();
|
|
|
|
$call_back_data['status'] = 3;
|
|
$call_back_data['agent_parent_id'] = $user['agent_parent_id'];
|
|
$call_back_data['agent_parent_username'] = $user['agent_parent_username'];
|
|
$call_back_data['zdl_id'] = $zdl['id'];
|
|
$call_back_data['zdl_username'] = $zdl['username'];
|
|
$call_back_data['zdl_money_before'] = $zdl['money'];
|
|
$call_back_data['zdl_money_after'] = $zdl['money'] - $order['money'];
|
|
$call_back_data['money_before'] = $user['money'];
|
|
$call_back_data['money_after'] = $user['money'] + $order['money'];
|
|
Db::name('order_record')->where(array('id' => $order['id']))->update($call_back_data);
|
|
|
|
//记录支付日志
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 6;
|
|
$order_log['pay_channel_name'] = 'hengfuPay';
|
|
$order_log['money'] = $order['money'];
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "回调支付成功";
|
|
$order_log['order_id'] = $order['id'];
|
|
Db::name('order_log')->insert($order_log);
|
|
//插入上分表并增加余分
|
|
$recharge = array();
|
|
$recharge['type'] = 4;
|
|
$recharge['amount'] = $order['money'];
|
|
$recharge['mode'] = 1;
|
|
$recharge['agent_or_admin'] = 3;
|
|
$recharge['controller_id'] = $zdl['id'];
|
|
$recharge['controller_username'] = $zdl['username'];
|
|
$recharge['controller_nickname'] = $zdl['nickname'];
|
|
$recharge['controller_type'] = '第三方充值平台充值,扣取总代理余分';
|
|
$recharge['controller_old_money'] = $zdl['money'];
|
|
$recharge['controller_new_money'] = $zdl['money'] - $order['money'];
|
|
$recharge['user_id'] = $order['user_id'];
|
|
$recharge['user_type'] = $user['agent'];
|
|
$recharge['user_agent_level'] = 0;
|
|
$recharge['username_for'] = $user['username'];
|
|
$recharge['nickname_for'] = $user['nickname'];
|
|
$recharge['user_parent_id'] = $user['agent_parent_id'];
|
|
$recharge['create_time'] = time();
|
|
$recharge['old_money'] = $user['money'];
|
|
$recharge['new_money'] = $user['money'] + $order['money'];
|
|
$recharge['controller_system'] = 3;
|
|
$recharge['remake'] = '第三方充值成功';
|
|
Db::name('recharge')->insert($recharge);
|
|
$updateZdl = array();
|
|
$updateZdl['money'] = $zdl['money'] - $order['money'];
|
|
$updateZdl['last_recharge_out'] = $order['money'];
|
|
$updateZdl['last_recharge_out_time'] = time();
|
|
$updateZdl['recharge_out_count'] = $zdl['recharge_out_count'] + 1;
|
|
$updateZdl['recharge_out_total_amount'] = $zdl['recharge_out_total_amount'] + $order['money'];
|
|
$updateUser = array();
|
|
$updateUser['money'] = $user['money'] + $order['money'];
|
|
$updateUser['last_recharge'] = $order['money'];
|
|
$updateUser['last_recharge_time'] = time();
|
|
$updateUser['recharge_count'] = $user['recharge_count'] + 1;
|
|
$updateUser['recharge_total_amount'] = $user['recharge_total_amount'] + $order['money'];
|
|
Db::name('user')->where(array('id' => $zdl['id']))->limit(1)->update($updateZdl);
|
|
Db::name('user')->where(array('id' => $order['user_id']))->limit(1)->update($updateUser);
|
|
exit('OK');
|
|
}else{
|
|
Log::record(Request::instance()->post(),'error_pay_callback');
|
|
exit('OK');
|
|
}
|
|
}
|
|
}
|
|
} |