private $ app _ id = ' wxXXXXXXXXXXXX ';//公眾賬號appid,先申請匹配的公眾賬號。
private $ app _ secret = ' xxxxxxxxxxxxxxxxxxxxxxxxxxx ';//微信官方賬號密,用戶獲取用戶授權令牌。
private $ app _ mchid = ' XXXXXXXX//商戶編號id
function __construct(){
//在這裏做某事....
}
/**
*微信支付
* @param string $openid用戶openid
*/
公共函數支付($re_openid)
{
include _ once(' wxhongbaohelper . PHP ');
$ common util = new common util();
$ wxHongBaoHelper = new wxHongBaoHelper();
$ wxHongBaoHelper-& gt;setParameter("nonce_str ",$ this-& gt;great _ rand());
//隨機字符串,η大於32位。
$ wxHongBaoHelper-& gt;
setParameter("mch_billno ",$ this-& gt;app_mchid.date('YmdHis ')。蘭德(1000,9999));
//訂單編號
$ wxHongBaoHelper-& gt;setParameter("mch_id ",$ this-& gt;app _ MC hid);//商家編號
$ wxHongBaoHelper-& gt;setParameter("wxappid ",$ this-& gt;app _ id);
$ wxHongBaoHelper-& gt;SetParameter("昵稱","紅包");//提供者的名稱
$ wxHongBaoHelper-& gt;SetParameter("send_name ","紅包");//發紅包人的名字
$ wxHongBaoHelper-& gt;setParameter("re_openid ",$ re _ OpenID);//與醫療脈搏通信相關的openid
$ wxHongBaoHelper-& gt;setParameter("total_amount ",100);//付款金額,以分鐘為單位
$ wxHongBaoHelper-& gt;setParameter("min_value ",100);//最低紅包金額,單位積分
$ wxHongBaoHelper-& gt;setParameter("max_value ",100);//最大紅包金額,單位積分。
$ wxHongBaoHelper-& gt;setParameter("total_num ",1);//放紅包的總人數。
$ wxHongBaoHelper-& gt;SetParameter("wishing ","感謝您參與紅包發放活動,祝您新年快樂!");
//紅包祝福
$ wxHongBaoHelper-& gt;setParameter("client_ip ",' 127 . 0 . 0 . 1 ');//調用接口的機器Ip地址
$ wxHongBaoHelper-& gt;SetParameter("act_name ","紅包活動");//實時名稱
$ wxHongBaoHelper-& gt;SetParameter("備註","過來搶!");//備註信息
$ post XML = $ wxHongBaoHelper-& gt;create _紅包_ XML();
$ URL = '/mmpaymkttransfers/sendredpack ';
$ response XML = $ wxHongBaoHelper-& gt;curl_post_ssl($url,$ post XML);
//用作結果調試輸出
//echo html entities($ response XML,ENT_COMPAT,' UTF-8 ');
$ response obj = SimpleXML _ load _ string($ response XML,' SimpleXMLElement ',LIBXML _ NOCDATA);
return $ response obj-& gt;return _代碼;
}
獲得隨機字符串正方形
1
2
三
四
五
六
七
八
九
10
11
/**
*生成隨機數
*/
公共函數great_rand(){
$ str = ' 1234567890 abcdefghijklmnopqrstuvwxyz ';
for($ I = 0;$ i & lt30;$i++){
$ j =蘭特(0,35);
$t1。= $ str[$ j];
}
return $ t 1;
}
簽名算法
1
2
三
四
五
六
七
八
九
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
例如:
appid:wxd 111665 abv 58 f4f
mch_id: 10000100
設備信息:1000
正文:測試
nonce_str: ibuaiVcKdpRxkhJA
步驟1:按照key=value的格式和參數名的ASCII字典順序對參數進行排序,如下所示:
string a = " appid = wxd 930 ea 5d 5a 258 f 4f & amp;body =測試& amp設備信息= 1000 & amp;mch_i
d=10000100。nonce _ str = ibuaivcdprxkhja ";
第二步:拼接支付密鑰:
stringSignTemp = " stringA & ampkey = 192006250 b4c 09247 EC 02 edce 69 F6 a 2d "
sign=MD5(stringSignTemp)。toUpperCase()
= " 9 a0a 8659 f 005d 6984697 e 2 ca 0 a 9 cf 3 b 7 "
*/
受保護的函數get_sign(){
define('PARTNERKEY '," qsrxxxxxxxxxxxxxxxxxxxxxxx ");
嘗試{
if(null = = partner key | | " " = = partner key){
thrownewsdkruntimiexception(" Key不能為空!"。“& ltbr >;);
}
如果($ this-& gt;Check _ sign _ parameters()= = false){//檢查生成的簽名參數。
Thrownewsdkruntimiexception("生成的簽名參數缺失!"。“& ltbr >;);
}
$ common util = new common util();
ksort($ this-& gt;參數);
$ unSignParaString = $ common util-& gt;formatQueryParaMap($ this-& gt;參數,假);
$ MD 5 sign util = new MD 5 sign util();
return $ m D5 sign util-& gt;
符號($unSignParaString,$ common util-& gt;trim string(partner key));
}catch (SDKRuntimeException $e)
{
骰子($ e->;error message());
}
}
CURL請求並發送證書。
1
2
三
四
五
六
七
八
九
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
函數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__)。目錄_分隔符。
‘正書’。目錄_分隔符。API client _ cert . PEM’);
curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__)。目錄_分隔符。
‘正書’。目錄_分隔符。API client _ key . PEM’);
curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__)。目錄_分隔符。
‘正書’。目錄_分隔符。rootca . PEM’);
if( count($aHeader)>= 1 ){
curl_setopt($ch,CURLOPT_HTTPHEADER,$ a header);
}
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$ vars);
$ data = curl _ exec($ ch);
if($data){
curl _ close($ ch);
返回$ data
}
否則{
$ error = curl _ errno($ ch);
//echo“調用失敗,錯誤代碼:$ error \ n”;
curl _ close($ ch);
返回false
}
}
入口文件
1
2
三
四
五
六
七
八
九
10
11
12
13
14
15
16
17
18
19
@要求“pay . PHP”;
//獲取用戶信息
$ GET = $ _ GET[' param '];
$ code = $ _ GET[' code '];
//判斷代碼是否存在。
if($ get = = ' access _ token ' & amp;& amp!空($code)){
$ param[' param ']= ' access _ token ';
$ param[' code ']= $ code;
$ Packet = new Packet();
//獲取用戶openid信息
$ userinfo = $ packet-& gt;_route('userinfo ',$ param);
if(empty($ userinfo[' OpenID ']){
exit(" no auth ");
}
//轉賬支付方式
$ packet->;_route('wxpacket ',array(' OpenID ' = & gt;$ userinfo[' OpenID ']);
}否則{
$ packet->;_ route(' userinfo ');
}