当前位置:网站首页>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('微信登录失败'))
}
})
// 支付结束
}参考:
边栏推荐
- [C language] the use of dynamic memory development "malloc"
- RMS TO EAP通过MQTT简单实现
- Matrix processing practice
- Pagoda panel MySQL cannot be started
- Implementation of smart home project
- Charm of code language
- 程序员搞开源,读什么书最合适?
- Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
- Click the picture in the mobile browser and the picture will not pop up
- Common fault analysis and Countermeasures of using MySQL in go language
猜你喜欢

Common fault analysis and Countermeasures of using MySQL in go language

Uni app running to wechat development tool cannot Preview

The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution

90%的人都不懂的泛型,泛型的缺陷和应用场景

Data visualization platform based on template configuration

Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常

Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process

Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
![[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution](/img/f3/782246100bca3517d95869be80d9c5.png)
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
![[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
随机推荐
如何獲取GC(垃圾回收器)的STW(暫停)時間?
MySQL数字类型学习笔记
把欧拉的创新带向世界 SUSE 要做那个引路人
tongweb设置gzip
【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值
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
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
Tdengine already supports the industrial Intel edge insight package
Apache DolphinScheduler 入门(一篇就够了)
程序员搞开源,读什么书最合适?
H. 265 introduction to coding principles
Coordinate system of view
Uni app running to wechat development tool cannot Preview
Flutter development: a way to solve the problem of blank space on the top of listview
天龙八部TLBB系列 - 关于包裹掉落的物品
[system design] index monitoring and alarm system
Swift uses userdefaults and codable to save an array of class objects or structure instances
Comparison of batch merge between Oracle and MySQL
How to get the STW (pause) time of GC (garbage collector)?