接上两篇支付宝支付和微信支付,下面我这边讲的是QQ支付:
下面简单介绍下QQApp支付后端的实现(这边是用php实现的):
一、 一些琐碎的申请和设置的方式就不说明了,这些微信上都有说,但相比于支付宝吗,微信和QQ的文档相比差之千里,太简单太low了,并且没有SDKDEMO,但小伙伴们不用担心,下面是我这边封装好的一个类,可供直接使用,都有标注说明
一、封装好的QQ调用类库:(这个不管是app支付还是其他支付都可共用)
<?php
class qqAppPay
{
const API_URL_PREFIX = 'https://qpay.qq.com';
const UNIFIEDORDER_URL = "/cgi-bin/pay/qpay_unified_order.cgi";
const ORDERQUERY_URL = "/cgi-bin/pay/qpay_order_query.cgi";
const CLOSEORDER_URL = "/cgi-bin/pay/qpay_close_order.cgi";
private $appid;
private $mch_id ='1452072601';
private $nonce_str;
private $sign;
private $body;
private $out_trade_no;
private $total_fee;
private $spbill_create_ip;
private $notify_url ='http://apps.ifeimo.com/Sysj218/Recharge/qqpayNotify';
private $trade_type;
private $key='F1DAB8538EC979F99C062F3BE308062C';
public $postCharset = "UTF-8";
public $signType = "HMAC-SHA1";
private $SSLCERT_PATH;
private $SSLKEY_PATH;
private $params = array();
public function __construct($wxappid)
{
$this->appid = $wxappid;
}
public function unifiedOrder( $params ){
$this->spbill_create_ip = $_SERVER['REMOTE_ADDR'];
$this->params['appid'] = $this->appid;
$this->params['mch_id'] = $this->mch_id;
$this->params['nonce_str'] = $this->nonce_str = $this->genRandomString();
$this->params['body'] = $this->body = $params['body'];
$this->params['out_trade_no'] = $this->out_trade_no = $params['out_trade_no'];
$this->params['total_fee'] = $this->total_fee = $params['total_fee'];
$this->params['spbill_create_ip'] = $this->spbill_create_ip;
$this->params['notify_url'] = $params['notify_url'] ? $params['notify_url'] : $this->notify_url;
$this->params['trade_type'] = $this->trade_type = $params['trade_type'];
$this->sign = $this->MakeSign( $this->params );
$this->params['sign'] = $this->sign;
$xml = $this->data_to_xml($this->params);
$response = $this->postXmlCurl($xml, self::API_URL_PREFIX.self::UNIFIEDORDER_URL);
if( !$response ){
return false;
}
$result = $this->xml_to_data( $response );
if( !empty($result['result_code']) && !empty($result['err_code']) ){
$result['err_msg'] = $this->error_code( $result['err_code'] );
}
return $result;
}
public function orderQuery( $out_trade_no ){
$this->params['appid'] = $this->appid;
$this->params['mch_id'] = $this->mch_id;
$this->params['nonce_str'] = $this->genRandomString();
$this->params['out_trade_no'] = $out_trade_no;
$this->sign = $this->MakeSign( $this->params );
$this->params['sign'] = $this->sign;
$xml = $this->data_to_xml($this->params);
$response = $this->postXmlCurl($xml, self::API_URL_PREFIX.self::ORDERQUERY_URL);
if( !$response ){
return false;
}
$result = $this->xml_to_data( $response );
if( !empty($result['result_code']) && !empty($result['err_code']) ){
$result['err_msg'] = $this->error_code( $result['err_code'] );
}
return $result;
}
public function closeOrder( $out_trade_no ){
$this->params['appid'] = $this->appid;
$this->params['mch_id'] = $this->mch_id;
$this->params['nonce_str'] = $this->genRandomString();
$this->params['out_trade_no'] = $out_trade_no;
$this->sign = $this->MakeSign( $this->params );
$this->params['sign'] = $this->sign;
$xml = $this->data_to_xml($this->params);
$response = $this->postXmlCurl($xml, self::API_URL_PREFIX.self::CLOSEORDER_URL);
if( !$response ){
return false;
}
$result = $this->xml_to_data( $response );
return $result;
}
public function sdkExecute($request) {
$this->setupCharsets($request);
$params['appid'] = $request['appid'];
$params['bargainorId'] = $request['bargainorId'];
$params['tokenId'] = $request['tokenId'];
$params['nonce'] = $request['nonce'];
$params['timestamp'] = time();
$params['sign'] = $request['sign'];
$params['sigType'] = $this->signType;
$params['completion'] = $this->notify_url;
foreach ($params as &$value) {
$value = $this->characet($value, $params['charset']);
}
return http_build_query($params);
}
protected function checkEmpty($value) {
if (!isset($value))
return true;
if ($value === null)
return true;
if (trim($value) === "")
return true;
return false;
}
function characet($data, $targetCharset) {
if (!empty($data)) {
$fileType = $this->fileCharset;
if (strcasecmp($fileType, $targetCharset) != 0) {
$data = mb_convert_encoding($data, $targetCharset, $fileType);
}
}
return $data;
}
private function setupCharsets($request) {
if ($this->checkEmpty($this->postCharset)) {
$this->postCharset = 'UTF-8';
}
$str = preg_match('/[\x80-\xff]/', $this->appId) ? $this->appId : print_r($request, true);
$this->fileCharset = mb_detect_encoding($str, "UTF-8, GBK") == 'UTF-8' ? 'UTF-8' : 'GBK';
}
public function getNotifyData(){
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
$data = array();
if( empty($xml) ){
return false;
}
$data = $this->xml_to_data( $xml );
if( !empty($data['return_code']) ){
if( $data['return_code'] == 'FAIL' ){
return false;
}
}
return $data;
}
public function replyNotify(){
$data['return_code'] = 'SUCCESS';
$data['return_msg'] = 'OK';
$xml = $this->data_to_xml( $data );
echo $xml;
die();
}
public function getAppPayParams( $prepayid ){
$data['appid'] = $this->appid;
$data['partnerid'] = $this->mch_id;
$data['prepayid'] = $prepayid;
$data['package'] = 'Sign=WXPay';
$data['noncestr'] = $this->genRandomString();
$data['timestamp'] = time();
$data['sign'] = $this->MakeSign( $data );
return $data;
}
public function MakeSign( $params ){
ksort($params);
$string = $this->ToUrlParams($params);
$string = $string . "&key=".$this->key;
$string = md5($string);
$result = strtoupper($string);
return $result;
}
public function ToUrlParams( $params ){
$string = '';
if( !empty($params) ){
$array = array();
foreach( $params as $key => $value ){
$array[] = $key.'='.$value;
}
$string = implode("&",$array);
}
return $string;
}
public function appMakeSign( $params,$appkey ){
ksort($params);
$string = $this->ToUrlParams($params);
$appkey = $appkey.'&';
$result = base64_encode(hash_hmac("sha1", $string, $appkey, true));
return $result;
}
public function data_to_xml( $params ){
if(!is_array($params)|| count($params) <= 0)
{
return false;
}
$xml = "<xml>";
foreach ($params as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
public function xml_to_data($xml){
if(!$xml){
return false;
}
libxml_disable_entity_loader(true);
$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $data;
}
public function validationSignature($params){
ksort($params);
$string = $this->ToUrlParams($params);
return $string;
$string = $string . "&key=".$this->key;
$string = md5($string);
$result = strtoupper($string);
return $result;
}
private static function getMillisecond(){
$time = explode ( " ", microtime () );
$time = $time[1] . ($time[0] * 1000);
$time2 = explode( ".", $time );
$time = $time2[0];
return $time;
}
private function genRandomString($len = 32) {
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars);
$output = "";
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
private function postXmlCurl($xml, $url, $useCert = false, $second = 30){
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if($useCert == true){
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
}
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
return false;
}
}
public function error_code( $code ){
$errList = array(
'LACK_PARAMS' => '缺少必要的请求参数',
'NOAUTH' => '商户未开通此接口权限',
'NOTENOUGH' => '用户帐号余额不足',
'ORDERNOTEXIST' => '订单号不存在',
'ORDERPAID' => '商户订单已支付,无需重复操作',
'ORDERCLOSED' => '当前订单已关闭,无法支付',
'SYSTEMERROR' => '系统错误!系统超时',
'APPID_NOT_EXIST' => '参数中缺少APPID',
'MCHID_NOT_EXIST' => '参数中缺少MCHID',
'APPID_MCHID_NOT_MATCH' => 'appid和mch_id不匹配',
'LACK_PARAMS' => '缺少必要的请求参数',
'OUT_TRADE_NO_USED' => '同一笔交易不能多次提交',
'SIGNERROR' => '参数签名结果不正确',
'XML_FORMAT_ERROR' => 'XML格式错误',
'REQUIRE_POST_METHOD' => '未使用post传递参数 ',
'POST_DATA_EMPTY' => 'post数据不能为空',
'NOT_UTF8' => '未使用指定编码格式',
);
if( array_key_exists( $code , $errList ) ){
return $errList[$code];
}
}
}
二、封装好的微信调用类库的工具类:
namespace Pay\Controller;
use Think\Controller;
class QQAppPayController extends Controller {
private $appidByTarget = [
'app标识' => '1232133213',
'app标识' => '1232133213',
'app标识' => '1232133213',
'app标识' => '1232133213',
'app标识' => '1232133213',
'app标识' => '1232133213',
];
private $appKeyByTarget = [
'app标识' => 'sd24sdfsdfsdfdfs',
'app标识' => 'sd24sdfsdfsdfdfs',
'app标识' => 'sd24sdfsdfsdfdfs',
'app标识' => 'sd24sdfsdfsdfdfs',
'app标识' => 'sd24sdfsdfsdfdfs',
'app标识' => 'sd24sdfsdfsdfdfs',
];
public function _initialize(){
}
public function getCallSDKInfo($order){
if(!$order || !is_array($order) || !$order['id'] || !$order['price'] || !$order['notifyUrl'] || !$order['target']){
return false;
}
$wxappid = $this->appidByTarget[$order['target']];
Vendor('qqPay.qqpay');
$QQPay = new \qqAppPay($wxappid);
$params = [
'body' => $order['description'],
'out_trade_no' => $order['id'],
'total_fee' => ($order['price']*100),
'time_start' => date('yyyyMMddHHmmss',$order['add_time']),
'time_expire' => date('yyyyMMddHHmmss',($order['add_time']+1800)),
'trade_type' => 'APP',
'fee_type' => 'CNY',
'notify_url' => $order['notifyUrl'],
];
$result = $QQPay->unifiedOrder($params);
$sign_array=[
'appId' => $result['appid'],
'bargainorId' => $result['mch_id'],
'tokenId' => $result['prepay_id'],
'nonce' => $result['nonce_str'],
'pubAcc' => ''
];
$sign_array['sign'] = $QQPay->appMakeSign($sign_array, $this->appKeyByTarget[$order['target']]);
$sign_array['timestamp'] = $order['add_time'];
return $sign_array;
}
}
三、接下来你就可以根据你的相关产品订单信息进行调用相应的类方法,下面我就简单的写一下:
class productPayController extents Controller{
public function paymentAction(){
IS_POST ||$this->returnError();
$parameters = I('post.');
$QqPay = new WechatAppPayController();
$order['notifyUrl'] = 'http://xxxxxxxxxxxxxxx;//微信的回调接口
$data = $QqPay->getCallSDKInfo($order);//调用QQ生成预订单并整理好返回验签参数,这里调用上面的类封装的方法
return $data //这里可以根据你们前端定义接受什么数据类型进行转化
}
}
php后端实现支付宝APP支付,请点击这里:GO
php后端实现微信APP支付,请点击这里:GO
注:这个就是我php后端实现支付的业务的相关代码,若有什么问题反馈请指出,谢谢,(这里并附上我自己的博客地址:blog.taobaoroom.cn,若有什么问题可根据博客上赋有的QQ群地址联系我哦!!