You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
7.5 KiB
192 lines
7.5 KiB
<?php
|
|
namespace app\home\service;
|
|
|
|
use app\model\RechargeApplyModel;
|
|
use app\model\UserWithdrawalModel;
|
|
use think\facade\Log;
|
|
|
|
|
|
/**
|
|
* 处理团队佣金业务
|
|
*/
|
|
class XdPayService extends BaseHomeService
|
|
{
|
|
|
|
public $key="b9df2f9576f648a98f2d4116ee4d022f";
|
|
public $merchant="1l087536";
|
|
public $payCode='12301';
|
|
public $payAppCode='13702';
|
|
public $pay_url="https://apis.xdpay168.com/client/collect/create";//代收
|
|
public $app_url="https://apis.xdpay168.com/client/pay/create";//代付
|
|
public $bla_url="https://apis.xdpay168.com/client/order/balance";//查余额
|
|
|
|
public function create_order($orderId,$amount)
|
|
{
|
|
$notify_url=env('PAY.NOTIFY_URL');
|
|
$post_data['merchant']=$this->merchant;
|
|
$post_data['payCode']=$this->payCode;
|
|
$post_data['amount']=$amount;
|
|
$post_data['orderId']=$orderId;
|
|
$post_data['notifyUrl']=$notify_url.url('xdpay_notify');
|
|
$sign=$this->generateQueryString($post_data,$this->key);
|
|
$post_data['sign']=$sign;
|
|
$header = array("Content-Type:application/x-www-form-urlencoded");
|
|
$res=$this->curlPost($this->pay_url,$post_data,10,$header);
|
|
$result=json_decode($res,true);
|
|
return $result;
|
|
}
|
|
public function xdpayNotify($data){
|
|
Log::info('收到xdpay异步回调:'.json_encode($data));
|
|
$data['reverse']=$data['reverse'] ? 'true':'false';
|
|
$sign=$this->generateQueryString($data,$this->key);
|
|
if($sign==strtolower($data['sign'])){
|
|
if($data['status']==1){
|
|
$order_info=RechargeApplyModel::getOrderByNo([
|
|
'order_no'=>$data['orderId']
|
|
]);
|
|
if($order_info && $data['amount']==$order_info['total_amount']){
|
|
if($order_info['status']==0){
|
|
return (new PayService())->dealPayNotify($order_info);
|
|
}
|
|
}else{
|
|
Log::info('xdpay支付订单不存在:'.json_encode($data));
|
|
}
|
|
}else{
|
|
Log::info('xdpay支付订单支付失败:'.json_encode($data));
|
|
}
|
|
}else{
|
|
Log::info('xdpay签名校验失败:'.json_encode($data));
|
|
}
|
|
}
|
|
public function apply_pay($orderId,$amount,$ifsc,$bank_account,$nike_name)
|
|
{
|
|
$notify_url=env('PAY.NOTIFY_URL');
|
|
$post_data['merchant']=$this->merchant;
|
|
$post_data['payCode']=$this->payAppCode;
|
|
$post_data['amount']=$amount;
|
|
$post_data['orderId']=$orderId;
|
|
$post_data['notifyUrl']=$notify_url.'/bs/xdpay_anotify';
|
|
$post_data['bankAccount']=$bank_account;
|
|
$post_data['customName']=$nike_name;
|
|
$post_data['remark']=$ifsc;
|
|
$sign=$this->generateQueryString($post_data,$this->key);
|
|
$post_data['sign']=$sign;
|
|
$header = array("Content-Type:application/x-www-form-urlencoded");
|
|
$res=$this->curlPost($this->app_url,$post_data,10,$header);
|
|
$result=json_decode($res,true);
|
|
if($result['code']==200){
|
|
return [
|
|
'code'=>200,
|
|
'msg'=>'ok',
|
|
'order_idx'=>$result['data']['platOrderId'],
|
|
'content'=>$res
|
|
];
|
|
}else{
|
|
return [
|
|
'code'=>300,
|
|
'msg'=>$res,
|
|
'order_idx'=>'',
|
|
'content'=>$res
|
|
];
|
|
}
|
|
}
|
|
public function xdpayApplyNotify($data){
|
|
Log::info('收到xdpay代付异步回调:'.json_encode($data));
|
|
$data['reverse']=$data['reverse'] ? 'true':'false';
|
|
$sign=$this->generateQueryString($data,$this->key);
|
|
if($sign==strtolower($data['sign'])){
|
|
if($data['status']==1){
|
|
$order_info=UserWithdrawalModel::getUserDrawalInfo([
|
|
'order_no'=>$data['orderId']
|
|
]);
|
|
if($order_info && $order_info['status']==3){
|
|
UserWithdrawalModel::where([
|
|
'id'=>$order_info['id']
|
|
])->update([
|
|
'status'=>4,
|
|
'deal_time'=>date('Y-m-d H:i:s')
|
|
]);
|
|
Log::info('xdpay代付支付成功:'.json_encode($data));
|
|
}else{
|
|
Log::info('xdpay代付订单不存在:'.json_encode($data));
|
|
}
|
|
}else{
|
|
UserWithdrawalModel::where([
|
|
'order_no'=>$data['orderId']
|
|
])->update([
|
|
'status'=>1,
|
|
]);
|
|
Log::info('xdpay代付失败:'.json_encode($data));
|
|
}
|
|
}else{
|
|
Log::info('xdpay代付签名校验失败:'.json_encode($data));
|
|
}
|
|
}
|
|
public function getBalance()
|
|
{
|
|
$post_data['merchant']=$this->merchant;
|
|
$sign=$this->generateQueryString($post_data,$this->key);
|
|
$post_data['sign']=$sign;
|
|
$header = array("Content-Type:application/x-www-form-urlencoded");
|
|
$res=$this->curlPost($this->bla_url,$post_data,10,$header);
|
|
$result=json_decode($res,true);
|
|
if($result['code']==200){
|
|
return [
|
|
'amount'=>$result['data']['balanceUsable']
|
|
];
|
|
}else{
|
|
return [
|
|
'amount'=>0
|
|
];
|
|
}
|
|
}
|
|
private function curlPost($url, $post_data = array(), $timeout = 5, $header = "", $data_type = "") {
|
|
$header = empty($header) ? '' : $header;
|
|
//支持json数据数据提交
|
|
if($data_type == 'json'){
|
|
$post_string = json_encode($post_data);
|
|
}elseif($data_type == 'array') {
|
|
$post_string = $post_data;
|
|
}elseif(is_array($post_data)){
|
|
$post_string = http_build_query($post_data, '', '&');
|
|
}
|
|
|
|
$ch = curl_init(); // 启动一个CURL会话
|
|
curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 // https请求 不验证证书和hosts
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
|
|
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
|
|
//curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
|
|
curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的数据包
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
//curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
private function generateQueryString($fields, $privateKey) {
|
|
// 按ASCII顺序对字段名进行自然排序
|
|
ksort($fields);
|
|
|
|
// 将字段按 k=v 格式拼接成字符串
|
|
$query = '';
|
|
foreach($fields as $key => $value) {
|
|
if($key!='sign'){
|
|
$query .= $key . '=' . strval($value) . '&';
|
|
}
|
|
}
|
|
// 去除末尾的 '&'
|
|
$query = rtrim($query, '&');
|
|
// 在字符串末尾拼接私钥
|
|
$query .= '&key=' . $privateKey;
|
|
$sign=md5($query);
|
|
return $sign;
|
|
}
|
|
|
|
|
|
}
|