当前位置:网站首页>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)
边栏推荐
猜你喜欢

【云原生 | Kubernetes篇】Ingress案例实战(十三)

13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
![[singleshotmultiboxdetector (SSD, single step multi frame target detection)]](/img/c7/8ad64582e09683818171b625769f37.png)
[singleshotmultiboxdetector (SSD, single step multi frame target detection)]

一次生产环境redis内存占用居高不下问题排查

Simply solve the problem that the node in the redis cluster cannot read data (error) moved

pytorch-多层感知机MLP
![[configuration method of win11 multi-user simultaneous login remote desktop]](/img/8f/eab81f089b236c4527a9866b2cfc25.png)
[configuration method of win11 multi-user simultaneous login remote desktop]

多表操作-自关联查询

Principle of persistence mechanism of redis

【 YOLOv3中Loss部分计算】
随机推荐
Redis cluster (master-slave) brain crack and solution
HiEngine:可媲美本地的云原生内存数据库引擎
如何让你的产品越贵越好卖
【pytorch 修改预训练模型:实测加载预训练模型与模型随机初始化差别不大】
vscode快捷键
Harbor镜像仓库搭建
Acid transaction theory
The ninth Operation Committee meeting of dragon lizard community was successfully held
C#实现WinForm DataGridView控件支持叠加数据绑定
MySQL statistical skills: on duplicate key update usage
Pytorch MLP
Prevent browser backward operation
redis主从中的Master自动选举之Sentinel哨兵机制
[upsampling method opencv interpolation]
13.(地图数据篇)百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
Yolov5 target detection neural network -- calculation principle of loss function
查看多台机器所有进程
Is it difficult to apply for a job after graduation? "Hundreds of days and tens of millions" online recruitment activities to solve your problems
Question and answer 45: application of performance probe monitoring principle node JS probe
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework