根據微信進階紅包介面,開發PHP版本的API介面,現在進行主要程式碼分析
紅包介面調用請求代碼,所有請求參數為必填參數與文檔對應:
class Wxapi {
private $app_id = 'wxXXXXXXXXXXXX'; //公眾帳號appid,首先申請與之配套的公眾帳號
private $app_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX';//公眾號secret,使用者擷取使用者授權token
private $app_mchid = 'XXXXXXXX';//商戶號id
function __construct(){
//do sth here....
}
/**
* 微信支付
* @param string $openid 使用者openid
*/
public function pay($re_openid)
{
include_once('WxHongBaoHelper.php');
$commonUtil = new CommonUtil();
$wxHongBaoHelper = new WxHongBaoHelper();
$wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());//隨機字串,丌長於 32 位
$wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date('YmdHis').rand(1000, 9999));//訂單號
$wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);//商戶號
$wxHongBaoHelper->setParameter("wxappid", $this->app_id);
$wxHongBaoHelper->setParameter("nick_name", '紅包');//提供方名稱
$wxHongBaoHelper->setParameter("send_name", '紅包');//紅包寄件者名稱
$wxHongBaoHelper->setParameter("re_openid", $re_openid);//相對於醫脈互連的openid
$wxHongBaoHelper->setParameter("total_amount", 100);//付款金額,單位分
$wxHongBaoHelper->setParameter("min_value", 100);//最小紅包金額,單位分
$wxHongBaoHelper->setParameter("max_value", 100);//最大紅包金額,單位分
$wxHongBaoHelper->setParameter("total_num", 1);//紅包?Х拋莧聳?br /> $wxHongBaoHelper->setParameter("wishing", '感謝您參與紅包派發活動,祝您新年快樂!');//紅包祝福詫
$wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//調用介面的機器 Ip 地址
$wxHongBaoHelper->setParameter("act_name", '紅包活動');//活勱名稱
$wxHongBaoHelper->setParameter("remark", '快來搶!');//備忘資訊
$postXml = $wxHongBaoHelper->create_hongbao_xml();
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
$responseXml = $wxHongBaoHelper->curl_post_ssl($url, $postXml);
//用作結果調試輸出
//echo htmlentities($responseXml,ENT_COMPAT,'UTF-8');
$responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
return $responseObj->return_code;
}
擷取隨機字串方法:
/**
* 產生隨機數
*/
public function great_rand(){
$str = '1234567890abcdefghijklmnopqrstuvwxyz';
for($i=0;$i<30;$i++){
$j=rand(0,35);
$t1 .= $str[$j];
}
return $t1;
}
簽名演算法:
/**
例如:
appid: wxd111665abv58f4f
mch_id: 10000100
device_info: 1000
Body: test
nonce_str: ibuaiVcKdpRxkhJA
第一步:對參數按照 key=value 的格式,並按照參數名 ASCII 字典序排序如下:
stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i
d=10000100&nonce_str=ibuaiVcKdpRxkhJA";
第二步:拼接支付密鑰:
stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A
9CF3B7"
*/
protected function get_sign(){
define('PARTNERKEY',"QSRXXXXXXXXXXXXXXXXXXXXX");
try {
if (null == PARTNERKEY || "" == PARTNERKEY ) {
throw new SDKRuntimeException("密鑰不可為空!" . "<br>");
}
if($this->check_sign_parameters() == false) { //檢查產生簽名參數
throw new SDKRuntimeException("產生簽名參數缺失!" . "<br>");
}
$commonUtil = new CommonUtil();
ksort($this->parameters);
$unSignParaString = $commonUtil->formatQueryParaMap($this->parameters, false);
$md5SignUtil = new MD5SignUtil();
return $md5SignUtil->sign($unSignParaString,$commonUtil->trimString(PARTNERKEY));
}catch (SDKRuntimeException $e)
{
die($e->errorMessage());
}
}
CURL請求以及發送認證:
function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
{
$ch = curl_init();
//逾時時間
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//這裡設定代理,如果有的話
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//cert 與 key 分別屬於兩個.pem檔案
//請確保您的libcurl版本是否支援雙向認證,版本高於7.20.1
curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_key.pem');
curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'rootca.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
//echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
入口檔案:
@require "pay.php";
//擷取使用者資訊
$get = $_GET['param'];
$code = $_GET['code'];
//判斷code是否存在
if($get=='access_token' && !empty($code)){
$param['param'] = 'access_token';
$param['code'] = $code;
$packet = new Packet();
//擷取使用者openid資訊
$userinfo = $packet->_route('userinfo',$param);
if(empty($userinfo['openid'])){
exit("NOAUTH");
}
//調取支付方法
$packet->_route('wxpacket',array('openid'=>$userinfo['openid']));
}else{
$packet->_route('userinfo');
}
開發所需文檔:
1)官方紅包進階介面文檔
2)官方公眾平台擷取使用者基本資料介面文檔
3)商戶平台開發文檔