2020.02新版
This commit is contained in:
50
plugins/qqpay/inc/qpayMch.config.php
Normal file
50
plugins/qqpay/inc/qpayMch.config.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* qpayMch.config.php
|
||||
* Created by HelloWorld
|
||||
* vers: v1.0.0
|
||||
* User: Tencent.com
|
||||
*/
|
||||
|
||||
define("QQ_MCH_ID", $channel['appid']);
|
||||
define("QQ_MCH_KEY", $channel['appkey']);
|
||||
define("QQ_OP_USERID", $conf['qq_op_userid']);
|
||||
define("QQ_OP_USERPWD", $conf['qq_op_userpwd']);
|
||||
|
||||
class QpayMchConf
|
||||
{
|
||||
/**
|
||||
* QQ钱包商户号
|
||||
*/
|
||||
const MCH_ID = QQ_MCH_ID;
|
||||
|
||||
/**
|
||||
* API密钥。
|
||||
* QQ钱包商户平台(http://qpay.qq.com/)获取
|
||||
*/
|
||||
const MCH_KEY = QQ_MCH_KEY;
|
||||
|
||||
/**
|
||||
* QQ钱包公众号APPID
|
||||
*/
|
||||
const MCH_APPID = '';
|
||||
|
||||
/**
|
||||
* API证书绝对路径
|
||||
* QQ钱包接口中,涉及资金回滚的接口会使用到商户证书,包括退款、撤销接口。商家在申请QQ钱包支付成功后,收到的相应邮件后,可以按照指引下载API证书,也可以按照以下路径下载:QQ钱包商户平台(https://qpay.qq.com/)-->账户管理-->API安全 。
|
||||
*/
|
||||
const SSLCERT_PATH = PAY_ROOT.'cert/apiclient_cert.pem';
|
||||
const SSLKEY_PATH = PAY_ROOT.'cert/apiclient_key.pem';
|
||||
|
||||
/**
|
||||
* 企业付款-操作员ID
|
||||
*/
|
||||
const OP_USERID = QQ_OP_USERID;
|
||||
|
||||
/**
|
||||
* 企业付款-操作员密码
|
||||
*/
|
||||
const OP_USERPWD = QQ_OP_USERPWD;
|
||||
|
||||
}
|
||||
46
plugins/qqpay/inc/qpayMchAPI.class.php
Normal file
46
plugins/qqpay/inc/qpayMchAPI.class.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* qpayMachAPI.php 业务调用方可做二次封装
|
||||
* Created by HelloWorld
|
||||
* vers: v1.0.0
|
||||
* User: Tencent.com
|
||||
*/
|
||||
require_once (PAY_ROOT.'inc/qpayMchUtil.class.php');
|
||||
class QpayMchAPI{
|
||||
protected $url;
|
||||
protected $isSSL;
|
||||
protected $timeout;
|
||||
|
||||
/**
|
||||
* QpayMchAPI constructor.
|
||||
*
|
||||
* @param $url 接口url
|
||||
* @param $isSSL 是否使用证书发送请求
|
||||
* @param int $timeout 超时
|
||||
*/
|
||||
public function __construct($url, $isSSL, $timeout = 5){
|
||||
$this->url = $url;
|
||||
$this->isSSL = $isSSL;
|
||||
$this->timeout = $timeout;
|
||||
}
|
||||
|
||||
public function reqQpay($params){
|
||||
$ret = array();
|
||||
//商户号
|
||||
$params["mch_id"] = QpayMchConf::MCH_ID;
|
||||
//随机字符串
|
||||
$params["nonce_str"] = QpayMchUtil::createNoncestr();
|
||||
//签名
|
||||
$params["sign"] = QpayMchUtil::getSign($params);
|
||||
//生成xml
|
||||
$xml = QpayMchUtil::arrayToXml($params);
|
||||
|
||||
if(isset($this->isSSL)){
|
||||
$ret = QpayMchUtil::reqByCurlSSLPost($xml, $this->url, $this->timeout);
|
||||
}else{
|
||||
$ret = QpayMchUtil::reqByCurlNormalPost($xml, $this->url, $this->timeout);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
}
|
||||
149
plugins/qqpay/inc/qpayMchUtil.class.php
Normal file
149
plugins/qqpay/inc/qpayMchUtil.class.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* qpayMachAPI.php
|
||||
* Created by HelloWorld
|
||||
* vers: v1.0.0
|
||||
* User: Tencent.com
|
||||
*/
|
||||
require_once (PAY_ROOT.'inc/qpayMch.config.php');
|
||||
class QpayMchUtil {
|
||||
|
||||
/**
|
||||
* 生成随机串
|
||||
* @param int $length
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function createNoncestr( $length = 32 ) {
|
||||
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
$str ="";
|
||||
for ( $i = 0; $i < $length; $i++ ){
|
||||
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将参数转为hash形式
|
||||
* @param $params
|
||||
* @param $urlencode
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function buildQueryStr($params) {
|
||||
$arrTmp = array();
|
||||
foreach ($params as $k => $v){
|
||||
//参数为空不参与签名
|
||||
if(isset($v) && $v != '' && $k != 'sign'){
|
||||
array_push($arrTmp, "$k=$v");
|
||||
}
|
||||
}
|
||||
return implode('&', $arrTmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参数签名
|
||||
* @param $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSign($params) {
|
||||
//第一步:对参数按照key=value的格式,并按照参数名ASCII字典序排序
|
||||
ksort($params);
|
||||
$stringA = QpayMchUtil::buildQueryStr($params);
|
||||
//第二步:拼接API密钥并md5
|
||||
$stringA = $stringA."&key=".QpayMchConf::MCH_KEY;
|
||||
$stringA = md5($stringA);
|
||||
//转成大写
|
||||
$sign = strtoupper($stringA);
|
||||
return $sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数组转换成xml字符串
|
||||
* @param $arr
|
||||
* @return string
|
||||
*/
|
||||
public static function arrayToXml($arr) {
|
||||
$xml = "<xml>";
|
||||
foreach ($arr as $key => $val){
|
||||
if (is_numeric($val)){
|
||||
$xml.="<$key>$val</$key>";
|
||||
}
|
||||
else
|
||||
$xml.="<$key><![CDATA[$val]]></$key>";
|
||||
}
|
||||
$xml.="</xml>";
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* xml转换成数组
|
||||
* @param $xml
|
||||
* @return array|mixed|object
|
||||
*/
|
||||
public static function xmlToArray($xml) {
|
||||
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||||
return $arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用curl 请求接口。post方式
|
||||
* @param $params
|
||||
* @param $url
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public static function reqByCurlNormalPost($params, $url, $timeout = 10) {
|
||||
return QpayMchUtil::_reqByCurl($params, $url, $timeout, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用ssl证书请求接口。post方式
|
||||
* @param $params
|
||||
* @param $url
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public static function reqByCurlSSLPost($params, $url, $timeout = 10) {
|
||||
return QpayMchUtil::_reqByCurl($params, $url, $timeout, true);
|
||||
}
|
||||
|
||||
private static function _reqByCurl($params, $url, $timeout = 10, $needSSL = false) {
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch,CURLOPT_URL, $url);
|
||||
curl_setopt($ch,CURLOPT_TIMEOUT, $timeout);
|
||||
|
||||
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
|
||||
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
|
||||
|
||||
curl_setopt($ch,CURLOPT_HEADER,FALSE);
|
||||
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
|
||||
//是否使用ssl证书
|
||||
if(isset($needSSL) && $needSSL != false){
|
||||
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
|
||||
curl_setopt($ch,CURLOPT_SSLCERT, QpayMchConf::SSLCERT_PATH);
|
||||
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
|
||||
curl_setopt($ch,CURLOPT_SSLKEY, QpayMchConf::SSLKEY_PATH);
|
||||
}
|
||||
curl_setopt($ch,CURLOPT_POST, true);
|
||||
curl_setopt($ch,CURLOPT_POSTFIELDS, $params);
|
||||
$ret = curl_exec($ch);
|
||||
if($ret){
|
||||
curl_close($ch);
|
||||
//log($ret); //业务记录交互流水。注:流水日志影响性能,如请求量过大,请慎重设计日志。
|
||||
return $ret;
|
||||
}
|
||||
else {
|
||||
$error = curl_errno($ch);
|
||||
//log($error); //业务记录错误日志
|
||||
print_r($error);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
26
plugins/qqpay/inc/qpayNotify.class.php
Normal file
26
plugins/qqpay/inc/qpayNotify.class.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
/**
|
||||
* QpayNotify.php 业务调用方可做二次封装
|
||||
* Created by HelloWorld
|
||||
* vers: v1.0.0
|
||||
* User: Tencent.com
|
||||
*/
|
||||
require_once (PAY_ROOT.'inc/qpayMchUtil.class.php');
|
||||
class QpayNotify{
|
||||
private $params;
|
||||
private $sign;
|
||||
|
||||
function getParams() {
|
||||
$post_data = file_get_contents("php://input");
|
||||
$params = QpayMchUtil::xmlToArray($post_data);
|
||||
$this->params = $params;
|
||||
$this->sign = $params['sign'];
|
||||
return $params;
|
||||
}
|
||||
|
||||
function verifySign() {
|
||||
$sign = QpayMchUtil::getSign($this->params);
|
||||
return $sign == $this->sign;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user