123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- // pages/orders/order-detail.ts
- import { api_executeOrder, api_executePayment, api_queryByParamName, api_userPaymentOrderUnpaid } from "../../api/login";
- // 获取应用实例
- const app = getApp<IAppOption>()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- status: 'ing',
- statusList: {
- ing: {
- logo: './images/ing.png',
- title: '等待付款',
- content: '请尽快完成支付,以便我们为您处理订单'
- },
- },
- goodsInfo: {} as any,
- paymentType: null as any, // 支付类型
- paymentChannel: null as any,
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options: any) {
- this.queryPayType()
- if (options.orderInfo) {
- const goods = JSON.parse(decodeURIComponent(options.orderInfo));
- console.log(goods, 'goods')
- this.setData({
- goodsInfo: goods,
- status: goods.status
- });
- }
- },
- // 获取后台配置的支付方式
- async queryPayType() {
- try {
- // wxlite_payment_service_provider
- const { data } = await api_queryByParamName({
- paramName: 'wxlite_payment_service_provider'
- });
- if (data.code == 200) {
- const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
- this.setData({
- paymentType: paramValue.vendor,
- paymentChannel: paramValue.channel
- });
- }
- } catch (error) {
- console.log(error, "error");
- }
- },
- onPayError() {
- wx.hideLoading()
- wx.showToast({
- title: '支付失败',
- icon: 'none'
- })
- },
- // 购买
- async onSubmit() {
- wx.showLoading({
- mask: true,
- title: "订单提交中...",
- });
- try {
- const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
- if(orderNo) {
- const {data} = await api_userPaymentOrderUnpaid({
- orderNo: orderNo,
- paymentType: 'WECHAT_MINI'
- })
- if (data.code === 200) {
- const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
- this.onExecutePay(paymentConfig, paymentType, orderNo)
- } else {
- this.onPayError()
- }
- } else {
- const { data } = await api_executeOrder({
- "orderType": "WECHAT_MINI",
- "paymentType": this.data.paymentType,
- "paymentCashAmount": salePrice,
- "paymentCouponAmount": 0,
- "shopId": shopId,
- "openId": app.globalData.userInfo?.liteOpenid,
- "goodsInfos": [{
- "goodsId": id,
- "goodsNum": 1,
- "goodsType": "ACTIVATION_CODE",
- "paymentCashAmount": salePrice,
- "paymentCouponAmount": 0
- }],
- "orderName": name,
- "orderDesc": name
- })
- if (data.code === 200) {
- const { paymentConfig, paymentType, orderNo } = data.data
- this.onExecutePay(paymentConfig, paymentType, orderNo)
-
- // 测试h5页面支付流程
- // const params = {
- // paymentType: 'adapay-cooleshow-6560',
- // pay_channel: 'wx_pub',
- // wxAppId: 'wxbde13f59d40cb4f2',
- // body: 'aaa',
- // price: '0.01',
- // orderNo,
- // userId: app.globalData.userInfo?.id
- // }
- // wx.navigateTo({
- // url: '../protocol/register?orderInfo=' + encodeURIComponent(JSON.stringify(params))
- // })
- } else {
- this.onPayError()
- }
- }
- } catch {
- wx.hideLoading()
- }
- },
- async onExecutePay( paymentConfig: any, paymentType: string, orderNo: string) {
- wx.login({
- success: async (wxres: any) => {
- const res = await api_executePayment({
- merOrderNo: paymentConfig.merOrderNo,
- paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
- paymentType,
- userId: app.globalData.userInfo?.id,
- code: wxres.code,
- wxMiniAppId: app.globalData.appId
- // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
- // wxPubAppId: 'wxbde13f59d40cb4f2'
- })
- wx.hideLoading()
- if(res.data.code === 200) {
- this.onPay(paymentType, res.data.data.reqParams, orderNo)
- } else {
- this.onPayError()
- }
- },
- fail: () => {
- this.onPayError()
- }
- })
- },
- onPay(paymentType: string, paymentConfig: any, orderNo: string) {
- const isYeePay = paymentType.indexOf('yeepay') !== -1
- const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
- : paymentConfig?.expend
- ? JSON.parse(paymentConfig?.expend?.pay_info)
- : paymentConfig
- const that = this
- wx.requestPayment({
- timeStamp: prePayInfo.timeStamp,
- nonceStr: prePayInfo.nonceStr,
- package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
- paySign: prePayInfo.paySign,
- signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
- success(resInfo) {
- wx.showToast({ title: '支付成功', icon: 'success' });
- wx.redirectTo({
- url: '/pages/orders/order-result?orderNo=' + orderNo
- })
- },
- fail(ressonInfo) {
- console.log('支付失败', ressonInfo)
- // wx.showToast({ title: '支付失败!', icon: 'none' });
- that.onPayError()
- wx.redirectTo({
- url: '/pages/orders/order-result?orderNo=' + orderNo
- })
- }
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- }
- })
|