首頁 >php教程 >php手册 >微信红包,企业支付完整命名空间版

微信红包,企业支付完整命名空间版

WBOY
WBOY原創
2016-06-07 11:35:281222瀏覽

包含微信普通红包,裂变红包,企业支付,红包查询,支付查询
<?php <br /> namespace Com;<br> <br> // +----------------------------------------------------------------------<br> // | Author: 左边 (加群:366504956(刚建,欢迎)  交流thinkphp下微信开发)<br> // +----------------------------------------------------------------------<br> <br> class ComPay {<br>     //证书<br>     private $apiclient_cert = '';<br>     private $apiclient_key = '';<br>     //pay的秘钥值<br>     private $apikey = "e10adc3949ba59abbe56e057f20f883e";<br>     //错误信息<br>     private $error = '';<br> <br>     private $mchid = '1309319401';//商户号<br>     private $mchappid = 'wx9dc408cb04cd6f2d';//公众号<br>     private $openid = 'oql2ZwUwTvQsD73jTZuzRc2KFYEA';//接收者openid<br>     private $amount = 100;//金额<br>     private $partnertradeno = '';//订单号<br>     private $spbillcreateip = '';//触发ip<br>     private $checkname = 'NO_CHECK';//校验要求<br> <br>     private $sendname = '发送者名字';<br>     private $wishing = '祝福语';<br>     private $actname = '活动名称';<br>     private $remark = '有钱,任性';<br> <br>     private $totalnum =3;<br>     private $amttype ='ALL_RAND';<br> <br>     //裂变红包<br>     private $api_group = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";<br>     //普通红包<br>     private $api_single = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";<br>     //企业支付<br>     private $api_compay = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";<br>     //约包查询<br>     private $api_redbag_select = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo";<br>     //企业支付查询<br>     private $api_compay_select = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo";<br>     <br>     <br>     /**<br>     *公用-支付用商户号<br>     *@var string<br>     */<br>     public function setMchid($mchid){<br>         $this->mchid = $mchid;<br>     }<br>     /**<br>     *公用-pay的秘钥值<br>     *@var string<br>     */<br>     public function setApiKey($apikey){<br>         $this->apikey = $apikey;<br>     }<br> <br> <br>     /**<br>     *企业支付用微信公众号<br>     *@var string<br>     */<br>     public function setMchAppid($mchappid){<br>         $this->mchappid = $mchappid;<br>     }<br>     /**<br>     *企业支付接收用户openid<br>     *@var string<br>     */<br>     public function setOpenid($openid){<br>         $this->openid = $openid;<br>     }<br> <br>     /**<br>     *企业支付金额<br>     *@var integer<br>     */<br>     public function setAmount($amount){<br>         $this->amount = $amount;<br>     }<br>     /**<br>     *企业支付描述<br>     *@var string<br>     */<br>     public function setDesc($desc){<br>         $this->remark = $desc;<br>     }<br>     <br>     /**<br>     *企业支付订单号<br>     *@var string<br>     */<br>     public function setPartnerTradeNo($partnertradeno){<br>         $this->partnertradeno = $partnertradeno;<br>     }<br>     /**<br>     *企业支付触发ip<br>     *@var string<br>     */<br>     public function setSpbillCreateIp($spbillcreateip){<br>         $this->spbillcreateip = $spbillcreateip;<br>     }<br>     /**<br>     *企业支付校验规则<br>     *@var string<br>     */<br>     public function setCheckName($checkname){<br>         $this->checkname = $checkname;<br>     }<br> <br>     /**<br>     *红包支付公众号<br>     *@var string<br>     */<br>     public function setWxappid($wxappid){<br>         $this->mchappid = $wxappid;<br>     }<br>     /**<br>     *红包支付订单号<br>     *@var string<br>     */<br>     public function setMchBillNo($mchbillno){<br>         $this->partnertradeno = $mchbillno;<br>     }<br>     /**<br>     *红包支付触发ip<br>     *@var string<br>     */<br>     public function setClientIp($clientip){<br>         $this->spbillcreateip = $clientip;<br>     }<br>     /**<br>     *红包接收者/裂一变红包的种子接收者<br>     *@var string<br>     */<br>     public function setReOpenid($reopenid){<br>         $this->openid = $reopenid;<br>     }<br>     /**<br>     *红包支付金额<br>     *@var integer<br>     */<br>     public function setTotalAmount($totalamount){<br>         $this->amount = $totalamount;<br>     }<br>     /**<br>     *红包支付公众号<br>     *@var string<br>     */<br>     public function setSendName($sendname){<br>         $this->sendname = $sendname;<br>     }<br>     /**<br>     *红包支祝福语<br>     *@var string<br>     */<br>     public function setWishing($wishing){<br>         $this->wishing = $wishing;<br>     }<br>     /**<br>     *红包支付活动名称<br>     *@var string<br>     */<br>     public function setActName($actname){<br>         $this->actname = $actname;<br>     }<br>     /**<br>     *红包支付备注信息<br>     *@var string<br>     */<br>     public function setRemark($remark){<br>         $this->remark = $remark;<br>     }<br>     /**<br>     *红包支付个数-裂变专用<br>     *@var string<br>     */<br>     public function setTotalNum($totalnum){<br>         $this->totalnum = $totalnum;<br>     }<br> <br>     public function setAppId($appid){<br>         $this->mchappid = $appid;<br>     }<br>     /**<br>     *错误反馈<br>     *@return string<br>     */<br>     public function error(){<br>         return $this->error;<br>     }<br> <br>     /**<br>     *普通红包支付<br>     *@return boolean<br>     */<br>     public function RedBag(){<br>         if(!$this->inited()) return;<br>         $obj = array();<br>         $obj['wxappid'] = $this->mchappid;<br>         $obj['mch_id'] = $this->mchid;<br>         $obj['mch_billno'] = $this->partnertradeno;<br>         $obj['client_ip'] = $this->spbillcreateip;<br>         $obj['re_openid'] = $this->openid;<br>         $obj['total_amount'] = $this->amount;<br>         $obj['total_num'] = 1;<br>         $obj['send_name'] = $this->sendname;<br>         $obj['wishing'] = $this->wishing;<br>         $obj['act_name'] = $this->actname;<br>         $obj['remark'] = $this->remark;<br>         $url = $this->api_single;<br>         return $this->Pay($url,$obj);<br>     }<br> <br> <br> <br>     /**<br>     *裂变红包支付<br>     *@return boolean<br>     */<br>     public function RedBagGroup(){<br>         if(!$this->inited()) return;<br>         $obj = array();<br>         $obj['wxappid'] = $this->mchappid;<br>         $obj['mch_id'] = $this->mchid;<br>         $obj['mch_billno'] = $this->partnertradeno;<br>         $obj['re_openid'] = $this->openid;<br>         $obj['total_amount'] = $this->amount;<br>         $obj['total_num'] = $this->totalnum;<br>         $obj['amt_type'] = $this->amttype;<br>         $obj['send_name'] = $this->sendname;<br>         $obj['wishing'] = $this->wishing;<br>         $obj['act_name'] = $this->actname;<br>         $obj['remark'] = $this->remark;<br>         $url = $this->api_single;<br>         return $this->Pay($url,$obj);<br>     }<br>     /**<br>     *企业支付<br>     *@return boolean<br>     */<br>     public function ComPay(){<br>         if(!$this->inited()) return;<br>         $obj = array();<br>         $obj['openid'] = $this->openid;<br>         $obj['amount'] = $this->amount;<br>         $obj['desc'] = $this->remark;<br>         $obj['mch_appid'] = $this->mchappid;<br>         $obj['mchid'] = $this->mchid;<br>         $obj['partner_trade_no'] = $this->partnertradeno;<br>         $obj['spbill_create_ip'] = $this->spbillcreateip;<br>         $obj['check_name'] = $this->checkname;<br>         $url = $this->api_compay;<br>         return $this->Pay($url,$obj);<br>     }<br>     /**<br>     *红包查询<br>     *@return array<br>     */<br>     public function BagSelect(){<br>         $this->license();<br>         $obj = array();<br>         $obj['appid'] = $this->mchappid;<br>         $obj['mch_id'] = $this->mchid;<br>         $obj['mch_billno'] = $this->partnertradeno;<br>         $obj['bill_type'] = 'MCHT';<br>         $url = $this->api_redbag_select;<br>         return $this->Pay($url,$obj);<br>     }<br>     /**<br>     *企业支付查询<br>     *@return array<br>     */<br>     public function ComPaySelect(){<br>         $this->license();<br>         $obj = array();<br>         $obj['appid'] = $this->mchappid;<br>         $obj['mch_id'] = $this->mchid;<br>         $obj['partner_trade_no'] = $this->partnertradeno;<br>         $url = $this->api_compay_select;<br>         return $this->Pay($url,$obj);<br>     }<br> <br>     /**<br>     *支付前准备<br>     *@return boolean<br>     */<br>     private function inited(){<br>         $inited = true;<br>         $amount = $this->amount;<br>         if(!is_numeric($amount)){<br>             $this->error = "金额参数错误";<br>             $inited = false;<br>         }elseif($amount             $this->error = "金额太小";<br>             $inited = false;<br>         }elseif($amount>20000){<br>             $this->error = "金额太大";<br>             $inited = false;<br>         }<br>         if(!$this->partnertradeno){<br>             $this->partnertradeno = $this->GenBillNo();<br>         }<br>         if(!$this->spbillcreateip)<br>             $this->spbillcreateip = $_SERVER['REMOTE_ADDR'];<br>         $this->license();<br>         return $inited;<br>     }<br>     /**<br>     *证书初始化<br>     *放在同目录 cacert/文件夹下<br>     */<br>     private function license(){<br>         if(!$this->apiclient_cert) <br>             $this->apiclient_cert = dirname(__FILE__)."/cacert/apiclient_cert.pem";<br>         if(!$this->apiclient_key) <br>             $this->apiclient_key = dirname(__FILE__)."/cacert/apiclient_key.pem";<br>     }<br> <br>     /**<br>     *生在订单号<br>     *@return boolean<br>     */<br>     private function GenBillNo(){<br>         $rnd_num = array('0','1','2','3','4','5','6','7','8','9');<br>         $rndstr = "";<br>         while(strlen($rndstr)             $rndstr .= $rnd_num[array_rand($rnd_num)];    <br>         }<br> <br>         return $this->mchid.date("Ymd").$rndstr;<br>     }<br> <br>     /**<br>     *完成支付操作<br>     *@url string<br>     *@obj array<br>     *@return boolean<br>     */<br>     private function Pay($url,$obj){    <br>         $obj['nonce_str'] = $this->create_noncestr();<br>         $sign = $this->getSign($obj);<br>         $obj['sign'] = $sign;<br>         $postXml = $this->arrayToXml($obj);<br>         $responseXml = $this->CurlPostSsl($url,$postXml);<br>         return $responseXml;<br>     }<br>     /**<br>     *创建随机字串<br>     *@return string<br>     */<br>     private function create_noncestr($length = 32){<br>         $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';<br>         $str = '';<br>         for ($i = 0; $i              $str .= substr($chars,mt_rand(0,strlen($chars)-1),1);<br>         }<br>         return $str;<br>     }<br>     /**<br>     *创建签名<br>     *@return string<br>     */<br>     private function getSign($arr){<br>         ksort($arr); //按照键名排序<br>         $sign_raw = '';<br>         foreach($arr as $k => $v){<br>             $sign_raw .= $k.'='.$v.'&';<br>         }<br>         $sign_raw .= 'key='.$this->apikey;<br> <br>         return strtoupper(md5($sign_raw));<br>     }<br> <br>     /**<br>      * WXHongBao::genXMLParam()<br>      * 生成post的参数xml数据包<br>      * @return $xml<br>      */<br>     private function arrayToXml($arr){<br>         $xml ="<xml>";<br>         foreach ($arr as $key => $val) {<br>             if (is_numeric($val)) {<br>                 $xml .= "".$val."".$key.">";<br>             }else{<br>                 $xml .= "".$key.">";<br>             }<br>         }<br>         $xml .= "</xml>";<br>         return $xml;        <br>     }<br> <br>     /**<br>      * curl提交<br>      * @return $boolean<br>      */<br>     private function CurlPostSsl($url,$xml,$second = 10){<br>         $ch = curl_init();       <br>         curl_setopt($ch,CURLOPT_TIMEOUT,$second);<br>         curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);        <br>         curl_setopt($ch,CURLOPT_URL,$url);<br>         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);<br>         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);<br> <br>         curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');<br>         curl_setopt($ch,CURLOPT_SSLCERT,$this->apiclient_cert);        <br>         curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');<br>         curl_setopt($ch,CURLOPT_SSLKEY,$this->apiclient_key);<br>         curl_setopt($ch,CURLOPT_POST, 1);<br>         curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);<br>         $data = curl_exec($ch);<br>         if($data){<br>             curl_close($ch);            <br>             $rsxml = simplexml_load_string($data);<br>             if($rsxml->return_code == 'SUCCESS' ){<br>                 return $data;<br>             }else{<br>                 $this->error = $rsxml->return_msg;<br>                 return false;    <br>             }<br>         }else{ <br>             $this->error = curl_errno($ch);<br>             curl_close($ch);<br>             return false;<br>         }<br>     }<br> <br> <br> <br> <br> <br> }

附件 ComPay.class.zip ( 3.03 KB 下载:138 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn