微信小程序开发入门教程(三)---小程序云开发支付功能
支付(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 ...
随机推荐
- thinkphp5中的raw的作用
模板中输出变量 默认不展示HTMl 使用raw将其中的中的HTMl内容展示出来 <div class="content"> <div class="co ...
- ios 输入框失去焦点,位置回调方法
微信网页开发,ios 在input,textarea 失去焦点后,页面无法回调. 以下方法可解决: $("input,textarea").on("blur", ...
- swiper手滑动轮播图后自动轮播失效解决办法
设置autoplay:true之后,再设置 autoplay:{disableOnInteraction: false} --------------------------------------- ...
- Python第三方库资源
[转载]Python第三方库资源 转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github ...
- 记笔记的软件(vnote)
前面我们已经把我们的 Ubuntu 系统在物理机上运行起来了,也做了一些简单的优化,教了大家怎么使用 Ubuntu 系统自带的应用商店和 apt 安装和卸载软件.接着我们安装了搜狗输入法,现在我们的系 ...
- GoLang语言环境搭建及idea集成开发(超详细)
一.所需安装包(windows) 1. https://golang.org/dl/ 下载 MSI installer.不会翻墙的自己找国内下载,双击运行,按照提示安装即可.环境变量自动配置 2.i ...
- 电脑无法上网,DNS出现fec0:0:0:ffff::1%1问题
具体描述:qq,微信可用网,但其他不能用. 一.win+r 输入cmd 打开命令行:ipconfig /all 查看DNS 二.打开文本编辑器,输入如下文本: @Echo onpushd\window ...
- docker-compose deploy replicaSet in standalone MongoDB cluster and with auth
经过两天的折腾,终于实现了自己想要的效果,就是通过docker-compose 部署最新的mongodb replicaSet, 主要是为了测试 4.2 最新的多文档事务,下面将整个步骤分享一下: d ...
- android适配知识总结
一.http适配 背景:API升级到28以后,不再支持明文的网络请求,只支持https请求.运行所报错误:java.net.UnknownServiceException: CLEARTEXT com ...
- Windows Ping | Tracert 's Bat 脚本并行测试
系统:windows 需求:测试多台PC输出三个网站并行ping.tracert结果,多台PC同时进行. 说明:以www.baidu.com.www.sina.com.cn.www.tencent.c ...