v3安全性很高,需要签名,unipay解决了签名,所以直接用就可以
先看文档:
const path = require('path'); // 引入内置的path模块
const unipayIns = unipay.initWeixinV3({
appId: 'your appId',
mchId: 'your mchId',
v3Key: 'you parterner key',
appCertPath: path.resolve(__dirname, 'your appCertPath'),
// appCertContent: "",
appPrivateKeyPath: path.resolve(__dirname, 'your appPrivateKeyPath'),
// appPrivateKeyContent: "",
})
调起支付功能--文档
获取支付参数
unipayIns.getOrderInfo
,此接口仅支持微信小程序、支付宝小程序、App 平台
根据接口要求入参就可以
详细自己看文档把
注意这边appCertPath他文件结尾是pem 不是p12文件,当时我填错了找了很久才找到,因为v2是p12
我的代码:
const unipay = require('uni-pay');
exports.main = async (event, context) => {
//這個path0,你原來用的是p12
let path0 = path.resolve(__dirname, 'cert/apiclient_cert.pem')
let path1 = path.resolve(__dirname, 'cert/apiclient_key.pem')
const unipayIns = unipay.initWeixinV3({
appId: appid, //小程序appid
mchId: mchid, //微信商户号
v3Key: key, //商户号的API密钥
appCertPath: path0, //API 证书文件路径
appPrivateKeyPath: path1 //私钥路径
})
// 付款
//event为客户端上传的参数
orderInfo = await unipayIns.getOrderInfo({
openid: event.openid, //这个是客户端上传的用户的openid
// subject: event.name, // 订单名称微信支付时不可填写此项
body: event.body,
outTradeNo: event.outTradeNo, //商户订单号
totalFee: event.totalFee, // 金额,单位元,在上传过来的时候就已经*100了
// 支付结果通知地址,没有该参数或者为空会报错,随便给了一个测试网址
notifyUrl: 'https://pay.weixin.qq.com',
tradeType: 'JSAPI',
spbillCreateIp: context.CLIENTIP
// attach: event.orderid, //备注,订单号或 长者id 在下一步通知中判断长度来确定
})