当前位置:网站首页>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)
边栏推荐
- Project summary notes series wstax kt session2 code analysis
- ZCMU--1390: 队列问题(1)
- 无线WIFI学习型8路发射遥控模块
- Open3d European clustering
- pytorch-softmax回归
- Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
- Redirection of redis cluster
- 【使用TensorRT通过ONNX部署Pytorch项目】
- XML解析
- Open3D 欧式聚类
猜你喜欢
【yolov5.yaml解析】
Simply solve the problem that the node in the redis cluster cannot read data (error) moved
Pytorch MLP
一次生产环境redis内存占用居高不下问题排查
【无标题】
yolov5目標檢測神經網絡——損失函數計算原理
Multi table operation - sub query
【Win11 多用户同时登录远程桌面配置方法】
How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
【 YOLOv3中Loss部分计算】
随机推荐
Liunx prohibit Ping explain the different usage of traceroute
Cdga | six principles that data governance has to adhere to
阻止浏览器后退操作
Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
yolov5目标检测神经网络——损失函数计算原理
谜语1
Empêcher le navigateur de reculer
阻止瀏覽器後退操作
Prevent browser backward operation
Pytorch weight decay and dropout
Acid transaction theory
Multi table operation - Auto Association query
1.php的laravel创建项目
Project summary notes series wstax kt session2 code analysis
Open3D 欧式聚类
[crawler] Charles unknown error
[leetcode] wild card matching
11.(地图数据篇)OSM数据如何下载使用
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
2048游戏逻辑