在我們做一些購物車的結(jié)算功能時是一定會有支付功能的,那我們?nèi)绾稳プ鑫⑿胖Ц哆@個功能呢,首先我們先要理清思路,并且要了解到接口需要哪些數(shù)據(jù)以及會返回哪些數(shù)據(jù)
注意:一定要先看接口文檔!
-
創(chuàng)建訂單。
○ 請求創(chuàng)建訂單的 API 接口:把(訂單金額、收貨地址、訂單中包含的商品信息)發(fā)送到服務器。
○ 服務器響應的結(jié)果:訂單編號。
-
訂單預支付。
○ 請求訂單預支付的 API 接口:把(訂單編號)發(fā)送到服務器。
○ 服務器響應的結(jié)果:訂單預支付對象,里面包含了訂單支付相關(guān)的必要參數(shù)。
-
發(fā)起微信支付。
○ 調(diào)用?
uni.requestPayment()
?這個 API,并傳遞訂單預支付對象,發(fā)起微信支付。○ 監(jiān)聽?
uni.requestPayment()
?這個 API 的 success,fail,complete 回調(diào)函數(shù)。
?這是我們先點擊結(jié)算按鈕是需要做一些校驗和提示的,通過校驗后再去做支付功能
// 點擊了結(jié)算按鈕
settlement() {
// #1 先判斷是否勾選了要結(jié)算的商品
if (!this.checkedCount) return uni.$showMsg('請選擇要結(jié)算的商品!')
// #2 再判斷用戶是否選擇了收貨地址
if (!this.addstr) return uni.$showMsg('請選擇收貨地址!')
// #3 最后判斷用戶是否登錄了
// if (!this.token) return uni.$showMsg('請先登錄!')
if (!this.token) return this.delayNavigate()
// #4 實現(xiàn)微信支付功能
this.payOrder()
}
接下來就是支付功能了文章來源:http://www.zghlxwxcb.cn/news/detail-485746.html
// 微信支付
async payOrder() {
// 1. 創(chuàng)建訂單
// 1.1 組織訂單的信息對象
const orderInfo = {
// 開發(fā)期間,注釋掉真實的訂單價格,
// order_price: this.checkedGoodsAmount,
// 寫死訂單總價為 1 分錢
order_price: 0.01,
consignee_addr: this.addstr,
goods: this.cart.filter(x => x.goods_state).map(x => (
{ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price }
))
}
// 1.2 發(fā)起請求創(chuàng)建訂單
const { data: res } = await uni.$http.post('/orders/create', orderInfo)
if (res.meta.status !== 200) return uni.$showMsg('創(chuàng)建訂單失敗!')
// 1.3 得到服務器響應的“訂單編號”
const orderNumber = res.message.order_number
// 2. 訂單預支付
// 2.1 發(fā)起請求獲取訂單的支付信息
const { data: res2 } = await uni.$http.post('/my/orders/req_unifiedorder', { order_number: orderNumber })
// 2.2 預付訂單生成失敗
if (res2.meta.status !== 200) return uni.$showMsg('預付訂單生成失?。?)
// 2.3 得到訂單支付相關(guān)的必要參數(shù)
const payInfo = res2.message.pay
// 3. 發(fā)起微信支付
// 3.1 調(diào)用 uni.requestPayment() 發(fā)起微信支付
const [err, succ] = await uni.requestPayment(payInfo)
// 3.2 未完成支付
if (err) return uni.$showMsg('訂單未支付!')
// 3.3 完成了支付,進一步查詢支付的結(jié)果
const { data: res3 } = await uni.$http.post('/my/orders/chkOrder', { order_number: orderNumber })
// 3.4 檢測到訂單未支付
if (res3.meta.status !== 200) return uni.$showMsg('訂單未支付!')
// 3.5 檢測到訂單支付完成
uni.showToast({
title: '支付完成!',
icon: 'success'
})
}
以上就是功能代碼啦,但是要注意的是,可能做出來后會沒有效果,因為這個東西是需要一些權(quán)限賬號的,如果是自己寫的話,沒有特定的權(quán)限是出不來支付功能的文章來源地址http://www.zghlxwxcb.cn/news/detail-485746.html
主要其實就是調(diào)用uni.requestPayment()這個api的,具體可以去參照uniapp的官方文檔uni.requestPayment(OBJECT) | uni-app官網(wǎng)
到了這里,關(guān)于使用uniapp開發(fā)微信小程序的微信支付流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!