当前位置:网站首页>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)
边栏推荐
- 7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
- 《看完就懂系列》15个方法教你玩转字符串
- 程序员内卷和保持行业竞争力
- 12.(地图数据篇)cesium城市建筑物贴图
- [yolov3 loss function]
- Web API configuration custom route
- 【PyTorch预训练模型修改、增删特定层】
- Pytorch weight decay and dropout
- Go language learning notes - first acquaintance with go language
- vscode快捷键
猜你喜欢
pytorch-权重衰退(weight decay)和丢弃法(dropout)
Pytorch softmax regression
Multi table operation - Auto Association query
如何让你的产品越贵越好卖
11. (map data section) how to download and use OSM data
XML parsing
idea设置打开文件窗口个数
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]
多表操作-子查询
石油化工企业安全生产智能化管控系统平台建设思考和建议
随机推荐
【 YOLOv3中Loss部分计算】
How to protect user privacy without password authentication?
Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
Install esxi 6.0 interactively
Multi table operation - Auto Association query
投资理财适合女生吗?女生可以买哪些理财产品?
iTOP-3568开发板NPU使用安装RKNN Toolkit Lite2
[deploy pytoch project through onnx using tensorrt]
liunx禁ping 详解traceroute的不同用法
Pytorch training process was interrupted
2048 game logic
Multi table operation - sub query
13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
1个插件搞定网页中的广告
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
How did the situation that NFT trading market mainly uses eth standard for trading come into being?
多表操作-子查询
ACID事务理论
yolov5目标检测神经网络——损失函数计算原理
ZCMU--1390: 队列问题(1)