微信红包接口
微信红包是微信支付推出的一款基于微信客户端的免费服务应用,微信红包以微信支付
为核心安全保障,为广大用户提供安全,快捷的移劢支付服务。
请求URL
现金红包
https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack
裂变红包
https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack
企业付款
https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers
请求的参数:
以现金红包为例:请求参数示例:
<xml> <act_name>123123</act_name> <client_ip> <![CDATA[127.0.0.1]]> </client_ip> <mch_billno> 1281216601201609200000000000 </mch_billno> <mch_id>1281216601</mch_id> <nonce_str> <![CDATA[CgSaD2zSG6ds2r2ulSuSmj3zKUXSukSS]]> </nonce_str> <re_openid> <![CDATA[okzyKwbDW-d7Spc0M112zWZjaa3E]]> </re_openid> <remark> 1231 </remark> <send_name> <![CDATA[haha哈]]> </send_name> <total_amount>1.00</total_amount> <total_num>1</total_num> <wishing> <![CDATA[祝您好运]]> </wishing> <wxappid> <![CDATA[wxa316d1ce30f2c952]]> </wxappid> <sign> <![CDATA[52F30DAD7E0ADFA842627179502BC2F3]]> </sign> </xml>
核心代码如下:
$config里面包含了必须的appid,和其他参数,可以看上图详情。只需调用yiy_send(要发送给谁的(openid),金额,配置参数)
/*随机字符串*/ private function random($length, $numeric = FALSE) { $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35); $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed)); if ($numeric) { $hash = ''; } else { $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64); $length--; } $max = strlen($seed) - 1; for ($i = 0; $i < $length; $i++) { $hash .= $seed{mt_rand(0, $max)}; } return $hash; } //发送红包 private function yiy_send($openid,$price,$config) { $uniacid = 2; $api = $config['api']; $activity = $config['activity']; $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; $pars = array(); $pars['nonce_str'] = $this->random(32); $pars['mch_billno'] = $api['mchid'] . date('YmdHis'); $pars['mch_id'] = $api['mchid']; $pars['wxappid'] = $api['appid']; $pars['send_name'] = $activity['provider']; $pars['re_openid'] = $openid; $pars['total_amount'] = $price*100; $pars['total_num'] = 1; $pars['wishing'] = $activity['wish']; $pars['client_ip'] = $api['ip']; $pars['act_name'] = $activity['title']; $pars['remark'] = $activity['remark']; ksort($pars); $string1 = ''; foreach($pars as $k => $v) { $string1 .= "{$k}={$v}&"; } $string1 .= "key={$api['password']}"; $pars['sign'] = strtoupper(md5($string1)); $xml = $this->array2xml($pars); define(M_PATH, dirname(__FILE__). '/./../../../../addons/ly_huilife/'); $extras = array(); //此处为证书位置(绝对路径),改成适合自己的,我这里放的比较那啥。。。。忽略/// $extras['CURLOPT_CAINFO'] = M_PATH . '/cert/rootca.pem.' . $uniacid; $extras['CURLOPT_SSLCERT'] = M_PATH . '/cert/apiclient_cert.pem.' . $uniacid; $extras['CURLOPT_SSLKEY'] = M_PATH . '/cert/apiclient_key.pem.' . $uniacid; $procResult = null; $resp = $this->CurlPostSsl($url, $xml, $extras); return $resp; } //提交 private function CurlPostSsl($url,$xml,$extras){ $ch = curl_init(); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch,CURLOPT_TIMEOUT,60); 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); curl_setopt($ch, CURLOPT_SSLVERSION, 1); curl_setopt($ch,CURLOPT_SSLCERT,$extras['CURLOPT_SSLCERT']); curl_setopt($ch,CURLOPT_SSLKEY,$extras['CURLOPT_SSLKEY']); curl_setopt($ch,CURLOPT_CAINFO,$extras['CURLOPT_CAINFO']); curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,$xml); $data = curl_exec($ch); var_dump($data); if($data){ curl_close($ch); $rsxml = simplexml_load_string($data); if($rsxml->return_code == 'SUCCESS' && $rsxml->result_code == 'SUCCESS'){ return array('code'=>1,'msg'=>'发放成功'); }else{ //这里你可以获取,并返回详细失败信息,我这里都用这种方式返回了。 return array('code'=>0,'msg'=>'#1未知错误,稍后再试'); } }else{ $error = curl_errno($ch); curl_close($ch); return array('code'=>0,'msg'=>$error ); } } function array2xml($arr, $level = 1) { $s = $level == 1 ? "<xml>" : ''; foreach ($arr as $tagname => $value) { if (is_numeric($tagname)) { $tagname = $value['TagName']; unset($value['TagName']); } if (!is_array($value)) { $s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : '') . $value . (!is_numeric($value) ? ']]>' : '') . "</{$tagname}>"; } else { $s .= "<{$tagname}>" . array2xml($value, $level + 1) . "</{$tagname}>"; } } $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s); return $level == 1 ? $s . "</xml>" : $s; }
正确的返回结果是:
注意:传入的金额是以分为单位,比如1元,要写成100,不然红包过小会失败的(红包大于等于1元,小于等于200元),比如这样返回:
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[每个红包的平均金额必须在1.00元到200.00元之间.]]></return_msg> <result_code><![CDATA[FAIL]]></result_code> <err_code><![CDATA[MONEY_LIMIT]]></err_code> <err_code_des><![CDATA[每个红包的平均金额必须在1.00元到200.00元之间.]]></err_code_des> <mch_billno><![CDATA[1281216601201609200000000000]]></mch_billno> <mch_id><![CDATA[1281216601]]></mch_id> <wxappid><![CDATA[wxa316d1ce30f2c952]]></wxappid> <re_openid><![CDATA[okzyKwbDW-d7Spc0M112zWZjaa3E]]></re_openid> <total_amount>1</total_amount> </xml>
更详细的文档:官方pdf:https://pay.weixin.qq.com/helper/cashredopenapi_V2.pdf
将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。