<强>微信小程序与php实现微信支付的简单实例强>
小程序访问地址:
<强> payfee.PHP: 强>
包括“WeixinPay.php”; $ appid=? $ openid=$ _GET [' id '); $ mch_id=? 美元关键="; out_trade_no=mch_id美元。时间(); $ total_fee=$ _GET['费'); 如果(空(total_fee美元))//押金 { 身体美元="充值押金”; $ total_fee=floatval (99 * 100); } 其他{ 身体美元="充值余额”; $ total_fee=floatval (total_fee * 100美元); } 美元weixinpay=new weixinpay (appid美元,美元openid, mch_id美元,美元关键,out_trade_no美元,美元的身体,total_fee美元); 返回=weixinpay→美元支付(); 回声json_encode(美元返回); >之前<强> WeixinPay。php: 强>
& lt; & # 63; php/* *小程序微信支付 */类WeixinPay { 保护appid美元; 保护mch_id美元; 保护关键美元; 保护openid美元; 保护out_trade_no美元; 保护身体美元; 保护total_fee美元; 函数__construct (appid美元,美元openid, mch_id美元,美元关键,out_trade_no美元,美元的身体,total_fee美元){ $ this→appid=$ appid; $ this→openid=$ openid; $ this→mch_id=$ mch_id; $ this→键=$键; $ this→out_trade_no=$ out_trade_no; $ this→身体=$身体; $ this→total_fee=$ total_fee; } 公共函数支付(){//统一下单接口 返回=这→美元weixinapp (); 返回美元返回; }//统一下单接口 私有函数unifiedorder () { $ url=' https://api.mch.weixin.qq.com/pay/unifiedorder '; $=参数数组( “appid”=比;$ this→appid,//小程序ID “mch_id”=比;$ this→mch_id,//商户号 “nonce_str”=比;$ this→createNoncestr()//随机字符串//吧硖濉?比;'测试',//商品描述 “身体”=比;$ this→身体,//皁ut_trade_no”=比;' 2015450806125348 ',//商户订单号 “out_trade_no”=比;$ this→out_trade_no,//皌otal_fee”=比;floatval(0.01 * 100)//总金额单位分 “total_fee”=比;$ this→total_fee,//皊pbill_create_ip”=比;$ _SERVER [' REMOTE_ADDR '],//终端IP “spbill_create_ip”=比;' 192.168.0.161 ',//终端IP “notify_url”=比;' http://www.weixin.qq.com/wxpay/pay.php ',//通知地址确保外网能正常访问 openid的=比;$ this→openid,//用户id “trade_type”=比;“JSAPI”//交易类型 );//统一下单签名 参数(“标志”)=美元这→getSign(参数); $ xmlData=https://www.yisu.com/zixun/$ this -> arrayToXml(参数); $=$ this ->返回xmlToArray ($ this -> postXmlCurl (xmlData, url,美元60)); 返回美元返回; } 私有静态函数postXmlCurl (xml、url、美元第二=30美元) { $ ch=curl_init ();//设置超时 curl_setopt ($ ch CURLOPT_TIMEOUT第二美元); curl_setopt ($ ch CURLOPT_URL $ url); curl_setopt ($ ch CURLOPT_SSL_VERIFYPEER假); curl_setopt ($ ch CURLOPT_SSL_VERIFYHOST假);//严格校验//设置头 curl_setopt ($ ch CURLOPT_HEADER假);//要求结果为字符串且输出到屏幕上 curl_setopt ($ ch CURLOPT_RETURNTRANSFER真实);//后提交方式 curl_setopt ($ ch CURLOPT_POST真实); curl_setopt ($ ch CURLOPT_POSTFIELDS $ xml); curl_setopt (ch美元CURLOPT_CONNECTTIMEOUT 20); curl_setopt (ch美元CURLOPT_TIMEOUT 40); set_time_limit (0);//运行旋度 美元$ data=curl_exec (ch);//返回结果 如果(元数据){ curl_close (ch); 返回元数据; 其他}{ (错误=curl_errno美元ch); curl_close (ch); 把新WxPayException(“旋度出错,错误码:美元错误”); } }//数组转换成xml 私有函数arrayToXml (arr) { $ xml=? lt; root>”; foreach (arr美元关键=美元比;$ val) { 如果(is_array (val)美元){ $ xml。=? lt;“。美元的关键。“在”。arrayToXml (val美元)。“& lt;/薄C涝墓丶!霸凇? 其他}{ $ xml。=? lt;“。美元的关键。“在”。val美元。“& lt;/薄C涝墓丶!霸凇? } } $ xml。=? lt;/root>”; 返回$ xml; }//xml转换成数组 私有函数xmlToArray ($ xml) {//禁止引用外部xml实体 libxml_disable_entity_loader(真正的); $ xmlstring=simplexml_load_string ($ xml的SimpleXMLElement LIBXML_NOCDATA); $ val=json_decode (json_encode (xmlstring美元),真正的); 返回val美元; }//微信小程序接口 私有函数weixinapp () {//统一下单接口 unifiedorder=这→美元unifiedorder ();//print_r ($ unifiedorder); $=参数数组( “appId”=比;$ this→appid,//小程序ID “时间戳”=比;”。时间()。”//时间戳 “nonceStr”=比;$ this→createNoncestr()//随机串 “包”=比;“prepay_id=薄? unifiedorder [' prepay_id ']//数据包 “signType”=比;MD5的//签名方式 );//签名 参数[' paySign ']=美元这→getSign(参数); 返回参数美元; }//作用:产生随机字符串,不长于32位 私有函数createNoncestr(长度=32美元){ $字符=癮bcdefghijklmnopqrstuvwxyz0123456789”; 美元str=" "; ($ i=0;美元我& lt;美元的长度;$我+ +){ str美元。=substr($字符,mt_rand (0, strlen($字符)- 1),1); } 返回str美元; }//作用:生成签名 私有函数getSign (Obj美元){ foreach (Obj为k=美元比;$ v) { 参数($ k)=v美元; }//签名步骤一:按字典序排序参数 ksort(参数); 字符串=这→美元formatBizQueryParaMap(美元参数,假);//签名步骤二:在字符串后加入关键 字符串=美元字符串。”,关键="。$ this→键;//签名步骤三:MD5加密 $ String=md5 ($ String);//签名步骤四:所有字符转为大写 $ result_=strtoupper ($ String); 返回result_美元; }///作用:格式化参数,签名过程需要使用 私有函数formatBizQueryParaMap (paraMap, urlencode美元){ 浅黄色美元=" "; ksort ($ paraMap); foreach ($ paraMap k=美元比;$ v) { 如果$ urlencode () { v (v=urlencode美元); } 迷。=k美元。“=薄C涝獀。“和”; } reqPar美元; 如果(strlen(浅黄色)美元比;0){ 美元$ reqPar=substr(浅黄色0 strlen(浅黄色)美元- 1); } 返回reqPar美元; } }微信小程序与php实现微信支付的简单实例