微信小程序使用微信支付

写一个确实可用的微信小程序支付的案例 。
微信小程序使用微信支付
工具/原料首先搭建好wxml 的页面,我们以0.01元为例子来进行支付测试!

<view class="container">
<text>支付0.01元</text>
<button plain="true" bindtap="setLoading">支 付</button>
</view>

微信小程序使用微信支付
js代码如下,里面是小项目的支付参数,你可以改成你的参数 。

【微信小程序使用微信支付】

setLoading: function () {//支付 var that = this; var url = this.data.url; wx.login({ success: function (res) { if (res.code) { //发起网络请求 wx.request({ url: url + '/Index/opendId', method: 'GET', data: { code: res.code, grant_type: 'authorization_code' }, header: { 'content-type': 'application/json' }, success: function (res) { // console.log(res.data) var sss = res.data var obj = JSON.parse(sss) var id = obj.openid that.setData({ openid: id }) /**/wx.request({ url: url + '/pay.php',//改成你自己的支付链接 header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { openId: that.data.openid },success: function (response) { //console.log(rs.data); //console.log('调起支付'); //console.log(response.data); wx.requestPayment({ 'timeStamp': response.data.timeStamp, 'nonceStr': response.data.nonceStr, 'package': response.data.package, 'signType': 'MD5', 'paySign': response.data.paySign, 'success': function (res) { var openid = that.data.openid//openID wx.request({ url: url + '/wx/admin.php/Home/Index/chongzhi/id/314', header: { "Content-Type": "application/x-www-form-urlencoded" }, method: "POST", data: { 'sopenId': openid,}, success: function (zf) { var data = http://www.2312333.com/list9/zf.data if (zf.data == 888) { wx.showToast({ title: '支付成功', icon: 'success', duration: 2000, success: function () { setTimeout(function () {wx.navigateBack({ delta: 1 }) }, 500); }, }) } else if (zf.data == 404) { wx.showToast({ title: "网络错误,请联系管理员!", icon: 'none', duration: 5000 }) }}, fail: function () { } }) }, 'fail': function (res) { wx.showToast({ title: '支付失败', icon: 'loading', duration: 3000 });} })}, fail: function (res) { console.log(res.data) } }); /***/} }) } } }); }
微信小程序使用微信支付
我们点击支付,必须用真机扫码才能支付 。我们来部署一下后端的代码,我用PHP写的简单的代码 。
微信小程序使用微信支付
其实微信小程序支付用不到SDK里面的代码,我们自己编写就好 。类似SDK里面pay文件的代码 。
微信小程序使用微信支付
最后测试支付成功0.01元 。
微信小程序使用微信支付