微信開發之發紅包php
http://mch.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5算法
把下面這堆xml 以post請求方式 提交到https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpackapi
<xml>微信
<sign><![CDATA[E1EE61A91C8E90F299DE6AE075D60A2D]]></sign>微信開發
<mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>app
<mch_id><![CDATA[888]]></mch_id>curl
<wxappid><![CDATA[wxcbda96de0b165486]]></wxappid>post
<send_name><![CDATA[send_name]]></send_name>加密
<re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid>url
<total_amount><![CDATA[200]]></total_amount>
<total_num><![CDATA[1]]></total_num>
<wishing><![CDATA[恭喜發財]]></wishing>
<client_ip><![CDATA[127.0.0.1]]></client_ip>
<act_name><![CDATA[新年紅包]]></act_name>
<remark><![CDATA[新年紅包]]></remark>
<nonce_str><![CDATA[50780e0cca98c8c8e814883e5caa672e]]></nonce_str>
</xml>
注意:須要證書(證書及使用說明詳見商戶證書)
參見例子:function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
注意:************
xml裏面要處理的參數
1.nonce_str 隨機字符串,不長於32位(自動生成,沒難度)
2.sign 簽名 (重點是這個比較複雜) 簽名生成算法地址 https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=4_3
3.mch_billno 商戶訂單號(每一個訂單號必須惟一)組成:mch_id+yyyymmdd+10位一天內不能重複的數字。接口根據商戶訂單號支持重入,如出現超時可再調用。(沒難度)
4.各類證書是須要到微信平臺下載
官方例子PHP
<?php /* 請確保您的libcurl版本是否支持雙向認證,版本高於7.20.1 */ 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_PROXY, '10.206.30.98'); //curl_setopt($ch,CURLOPT_PROXYPORT, 8080); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); //如下兩種方式需選擇一種 //第一種方法,cert 與 key 分別屬於兩個.pem文件 //默認格式爲PEM,能夠註釋 //curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert.pem'); //默認格式爲PEM,能夠註釋 //curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); //curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/private.pem'); //第二種方式,兩個文件合成一個.pem文件 //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.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; } } $data = curl_post_ssl('https://api.mch.weixin.qq.com/secapi/pay/refund', 'merchantid=1001000'); print_r($data); 如下是封裝好的方法 wxHongBaoHelper private function fahb($weixin_openid, $price) { define('DS', DIRECTORY_SEPARATOR); /** * 微信配置 */ //商戶 appid define('APPID', "xxxxxxxxxxxxxx"); define('SIGNTYPE', "sha1"); //通加密串 define('PARTNERKEY',"xxxxxxxxxxxxxx"); // define('APPSERCERT', "xxxxxxxxxxxxxx"); //商戶id define('MCHID', "00000000000"); //必定要修改 $openid = $weixin_openid; $money = $price*100;//紅包金額,單位分 $mch_billno=MCHID.date('YmdHis').rand(1000, 9999);//訂單號 include_once('WxHongBaoHelper.php'); $commonUtil = new CommonUtil(); $wxHongBaoHelper = new WxHongBaoHelper(); $wxHongBaoHelper->setParameter("nonce_str", $commonUtil->create_noncestr());//隨機字符串,丌長於 32 位 $wxHongBaoHelper->setParameter("mch_billno", $mch_billno);//訂單號 $wxHongBaoHelper->setParameter("mch_id", MCHID);//商戶號 $wxHongBaoHelper->setParameter("wxappid", APPID); $wxHongBaoHelper->setParameter("nick_name", '');//提供方名稱 $wxHongBaoHelper->setParameter("send_name", '');//紅包發送者名稱 $wxHongBaoHelper->setParameter("re_openid", $openid);//相對於醫脈互通的openid $wxHongBaoHelper->setParameter("total_amount", $money);//付款金額,單位分 $wxHongBaoHelper->setParameter("min_value", $money);//最小紅包金額,單位分 $wxHongBaoHelper->setParameter("max_value", $money);//最大紅包金額,單位分 $wxHongBaoHelper->setParameter("total_num", 1);//紅包収放總人數 $wxHongBaoHelper->setParameter("wishing", '');//紅包祝福詫 $wxHongBaoHelper->setParameter("client_ip", 'x.x.x.x');//調用接口的機器 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); $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA); $arrayName = array('responseObj' => $responseObj, 'mch_billno' => $mch_billno); return $arrayName; }