| 主機參考: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實現?(完整代碼)詳情請關注主機參考其他相關文章了解更多!
這幾篇文章你可能也喜歡:
- 您確定該服務是否破壞了? 使用Zabbix5電子郵件警報(Zabbix電子郵件警報原理)
- 在Tea Restaurant Mini計劃開始一個月后,雙重表演的秘訣是揭示的! (茶餐廳用餐)
- 如何使用迷你程序開發來增強移動應用程序(實施視頻程序功能)
- 為什么選擇Wydian迷你計劃來開始您的電子商務業務? (您為什么不能在Waidian上搜索迷你程序?)
- 如何制定一個微信微信計劃的優勢是什么?
本文由主機參考刊發,轉載請注明:如何用PHP支付微信統一下單?(完整代碼)(微信支付php) http://m.k2270.com/79872.html
主機參考

















評論前必須登錄!
注冊