VPS参考测评推荐
专注分享VPS主机优惠信息
衡天云优惠活动
华纳云最新优惠促销活动
jtti最新优惠促销活动
新罗云最新优惠促销活动

如何用PHP支付微信统一下单?(完整代码)(微信支付php)

locvps
主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作

H5:如果付款中出现“签名错误”怎么办?注意,调用微信“统一下单”接口,返回pre_pay_id后,用户需要重新签名,返回HTML;“支付目录未授权”,进入微信商户管理后台设置即可。

一.模型层,Wxpay.php

lt马鞭phpnamespace app \ common \ modelclass Wxpay { private $ appid = # 39wxe8 * * * * * d4 # 39;//微信微信官方账号appid private $ secret = # 3937c4 * * * * * * * 5f0 # 39;//微信微信官方账号appsecret private $ mchid = # 3913******02';//商家号private $ key = # 395363e * * * * * * 49e8 # 39;//支付密钥private $ sslcert _ path = # 39apiclient _ cert.pem # 39;//证书private $ sslkey _ path = # 39apiclient _ key.pem # 39;//证书的绝对路径是public function _ _ construct($ appid = # 39;',$ secret = # 39',$ mchid = # 39',$ key = # 39'){如果(!empty($ appid))$ this - gt;appid = $ appid如果(!empty($ secret))$ this - gt;secret = $ secret如果(!empty($ mchid))$ this - gt;mchid = $ mchid如果(!empty($ key))$ this - gt;key = $ key}/* * H5在微信中调用支付* @ paramstring $ OpenID:微信用户OpenID * @ paramstring $ out _ trade _ no:商家生成的订单号(唯一性)* @params int $total_fee:支付金额,单位*返回数组$ret:返回支付所需数据* */公共函数payforweixin ($ openid,$ out _ trade _ no,$total_fee,$ attach = # 39微信支付 # 39;,$ body = # 39微信支付 # 39;){//支付数据$ data[ # 39;openid # 39]= $ OpenID;$ data[ # 39;out _ trade _ no # 39]= $ out _ trade _ no;$ data[ # 39;total _ fee # 39]= $ total _ fee * 100;$ data[ # 39;spbill _ create _ ip # 39]= $ _ SERVER[ ;远程ADDR ;];$ data[ # 39;附上 # 39;]= $ attach;$ data[ # 39;正文 # 39;]= $ body;$ data[ # 39;appid # 39]= $ this - gt;appid$ data[ # 39;mch _ id # 39]= $ this - gt;mchid$ data[ # 39;nonce _ str # 39]= random(12);$ data[ # 39;trade _ type # 39]= ;JSAPI ;$ data[ # 39;notify _ url # 39]= ;http://****。com/home/wxpaynofiy/notify . html ;;$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt ltappid gt"。$ data[ # 39;appid # 39]." lt/appid gt; lt附加 gt"。$ data[ # 39;附上 # 39;]." lt/attach gt; ltbody gt"。$ data[ # 39;正文 # 39;]." lt/body gt; ltmch _ id gt"。$ data[ # 39;mch _ id # 39]." lt/mch _ id gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; lt通知_ url gt"。$ data[ # 39;notify _ url # 39]." lt/notify _ URL gt; ltopenid gt"。$ data[ # 39;openid # 39]." lt/OpenID gt; ltout _ trade _ no gt"。$ data[ # 39;out _ trade _ no # 39]." lt/out _ trade _ no gt; ltspbill _ create _ ip gt"。$ data[ # 39;spbill _ create _ ip # 39]." lt/sp bill _ create _ IP gt; lttotal _ fee gt"。$ data[ # 39;total _ fee # 39]." lt/total _ fee gt; lt贸易类型 gt"。$ data[ # 39;trade _ type # 39]." lt/trade _ type gt; lt符号 gt"。$符号。" lt/sign gt; lt/XML gt;";$ url = # 39https://api.mch.weixin.qq.com/pay/unifiedorder';$ result = $ this - gt;https_post($url,$ data XML);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39] == '成功 # 39; $ ret[ # 39;return _ msg # 39] == '好 # 39;){返回数组( # 39;appId # 39= gt$ this - gt;appid, # 39;时间戳 # 39;= gt时间(), # 39;非核心 # 39;= gt$ data[ # 39;nonce _ str # 39], '套餐 # 39;= gt'prepay _ id = # 39。$ ret[ # 39;prepay _ id # 39], 'signType # 39= gt'MD5 # 39, 'paySign # 39= gt$符号);} else { $ this - gt;错误日志( 微信支付失败,“,$ ret);返回null}}/* *微信二维码支付* @ paramstring $ OpenID:用户的OpenID * @ paramstring $ out _ trade _ no:商家订单号* @params number $total_fee:订单金额,单位*返回字符串$code_url:二维码url链接*/公共函数payforqrcode ($ out _ trade _ no,$total_fee,body = "魔盒CMS,$ attach = 微信支付”){//支付数据$ data[ # 39;out _ trade _ no # 39]= $ out _ trade _ no;$ data[ # 39;total _ fee # 39]= $ total _ fee * 100;$ data[ # 39;spbill _ create _ ip # 39]= $ _ SERVER[ ;远程ADDR ;];$ data[ # 39;附上 # 39;]= $ attach;$ data[ # 39;正文 # 39;]= $ body;$ data[ # 39;appid # 39]= $ this - gt;appid$ data[ # 39;mch _ id # 39]= $ this - gt;mchid$ data[ # 39;nonce _ str # 39]= random(12);$ data[ # 39;trade _ type # 39]= ;本地 ;$ data[ # 39;notify _ url # 39]= ;http://****。com/home/wxpaynofiy/notify . html ;;$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt ltappid gt"。$ data[ # 39;appid # 39]." lt/appid gt; lt附加 gt"。$ data[ # 39;附上 # 39;]." lt/attach gt; ltbody gt"。$ data[ # 39;正文 # 39;]." lt/body gt; ltmch _ id gt"。$ data[ # 39;mch _ id # 39]." lt/mch _ id gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; lt通知_ url gt"。$ data[ # 39;notify _ url # 39]." lt/notify _ URL gt; ltout _ trade _ no gt"。$ data[ # 39;out _ trade _ no # 39]." lt/out _ trade _ no gt; ltspbill _ create _ ip gt"。$ data[ # 39;spbill _ create _ ip # 39]." lt/sp bill _ create _ IP gt; lttotal _ fee gt"。$ data[ # 39;total _ fee # 39]." lt/total _ fee gt; lt贸易类型 gt"。$ data[ # 39;trade _ type # 39]." lt/trade _ type gt; lt符号 gt"。$符号。" lt/sign gt; lt/XML gt;";$ url = # 39https://api.mch.weixin.qq.com/pay/unifiedorder';$ result = $ this - gt;https_post($url,$ data XML);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39] == '成功 # 39; $ ret[ # 39;return _ msg # 39] == '好 # 39;){ return $ ret[ # 39;code _ url # 39];} else { $ this - gt;错误日志( 获取微信支付二维码失败,“,$ ret);返回null}}/* *订单查询* @ paramstring $ transaction _ ID:微信订单号* @ paramstring $ out _ trade _ no:商家订单号(从微信订单号中选择)* */公共函数find Order($ out _ trade _ no){ $ data[ # 39;appid # 39]= $ this - gt;appid$ data[ # 39;mch _ id # 39]= $ this - gt;mchid$ data[ # 39;nonce _ str # 39]= random(12);$ data[ # 39;out _ trade _ no # 39]= $ out _ trade _ no;$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt ltappid gt"。$ data[ # 39;appid # 39]." lt/appid gt; ltmch _ id gt"。$ data[ # 39;mch _ id # 39]." lt/mch _ id gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; ltout _ trade _ no gt"。$ data[ # 39;out _ trade _ no # 39]." lt/out _ trade _ no gt; lt符号 gt"。$符号。" lt/sign gt; lt/XML gt;";$ url = # 39https://api.mch.weixin.qq.com/pay/orderquery';$ result = $ this - gt;https_post($url,$ data XML);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39] == '成功 # 39; $ ret[ # 39;return _ msg # 39] == '好 # 39;){ return $ ret} else { $ this - gt;错误日志( 查询微信支付订单失败,“,$ ret);返回null}}/* *退款订单查询* @ paramstring $ transaction _ ID:微信订单号* @ paramstring $ out _ trade _ no:商家订单号(微信订单号中选一个)* */公共函数findRefundOrder($ out _ trade _ no){ $ data[ # 39;appid # 39]= $ this - gt;appid$ data[ # 39;mch _ id # 39]= $ this - gt;mchid$ data[ # 39;nonce _ str # 39]= random(12);$ data[ # 39;out _ trade _ no # 39]= $ out _ trade _ no;$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt ltappid gt"。$ data[ # 39;appid # 39]." lt/appid gt; ltmch _ id gt"。$ data[ # 39;mch _ id # 39]." lt/mch _ id gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; ltout _ trade _ no gt"。$ data[ # 39;out _ trade _ no # 39]." lt/out _ trade _ no gt; lt符号 gt"。$符号。" lt/sign gt; lt/XML gt;";$ url = # 39https://api.mch.weixin.qq.com/pay/refundquery';$ result = $ this - gt;https_post($url,$ data XML);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39] == '成功 # 39; $ ret[ # 39;return _ msg # 39] == '好 # 39;){ return $ ret} else { $ this - gt;错误日志( 查询微信支付退款单失败,“,$ ret);ret $ ret[ # 39;err _ code _ des # 39];}}/* *申请退款* @ paramstring $ out _ trade _ no:商家订单号* @ paramstring $ out _ refund _ no:商家退款号* @ paramint $ total _ fee:订单金额* @ paramint $ refund _ fee:退款金额* @ paramstring $ refund _ desc:退款原因* */公共函数退款($ out _ trade _ no,$ out _ refund _ no,$ total _ fee,$ refund _ fee,$ refund _ desc = # 39;退款 # 39;){ $ data[ # 39;appid # 39]= $ this - gt;appid$ data[ # 39;mch _ id # 39]= $ this - gt;mchid$ data[ # 39;nonce _ str # 39]= random(12);$ data[ # 39;out _ trade _ no # 39]= $ out _ trade _ no;$ data[ # 39;out _ refund _ no # 39]= $ out _ refund _ no;$ data[ # 39;total _ fee # 39]= $ total _ fee * 100;$ data[ # 39;退款_费用 # 39;]= $退款_费用* 100;$ data[ # 39;退款_ desc # 39;]= $退款_ desc;$ data[ # 39;notify _ url # 39]= ;http://*****。com/home/wxpaynofiy/refund . html ;;$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt ltappid gt"。$ data[ # 39;appid # 39]." lt/appid gt; ltmch _ id gt"。$ data[ # 39;mch _ id # 39]." lt/mch _ id gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; ltout _ trade _ no gt"。$ data[ # 39;out _ trade _ no # 39]." lt/out _ trade _ no gt; ltout _ refund _ no gt"。$ data[ # 39;out _ refund _ no # 39]." lt/out _ refund _ no gt; lttotal _ fee gt"。$ data[ # 39;total _ fee # 39]." lt/total _ fee gt; lt退款_费用 gt"。$ data[ # 39;退款_费用 # 39;]." lt/refund _ fee gt; lt退款_ desc gt;"。$ data[ # 39;退款_ desc # 39;]." lt/退款_ desc gt; lt通知_ url gt"。$ data[ # 39;notify _ url # 39]." lt/notify _ URL gt; lt符号 gt"。$符号。" lt/sign gt; lt/XML gt;";$ url = # 39https://api.mch.weixin.qq.com/secapi/pay/refund';$ result = $ this - gt;https_post($url,$dataXML,true);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39] == '成功 # 39; $ ret[ # 39;return _ msg # 39] == '好 # 39;){ return $ ret} else { $ this - gt;错误日志( 微信退款失败,“,$ ret);返回null}}/* *企业向用户支付变更* @ param string $ OpenID:user OpenID * @ param int $ total _ fee:支付金额,公司* @ paramstring $ out _ trade _ no:商家订单号* @ paramstring $ username:微信用户名(注意如果微信昵称为空支付会出错)* @ paramstring $ desc:支付描述* @ paramstring $ check _ name:是否检测用户名* */公共函数payforuser ($ openid,$ total _ fee,$ out _ trade _ no,$ username = # 39魔盒CMS # 39,$ desc = # 39;魔盒CMS付费用户 # 39;,$ check _ name = # 39NO _ CHECK # 39){ $ data[ # 39;金额 # 39;]= $ total _ fee * 100;$ data[ # 39;check _ name # 39]= $ check _ name;$ data[ # 39;desc # 39;]= $ desc;$ data[ # 39;mch _ appid # 39]= $ this - gt;appid$ data[ # 39;麦克希德 # 39;]= $ this - gt;mchid$ data[ # 39;nonce _ str # 39]= random(12);$ data[ # 39;openid # 39]= $ OpenID;$ data[ # 39;partner _ trade _ no # 39]= $ out _ trade _ no;$ data[ # 39;re _ user _ name # 39]= $ username;$ data[ # 39;spbill _ create _ ip # 39]= $ _ SERVER[ ;远程ADDR ;];$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt ltmch _ appid gt"。$ data[ # 39;mch _ appid # 39]." lt/mch _ appid gt; ltmchid gt"。$ data[ # 39;麦克希德 # 39;]." lt/MC hid gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; ltpartner _ trade _ no gt"。$ data[ # 39;partner _ trade _ no # 39]." lt/partner _ trade _ no gt; ltopenid gt"。$ data[ # 39;openid # 39]." lt/OpenID gt; ltcheck _ name gt"。$ data[ # 39;check _ name # 39]." lt/check _ name gt; ltre _ user _ name gt"。$ data[ # 39;re _ user _ name # 39]." lt/re _ user _ name gt; lt金额 gt"。$ data[ # 39;金额 # 39;]." lt/amount gt; ltdesc gt;"。$ data[ # 39;desc # 39;]." lt/desc gt; ltspbill _ create _ ip gt"。$ data[ # 39;spbill _ create _ ip # 39]." lt/sp bill _ create _ IP gt; lt符号 gt"。$符号。" lt/sign gt; lt/XML gt;";$ url = # 39https://API . mch . weixin . QQ . com/mmpaymkttransfers/promotion/transfers # 39;;$ result = $ this - gt;https_post($url,$dataXML,true);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39]=='成功 # 39; $ ret[ # 39;结果代码 # 39;] == '成功 # 39;){//支付成功返回商家订单号、微信订单号和微信支付成功时间$ result[ # 39;partner _ trade _ no # 39]= $ ret[ # 39;partner _ trade _ no # 39];$ result[ # 39;付款_否 # 39;]= $ ret[ # 39;付款_否 # 39;];$ result[ # 39;支付时间 # 39;]= $ ret[ # 39;支付时间 # 39;];返回$ ret} else { $ this - gt;错误日志( # 39;向用户付款失败 # 39;,$ ret);返回null}}/* *普通红包* @ paramstring $ out _ trade _ no:商家订单号* @ paramstring $ OpenID:收到红包的用户的OpenID * @ params int $ total _ fee:红包金额, Unit * @params int $total_num:红包发放总数* @ paramstring $ wishing:红包问候* @ paramstring $ act _ name:活动名称* @ paramstring $备注:备注* @ paramstring $ scene _ ID:scene value ID使用红包,红包金额大于200或小于1元时,必须传送。 PRODUCT_1:商品促销,PRODUCT_2:抽奖,PRODUCT_3:虚拟商品兑换,PRODUCT_4:企业内部福利,PRODUCT_5:渠道分销,PRODUCT_6:保险回馈,PRODUCT_7:抽奖奖励,PRODUCT_8:刮税奖励* */公共函数redPack($openid,total _ fee,out _ trade _ no,total _ num = 1,wishing = # 39感谢您在* * *平台购物 # 39;,$ act _ name = # 39* * *购物时给红包 # 39;,$ remark = # 39获得购物红包 # 39;){ $ data[ # 39;mch _ billno # 39]= $ out _ trade _ no;$ data[ # 39;mch _ id # 39]= $ this - gt;mchid$ data[ # 39;wxappid # 39]= $ this - gt;appid$ data[ # 39;send _ name # 39] = '发红包人的名字 # 39;;$ data[ # 39;re _ openid # 39]= $ OpenID;$ data[ # 39;total _ amount # 39]= $ total _ fee;$ data[ # 39;total _ num # 39]= $ total _ num;$ data[ # 39;许愿 # 39;]= $许愿;$ data[ # 39;client _ ip # 39]= $ _ SERVER[ ;远程ADDR ;];$ data[ # 39;act _ name # 39]= $ act _ name;$ data[ # 39;备注 # 39;]= $ remark;$ data[ # 39;nonce _ str # 39]= random(12);$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt lt符号 gt"。$符号。" lt/sign gt; ltmch _ billno gt"。$ data[ # 39;mch _ billno # 39]." lt/mch _ bill no gt; ltmch _ id gt"。$ data[ # 39;mch _ id # 39]." lt/mch _ id gt; ltwxappid gt"。$ data[ # 39;wxappid # 39]." lt/wx appid gt; ltsend _ name gt"。$ data[ # 39;send _ name # 39]." lt/send _ name gt; ltre _ openid gt"。$ data[ # 39;re _ openid # 39]." lt/re _ OpenID gt; lttotal_amount >"。$ data[ # 39;total _ amount # 39]." lt/total _ amount gt; lt总数 gt"。$ data[ # 39;total _ num # 39]." lt/total _ num gt; lt许愿 gt"。$ data[ # 39;许愿 # 39;]." lt/wishing gt; ltclient _ ip gt"。$ data[ # 39;client _ ip # 39]." lt/client _ IP gt; ltact _ name gt"。$ data[ # 39;act _ name # 39]." lt/act _ name gt; lt备注 gt"。$ data[ # 39;备注 # 39;]." lt/remark gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; lt/XML gt;";$ url = # 39https://API . mch . weixin . QQ . com/mmpaymkttransfers/sendredpack # 39;;$ result = $ this - gt;https_post($url,$dataXML,true);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39]=='成功 # 39; $ ret[ # 39;结果代码 # 39;] == '成功 # 39;){ return $ ret} else { $ this - gt;错误日志( # 39;发普通红包失败 # 39;,$ ret);ret $ ret[ # 39;err _ code _ des # 39];}}/* *裂变红包:一次可发一套红包。第一个收到它的用户是种子用户。种子用户收到一组红包中的一个,可以通过社交分享把剩下的红包给其他用户。*裂变红包充分利用了人际传播的优势。* @ paramstring $ out _ trade _ no:商家订单号* @ paramstring $ OpenID:收到红包的用户的OpenID * @ param int $ total _ fee:红包金额,单位* @params int $total_num:发出的红包总数* @ paramstring $ wishing:红包问候* @ paramstring $ act _ name:活动名称* @ paramstring $备注:备注* @ paramstring $ scene _ ID:场景值ID。红包的使用,当红包金额大于200或小于1元时,必须传输。PRODUCT_1:商品促销,PRODUCT_2:抽奖,PRODUCT_3:虚拟商品兑换,PRODUCT_4:企业内部福利,PRODUCT_5:渠道分销,PRODUCT_6:保险回馈,PRODUCT_7:抽奖奖励,PRODUCT_8:刮税奖励* */公共函数redPackGroup($openid,total _ fee,out _ trade _ no,total _ num,wishing = # 39感谢您来* * *购物 # 39;,$ act _ name = # 39* *购物时给红包 # 39;,$ remark = # 39获得购物红包 # 39;){ $ data[ # 39;mch _ billno # 39]= $ out _ trade _ no;$ data[ # 39;mch _ id # 39]= $ this - gt;mchid$ data[ # 39;wxappid # 39]= $ this - gt;appid$ data[ # 39;send _ name # 39] = '发红包人的名字 # 39;;$ data[ # 39;re _ openid # 39]= $ OpenID;$ data[ # 39;total _ amount # 39]= $ total _ fee;$ data[ # 39;amt _ type # 39] = 'ALL _ RAND # 39;//ALL_RAND— all random,商家指定的总金额和发出的红包总数,每个红包的金额会由微信支付$data随机计算[ # 39;total _ num # 39]= $ total _ num;$ data[ # 39;许愿 # 39;]= $许愿;$ data[ # 39;client _ ip # 39]= $ _ SERVER[ ;远程ADDR ;];$ data[ # 39;act _ name # 39]= $ act _ name;$ data[ # 39;备注 # 39;]= $ remark;$ data[ # 39;nonce _ str # 39]= random(12);$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt lt符号 gt"。$符号。" lt/sign gt; ltmch _ billno gt"。$ data[ # 39;mch _ billno # 39]." lt/mch _ bill no gt; ltmch _ id gt"。$ data[ # 39;mch _ id # 39]." lt/mch _ id gt; ltwxappid gt"。$ data[ # 39;wxappid # 39]." lt/wx appid gt; ltsend _ name gt"。$ data[ # 39;send _ name # 39]." lt/send _ name gt; ltre _ openid gt"。$ data[ # 39;re _ openid # 39]." lt/re _ OpenID gt; lttotal_amount >"。$ data[ # 39;total _ amount # 39]." lt/total _ amount gt; ltamt _ type gt"。$ data[ # 39;amt _ type # 39]." lt/AMT _ type gt; lt总数 gt"。$ data[ # 39;total _ num # 39]." lt/total _ num gt; lt许愿 gt"。$ data[ # 39;许愿 # 39;]." lt/wishing gt; ltclient _ ip gt"。$ data[ # 39;client _ ip # 39]." lt/client _ IP gt; ltact _ name gt"。$ data[ # 39;act _ name # 39]." lt/act _ name gt; lt备注 gt"。$ data[ # 39;备注 # 39;]." lt/remark gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; lt/XML gt;";$ url = # 39https://API . mch . weixin . QQ . com/mmpaymkttransfers/sendgroupredpack # 39;;$ result = $ this - gt;https_post($url,$dataXML,true);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39]=='成功 # 39; $ ret[ # 39;结果代码 # 39;] == '成功 # 39;){ return $ ret} else { $ this - gt;错误日志( # 39;未能发放裂变红包 # 39;,$ ret);ret $ ret[ # 39;err _ code _ des # 39];}}/* *查询红包记录* @ paramstring $ out _ trade _ no:商家订单号* */公共函数findredpack($ out _ trade _ no){ $ data[ # 39;mch _ billno # 39]= $ out _ trade _ no;$ data[ # 39;mch _ id # 39]= $ this - gt;mchid$ data[ # 39;appid # 39]= $ this - gt;appid$ data[ # 39;bill _ type # 39] = 'MCHT # 39;;//MCHT:通过商家订单号获取红包信息。$ data[ # 39;nonce _ str # 39]= random(12);$ sign = $ this - gt;get param($ data);$ dataXML = ltxml gt lt符号 gt"。$符号。" lt/sign gt; ltmch _ billno gt"。$ data[ # 39;mch _ billno # 39]." lt/mch _ bill no gt; ltmch _ id gt"。$ data[ # 39;mch _ id # 39]." lt/mch _ id gt; ltappid gt"。$ data[ # 39;appid # 39]." lt/appid gt; ltbill _ type gt"。$ data[ # 39;bill _ type # 39]." lt/bill _ type gt; ltnonce _ str gt"。$ data[ # 39;nonce _ str # 39]." lt/nonce _ str gt; lt/XML gt;";$ url = # 39https://API . mch . weixin . QQ . com/mmpaymkttransfers/gethbinfo # 39;;$ result = $ this - gt;https_post($url,$dataXML,true);$ ret = $ this - gt;xmlToArray($ result);if($ ret[ # 39;return _ code # 39]=='成功 # 39; $ ret[ # 39;结果代码 # 39;] == '成功 # 39;){ return $ ret} else { $ this - gt;错误日志( # 39;查询红包记录失败 # 39;,$ ret);ret $ ret[ # 39;err _ code _ des # 39];}}/* *获取用户微信的OpenID * */公共函数OpenID($ c = false){ if($ _ Get[ # 39;国家 # 39;]!= zgm ){ $t = $c?"snsapi _ userinfo : snsapi _ base ;$ URL = urlencode(get _ URL());$ url = https://open.weixin.qq.com/connect/oauth2/authorize? appid = ;。$ this - gt;appid。" redirect _ uri = 。$url。" 代码 scope = 。$ t. state = zgm #微信_重定向 ;回声 lthtml gt lt脚本 gtwindow . location . href = # 39;$ url # 39; lt/script gt; lt/html gt;";退出;} if($ _ GET[ # 39;代码 # 39;]){ $ URL = ;https://api.weixin.qq.com/sns/oauth2/access_token? appid = ;。$ this - gt;appid。" secret = 。$ this - gt;秘密。" code = 。$ _ GET[ # 39;代码 # 39;]." grant _ type = authorization _ code ;$ wx _ db = JSON _ decode($ this - gt;https _ get($ URL));if($ c){ $ URL _ 2 = ;https://api.weixin.qq.com/sns/userinfo?访问令牌= 。$ wx _ db- gt;访问令牌。" openid = 。$ wx _ db- gt;openid。" lang = zh _ CN ;$ db = JSON _ decode($ this - gt;https _ get($ URL _ 2));返回$ db} else { return $ wx _ db- gt;openid}}}/* *发起网络GET请求* @ paramstring $ URL:URL link */private function https _ GET $ URL { $ curl = curl _ init();curl_setopt($curl,CURLOPT_URL,$ URL);curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);curl_setopt($curl,CURLOPT_HEADER,FALSE);curl_setopt($curl,CURLOPT_TIMEOUT,60);if(curl _ errno($ curl)){ return # 39;Errno # 39。curl _ error($ curl);} else { $ result = curl _ exec($ curl);} curl _ close($ curl);返回$ result}//对参数进行排序生成MD5加密签名私有函数getparam ($ paramarray,$ isen code = false){ $ paramstr = # 39;';ksort($ param array);$ I = 0;foreach($ param array as $ key = gt;$ value){ if($ key = = # 39;签名 # 39;){继续;} if ($i == 0){ $paramStr。= '';}else{ $paramStr。= ' ';} $paramStr。= $key。'='。($isencode?urlencode($ value):$ value);++ $ I;} $stringSignTemp=$paramStr。" key = 。$ this - gt;关键;$ sign = strto upper(MD5($ stringSignTemp));return $ sign} //POST提交数据私有函数https _ post ($ URL,$ data,$ SSL = false){ $ ch = curl _ init();curl_setopt ( $ch,CURLOPT_URL,$ URL);curl_setopt ( $ch,CURLOPT_CUSTOMREQUEST, 帖子 );curl_setopt ( $ch,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt ( $ch,CURLOPT_SSL_VERIFYHOST,FALSE);if($ssl) { curl_setopt ( $ch,CURLOPT_SSLCERT,$ this - gt;SSL cert _ path);curl_setopt ( $ch,CURLOPT_SSLKEY,$ this - gt;SSL key _ path);} curl_setopt ( $ch,CURLOPT_FOLLOWLOCATION,1);curl_setopt ( $ch,CURLOPT_AUTOREFERER,1);curl_setopt ( $ch,CURLOPT_POSTFIELDS,$ data);curl_setopt ( $ch,CURLOPT_RETURNTRANSFER,true);$ result = curl _ exec($ ch);if(curl _ errno($ ch)){ return # 39;错误号: # 39;。curl _ error($ ch);} curl _ close($ ch);返回$ result}/* * XML to array * @ params XML $ XML:XML data * return array $ data:escaped array */private function xmlto array($ XML){ libxml _ disable _ entity _ loader(true);$ XML string = SimpleXML _ load _ string($ XML, # 39;SimpleXMLElement # 39,LIBXML _ NOCDATA);$ val = JSON _ decode(JSON _ encode($ XML string),true);返回$ val}/* * Log * @ paramstring $ msg:prompt语句* @ param array $ ret:error result */私有函数错误日志($ msg,$ ret){ file _ put _ contents(root _ path。# 39;runtime/error/wx pay . log # 39;, [ ;。日期( # 39;y-m-d H:I:s # 39;) ."] ;。$味精。", 。json_encode($ret)。PHP_EOL,FILE _ APPEND);}}二。控制器层,Test.php

lt马鞭phpnamespace app \ goods \ controller;使用app \ common \ model \ WxpayClass {/* *调用微信支付* 1。获取微信用户的openid*二。调用微信支付接口*三。生成H5 */public function wx(){ $ wx pay = new wx pay()中使用的签名内容;$ OpenID = $ wx pay - gt;OpenID();$ pay = $ wx pay - gt;payForWeixin($openid,date( # 39;YmdHis # 39).兰德(1,5), # 39;0.1');$ pay sign = strto upper(MD5( # 39;appId = # 39。$ pay[ # 39;appId # 39].' nonceStr = # 39。$ pay[ # 39;非核心 # 39;].' package = # 39。$ pay[ # 39;套餐 # 39;].' MD5 时间戳= # 39;。$ pay[ # 39;时间戳 # 39;].' key = 536 * * * * * 9e8 # 39));$ H5 = array( # 39;appId # 39= gt$ pay[ # 39;appId # 39], '时间戳 # 39;= gt$ pay[ # 39;时间戳 # 39;], '非核心 # 39;= gt$ pay[ # 39;非核心 # 39;], '套餐 # 39;= gt$ pay[ # 39;套餐 # 39;], 'signType # 39= gt$ pay[ # 39;signType # 39], 'paySign # 39= gt$ paySign);$ this - gt;分配( # 39;wxpay # 39,$ H5);return view();}}三。用HTML调用微信支付,wx.html

lt!DOCTYPE html gt lthtml lang = 恩 gt lthead gt ltmeta charset = ut F-8 ; gt lttitle gt订单示例

lt马鞭phpnamespace app \ home \ controller;class Wxpaynofiy { public function notify(){ $ XML = isset($ GLOBALS[ ;HTTP _ RAW _ POST _ DATA ]) ?$ GLOBALS[ # 39;HTTP _ RAW _ POST _ DATA # 39]:file _ get _ contents( ;PHP://input ;);$ data = xmlto array($ XML);if($data[ # 39;return _ code # 39] == '成功 # 39; $ data[ # 39;结果代码 # 39;] == '成功 # 39;){ if($ this - gt;checkSign($ data)){ $ transaction _ id = $ data[ # 39;交易标识 # 39;];//微信支付订单号$ out _ trade _ no = $ data[ # 39;out _ trade _ no # 39];//商家订单号$ this ->;错误日志( # 39;微信支付返回结果,微信支付订单号: # 39;。$transaction_id。',商家订单号: # 39;。$out_trade_no,[]);} else { $ this - gt;错误日志( # 39;微信支付返回结果签名验证失败 # 39;,$ data);} } else { $ this - gt;错误日志( # 39;微信支付返回结果 # 39;,$ data);} echo # 39 ltxml gt ltreturn _ code gt lt![CDATA[成功]] gt; lt/return _ code gt; ltreturn _ msg gt lt![CDATA[OK]] gt; lt/return _ msg gt; lt/XML gt;';}/* *验证签名* @params array $result:微信支付成功返回的结果数组* return bool $ret: success true,failure false * */私有函数check sign(array $ data){ $ str = # 39;';ksort($ data);foreach($ data as $ k = gt;$v) { if($k!= '标志 # 39;)$str。= $ k. # 39='。$ v. # 39 ';} $temp = $str。'key = 5363 * * * * * * 49e8 # 39;//key:商户支付密钥$ sign = strtop(MD5($ temp));return $ sign = = $ data[ # 39;标志 # 39;] ?真:假;}私有函数errorLog($msg,$ ret){ file _ put _ contents(ROOT _ PATH。'runtime/error/wxpaynofiy . log # 39;, [ ;。日期( # 39;y-m-d H:I:s # 39;) ."] ;。$味精。", 。json_encode($ret)。PHP_EOL,FILE _ APPEND);}}相关建议:

微信支付和支付宝支付整合的PHP实现

PHP如何实现微信支付?

视频教程:php微信界面开发实用项目Chatbot+微信支付以上是微信支付的统一顺序。如何用PHP实现?(完整代码)详情请关注主机参考其他相关文章了解更多!

这几篇文章你可能也喜欢:

本文由主机参考刊发,转载请注明:如何用PHP支付微信统一下单?(完整代码)(微信支付php) https://zhujicankao.com/79872.html

【腾讯云】领8888元采购礼包,抢爆款云服务器 每月 9元起,个人开发者加享折上折!
打赏
转载请注明原文链接:主机参考 » 如何用PHP支付微信统一下单?(完整代码)(微信支付php)
主机参考仅做资料收集,不对商家任何信息及交易做信用担保,购买前请注意风险,有交易纠纷请自行解决!请查阅:特别声明

评论 抢沙发

评论前必须登录!