微信小程序开发入门教程(三)---小程序云开发支付功能
支付(shoukuan)功能真的很重要!由于我还没有商户号,以下代码未实际验证
1、服务端
进入云开发,新建云函数pay(应该也可以在开发者工具编写后上传)
编写后端代码index.js
这里用到第三方库wx-js-utils(https://github.com/lcxfs1991/wx-js-utils )
const cloud = require('wx-server-sdk');
const {
WXPay,
WXPayUtil
} = require('wx-js-utils');
cloud.init();
const appId = 'wx****************'; // 小程序appid
const mchId = '152*******'; // 商户号
const key = '****************************'; // 商户密钥
const timeout = ; // 超时时间
let wxpay = new WXPay({
appId,
mchId,
key,
timeout: ,
signType: 'MD5',
useSandbox: false // 不使用沙箱环境
});
exports.main = async(event, context) => {
const curTime = Date.now();
const tradeNo = `${event.userInfo.openId.substr(-)}-${curTime}`; // 生成订单号
const body = '测试订单'; // 订单商品名称
const spbill_create_ip = '127.0.0.1'; // 发起支付的IP
const notify_url = 'http://www.qq.com'; // 回调地址
const total_fee = event.price * ; // 支付金额,单位为分
const time_stamp = '' + Math.ceil(Date.now() / );
const out_trade_no = `${tradeNo}`;
let orderParam = {
body,
spbill_create_ip,
notify_url,
out_trade_no,
total_fee,
openid: event.userInfo.openId,
trade_type: 'JSAPI',
timeStamp: time_stamp,
};
const {
return_code,
result_code,
...restData
} = await wxpay.unifiedOrder(orderParam); // 统一下单
if (return_code === 'SUCCESS' && result_code === 'SUCCESS') {
const {
prepay_id,
nonce_str
} = restData;
const sign = WXPayUtil.generateSignature({
appId,
nonceStr: nonce_str,
package: `prepay_id=${prepay_id}`,
signType: 'MD5',
timeStamp: time_stamp
}, key); // 签名
return {
code: ,
data: {
out_trade_no,
time_stamp,
...restData,
sign
}
}
}
return {
code: -
}
};
2、小程序
app.js
App({
onLaunch() {
wx.cloud.init({
traceUser: true,
});
}
});
index.wxml
<view class='container'>
<input class='ipt' value='{{price}}' bindinput='onInput' type='digit' />
<button class='btn-pay' bindtap='pay'>Pay</button>
</view>
index.wxss
.container {
display: flex;
flex-direction: column;
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
}
.ipt {
border-bottom: 1px solid #f1f2f3;
text-align: center;
font-size: 50rpx;
font-weight: bold;
width: 220rpx;
color: #17233d;
}
.btn-pay {
margin-top: 100rpx;
padding: 14rpx 100rpx;
line-height: .5em;
font-size: 36rpx;
background: #5cadff;
color: #fff;
}
.btn-pay::after {
border: ;
}
index.js
Page({
data: {
price: 0.01
},
onInput(event) {
this.setData({ price: event.detail.value });
},
pay() {
const price = parseFloat(this.data.price).toFixed();
wx.showLoading({
title: ''
});
wx.cloud.callFunction({
name: 'pay', // 调用pay函数
data: { price }, // 支付金额
success: (res) => {
wx.hideLoading();
const { result } = res;
const { code, data } = result;
if (code !== ) {
wx.showModal({
title: '提示',
content: '支付失败',
showCancel: false
});
return;
}
console.log(data);
wx.requestPayment({
timeStamp: data.time_stamp,
nonceStr: data.nonce_str,
package: `prepay_id=${data.prepay_id}`,
signType: 'MD5',
paySign: data.sign,
success: () => {
wx.showToast({title: '支付成功'});
}
});
},
fail: (res) => {
wx.hideLoading();
console.log('FAIL');
console.log(res);
}
});
}
});
最终效果:页面显示0.01元和pay按钮。
补充:参考https://www.jianshu.com/p/bd96741287a8和https://blog.csdn.net/gf771115/article/details/100917779
还可以使用https://github.com/befinal/node-tenpay
还有:https://developers.weixin.qq.com/community/develop/article/doc/0004c4a50a03107eaa79f03cc56c13
参考:
https://juejin.im/post/5c876108e51d45543d2836e4
https://cloud.tencent.com/edu/learning/course-100005-1276
关于微信小程序认证问题 https://blog.csdn.net/forthejoker/article/details/79654610
下载github项目的一个方法 https://blog.csdn.net/qq_35433926/article/details/89415895
支付官方文档 https://pay.weixin.qq.com/wiki/doc/api/index.html
https://www.jianshu.com/p/bd96741287a8
微信小程序开发入门教程(三)---小程序云开发支付功能的更多相关文章
- 一看就懂的Android APP开发入门教程
一看就懂的Android APP开发入门教程 作者: 字体:[增加 减小] 类型:转载 这篇文章主要介绍了Android APP开发入门教程,从SDK下载.开发环境搭建.代码编写.APP打包等步骤 ...
- 微信小程序开发入门教程(一)---hello world
由于无法备案网站,前期做了个微信小程序(开发版)就搁置了,几乎忘了开发过程.现在重新梳理,做个记录. 一.最基本的小程序前端例子hello 1.下载安装 微信开发者工具 官网: https://d ...
- 微信小程序(七)-项目实例(原生框架 MINA转云开发)==02-云开发-配置
云开发:1.就是用云函数的型式来使用云存储和云数据库完成各种操作! 2.只关注调什么函数,完成什么功能即可,无需关心HTTP请求哪一套! 3.此模式不代表没有服务器,只是部署在云环境中 ...
- Elasticsearch入门教程(三):Elasticsearch索引&映射
原文:Elasticsearch入门教程(三):Elasticsearch索引&映射 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文 ...
- JasperReports入门教程(三):Paramters,Fields和Detail基本组件介绍
JasperReports入门教程(三):Paramter,Field和Detail基本组件介绍 前言 前两篇博客带领大家进行了入门,做出了第一个例子.也解决了中文打印的问题.大家跟着例子也做出了de ...
- Arduino可穿戴开发入门教程(大学霸内部资料)
Arduino可穿戴开发入门教程(大学霸内部资料) 试读下载地址:链接:http://pan.baidu.com/s/1mg9To28 密码:z5v8 介绍:Arduino可穿戴开发入门教程(大学霸内 ...
- iOS开发入门教程
iOS开发入门教程 http://my.oschina.net/mailzwj/blog/133273 摘要 iOS开发入门教程,从创建项目到运行项目,包括OC基础,调试,模拟器设置等相关知识. iO ...
- ENVI Services Engine5.1 应用开发入门教程
原文地址: ENVI Services Engine5.1 应用开发入门教程_ENVI-IDL中国_新浪博客 http://blog.sina.com.cn/s/blog_764b1e9d0102uy ...
- 移动H5开发入门教程:12点webAPP前端开发经验
如果你是一名移动H5前端开发人员,25学堂的小编认为下面的分享的12点webAPP前端开发经验是你必须掌握的基础知识点.算是一篇移动H5开发入门教程吧! 1. viewport:也就是可视区域.对于桌 ...
- C#,ArcGIS Engine开发入门教程
C#,ArcGIS Engine开发入门教程 转自:http://blog.csdn.net/yanleigis/article/details/2233674 目录(?)[+] 五实现 一 加载A ...
随机推荐
- 关于STM32的IAP与APP互相跳转
关于STM32的IAP与APP互相跳转 之前做了一个不带系统的IAP与APP互相跳转,在网上找了资料后,很顺畅就完成了,后来在IAR集成开发环境下,IAP无系统,APP用UCOS系统做互相跳转出现了很 ...
- Error starting daemon: error initializing graphdriver: devmapper: Device docker-thinpool is not a thin pool
Error starting daemon: error initializing graphdriver: devmapper: Device docker-thinpool is not a th ...
- hdu 2680 Dijstra
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- java大框架
本文章,列出了一些程序员需要学习的技术和知识点,有些技术和知识点没有写道,欢迎大家进行修改和补充,有些技术公司用到,大家需要先学习,有些技术和知识点过时,大家可以了解.本人笔记连接[[http://2 ...
- 手工实现HttpBasic校验
HttpBasic: 是RFC中定义的一种控制HTTP协议访问资源的方式.具体当HTTP请求受限资源时,就需要在请求头中添加以"Authorization"为key的heade ...
- UML中的类图
模型 类 接口 关系 关联关系 描述了类的结构之间的关系.具有方向.名字.角色和多重性等信息.一般的关联关系语义较弱.也有两种语义较强,分别是聚合与组合 聚合 特殊关联关系,指明一个聚集(整体)和组成 ...
- WPF - 仿QQ2014
声明:非原创.项目是网上发现的,以学习为目的重写了部分代码,合理地调整了下布局,巧妙地简化了下Style样式.重写还算是有价值的,并非完全复制. 效果: 获取项目源码:https://pan.baid ...
- js页面内容只读,不可复制
// document.oncontextmenu=new Function("event.returnValue=false"); // document.onselectsta ...
- 【uoj#94】【集训队互测2015】胡策的统计(集合幂级数)
题目传送门:http://uoj.ac/problem/94 这是一道集合幂级数的入门题目.我们先考虑求出每个点集的连通生成子图个数,记为$g_S$,再记$h_S$为点集$S$的生成子图个数,容易发现 ...
- ulimit 命令详解 socket查看linux最大文件打开数
ulimit 命令详解 Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数 可以用ulimit -a 来显示当前的各种用户进程限 ...