设置单个红包的金额,类型等,生成红包信息。预下单完成后,需要在72小时内调用jsapi完成抽红包的操作。(红包过期失效后,资金会退回到商户财付通帐号。)
接口调用说明
服务器端调用
http请求方式: POST
https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder
POST数据格式:XML
需要商户证书
请求参数说明
请求示例
<xml> <sign><![CDATA[E1EE61A91C8E90F299DE6AE075D60A2D]]></sign> <mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno> <mch_id><![CDATA[10000097]]></mch_id> <wxappid><![CDATA[wxcbda96de0b165486]]></wxappid> <send_name><![CDATA[send_name]]></send_name> <hb_type><![CDATA[NORMAL]]></hb_type> <auth_mchid><![CDATA[10000098]]></auth_mchid> <auth_appid><![CDATA[wx7777777]]></auth_appid> <total_amount><![CDATA[200]]></total_amount> <amt_type><![CDATA[ALL_RAND]]></amt_type> <total_num><![CDATA[3]]></total_num> <wishing><![CDATA[恭喜发财 ]]></wishing> <act_name><![CDATA[ 新年红包 ]]></act_name> <remark><![CDATA[新年红包 ]]></remark> <risk_cntl><![CDATA[NORMAL]]></risk_cntl> <nonce_str><![CDATA[50780e0cca98c8c8e814883e5caa672e]]></nonce_str> </xml>
返回数据说明
以下字段在return_code 和result_code都为SUCCESS的时候有返回
成功示例
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[发放成功.]]></return_msg>
<result_code><![CDATA[SUCCESS]]></result_code>
<err_code><![CDATA[0]]></err_code>
<err_code_des><![CDATA[发放成功.]]></err_code_des>
<mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>
<mch_id>10010404</mch_id>
<wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>
<sp_ticket><![CDATA[0cca98c8c8e814883]]></sp_ticket>
<total_amount>3</total_amount>
<detail_id><![CDATA[001001040420141117000004888]]></detail_id>
<send_time><![CDATA[20150101080000]]></send_time>
</xml>
失败示例
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[系统繁忙,请稍后再试.]]></return_msg> <result_code><![CDATA[FAIL]]></result_code> <err_code><![CDATA[268458547]]></err_code> <err_code_des><![CDATA[系统繁忙,请稍后再试.]]></err_code_des> <mch_billno><![CDATA[0010010404201411170000046542]]></mch_billno> <mch_id>10010404</mch_id> <wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid> <total_amount>3</total_amount> </xml> /** * 摇一摇红包预下单 * @author jiosen */ class Yhb_pub extends Wxpay_client_pub { var $code;//code码,用以获取openid var $openid;//用户的openid function __construct() { //设置接口链接 $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder"; //设置curl超时时间 $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /** * 生成接口参数xml */ function createXml() { try { //检测必填参数 if($this->parameters["mch_billno"] == null) { throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."<br>"); }elseif ($this->parameters["send_name"] == null ) { throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."<br>"); }elseif ($this->parameters["total_amount"] == null) { throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."<br>"); }elseif ($this->parameters["total_num"] == null) { throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."<br>"); }elseif ($this->parameters["wishing"] == null) { throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."<br>"); }elseif ($this->parameters["act_name"] == null) { throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."<br>"); }elseif ($this->parameters["remark"] == null) { throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."<br>"); } $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号 $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串 //$this->parameters["re_openid"] = $this->openid;//用户openid $this->parameters["hb_type"] = 'NORMAL';//红包类型 NORMAL-普通红包;GROUP-裂变红包(可分享红包给好友,无关注公众号能力)。 $this->parameters["auth_mchid"] = '1000052601';//摇周边商户号 $this->parameters["auth_appid"] = 'wxbf42bd79c4391863';//摇周边 appid $this->parameters["risk_cntl"] = 'NORMAL';//风控设置 $this->parameters["sign"] = $this->getSign($this->parameters);//签名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } function hbpreorder() { $this->postXmlSSL(); $this->result = $this->xmlToArray($this->response); return $this->result; } /** * 作用:生成可以获得code的url */ function createOauthUrlForCode($redirectUrl) { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["redirect_uri"] = "$redirectUrl"; $urlObj["response_type"] = "code"; $urlObj["scope"] = "snsapi_base"; $urlObj["state"] = "STATE"."#wechat_redirect"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString; } /** * 作用:生成可以获得openid的url */ function createOauthUrlForOpenid() { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["secret"] = WxPayConf_pub::APPSECRET; $urlObj["code"] = $this->code; $urlObj["grant_type"] = "authorization_code"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString; } /** * 作用:通过curl向微信提交code,以获取openid */ function getOpenid() { $url = $this->createOauthUrlForOpenid(); //初始化curl $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //运行curl,结果以jason形式返回 $res = curl_exec($ch); curl_close($ch); //取出openid $data = json_decode($res,true); $this->openid = $data['openid']; return $this->openid; } /** * 作用:设置code */ function setCode($code_) { $this->code = $code_; } }
这里需要注意的是 auth_mchid 和 auth_appid 要填摇周边平台给出的appid 和商户号
调用 (这里不贴前端页面了)