vr-shopxo-source/extend/payment/QQ.php

678 lines
24 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://opensource.org/licenses/mit-license.php )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace payment;
use app\service\PayLogService;
/**
* QQ支付
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
*/
class QQ
{
// 插件配置参数
private $config;
/**
* 构造方法
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-17
* @desc description
* @param [array] $params [输入参数(支付配置参数)]
*/
public function __construct($params = [])
{
$this->config = $params;
}
/**
* 配置信息
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
*/
public function Config()
{
// 基础信息
$base = [
'name' => 'QQ支付', // 插件名称
'version' => '1.0.1', // 插件版本
'apply_version' => '不限', // 适用系统版本描述
'apply_terminal'=> ['pc', 'h5', 'qq', 'ios', 'android'], // 适用终端 默认全部 ['pc', 'h5', 'app', 'alipay', 'weixin', 'baidu']
'desc' => '适用PC+H5+APP+QQ小程序即时到帐支付方式买家的交易资金直接打入卖家账户快速回笼交易资金。 <a href="https://qpay.qq.com/" target="_blank">立即申请</a>', // 插件描述支持html
'author' => 'Devil', // 开发者
'author_url' => 'http://shopxo.net/', // 开发者主页
];
// 配置信息
$element = [
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'app_appid',
'placeholder' => '开放平台AppID',
'title' => '开放平台AppID',
'is_required' => 0,
'message' => '请填写QQ开放平台APP支付分配的AppID',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'appid',
'placeholder' => 'QQ小程序ID',
'title' => 'QQ小程序ID',
'is_required' => 0,
'message' => '请填写QQ分配的小程序ID',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'mch_id',
'placeholder' => 'QQ支付商户号',
'title' => 'QQ支付商户号',
'is_required' => 0,
'message' => '请填写QQ支付分配的商户号',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'key',
'placeholder' => '密钥',
'title' => '密钥',
'desc' => 'QQ支付商户平台API配置的密钥',
'is_required' => 0,
'message' => '请填写密钥',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'op_user_id',
'placeholder' => '操作员帐号',
'title' => '操作员帐号(退款操作必填项)',
'desc' => '也可以是商户号操作员工创建参考https://kf.qq.com/faq/170112AZ7Fzm170112VNz6zE.html',
'is_required' => 0,
'message' => '请填写操作员帐号',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'op_user_passwd',
'placeholder' => '操作员密码',
'title' => '操作员密码(退款操作必填项)',
'desc' => '如果操作员帐号填写的是商户号,那这里就是商户号登录密码',
'is_required' => 0,
'message' => '请填写操作员密码',
],
[
'element' => 'textarea',
'name' => 'apiclient_cert',
'placeholder' => '证书(apiclient_cert.pem)',
'title' => '证书(apiclient_cert.pem)(退款操作必填项)',
'is_required' => 0,
'rows' => 6,
'message' => '请填写证书(apiclient_cert.pem)',
],
[
'element' => 'textarea',
'name' => 'apiclient_key',
'placeholder' => '证书密钥(apiclient_key.pem)',
'title' => '证书密钥(apiclient_key.pem)(退款操作必填项)',
'is_required' => 0,
'rows' => 6,
'message' => '请填写证书密钥(apiclient_key.pem)',
],
[
'element' => 'select',
'title' => '异步通知协议',
'message' => '请选择协议类型',
'name' => 'agreement',
'is_multiple' => 0,
'element_data' => [
['value'=>1, 'name'=>'默认当前协议'],
['value'=>2, 'name'=>'强制https转http协议'],
],
],
];
return [
'base' => $base,
'element' => $element,
];
}
/**
* 支付入口
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
* @param [array] $params [输入参数]
*/
public function Pay($params = [])
{
// 参数
if(empty($params))
{
return DataReturn('参数不能为空', -1);
}
// 配置信息
if(empty($this->config))
{
return DataReturn('支付缺少配置', -1);
}
// 获取支付参数
$ret = $this->GetPayParams($params);
if($ret['code'] != 0)
{
return $ret;
}
// 请求接口处理
$result = $this->XmlToArray($this->HttpRequest('https://qpay.qq.com/cgi-bin/pay/qpay_unified_order.cgi', $this->ArrayToXml($ret['data'])));
if(!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['prepay_id']))
{
return $this->PayHandleReturn($ret['data'], $result, $params);
}
$msg = is_string($result) ? $result : (empty($result['retmsg']) ? (empty($result['return_msg']) ? '支付接口异常' : $result['return_msg']) : $result['retmsg']);
if(!empty($result['err_code_des']))
{
$msg .= '-'.$result['err_code_des'];
}
return DataReturn($msg, -1);
}
/**
* 支付返回处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-08
* @desc description
* @param [array] $pay_params [支付参数]
* @param [array] $data [支付返回数据]
* @param [array] $params [输入参数]
*/
private function PayHandleReturn($pay_params = [], $data = [], $params = [])
{
$redirect_url = empty($params['order_id']) ? '' : urlencode(MyUrl('index/order/detail', ['id'=>$params['order_id']]));
$result = DataReturn('支付接口异常', -1);
switch($pay_params['trade_type'])
{
// web支付
case 'NATIVE' :
// 手机模式下直接返回微信的支付url地址打开支付缺点是支付后会直接关闭站点
// QQ支付本身没有提供H5支付方案这种方式也可以直接支付缺点是支付后不能回调到原来浏览器
// 公众号后续再采用公众号的方式支付,体验会更好一些,只是可以不关闭站点
if(APPLICATION == 'web' && IsMobile())
{
$result = DataReturn('success', 0, $data['code_url']);
} else {
if(empty($params['check_url']))
{
return DataReturn('支付状态校验地址不能为空', -50);
}
if(APPLICATION == 'app')
{
$data = [
'pay_url' => $data['code_url'],
'qrcode_url' => $data['code_url'],
'order_no' => $params['order_no'],
'name' => 'QQ支付',
'msg' => '打开QQAPP扫一扫进行支付',
'check_url' => $params['check_url'],
];
} else {
$pay_params = [
'type' => 'qq',
'url' => $data['code_url'],
'order_no' => $params['order_no'],
'name' => 'QQ支付',
'msg' => '打开QQAPP扫一扫进行支付',
'check_url' => $params['check_url'],
];
MySession('payment_qrcode_data', $pay_params);
$data = MyUrl('index/pay/qrcode');
}
$result = DataReturn('success', 0, $data);
}
break;
// QQ中公众号支付
case 'JSAPI' :
$result = DataReturn('公众号暂不支持', -1);
break;
// QQ小程序支付
case 'MINIAPP' :
$result = DataReturn('success', 0, 'prepay_id='.$data['prepay_id']);
break;
// APP支付
case 'APP' :
$pay_data = array(
'appid' => $this->pay_params['app_appid'],
'partnerid' => $this->pay_params['mch_id'],
'prepayid' => $data['prepay_id'],
'package' => 'Sign=WXPay',
'noncestr' => md5(time().rand()),
'timestamp' => (string) time(),
);
$pay_data['sign'] = $this->GetSign($pay_data);
$result = DataReturn('success', 0, $pay_data);
break;
}
return $result;
}
/**
* 获取支付参数
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-07
* @desc description
* @param [array] $params [输入参数]
*/
private function GetPayParams($params = [])
{
// 平台
$client_type = APPLICATION_CLIENT_TYPE;
if($client_type == 'pc' && IsMobile())
{
$client_type = 'h5';
}
// 支付类型
$trade_type = empty($params['trade_type']) ? $this->GetTradeType($client_type) : $params['trade_type'];
if(empty($trade_type))
{
return DataReturn('支付类型不匹配', -1);
}
// 异步地址处理
$notify_url = (__MY_HTTP__ == 'https' && isset($this->config['agreement']) && $this->config['agreement'] == 1) ? 'http'.mb_substr($params['notify_url'], 5, null, 'utf-8') : $params['notify_url'];
// appid
$appid = $this->PayAppID($client_type);
// 请求参数
$parameter = [
'appid' => $appid,
'mch_id' => $this->config['mch_id'],
'nonce_str' => md5(time().$params['order_no']),
'body' => $params['site_name'].'-'.$params['name'],
'notify_url' => $notify_url,
'out_trade_no' => $params['order_no'],
'fee_type' => 'CNY',
'total_fee' => (int) (($params['total_price']*1000)/10),
'spbill_create_ip' => GetClientIP(),
'trade_type' => $trade_type,
'attach' => empty($params['attach']) ? $params['site_name'].'-'.$params['name'] : $params['attach'],
];
$parameter['sign'] = $this->GetSign($parameter);
// 支付请求记录
PayLogService::PayLogRequestRecord($params['order_no'], ['request_params'=>$parameter]);
return DataReturn('success', 0, $parameter);
}
/**
* appid获取
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-25
* @desc description
* @param [string] $client_type [客户端类型]
*/
public function PayAppID($client_type)
{
$arr = [
'app' => $this->config['app_appid'],
'ios' => $this->config['app_appid'],
'android' => $this->config['app_appid'],
];
return array_key_exists($client_type, $arr) ? $arr[$client_type] : $this->config['appid'];
}
/**
* 获取支付交易类型
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-08
* @param [string] $client_type [客户端类型]
* @desc description
*/
private function GetTradeType($client_type)
{
// 平台类型定义
$type_all = [
'pc' => 'NATIVE',
'h5' => 'NATIVE',
'qq' => 'MINIAPP',
'app' => 'APP',
'ios' => 'APP',
'android' => 'APP',
];
return isset($type_all[$client_type]) ? $type_all[$client_type] : '';
}
/**
* 支付回调处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
* @param [array] $params [输入参数]
*/
public function Respond($params = [])
{
$result = empty($GLOBALS['HTTP_RAW_POST_DATA']) ? $this->XmlToArray(file_get_contents('php://input')) : $this->XmlToArray($GLOBALS['HTTP_RAW_POST_DATA']);
if(isset($result['trade_state']) && $result['trade_state'] == 'SUCCESS' && $result['sign'] == $this->GetSign($result))