当前位置:网站首页>Uniapp + unicloud + Unipay realize wechat applet payment function
Uniapp + unicloud + Unipay realize wechat applet payment function
2022-07-05 11:49:00 【zrong180106】
Project directory structure
Import unipay plug-in unit
uni-pay - DCloud Plug in market
File directory :
uniCloud/cloudfunctions/common/uni-pay
newly build getOrderInfo Cloud functions 
stay getOrderInfo/index.js Introduction in uni-pay
// const unipay = require('unipay')
// Will report a mistake : MODULE_NOT_FOUND:Cannot find module 'unipay'
correct : const unipay = require('uni-pay')
'use strict';
// const unipay = require('unipay') // Report errors : MODULE_NOT_FOUND:Cannot find module 'unipay'
// introduce unipay
const unipay = require('uni-pay')
exports.main = async (event, context) => {
const unipayIns = unipay.initWeixin({
appId: 'xxxxxx', // Applet appid
mchId: 'xxxx', // Wechat merchant No
key: 'xxxxxx', // Name of merchant number API secret key
//pfx: fs.readFileSync('/path/to/your/pfxfile'), // p12 File path , When using wechat for refund, you need , It should be noted that alicloud does not support reading files through relative paths at present , Please use the form of absolute path
})
//event Parameters uploaded for the client
let orderInfo = await unipayIns.getOrderInfo({
openid: event.openid, // This is for the user uploaded by the client openid
// subject: event.name, // The order name cannot be filled in wechat payment
body: ' Service charge ',
outTradeNo: event.suiji, // Give him a random number so that he can initiate payment for the second time
totalFee: event.pric, // amount of money , Unit element , It was already... When it was uploaded *100 了
// Payment result notification address , If there is no such parameter or it is empty, an error will be reported , Casually gave a test website
notifyUrl: 'https://xxxxx',
// attach: event.out_trade, // remarks , Order number or Seniors id Determine the length in the next notice
})
return { orderInfo }
};
Write a payment request on the client
<button @click="weixinPay"> payment </button>
// Wechat payment
weixinPay() {
var that = this
// 1. Pass on weixin Get wechat code
uni.login({
provider: 'weixin',
success(code) {
console.log('code:', code.code) // get code
//2: Get wechat openid
uni.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
method:'GET',
data: {
appid: "xxxxxxxxxx", // Of your applet APPID
secret: "xxxxxxxxxxxx", // Of your applet secret,
js_code: code.code //wx.login After successful login code
},
success: (cts) => {
console.log(cts);
// cts.data.openid Get openid
//3: Call cloud function Unified order
uniCloud.callFunction({
name: 'getOrderInfo',
data: { // Pass some basic information of the order
openid: cts.data.openid,
// name: that.order.name,
// out_trade: that.order.out_trade, // The order number
out_trade: 123456, // The order number
suiji: Math.floor(Math.random() * 100000000),
pric: 1, // Unit score
}
}).then(odr => {
console.log('OrderInfo:', odr)
uni.hideLoading(); // hide loding...
uni.requestPayment({ // Call payment api
provider: 'weixin',
...odr.result.orderInfo,
success() {
uni.showModal({
title: ' Successful payment ',
content: ' Please contact the consultant to execute the order !'
})
},
fail() {}
})
})
}
});
},
fail(err) {
reject(new Error(' Wechat login failed '))
}
})
// End of payment
}
Reference resources :
uniapp Use unipay Cloud function completes wechat payment !-- Good space network ! (jspkongjian.com)
边栏推荐
- Pytorch weight decay and dropout
- Project summary notes series wstax kt session2 code analysis
- redis的持久化机制原理
- [LeetCode] Wildcard Matching 外卡匹配
- COMSOL -- 3D casual painting -- sweeping
- Simply solve the problem that the node in the redis cluster cannot read data (error) moved
- Principle of redis cluster mode
- idea设置打开文件窗口个数
- [deploy pytoch project through onnx using tensorrt]
- Dynamic SQL of ibatis
猜你喜欢
随机推荐
Pytorch MLP
Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
871. Minimum Number of Refueling Stops
1.php的laravel创建项目
c#操作xml文件
SET XACT_ABORT ON
Pytorch weight decay and dropout
Install esxi 6.0 interactively
XML解析
pytorch-多层感知机MLP
How to protect user privacy without password authentication?
[leetcode] wild card matching
以交互方式安装ESXi 6.0
Cdga | six principles that data governance has to adhere to
[crawler] bugs encountered by wasm
Network five whip
多表操作-自关联查询
Idea set the number of open file windows
【主流Nivida显卡深度学习/强化学习/AI算力汇总】
12.(地图数据篇)cesium城市建筑物贴图