wx小程序关于付款流程

2020-02-14  本文已影响0人  倩仔6
------pay.js-------
function wechatPay(payParam, orderId) {
  wx.requestPayment({
    timeStamp: payParam.timeStamp,
    nonceStr: payParam.nonceStr,
    package: payParam.package,
    signType: payParam.signType,
    paySign: payParam.paySign,
    success: function (res) {
      console.log("=============支付过程成功=============");
      wx.redirectTo({
        url: "/pages/payResult/payResult?status=1&orderId=" + orderId
      });
    },
    fail: function (res) {
      console.log("=============支付过程失败=============");
      wx.redirectTo({
        url: "/pages/payResult/payResult?status=0&orderId=" + orderId
      });
    },
    complete: function (res) {
      console.log("=============支付过程结束=============");
    }
  })
}

module.exports = {
  wechatPay
}

用法·

    <view class="payment" bindtap="choosePay">
        <view class="payment-item">
            <view class="l">
                <text class="name">选择付款方式</text>
                <text class="txt">{{payType == 1? '微信支付 ' : '余额支付 '}}></text>
            </view>
        </view>
    </view>
    <!-- 选择付款方式弹框 -->
    <view class="attr-pop-box" hidden="{{!openAttr}}">
        <view class="attr-pop {{isIphoneX?'isIPX1': ''}}">
            <view class="one">
                <text class="txt">选择付款方式</text>
                <image class="close" bindtap="closeAttr" src="/static/images/cross.png"></image>
            </view>
            <view class="two" bindtap="choosePayType" data-index="1">
                <text class="txt">微信付款</text>
                <image class="choose" src="{{isChecked ? '/static/images/unchoose.png' : '/static/images/choose.png'}}"></image>
            </view>
            <view class="three" bindtap="choosePayType" data-index="2">
                <text class="left">
                    <text class="txt">余额付款</text>
                    <text class="txt2">当前余额:¥{{usefulBalanceYuan}}</text>
                </text>
                <image class="choose" src="{{isChecked ? '/static/images/choose.png' : '/static/images/unchoose.png'}}"></image>
            </view>
            <view class="four" bindtap="closeAttr">
                <text class="ok">确定</text>
            </view>
        </view>
    </view>
  <view class="r" bindtap="submitOrder">去付款</view>

var pay = require('../../utils/pay.js')
      Page({
    data: {
        isChecked: false, // 选择付款方式图标

        openAttr: false,
        payType: 1, //付款方式
}
})
    // 去付款
    submitOrder: function () {
        if (this.data.addressId < 0) {
            util.showErrorToast("请选择收货地址");
            return false;
        } else {
            util
                .request(
                    api.CreatOrder, {
                    memberComment: this.data.memberComment,
                    storeId: this.data.storeId,
                    claimGoodsType: this.data.claimGoodsType,
                    amount: this.data.amount,
                    actualAmount: this.data.actualAmount,
                    cartList: this.data.cartList,
                    mobile: this.data.mobile,
                    addressId: this.data.addressId,
                    payType: this.data.payType
                },
                    "POST"
                )
                .then(res => {
                    if (res.errno === 0) {
                        const orderNo = res.data.paymentNo;
                        const payParam = JSON.parse(res.data.wxPayReturnParam.apiJson);
                        console.log("预支付成功返回:", payParam)

                        //微信支付开始
                        pay.wechatPay(payParam, orderNo);

                    } else {
                        util.showErrorToast(res.errmsg);
                    }
                });
        }
    },
    // 选择付款方式
    choosePayType: function (params) {
        var that = this;
        var isChecked = !that.data.isChecked
        that.setData({
            payType: params.currentTarget.dataset.index * 1,
            isChecked
        });
    },

    // 付款弹窗
    choosePay: function (params) {
        var that = this;
        that.setData({
            openAttr: true
        });
    },

    // 关闭选择付款弹窗
    closeAttr: function () {
        this.setData({
            openAttr: false
        });
    },
上一篇下一篇

猜你喜欢

热点阅读