当前位置:网站首页>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('微信登录失败'))
}
})
// 支付结束
}
参考:
边栏推荐
- Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
- Coffeescript Chinese character to pinyin code
- (1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
- [C language] the use of dynamic memory development "malloc"
- Fluent development: setting method of left and right alignment of child controls in row
- Understand the window query function of tdengine in one article
- cent7安装Oracle数据库报错
- Data visualization platform based on template configuration
- Tdengine already supports the industrial Intel edge insight package
- Tdengine connector goes online Google Data Studio app store
猜你喜欢
善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
Analysis on the wallet system architecture of Baidu trading platform
程序员如何活成自己喜欢的模样?
自动化规范检查软件如何发展而来?
Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
Kotlin Compose 与原生 嵌套使用
Coordinate system of view
Charm of code language
Node red series (29): use slider and chart nodes to realize double broken line time series diagram
能源势动:电力行业的碳中和该如何实现?
随机推荐
ThreadLocal source code learning
The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
Officially launched! Tdengine plug-in enters the official website of grafana
Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
Cent7 Oracle database installation error
[app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow
《微信小程序-基础篇》小程序中的事件与冒泡
Swift uses userdefaults and codable to save an array of class objects or structure instances
The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
Analysis on the wallet system architecture of Baidu trading platform
To bring Euler's innovation to the world, SUSE should be the guide
How Windows bat script automatically executes sqlcipher command
Understand the window query function of tdengine in one article
一个程序员的职业生涯到底该怎么规划?
. Net delay queue
cent7安装Oracle数据库报错
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常