生活随笔
收集整理的這篇文章主要介紹了
微信JSAPI支付v3流程(uniapp和node版)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、微信JSAPI支付
請(qǐng)?zhí)崆皽?zhǔn)備好接入前的準(zhǔn)備文檔獲取相關(guān)的配置數(shù)據(jù),否則下面需要的數(shù)據(jù)你可能會(huì)比較懵!
并且需要提前了解微信JSAPI支付文檔
二、獲取用戶openid
獲取openid方法例子
三、h5調(diào)起支付
1.第一種通過(guò)WeixinJSBridge調(diào)起微信支付服務(wù)
參數(shù)獲取請(qǐng)看本文JSAPI支付簽名
WeixinJSBridge
.invoke('getBrandWCPayRequest', {appId
:'xxxxxxxx',timeStamp
:'xxxxxxxx',nonceStr
:'xxxxxxxx',package:'xxxxxxxx', signType
:"RSA",paySign
:'xxxxxxxx',},(res
) =>{if (res
.err_msg
== "get_brand_wcpay_request:ok") {}}
);
2.第二種通過(guò)js-sdk中的wx.chooseWXPay調(diào)起微信支付服務(wù)
首先需要閱讀js-sdk文檔,js-sdk文檔
通過(guò)js-sdk獲取支付的能力
npm install weixin
-js
-sdk
下文中的getJssdk獲取請(qǐng)看本文JS-SDK權(quán)限簽名的方法
import wx
from 'weixin-js-sdk';
import {getJssdk
} from "@/api/wx.js";
export const setJsSdk
= async ()=>{let params
= {url
:location
.href
.split('#')[0]}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
);});
}
調(diào)用wx.chooseWXPay進(jìn)行支付調(diào)起
參數(shù)獲取請(qǐng)看本文JSAPI支付簽名
wx
.chooseWXPay({timestamp
: xxxxxxxx
, nonceStr
: 'xxxxxxxx', package: 'xxxxxxxx', signType
: 'xxxxxxxx', paySign
: 'xxxxxxxx', success
: function (res
) {}
});
四、node對(duì)接對(duì)應(yīng)的相關(guān)接口
1.JS-SDK權(quán)限簽名
對(duì)應(yīng)上面目錄三-2的getJssdk接口使用
需提前準(zhǔn)備
appid:appid憑證
appSecret:密鑰
const crypto
= require("crypto");
const axios
= require("axios");
const appid
= "xxxxxxxx";
const appSecret
= 'xxxxxxxx';
const getJssdk = (params
)=>{
let 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
;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
;let nonceStr
= 'xxxxxxxxxxxxxxxxxxxx';let string1
= `jsapi_ticket=${jsapi_ticket}&noncestr=${nonceStr}×tamp=${timestamp}&url=${url}`;let signature
= jiamiSha1(string1
);return {code
:0,message
:'',data
:{timestamp
,nonceStr
,signature
}}
}
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
需提前準(zhǔn)備
appid:appid憑證
mchid:商戶號(hào)
serial_no:商戶序列號(hào)
pem:證書私鑰
const axios
= require("axios");
const crypto
= require("crypto");
const pem
= require("./apiclient_key.js");
const appid
= "xxxxxxxxxxxxx";
const mchid
= 'xxxxxxxxxxxxx';
const serial_no
= 'xxxxxxxxxxxxx';
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
,"description": data
.description
,"attach":JSON.stringify(data
.attach
),"notify_url": data
.notify_url
,"amount": {"total": data
.total
,"currency": "CNY"},"payer": {"openid": data
.openid
}}let 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
,timeStamp
:timestamp
,nonceStr
:nonce_str
,package:`prepay_id=${prepay_id}`,signType
:"RSA",paySign
:signaturePaySign
,}});})
}
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.支付成功的回調(diào)
支付結(jié)果通知文檔-https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
在上面2.JSAPI支付簽名后把參數(shù)給前端,發(fā)起支付后,支付成功會(huì)通知調(diào)用notify_url地址進(jìn)行接收參數(shù)。
需要準(zhǔn)備
key:APIv3密鑰(微信商戶平臺(tái)—>賬戶設(shè)置—>API安全—>設(shè)置APIv3密鑰)
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解密回調(diào)的加密參數(shù)
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
;
}
總結(jié)
以上是生活随笔為你收集整理的微信JSAPI支付v3流程(uniapp和node版)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。