当前位置:网站首页>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)
边栏推荐
- 一次生产环境redis内存占用居高不下问题排查
- 【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
- 【load dataset】
- Harbor image warehouse construction
- 阻止瀏覽器後退操作
- Halcon 模板匹配实战代码(一)
- Multi table operation - sub query
- How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
- 1. Laravel creation project of PHP
- Install esxi 6.0 interactively
猜你喜欢

The ninth Operation Committee meeting of dragon lizard community was successfully held

Redirection of redis cluster

HiEngine:可媲美本地的云原生内存数据库引擎

idea设置打开文件窗口个数

【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】

How to make your products as expensive as possible

【PyTorch预训练模型修改、增删特定层】

Network five whip

12. (map data) cesium city building map

谜语1
随机推荐
yolov5目標檢測神經網絡——損失函數計算原理
Unity Xlua MonoProxy Mono代理类
[yolov5.yaml parsing]
15 methods in "understand series after reading" teach you to play with strings
SET XACT_ABORT ON
redis 集群模式原理
How to get a token from tokenstream based on Lucene 3.5.0
Riddle 1
调查显示传统数据安全工具在60%情况下无法抵御勒索软件攻击
Redis master-slave mode
Reading notes of growth hacker
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
pytorch-线性回归
Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
redis主从中的Master自动选举之Sentinel哨兵机制
idea设置打开文件窗口个数
[leetcode] wild card matching
Dynamic SQL of ibatis
ZCMU--1390: 队列问题(1)
Programmers are involved and maintain industry competitiveness