当前位置:网站首页>uniapp + uniCloud+unipay 实现微信小程序支付功能
uniapp + uniCloud+unipay 实现微信小程序支付功能
2022-07-05 09:42:00 【zrong180106】
项目目录结构
导入unipay插件
文件目录:
uniCloud/cloudfunctions/common/uni-pay
新建getOrderInfo云函数
在getOrderInfo/index.js中引入uni-pay
// const unipay = require('unipay')
// 会报错: MODULE_NOT_FOUND:Cannot find module 'unipay'
正确: const unipay = require('uni-pay')
'use strict';
// const unipay = require('unipay') // 报错: MODULE_NOT_FOUND:Cannot find module 'unipay'
// 引入unipay
const unipay = require('uni-pay')
exports.main = async (event, context) => {
const unipayIns = unipay.initWeixin({
appId: 'xxxxxx', //小程序appid
mchId: 'xxxx', //微信商户号
key: 'xxxxxx', //商户号的API密钥
//pfx: fs.readFileSync('/path/to/your/pfxfile'), // p12文件路径,使用微信退款时需要,需要注意的是阿里云目前不支持以相对路径读取文件,请使用绝对路径的形式
})
//event为客户端上传的参数
let orderInfo = await unipayIns.getOrderInfo({
openid: event.openid, //这个是客户端上传的用户的openid
// subject: event.name, // 订单名称微信支付时不可填写此项
body: '服务费',
outTradeNo: event.suiji, //给他个随机号让他可以第二次发起支付
totalFee: event.pric, // 金额,单位元,在上传过来的时候就已经*100了
// 支付结果通知地址,没有该参数或者为空会报错,随便给了一个测试网址
notifyUrl: 'https://xxxxx',
// attach: event.out_trade, //备注,订单号或 长者id 在下一步通知中判断长度来确定
})
return { orderInfo }
};
在客户端编写支付请求
<button @click="weixinPay">支付</button>
// 微信支付
weixinPay() {
var that = this
// 1.传递weixin 获取微信的code
uni.login({
provider: 'weixin',
success(code) {
console.log('code:', code.code) // 获得code
//2:获得微信openid
uni.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
method:'GET',
data: {
appid: "xxxxxxxxxx", // 你的小程序的APPID
secret: "xxxxxxxxxxxx", //你的小程序的secret,
js_code: code.code //wx.login 登录成功后的code
},
success: (cts) => {
console.log(cts);
// cts.data.openid 拿到openid
//3:调用云函数 统一下单
uniCloud.callFunction({
name: 'getOrderInfo',
data: { // 传递订单的一些基本信息
openid: cts.data.openid,
// name: that.order.name,
// out_trade: that.order.out_trade, // 订单号
out_trade: 123456, // 订单号
suiji: Math.floor(Math.random() * 100000000),
pric: 1, // 单位分
}
}).then(odr => {
console.log('OrderInfo:', odr)
uni.hideLoading(); //隐藏loding...
uni.requestPayment({ // 调用支付api
provider: 'weixin',
...odr.result.orderInfo,
success() {
uni.showModal({
title: '支付成功',
content: '请和顾问联系执行订单即可!'
})
},
fail() {}
})
})
}
});
},
fail(err) {
reject(new Error('微信登录失败'))
}
})
// 支付结束
}
参考:
边栏推荐
- Oracle combines multiple rows of data into one row of data
- Node red series (29): use slider and chart nodes to realize double broken line time series diagram
- Generics, generic defects and application scenarios that 90% of people don't understand
- @SerializedName注解使用
- Tdengine can read and write through dataX, a data synchronization tool
- ThreadLocal source code learning
- oracle和mysql批量Merge对比
- Tianlong Babu TLBB series - single skill group injury
- Mysql80 service does not start
- A high density 256 channel electrode cap for dry EEG
猜你喜欢
ArcGIS Pro creating features
【OpenCV 例程200篇】219. 添加数字水印(盲水印)
Windows uses commands to run kotlin
一个程序员的职业生涯到底该怎么规划?
【C语言】动态内存开辟的使用『malloc』
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
Officially launched! Tdengine plug-in enters the official website of grafana
Uncover the practice of Baidu intelligent testing in the field of automatic test execution
Kotlin Compose 与原生 嵌套使用
Tongweb set gzip
随机推荐
.Net之延迟队列
Apache DolphinScheduler 系统架构设计
Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
Application of data modeling based on wide table
How to get the STW (pause) time of GC (garbage collector)?
Charm of code language
[200 opencv routines] 219 Add digital watermark (blind watermark)
The Alipay in place function can't be found, and the Alipay in place function is offline
La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
mysql80服务不启动
Energy momentum: how to achieve carbon neutralization in the power industry?
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
Data visualization platform based on template configuration
Swift saves an array of class objects with userdefaults and nssecurecoding
Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
Node red series (29): use slider and chart nodes to realize double broken line time series diagram
How to correctly evaluate video image quality
Kotlin compose multiple item scrolling
View Slide