| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
支付微信V3版小程序支付php簽名,檢查簽名,分享數據解密代碼。
支付微信v3 php解密代碼
數據解密需要鈉擴展,并且需要安裝大多數php版本。
證書的序列號可以在https://myssl.com/cert_decode.html.這里查看
我用的是PHP 7.4版。
直接裝載代碼:
//微信原生支付類Wxpay{ /* *支付(小程序支付)* @param type $sn訂單號* @param type $money金額* @param type $openid用戶小程序openid * @return type */公共靜態函數getPayParam($sn,$money,$ OpenID){ $ URL = # 39;https://API . mch . weixin . QQ . com/v3/pay/transactions/jsapi # 39;;$ notify _ URL = URL( # 39;/API/weixin/notify # 39;);$ data =[];$ data[ # 39;appid # 39] = Action::config(CONFIG_WXXCX, # 39;app _ id # 39);$ data[ # 39;麥克希德 # 39;] = Action::config(CONFIG_WXXCX, # 39;麥克希德 # 39;);//商家號$ data[ # 39;描述 # 39;] = 'xxx # 39;//描述?$ data[ # 39;out _ trade _ no # 39]= $ sn;//商戶系統內部訂單號為$ data[ # 39;time _ expire # 39] =日期( # 39;y-m-d # 39;) .'T # 39。日期( # 39;h:I:s # 39;,(time() + 1800))。'+08:00';//訂單到期時間為2018-06-08t 10:34:56+08:00 $ data[ # 39;notify _ url # 39]= $ notify _ URL;//異步通知接口地址$ data[ # 39;金額 # 39;] = ['總計 # 39;= gt$money * 100, # 39;貨幣 # 39;= gt'CNY # 39;];//Amount $ data[ # 39;付款人 # 39;] = ['openid # 39= gt$ OpenID];//user $ re = self:: wxcurl ($ url,$ data, # 39;郵政 # 39;);如果(!isset($re[ # 39;prepay _ id # 39])){ API _ fail( # 39;參數獲取失敗 # 39;);} $ result =[];$ result[ # 39;appId # 39] = Action::config(CONFIG_WXXCX, # 39;app _ id # 39);$ result[ # 39;時間戳 # 39;]=(string)time();$ result[ # 39;非核心 # 39;]= uniqid();$ result[ # 39;套餐 # 39;] = 'prepay _ id = # 39。$ re[ # 39;prepay _ id # 39];$ result[ # 39;signType # 39] = 'RSA # 39;$ result[ # 39;paySign # 39]= self::getPaySign($ result);返回$ result}/* * *查詢順序* @ param type $ sn */公共靜態函數select ($ sn,$ return = false){ $ mch id = action::config(config _ wxxxx, # 39;麥克希德 # 39;);//商品編號$ url = # 39https://API . mch . weixin . QQ . com/v3/pay/transactions/out -trade -no/ # 39;。$sn。'?mchid = # 39。$ mchid$re = self::wxCurl($url,[], # 39;獲取 # 39;);if($ return){ return $ re;} if(isset($re[ # 39;trade _ state # 39]) ; $ re[ # 39;trade _ state # 39] == '成功 # 39;){返回true}返回false}/* * * Close order * @ param type $ sn */public static function Close($ sn){ $ mch ID = action::config(config _ wxxxx, # 39;麥克希德 # 39;);//商品編號$ url = # 39https://API . mch . weixin . QQ . com/v3/pay/transactions/out -trade -no/ # 39;。$sn。'/close # 39;;$re = self::wxCurl($url,[ # 39;麥克希德 # 39;= gt$mchid], # 39;郵政 # 39;);返回true}/* * *退款* @ param type $ sn */公共靜態函數退款($ order _ sn,$ refund _ sn,$ total,$ refund,$ msg = # 39退款 # 39;){ $ url = # 39https://API . mch . weixin . QQ . com/v3/退款/國內/退款 # 39;;$ data =[];$ data[ # 39;notify _ url # 39]= URL( # 39;ag/weixin/notify _ refund # 39;);$ data[ # 39;out _ trade _ no # 39]= $ order _ sn;//訂單號$ data[ # 39;out _ refund _ no # 39]= $ refund _ sn;//退款號碼$ data[ # 39;原因 # 39;]= $ msg;$ data[ # 39;金額 # 39;]=['退款 # 39;= gt$退款*100, # 39;總計 # 39;= gt總計$ 100, # 39;貨幣 # 39;= gt'CNY # 39;];$re = self::wxCurl($url,$data, # 39;郵政 # 39;);return $ re}//請求公共靜態函數WXC URL ($ URL,$ data = [],$ method = # 39獲取 # 39;){ $ Authorization = self::get sigre($ URL,$data,$ method);$ header =[ # 39;content -類型:application/JSON # 39;, '接受:應用/JSON # 39;, 'user -代理:Mozilla/5.0(Windows NT 10.0;Win64x64) AppleWebKit/537.36 (KHTML,像壁虎一樣)Chrome/89 . 0 . 4389 . 90 Safari/537.36 Edg/89 . 0 . 774 . 63 # 39;, '授權: # 39;。$授權];$redata = $data?JSON _ encode($ data): # 39;';$res = reCurl($url,$redata,$ header);返回$res?json_decode($res,true):[];}//后端請求簽名公共靜態函數get sign ($ URL,$ data,$ method = # 39獲取 # 39;){ $ URL _ parts = parse _ URL($ URL);$ canonical _ URL =($ URL _ parts[ # 39;路徑 # 39;] .(!empty($URL _ parts[ # 39;查詢 # 39;]) ?"?$ { URL _ parts[ # 39;查詢 # 39;]} ;: "));$ http _ method = $ method$ timest = time();$ nonce = uni qid();$body = $data?JSON _ encode($ data): # 39;';$ MC hid = Action::CONFIG(CONFIG _ WXXCX, # 39;麥克希德 # 39;);//商家ID $ serial _ no = action::config(config _ wxxxx, # 39;序列號 # 39;);//證書號$ private _ key = self::get private key(base _ path。# 39;cert/API client _ key . PEM # 39;);//商家私鑰$message = $http_method。" \ n 。$canonical_url。"\ n 。$時間戳。"\ n 。$nonce。"\ n 。$body。"\ n ;openssl_sign($message,$raw_sign,$private_key, # 39;sha256WithRSAEncryption # 39);$ sign = base64 _ encode($ raw _ sign);$ token = sprintf( # 39;mchid = % s ,nonce _ str = % s ,時間戳= % d ,序列號= % s ,簽名= % s ',$mchid,$nonce,$timest,$serial_no,$ sign);return # 39微信支付2-sha 256-RSA 2048 # 39;。$ token}//前端小程序簽名公共靜態函數getpaysign($ result){ $ private _ key = self::get private key(base _ path。# 39;cert/API client _ key . PEM # 39;);//商家私鑰$ message = $ result[ # 39;appId # 39] ."\ n 。$ result[ # 39;時間戳 # 39;] ."\ n 。$ result[ # 39;非核心 # 39;] ."\ n 。$ result[ # 39;套餐 # 39;] ."\ n ;openssl_sign($message,$raw_sign,$private_key, # 39;sha256WithRSAEncryption # 39);$ sign = base64 _ encode($ raw _ sign);return $ sign}//驗證簽名公共靜態函數check sign(){ $ header = context::get( # 39;標題 # 39;);$ serial _ no = $ header[ # 39;微信支付-連載 # 39;] ??'';//微信平臺序列號$ timeSt = $ header[ # 39;微信支付-時間戳 # 39;] ??'';$ nonce = $ header[ # 39;微信支付-nonce # 39;] ??'';$ body = Context::get( # 39;raw # 39);$ wx _ sign = $ header[ # 39;微信支付-簽名 # 39;] ??'';$ wx _ serial _ no = Action::CONFIG(CONFIG _ WXXCX, # 39;wx _ serial _ no # 39);//保存的序列號如果(!$serial_no || $wx_serial_no!= $ serial _ no){ \ SFF \ Log::write( # 39;過期簽名 # 39;);返回false} $message = $timeSt。"\ n 。$nonce。"\ n 。$body。"\ n ;$ wx _ sign = base64 _ decode($ wx _ sign);$ public _ key = self::get public key(BASE _ PATH。'cert/wx _ public _ cert . PEM # 39;);//平臺公鑰$ res = openssl _ verify ($ message,$ wx _ sign,$ public _ key,OpenSSL _ algo _ sha 256);if ($res == 1) {返回true} \ SFF \ Log::write( # 39;檢查失敗 # 39;);返回false}//獲取私鑰公共靜態函數獲取私鑰($ file path){ return OpenSSL _ Get _ private key(file _ Get _ contents($ file path));}//獲取公鑰公共靜態函數獲取公鑰($ file path){ return OpenSSL _ pkey _ Get _ public(file _ Get _ contents($ file path));}//加密數據公共靜態函數get encrypt ($ str) {//$ str是要加密的字符串$ public _ key _ path = base _ path。# 39;cert/wx _ public _ cert . PEM # 39;;//'平臺證書路徑 # 39;;$ public _ key = file _ get _ contents($ public _ key _ path);$ encrypted = # 39';if(OpenSSL _ public _ encrypted($ str,$ encrypted,$ public _ key,OpenSSL _ pkcs1 _ oaep _ padding)){//base64 code $ sign = base64 _ encrypted($ encrypted);} else {拋出新的異常( # 39;加密失敗 # 39;);} return $ sign}//解密數據公共靜態函數解密tostring ($ cipher text,$ associated data,$ non centr){ $ AES key = action::config(config _ wxxxx, # 39;mch _ keyv3 # 39);//商家apiv3 key解密$ str = base64 _ decode($密文);if(strlen($ str) lt;= 16){ return # 39;';}//ext-鈉(默認安裝在 gt= PHP 7.2)return \ na _ crypto _ aead _ AES 256 GCM _ decrypt($ str,$associatedData,$ nonceStr,$ AES key);}//下載平臺證書公共靜態函數down cert(){ $ URL = # 39;https://api.mch.weixin.qq.com/v3/certificates';$re = self::wxCurl($url,[], # 39;獲取 # 39;);如果(!isset($re[ # 39;數據 # 39;])){ API _ fail( # 39;未能獲得證書 # 39;);} $密文= $ re[ # 39;數據 # 39;][0]['encrypt _ certificate # 39]['密文 # 39;];$ associated data = $ re[ # 39;數據 # 39;][0]['encrypt _ certificate # 39]['associated _ data # 39];$ non castr = $ re[ # 39;數據 # 39;][0]['encrypt _ certificate # 39]['nonce # 39];$ data = self::decryptToString($密文,$associatedData,$ non castr);如果(!$ data){ API _ fail( # 39;無法獲得證書解密 # 39;);}文件_上傳_內容(基本路徑。'/cert/wx _ public _ cert . PEM # 39;,$ data);返回$ data}}以上是微信支付php版本v3的解密代碼分享詳情。更多請關注主機參考其他相關文章!
這幾篇文章你可能也喜歡:
本文由主機參考刊發,轉載請注明:分享微信支付v3 php解密代碼 http://m.k2270.com/73467.html
主機參考

















評論前必須登錄!
注冊