Administrator
发布于 2025-10-24 / 5 阅读
0
0

微信小程序支付核心片段

// 页面初始化时获取用户信息

Page({

data: {

payInfo: null

},

// 调用后端接口获取支付参数

async getPaymentParams() {

try {

const res = await wx.request({

url: 'https://your-api.com/create_order',

method: 'POST',

data: {

productId: '12345',

amount: 9.9,

openid: '用户openid'

}

});

if (res.data.code === 200) {

this.setData({

payInfo: res.data.data

});

this.initiatePayment();

}

} catch (error) {

console.error('获取支付参数失败', error);

}

},

// 发起支付请求

initiatePayment() {

const { timeStamp, nonceStr, package: payPackage, signType, paySign } = this.data.payInfo;

wx.requestPayment({

timeStamp,

nonceStr,

package: payPackage,

signType,

paySign,

success(res) {

console.log('支付成功', res);

// 处理支付成功后的业务逻辑(如更新订单状态)

wx.showToast({

title: '支付成功',

icon: 'success'

});

},

fail(err) {

console.error('支付失败', err);

wx.showModal({

title: '支付失败',

content: '请重试或联系客服',

showCancel: false

});

}

});

}

});


评论