1. 程式人生 > >微信支付基礎程式碼示意

微信支付基礎程式碼示意

官方API點選這裡
官方支付業務流程
官方支付API文件


var app = getApp();
 2 Page({
 3   data: {},
 4   onLoad: function (options) {
 5     // 頁面初始化 options為頁面跳轉所帶來的引數
 6     var that = this
 7     //登陸獲取code
 8     wx.login({
 9       success: function (res) {
10         console.log(res.code)
11         //獲取openid
12 that.getOpenId(res.code) 13 } 14 }); 15 }, 16 getOpenId: function (code) { 17 var that = this; 18 wx.request({ 19 url: "https://api.weixin.qq.com/sns/jscode2session?appid=小程式appid&secret=小程式應用金鑰&js_code=" + code + "&grant_type=authorization_code", 20 data:
{}, 21 method: 'GET', 22 success: function (res) { 23 that.generateOrder(res.data.openid) 24 }, 25 fail: function () { 26 // fail 27 }, 28 complete: function () { 29 // complete 30 } 31 }) 32 }, 33 /**生成商戶訂單 */ 34 generateOrder: function
(openid) { 35 var that = this 36 //統一支付 37 wx.request({ 38 url: '後臺路徑', 39 method: 'GET', 40 data: { 41 gfee: '商品價錢', 42 gname: '商品名稱', 43 openId:openid 44 (商品價錢和商品名稱根據自身需要是否傳值,openid為必傳) 45 }, 46 success: function (res) { 47 var pay = res.data 48 //發起支付 49 var timeStamp = pay[0].timeStamp; 50 var packages = pay[0].package; 51 var paySign = pay[0].paySign; 52 var nonceStr = pay[0].nonceStr; 53 var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr }; 54 that.pay(param) 55 }, 56 }) 57 }, 58 59 /* 支付 */ 60 pay: function (param) { 61 console.log("支付") 62 console.log(param) 63 wx.requestPayment({ 64 timeStamp: param.timeStamp, 65 nonceStr: param.nonceStr, 66 package: param.package, 67 signType: param.signType, 68 paySign: param.paySign, 69 success: function (res) { 70 // success 71 wx.navigateBack({ 72 delta: 1, // 回退前 delta(預設為1) 頁面 73 success: function (res) { 74 wx.showToast({ 75 title: '支付成功', 76 icon: 'success', 77 duration: 2000 78 }) 79 }, 80 fail: function () { 81 // fail 82 83 }, 84 complete: function () { 85 // complete 86 } 87 }) 88 }, 89 fail: function (res) { 90 // fail 91 }, 92 complete: function () { 93 // complete 94 } 95 }) 96 } 97 })