微信开发之发红包
http://mch.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5
把下面这堆xml 以post请求方式 提交到https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack
<xml>
<sign><![CDATA[E1EE61A91C8E90F299DE6AE075D60A2D]]></sign>
<mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>
<mch_id><![CDATA[888]]></mch_id>
<wxappid><![CDATA[wxcbda96de0b165486]]></wxappid>
<send_name><![CDATA[send_name]]></send_name>
<re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid>
<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;
}
来源:oschina
链接:https://my.oschina.net/u/2962/blog/521069