日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

微信JSAPI支付v3流程(uniapp和node版)

發布時間:2023/12/31 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信JSAPI支付v3流程(uniapp和node版) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、微信JSAPI支付

請提前準備好接入前的準備文檔獲取相關的配置數據,否則下面需要的數據你可能會比較懵!
并且需要提前了解微信JSAPI支付文檔

二、獲取用戶openid

獲取openid方法例子

三、h5調起支付

1.第一種通過WeixinJSBridge調起微信支付服務

參數獲取請看本文JSAPI支付簽名

WeixinJSBridge.invoke('getBrandWCPayRequest', {appId:'xxxxxxxx',//公眾號ID,由商戶傳入timeStamp:'xxxxxxxx',//時間戳,自1970年以來的秒數nonceStr:'xxxxxxxx',//隨機串package:'xxxxxxxx', // 統一支付接口返回的prepay_id參數值signType:"RSA",//微信簽名方式:paySign:'xxxxxxxx',//微信簽名},(res) =>{if (res.err_msg == "get_brand_wcpay_request:ok") {//支付成功}} );

2.第二種通過js-sdk中的wx.chooseWXPay調起微信支付服務

首先需要閱讀js-sdk文檔,js-sdk文檔
通過js-sdk獲取支付的能力

npm install weixin-js-sdk

下文中的getJssdk獲取請看本文JS-SDK權限簽名的方法

import wx from 'weixin-js-sdk'; import {getJssdk} from "@/api/wx.js"; export const setJsSdk = async ()=>{let params = {url:location.href.split('#')[0]}//JS-SDK使用權限簽名let res = await getJssdk(params);let data = res.data;wx.config({debug: false,appId: appid, // 必填,公眾號的唯一標識jsApiList:['chooseWXPay',//發起微信支付],timestamp:data.timestamp,//生成簽名的時間戳nonceStr:data.nonceStr,//生成簽名的隨機串signature:data.signature,//簽名});wx.error(e => {console.log('wx sdk errors:', e);}); }

調用wx.chooseWXPay進行支付調起
參數獲取請看本文JSAPI支付簽名

wx.chooseWXPay({timestamp: xxxxxxxx, // 支付簽名時間戳,注意微信jssdk中的所有使用timestamp字段均為小寫。但最新版的支付后臺生成簽名使用的timeStamp字段名需大寫其中的S字符nonceStr: 'xxxxxxxx', // 支付簽名隨機串,不長于 32 位package: 'xxxxxxxx', // 統一支付接口返回的prepay_id參數值,提交格式如:prepay_id=\*\*\*)signType: 'xxxxxxxx', // 微信支付V3的傳入RSA,微信支付V2的傳入格式與V2統一下單的簽名格式保持一致paySign: 'xxxxxxxx', // 支付簽名success: function (res) {// 支付成功后的回調函數} });

四、node對接對應的相關接口

1.JS-SDK權限簽名

對應上面目錄三-2的getJssdk接口使用
需提前準備
appid:appid憑證
appSecret:密鑰

const crypto = require("crypto"); const axios = require("axios"); const appid = "xxxxxxxx"; const appSecret = 'xxxxxxxx';//獲取js-sdk參數 const getJssdk = (params)=>{//----------------------------------------------------------------------------- // 這兩個接口獲取的臨時數據必須進行定時任務每個小時更新一次緩存起來 // 由于獲取jsapi_ticket的api調用次數非常有限,頻繁刷新jsapi_ticket會導致api調用受限,影響自身業務,開發者必須在自己的服務全局緩存jsapi_ticket 。//獲取access_tokenlet result1 = await axios(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}`);let access_token = result1.data.access_token;//獲取jsapi_ticket 是公眾號用于調用微信JS接口的臨時票據let result2 = await axios(`https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`);let jsapi_ticket = result2.data.ticket; //-----------------------------------------------------------------------------let timestamp = new Date().getTime();//時間戳let url = params.url;//當前網頁的URL,不包含#及其后面部分let nonceStr = 'xxxxxxxxxxxxxxxxxxxx';//隨機字符串let string1 = `jsapi_ticket=${jsapi_ticket}&noncestr=${nonceStr}&timestamp=${timestamp}&url=${url}`;let signature = jiamiSha1(string1);//sha1加密//返回數據給客戶端return {code:0,message:'',data:{timestamp,nonceStr,signature}} } //sha1加密 function jiamiSha1(str){// 需要加密的字符串let sf = crypto.createHash('sha1');//使用加密算法sf.update(str);//進行加密let content = sf.digest("hex");//以二進制數據為字符串形式展示return content; }; module.exports = getJssdk;

2.JSAPI支付簽名

JSAPI支付簽名下單文檔-https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
需提前準備
appid:appid憑證
mchid:商戶號
serial_no:商戶序列號
pem:證書私鑰

const axios = require("axios"); const crypto = require("crypto"); const pem = require("./apiclient_key.js"); const appid = "xxxxxxxxxxxxx";//appid const mchid = 'xxxxxxxxxxxxx';//商戶號 const serial_no = 'xxxxxxxxxxxxx';//商戶序列號// jsapi下單 /* data:{out_trade_no:'商戶訂單號',description:'說明'attach:'攜帶回調參數'notify_url:'通知地址',total:'分',openid:'用戶openid', } */ function paysgin(data){return new Promise(async(resolve,reject)=>{let url = '/v3/pay/transactions/jsapi';let params = {"mchid": mchid,//直連商戶號"out_trade_no": data.out_trade_no,//商戶系統內部訂單號,只能是數字、大小寫字母_-*且在同一個商戶號下唯一"appid": appid,//應用ID"description": data.description,//商品描述"attach":JSON.stringify(data.attach),//附加數據"notify_url": data.notify_url,//通知地址"amount": {"total": data.total,//總金額,單位為分"currency": "CNY"},"payer": {"openid": data.openid//用戶標識}}//獲取prepay_idlet result = await axios({url:"https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi",method:"post",headers:{"Authorization":sgin('POST',url,params)},data:params });// 配置調起支付參數let prepay_id = result.data.prepay_id;let timestamp = parseInt(new Date().getTime()/1000).toString();let nonce_str = new Date().getTime().toString();let jiamiPaySign = appid + "\n" + timestamp + "\n" + nonce_str + "\n" + `prepay_id=${prepay_id}` + "\n";let signaturePaySign = sha256(jiamiPaySign);//-----------------------------------------------------// 保存支付參數到數據庫//-----------------------------------------------------resolve({code:0,msg:'',data:{appId:appid,//公眾號ID,由商戶傳入timeStamp:timestamp,//時間戳,自1970年以來的秒數nonceStr:nonce_str,//隨機串package:`prepay_id=${prepay_id}`,signType:"RSA",//微信簽名方式:paySign:signaturePaySign,//微信簽名}});}) } //RSA-SHA256加密 function sha256(str){let privateKey = pem;let sign = crypto.createSign('RSA-SHA256');sign.update(Buffer.from(str, 'utf-8'));let signature = sign.sign(privateKey, 'base64');return signature; } //簽名 function sgin(method,url,params=""){let timestamp = parseInt(new Date().getTime()/1000);let nonce_str = new Date().getTime();params = JSON.parse(JSON.stringify(params));let message = method + "\n"+ url + "\n"+ timestamp + "\n"+ nonce_str + "\n"+ JSON.stringify(params) + "\n";let signature = sha256(message);let auth = `WECHATPAY2-SHA256-RSA2048 mchid="${mchid}",serial_no="${serial_no}",nonce_str="${nonce_str}",timestamp="${timestamp}",signature="${signature}"`;return auth; } module.exports = paysgin

3.支付成功的回調

支付結果通知文檔-https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
在上面2.JSAPI支付簽名后把參數給前端,發起支付后,支付成功會通知調用notify_url地址進行接收參數。
需要準備
key:APIv3密鑰(微信商戶平臺—>賬戶設置—>API安全—>設置APIv3密鑰)

//event.body 是 回調回來的數據 let body = JSON.parse(event.body); console.log('body : ', body.resource) const key = "xxxxxxxxxxxxxxx"; const ciphertext = body.resource.ciphertext; const nonce = body.resource.nonce; const associated_data = body.resource.associated_data; //解密 let data = JSON.parse(decodeByAES(ciphertext,key,nonce,associated_data)); data.attach = JSON.parse(decodeURIComponent(data.attach)); data.success_time = decodeURIComponent(data.success_time.replace(/\+/g, '%20').replace(/T/g,' ')); console.log("解密",data)//----------------------------------------------------- // 進行相關回調通知,數據庫操作,消息提醒等等。。。 //-----------------------------------------------------

decodeByAES解密回調的加密參數

const crypto = require("crypto");function decodeByAES(cipherText,key,iv,add){let rst = '';cipherText = Buffer.from(cipherText, 'base64');let authTag = cipherText.slice(cipherText.length - 16);let data = cipherText.slice(0, cipherText.length - 16);let decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);decipher.setAuthTag(authTag);decipher.setAAD(Buffer.from(add));rst = decipher.update(data, 'binary', 'utf8');try {rst += decipher.final('utf-8');} catch (e) {think.logger.error(e.toString());}return rst; }

總結

以上是生活随笔為你收集整理的微信JSAPI支付v3流程(uniapp和node版)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。