php版微信js-sdk支付接口类用法示例
本文实例讲述了php版微信js-sdk支付接口类用法。分享给大家供大家参考,具体如下:
这个支付类是根据官方的文档修改而来!主要实现生成JSAPI、Native的package签名包和Native响应的XML格式数据。注释都标上了各方法的用意。由于package包签名,略复杂,这个要自己多花时间去对应去看和log出文件来一一对比!当然只要你用上教程的类,设置好对应的参数就可以正确的生成package参数等
<?php if(isset($argc)&&$argc>=1&&$argv[0]==__FILE__){ //初始化pay的必要信息 $pay=newWechatPay(array( WechatPay::APPID=>'wx99dabzpiuq83985b8', WechatPay::APPSERCER=>'ac12e7e4abaer63hkoa0cc36a9663fa', WechatPay::PARTNERKEY=>'bae4sfa3562rsfaq23s2045', WechatPay::PARTNERID=>'1268969802', WechatPay::PAYSIGNKEY=>'9Fqsxb3PK4IVOCEc4yCquy5zecS9LeeMjF2Nn4B4YKoOxPwaQdFwMezKT8oNlBYaWcuT', WechatPay::SIGNTYPE=>'sha1', )); //设置package必要的参数jsapinative都通用 $pay->setParams(WechatPay::BANK_TYPE,"WX"); $pay->setParams(WechatPay::BODY,"test"); $pay->setParams(WechatPay::PARTNER,$pay->partnerid); $pay->setParams(WechatPay::OUT_TRADE_NO,commonUtil::createNoncestr()); $pay->setParams(WechatPay::TOTAL_FEE,"1"); $pay->setParams(WechatPay::FEE_TYPE,"1"); $pay->setParams(WechatPay::TIMESTAMP,time()); $pay->setParams(WechatPay::NOTIFY_URL,"http://www.demo.com/notify"); $pay->setParams(WechatPay::SPBILL_CREATE_IP,"127.0.0.1"); $pay->setParams(WechatPay::INPUT_CHARSET,"UTF-8"); //JSAPI的签名json print_r($pay->createJsApiPackage()); //生成nativeXML print_r($pay->createNativePackage()); //生成nativeURL print_r($pay->createNativeUrl("9701")); }
JSAPI生成的package签名包参数:
{ "appId":"wx9998ff5f4dede5b7", "package":"bank_type=WX&body=test&fee_type=1&input_charset=UTF-8¬ify_url=http%3A%2F%2Fwww.demo.com%2Fnotify&out_trade_no=Vf5qsSwtu0hc2loH&partner=wx9998ff5f4dede5b7&spbill_create_ip=127.0.0.1×tamp=1409295711&total_fee=1&sign=FEE0167BD0D89A88BF6850590EA889B6", "timeStamp":1409295711, "nonceStr":"Vf5qsSwtu0hc2loH", "paySign":"f816264c750923863c370a1739640244b0c2d39c", "signType":"sha1" }
Native响应的XML格式:
<xml> <AppId><![CDATA[wx9998ff5f4dede5b7]]></AppId> <Package> <![CDATA[bank_type=WX&body=test&fee_type=1&input_charset=UTF-8¬ify_url=http%3A%2F%2Fwww.demo.com%2Fnotify&out_trade_no=GDl3what4sALDEAd&partner=wx9998ff5f4dede5b7&spbill_create_ip=127.0.0.1×tamp=1409296124&total_fee=1&sign=BF949B85570644B939B369FD44B0C4A9]]> </Package> <TimeStamp>1409296124</TimeStamp> <NonceStr><![CDATA[GDl3what4sALDEAd]]></NonceStr> <RetCode>0</RetCode> <RetErrMsg><![CDATA[ok]]></RetErrMsg> <AppSignature><![CDATA[ca4a2467b817a62c38a9801fcf451f51692027bf]]></AppSignature> <SignMethod><![CDATA[sha1]]></SignMethod> </xml>
Native的URL链接:
weixin://wxpay/bizpayurl?appid=wx9998ff5f4dede5b7&noncestr=VY7cVA6mtVrc1BVq&productid=9701&sign=43508b65b50e1d7e1089be66d55a709469155d73×tamp=1409296323
无论哪一种方式,我们都要通过setParams来设置必要初始化参数和商品价格和状态等!
WechatPayclass:
<?php classWechatPay{ const BANK_TYPE='bank_type', BODY='body', PARTNER='partner', OUT_TRADE_NO='out_trade_no', TIMESTAMP='timestamp', TOTAL_FEE='total_fee', FEE_TYPE='fee_type', NOTIFY_URL='notify_url', SPBILL_CREATE_IP='spbill_create_ip', INPUT_CHARSET='input_charset', APPID='appid', APPSERCER='appsercer', PAYSIGNKEY='appkey', PARTNERID='partnerid', PARTNERKEY='partnerkey', SIGNTYPE='signtype'; public $params=array(),$partnerid=''; staticprotected $_instance; protected $_appid,$_appkey,$_signtype,$_partnerkey,$_appsercer; staticpublicfunctiongetInstance(array$options=array()){ if(empty(self::$_instance)){ self::$_instance=newself($options); } returnself::$_instance; } publicfunction__construct(array$options=array()){ $this->_appid=$options[self::APPID]; $this->_appkey=$options[self::PAYSIGNKEY]; $this->_signtype=$options[self::SIGNTYPE]; $this->_partnerkey=$options[self::PARTNERKEY]; $this->_appsercer=$options[self::APPSERCER]; $this->partnerid=$options[self::APPID]; } publicfunctionsetParams($param,$paramValue){ $this->params[CommonUtil::trimString($param)]=CommonUtil::trimString($paramValue); } publicfunctiongetParams($param){ return$this->params[$param]; } protectedfunctioncreateNoncestr($length=16){ $chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str=""; for($i=0;$i<$length;$i++){ $str.=substr($chars,mt_rand(0,strlen($chars)-1),1); } return$str; } publicfunctioncheckParams(){ //必要的9个参与签名的参数 if($this->params[self::BANK_TYPE]==null||$this->params[self::BODY]==null||$this->params[self::PARTNER]==null|| $this->params[self::OUT_TRADE_NO]==null||$this->params[self::TOTAL_FEE]==null||$this->params[self::FEE_TYPE]==null|| $this->params[self::NOTIFY_URL]==null||$this->params[self::SPBILL_CREATE_IP]==null||$this->params[self::INPUT_CHARSET]==null ){ returnfalse; } returntrue; } /* *生成package包 *@params初始化类时用setParams方法定义必要的9个参数 *排序后格式化urlquery形式再md5SignUtil类签名,再给合URL */ protectedfunctiongetPackageSign(){ try{ if(null==$this->_partnerkey||""==$this->_partnerkey){ thrownewException("密钥不能为空!"."<br>"); } $commonUtil=newCommonUtil(); ksort($this->params); $unSignParaString=$commonUtil->formatUrlQuery($this->params,false); $paraString=$commonUtil->formatUrlQuery($this->params,true); $md5SignUtil=newMD5SignUtil(); return$paraString."&sign=".$md5SignUtil->sign($unSignParaString,commonUtil::trimString($this->_partnerkey)); }catch(Exception$e){ echo($e->getMessage()); } } /* *生成签名方法 *@paramsappidappkeypackagetimestampnoncestr等参数而native事例代码中加上retcodereterrmsg两个参数 */ publicfunctiongetPaySign($signObj){ foreach($signObjas$k=>$v){ $signParams[strtolower($k)]=$v; } try{ if($this->_appkey==""){ thrownewException("APPKEY为空!"."<br>"); } $signParams["appkey"]=$this->_appkey; ksort($signParams,SORT_STRING); $commonUtil=newCommonUtil(); $signString=$commonUtil->formatPayUrlQuery($signParams,false); returnsha1($signString); }catch(Exception$e){ echo($e->getMessage()); } } //JSAPI签名其中nonceStr是作为订单号灌穿整个支付流程 publicfunctioncreateJsApiPackage(){ try{ if($this->checkParams()==false){ thrownewException("生成package参数缺失!"."<br>"); } $payObj["appId"]=$this->_appid; $payObj["package"]=$this->getPackageSign(); $payObj["timeStamp"]=$this->getParams(self::TIMESTAMP); $payObj["nonceStr"]=$this->getParams(self::OUT_TRADE_NO); $payObj["paySign"]=$this->getPaySign($payObj); $payObj["signType"]=$this->_signtype; returnjson_encode($payObj); }catch(Exception$e){ die($e->getMessage()); } } /* *构建发货状态数组主要三个参数openidtransidorderid */ publicfunctioncreateDeliverPost(Array$params){ $deliver=array(); $deliver['appid']=$this->_appid; $deliver['openid']=$params['openid']; $deliver['transid']=$params['transid']; $deliver['out_trade_no']=$params['out_trade_no']; $deliver['deliver_timestamp']=current_time('timestamp'); $deliver['deliver_status']=1; $deliver['deliver_msg']='OK'; $deliver['app_signature']=$this->getPaySign($deliver); $deliver['sign_method']='sha1'; return$deliver; } /* *生成扫描或者点击原生URL后,响应的XML格式 *@params$retcode$reterrmsg定义该商品的状态 */ publicfunctioncreateNativePackage($retcode=0,$reterrmsg="ok"){ try{ if($this->checkParams()==false&&$retcode==0){//如果是正常的返回,检查财付通的参数 thrownewException("生成package参数缺失!"."<br>"); } $nativeObj["AppId"]=$this->_appid; $nativeObj["Package"]=$this->getPackageSign(); $nativeObj["TimeStamp"]=$this->getParams(self::TIMESTAMP); $nativeObj["NonceStr"]=$this->getParams(self::OUT_TRADE_NO); $nativeObj["RetCode"]=$retcode; $nativeObj["RetErrMsg"]=$reterrmsg; $nativeObj["AppSignature"]=$this->getPaySign($nativeObj); $nativeObj["SignMethod"]=$this->_signtype; $commonUtil=newCommonUtil(); $xml=$commonUtil->arrayToXml($nativeObj); exit($xml); }catch(Exception$e){ echo($e->getMessage()); } } /* *生成原生URL以订单号为参数这是灌穿整个支付流程 */ publicfunctioncreateNativeUrl($productid){ $commonUtil=newCommonUtil(); $nativeObj["appid"]=$this->_appid; $nativeObj["productid"]=urlencode($productid); $nativeObj["timestamp"]=time(); $nativeObj["nonceStr"]=commonUtil::createNoncestr(); $nativeObj["sign"]=$this->getPaySign($nativeObj); $nativeString=$commonUtil->formatPayUrlQuery($nativeObj,false); return"weixin://wxpay/bizpayurl?".$nativeString; } /* *取IP地址 */ publicfunctiongetIp(){ switch(true){ case!empty($_SERVER["HTTP_CLIENT_IP"]): $ip=$_SERVER["HTTP_CLIENT_IP"]; break; case!empty($_SERVER["HTTP_X_FORWARDED_FOR"]): $ip=$_SERVER["HTTP_X_FORWARDED_FOR"]; break; case!empty($_SERVER["REMOTE_ADDR"]): $ip=$_SERVER["REMOTE_ADDR"]; break; default: $ip="127.0.0.1"; } return$ip; } } classMD5SignUtil{ publicfunctionsign($content,$key){ try{ if(null==$key){ thrownewException("财付通签名key不能为空!"."<br>"); } if(null==$content){ thrownewException("财付通签名内容不能为空"."<br>"); } $signStr=$content."&key=".$key; returnstrtoupper(md5($signStr)); }catch(Exception$e){ echo($e->getMessage()); } } publicstaticfunctionverifySignature($content,$sign,$md5Key){ $signStr=$content."&key=".$md5Key; $calculateSign=strtolower(md5($signStr)); $tenpaySign=strtolower($sign); return$calculateSign==$tenpaySign; } } classCommonUtil{ publicfunctiongenAllUrl($toURL,$paras){ $allUrl=null; if(null==$toURL){ die("toURLisnull"); } if(strripos($toURL,"?")==""){ $allUrl=$toURL."?".$paras; }else{ $allUrl=$toURL."&".$paras; } return$allUrl; } //订单号,可根据实际自定义 staticpublicfunctioncreateOrderNo(){ $nonce=CommonUtil::createNoncestr(4); returnstrtoupper(date('ymds').substr(microtime(),2,4).$nonce); } //随机字符串 staticpublicfunctioncreateNoncestr($length=16){ $chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str=""; for($i=0;$i<$length;$i++){ $str.=substr($chars,mt_rand(0,strlen($chars)-1),1); } return$str; } publicfunctionsplitParaStr($src,$token){ $resMap=array(); $items=explode($token,$src); foreach($itemsas$item){ $paraAndValue=explode("=",$item); if($paraAndValue!=""){ $resMap[$paraAndValue[0]]=$paraAndValue[1]; } } return$resMap; } staticfunctiontrimString($value){ $ret=null; if(null!=$value){ $ret=$value; if(strlen($ret)==0){ $ret=null; } } return$ret; } publicfunctionformatUrlQuery($paraMap,$urlencode){ $buff=""; ksort($paraMap,SORT_STRING); foreach($paraMapas$k=>$v){ if(null!=$v&&"null"!=$v&&"sign"!=$k){ if($urlencode){ $v=urlencode($v); } $buff.=$k."=".$v."&"; } } $reqPar=''; if(strlen($buff)>0){ $reqPar=substr($buff,0,strlen($buff)-1); } return$reqPar; } publicfunctionformatPayUrlQuery($paraMap,$urlencode){ $buff=""; ksort($paraMap,SORT_STRING); foreach($paraMapas$k=>$v){ if($urlencode){ $v=urlencode($v); } $buff.=strtolower($k)."=".$v."&"; } $reqPar=''; if(strlen($buff)>0){ $reqPar=substr($buff,0,strlen($buff)-1); } return$reqPar; } /* *输出一级数组的xml格式 */ publicfunctionarrayToXml($arr){ $xml="<xml>"; foreach($arras$key=>$val){ if($key=='TimeStamp'||$key=='RetCode'){ $xml.="<".$key.">".$val."</".$key.">"; }else $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } $xml.="</xml>"; return$xml; } }
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。