微信小程序开发入门教程(三)---小程序云开发支付功能
支付(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 ...
随机推荐
- Windows 证书签名的伪造
Windows 系统中的一些非常重要文件通常会被添加数字签名,其目的是用来防止被篡改,能确保用户通过互联网下载时能确信此代码没有被非法篡改和来源可信,从而保护了代码的完整性.保护了用户不会被病毒.恶意 ...
- China Union Pay helper
static string proxyIpAddress = AppConfig.GetProxyIpAddress; static string proxyUserName = AppConfig. ...
- Spring在Thread中注入Bean无效的解决方式
在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务.而通过Spring注入来管理和使用服务是较为合理的方式.但是若直接在Thread子类中通过注解方式注入Bean是无 ...
- JS写斐波那契数列的几种方法
斐波那契数,指的是这样一个数列:1.1.2.3.5.8.13.21.……在数学上,斐波那契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=Fn-1+Fn-2(n>=2,n∈N*),用文字 ...
- http、tcp简述
网络简述第一章 http.tcp简述 一.网络7层协议从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层 : 其中高层(即7.6.5.4层)定 ...
- vi学习笔记
dd 删除一行 de删除光标后面的单词 o向下插入一行 O向上插入一行 y复制 yy复制一行 ye复制光标后面的单词 p粘贴 == 代码自动布局 批量注释 ctrl + v , 输入大写I, 选 ...
- 前端 vue/react 或者 js 导入/导出 xlsx/xls (带样式)表格的功能
第一种导出表格的功能: yarn add xlsx script-loader file-saver xlsx-style 效果展示 xlsx-style的bug修复:node_module/xlsx ...
- Linux学习(四)-Linux常用命令
1.运行级别类 1.1运行级别说明: 0:关机 1:单用户[可用于找回丢失密码] 2:多用户状态没有网络服务 3:多用户状态有网络服务 4:系统未使用保留给用户 5:图形界面 6:系统重启 常用运行级 ...
- vue项目js实现图片放大镜功能
效果图: 我写的是vue的组件形式,方便复用,图片的宽高,缩放的比例可以自己定义 magnifier.vue <template> <div class="magnif ...
- List集合复制
方法一: public static void main(String[] args) { // TODO Auto-generated method stub List<String> ...