小程序商品下單支付
用于小程序的商品下單支付操作
使用步驟
第一步:配置接口地址
在 config.js 文件中找到 config 設(shè)置
第二步:檢查接口定義
搜索 shop_order_pay 接口是否已經(jīng)存在定義(有的小程序模板已經(jīng)定義)
如果沒有定義,在 config 底部新增以下代碼:
shopOrderPayUrl: getApiUrl('shop_order_pay'并復(fù)制紅色部分待用。如果已經(jīng)存在,則直接復(fù)制使用即可。第三步:接口調(diào)用請(qǐng)求
如果定義名稱不同,使用第二步復(fù)制的內(nèi)容替換紅框部分
/**
* 訂單提交
* 接收參數(shù)
* 無(wú)需接收參數(shù),從頁(yè)面數(shù)據(jù)中取值
* 接口傳參
* addr_id:收貨地址ID
* prom_type:商品類型(虛擬、實(shí)物)
* pay_type:支付類型(微信、會(huì)員余額)
* remark:訂單備注
* coupon_id:優(yōu)惠券ID
* querystr:訂單數(shù)據(jù)密鑰字符串
*/
onSubmitOrder() {
let _this = this;
// 防止重復(fù)提交訂單
if (_this.data.disabled) return false;
// 驗(yàn)證是否需要添加收貨地址
let prom_type = _this.data.prom_type;
let addr_id = _this.data.address.addr_id || 0;
if (0 == addr_id && 0 == prom_type) {
App.showError('請(qǐng)?zhí)砑邮肇浀刂?#39;, function() {
_this.onSelectAddress();
});
return false;
}
// 接口傳參
let postData = {
addr_id: addr_id,
prom_type: prom_type,
pay_type: _this.data.curPayType,
remark: _this.data.remark || '',
coupon_id: _this.data.selectCouponId,
querystr: _this.data.options.querystr,
};
// 秒殺商品參數(shù)
if (_this.data.product_data[0].active_time_id) {
postData.order_source = '20';
postData.order_source_id = _this.data.product_data[0].active_time_id;
}
App._requestPost(_this, App.globalData.config.shopOrderPayUrl, postData,
function (res) {
let order_id = res.data.OrderData.order_id;
let order_code = res.data.OrderData.order_code;
_this.setData({ order_id, order_code });
if (_this.data.curPayType == 1) {
// 微信支付
if (res.data.WeChatPay.return_code == 'FAIL') {
App.showError(res.data.WeChatPay.return_msg);
return false;
}
App.wxPayment({
order_id: order_id,
payment: res.data.WeChatPay,
success: res1 => {
_this._orderPayDealWith();
},
fail: res1 => {
if (!res.msg.error) App.showError(res.msg.error);
},
});
} else if (_this.data.curPayType == 2) {
// 余額支付
App.showSuccess(res.msg, () => {
_this.redirectToOrderIndex();
});
}
},
function (res) {
App.showError(res.msg);
}
);
},
接口傳值
addr_id:收貨地址ID,實(shí)物商品必傳
prom_type:商品類型(0實(shí)物、1虛擬),必傳
pay_type:支付類型(1微信、2余額),必傳
remark:訂單備注,選填
coupon_id:優(yōu)惠券ID,選填
querystr:訂單數(shù)據(jù)密鑰字符串,必傳
文檔最后更新時(shí)間:2026-01-13 15:00:08
未解決你的問題?請(qǐng)到「問答社區(qū)」反饋你遇到的問題
