微信小程序开发入门教程(三)---小程序云开发支付功能
支付(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 ...
随机推荐
- GCD&&素筛&&快速幂 --A - Pseudoprime numbers
Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). Th ...
- Do Not Try This Problem(分块思想)
题意:https://codeforces.com/group/ikIh7rsWAl/contest/259944/problem/D 给你q个操作,4个数n,a,k,c,从n好位置开始每次加a的位置 ...
- Scrapy里Selectors 四种基础的方法
在Scrapy里面,Selectors 有四种基础的方法xpath():返回一系列的selectors,每一个select表示一个xpath参数表达式选择的节点css():返回一系列的selector ...
- IntelliJ IDEA 搭建 Go 开发环境
本文介绍 Windows7 x64 基于 IntelliJ IDEA 搭建 Go 语言开发环境.主要是一些操作过程截图以及简单文字描述,如有不清楚的地方,欢迎指正.所有软件使用当前(2016.12. ...
- 12-MySQL DBA笔记-MySQL复制
第12章 MySQL复制 本章将为读者讲述MySQL的复制技术,首先,介绍最基础的主从复制,它是其他所有复制技术的基础,接着再为读者讲述各种复制架构的搭建,最后,列举了一些常见的复制问题及处理方式.复 ...
- CodeFirst实体类中,为什么都把ICollection<x>定义成virtual?
主要是用于延迟加载,提高性能用的 只有定义成virtual后才可以延迟加载. 延迟加载,默认情况下,延迟加载被支持,如果你希望禁用它,必须显式声明,最好的位置是在 DbContext 的构造器中. p ...
- Abp 聚合测试
Abp 官网开始的教程例子,是IRpositoty<entity> 直接出现在应用层.但是如果是一个聚合根也会这样吗? 那么聚合根是访问仓储的最小单元,要通过聚合根来操作业务,就是实体, ...
- 小知识:修改IDEA的模板
小知识:修改IDEA的模板 有时候我们会发现,IDEA默认创建的模板并不是我们常用的.与其每次都在创建后进行修改,不如直接对模板进行修改. 给不知道怎么修改的同学指一下路: File->sett ...
- iot平台异构对接文档
iot平台异构对接文档 准备工作 平台提供的XAgent开发指南.pdf demo程序xagent-ptp-demo 平台上添加产品得到产品id和key 部署时需要插件的基础程序<xlink-x ...
- xss part2
0x01 xss challenge level 6-10 1.1 level 6 test with typical, notice the script has changed change sc ...