当前位置: 首页 > 后端技术 > PHP

一个PHP文件处理微信支付系列的原始支付(扫码支付)

时间:2023-03-29 14:38:09 PHP

网上很多PHP微信扫码支付接入教程比较复杂,需要配置和导入较多的文件。整理后给它一个单文件版本,只有200行代码,希望能给大家想接入微信扫码支付带来一些帮助和参考。直接运行文件,得到支付二维码图片。注意事项:1、该文件需要放在支付授权目录下,可以在微信支付商户平台->产品中心->开发配置中设置。2、如果签名有误,可以通过微信支付签名验证工具进行验证:https://pay.weixin.qq.com/wik...代码如下:createJsBizPackage($payAmount,$outTradeNo,$orderName,$notifyUrl,$payTime);//生成二维码$url='http://qr.liantu.com/api.php?text='.$arr['code_url'];echo"";classWxpayService{protected$mchid;受保护的$appid;受保护的$apiKey;公共函数__construct($mchid,$appid,$key){$this->mchid=$mchid;$这个->appid=$appid;$this->apiKey=$key;}/***发起订单*@paramfloat$totalFee收取的总手续费的单位*@paramstring$outTradeNo唯一订单号*@paramstring$orderName订单名称*@paramstring$notifyUrl支付结果通知urlwithout问号*@paramstring$timestamp订单发起时间*@returnarray*/publicfunctioncreateJsBizPackage($totalFee,$outTradeNo,$orderName,$notifyUrl,$timestamp){$config=array('mch_id'=>$this->mchid,'appid'=>$this->appid,'key'=>$this->apiKey,);$orderName=iconv('GBK','UTF-8',$orderName);$unified=array('appid'=>$config['appid'],'attach'=>'pay',//业务数据包,原样返回,如果填写中文,注意转换为utf-8'body'=>$orderName,'mch_id'=>$config['mch_id'],'nonce_str'=>self::createNonceStr(),'notify_url'=>$notifyUrl,'out_trade_no'=>$outTradeNo,'spbill_create_ip'=>'127.0.0.1','total_fee'=>intval($totalFee*100),//单位转为分'trade_type'=>'NATIVE',);$unified['sign']=self::getSign($unified,$config['key']);$responseXml=self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder',self::arrayToXml($unified));$unifiedOrder=simplexml_load_string($responseXml,'SimpleXMLElement',LIBXML_NOCDATA);if($unifiedOrder===false){die('解析xml错误');}if($unifiedOrder->return_code!='SUCCESS'){die($unifiedOrder->return_msg);}if($unifiedOrder->result_code!='SUCCESS'){die($unifiedOrder->err_code);}$codeUrl=(array)($unifiedOrder->code_url);if(!$codeUrl[0])exit('getcode_urlerror');$arr=array("appId"=>$config['appid'],"timeStamp"=>$timestamp,"nonceStr"=>self::createNonceStr(),"package"=>"prepay_id=".$unifiedOrder->prepay_id,"signType"=>'MD5',"code_url"=>$codeUrl[0],);$arr['paySign']=self::getSign($arr,$config['key']);返回$arr;}publicfunctionnotify(){$config=array('mch_id'=>$this->mchid,'appid'=>$this->appid,'key'=>$this->apiKey,);$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];$postObj=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);if($postObj===false){die('解析xml错误');}if($postObj->return_code!='SUCCESS'){die($postObj->return_msg);}if($postObj->result_code!='SUCCESS'){die($postObj->err_code);}$arr=(数组)$postObj;取消设置($arr['sign']);if(self::getSign($arr,$config['key'])==$postObj->sign){echo'';返回$postObj;}}/***curlget**@paramstring$url*@paramarray$options*@returnmixed*/publicstaticfunctioncurlGet($url='',$options=array()){$ch=curl_init($网址);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_TIMEOUT,30);如果(!empty($options)){curl_setopt_array($ch,$options);}//https请求验证书和主机curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);$data=curl_exec($ch);curl_close($ch);返回$数据;}公共静态函数curlPost($url='',$postData='',$options=array()){if(is_array($postData)){$postData=http_build_query($postData);}$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);curl_setopt($ch,CURLOPT_TIMEOUT,30);//设置curl允许执行的最大秒数if(!empty($options)){curl_setopt_array($ch,$options);}//https请求不验证证书和主机curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);$data=curl_exec($ch);curl_close($ch);返回$数据;}publicstaticfunctioncreateNonceStr($length=16){$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW3;57str'=for($i=0;$i<$length;$i++){$str.=substr($chars,mt_rand(0,strlen($chars)-1),1);}返回$str;}publicstaticfunctionarrayToXml($arr){$xml="";foreach($arras$key=>$val){if(is_numeric($val)){$xml.="<".$键。“>”。$瓦尔。“”;}否则$xml.="<"。$键。">”;}$xml.="";返回$xml;}/***获取签名*/publicstaticfunctiongetSign($params,$key){ksort($params,SORT_STRING);$unSignParaString=self::formatQueryParaMap($params,false);$signStr=strtoupper(md5($unSignParaString."&key=".$key));返回$signStr;}受保护的静态函数formatQueryParaMap($paraMap,$urlEncode=false){$buff="";ksort($paraMap);foreach($paraMapas$k=>$v){if(null!=$v&&"null"!=$v){if($urlEncode){$v=urlencode($v);}$buff.=$k.“=”。$v。"&";}}$reqPar='';如果(strlen($buff)>0){$reqPar=substr($buff,0,strlen($buff)-1);}返回$reqPar;}}github下载地址:https://github.com/dedemao/we...