代码实现:
/** * 微信支付 * @param string $openid 用户openid */ public function pay($re_openid) { include_once('WxPacketClass.php'); $wxHongBaoHelper = new WxPacketClass($this->app_sign); $wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());//随机字符串,丌长于 32 位 $wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date('YmdHis').rand(1000, 9999));//订单号(28位) $wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);//商户号 $wxHongBaoHelper->setParameter("wxappid", $this->app_id); $wxHongBaoHelper->setParameter("send_name", '扬和宏科技');//红包发送者名称 $wxHongBaoHelper->setParameter("re_openid", $re_openid);//openid $wxHongBaoHelper->setParameter("total_amount", 100);//付款金额,单位分 $wxHongBaoHelper->setParameter("total_num", 1);//红包収放总人数 $wxHongBaoHelper->setParameter("wishing", '给您拜个晚年,祝您晚年幸福!');//红包祝福诧 $wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//调用接口的机器 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); return $responseObj->return_code; }片段二、
//生成红包接口XML信息 /* <xml> <sign>![CDATA[E1EE61A9]]</sign> <mch_billno>![CDATA[00100]]</mch_billno> <mch_id>![CDATA[888]]</mch_id> <wxappid>![CDATA[wxcbda96de0b165486]]</wxappid> <send_name>![CDATA[send_name]]</send_name> <re_openid>![CDATA[onqOjjXXXXXXXXX]]</re_openid> <total_amount>![CDATA[100]]</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> <act_id>![CDATA[act_id]]</act_id> <remark>![CDATA[新年红包]]</remark> </xml> */ function create_hongbao_xml($retcode = 0, $reterrmsg = "ok"){ try { $this->setParameter('sign', $this->get_sign()); $commonUtil = new CommonUtil(); return $commonUtil->arrayToXml($this->parameters); }catch (SDKException $e) { die($e->errorMessage()); } }片段三、
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_URL,$url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); //cert 与 key 分别属于两个.pem文件 curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR.'apiclient_cert.pem'); curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR.'apiclient_key.pem'); curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR.'rootca.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); curl_close($ch); return false; } }代码结构:
有需要的可以联系我~
因留言的朋友过于频繁,所以将该源码发布在github,地址:https://github.com/yangsir/wechat_red_packet#wechat_red_packet,可以移步~
已有41条评论