支付组件

通过微信扫码,在微信浏览器内打开网页,提交订单并进行微信支付的前端代码

jQuery:
在这里插入图片描述

VUE项目:

	// 确定支付
	       pay(msg){
	         //console.log('用户选择的支付方式===' + msg)
	         var that = this;
	        console.log('用户uid===' + that.uid)
	        console.log('订单号oid===' + that.oid)
	        if(msg[0] == 2){
	        //   that.$vux.loading.show({
	        //       text: '支付跳转中'
	        //   });
	          that.wePay()
	        }else{
	          that.$vux.toast.text('请选择支付方式', 'middle');
	        }
	      },
	
	
	  // 微信支付
	      wePay(){
	        var that = this;
	        that.$vux.loading.show({
	              text: '支付跳转中~' 
	          });
	        that.$axios.post('webPay/?oid=' + that.oid + '&price=' + that.price).then(response => {
	          that.msg = response;
	          that.$vux.loading.hide()
	          that.callpay(response.data)
	        })
	        .catch((error) => {
	          // alert(error)
	          setTimeout(function(){
	            //that.$vux.loading.hide()
	            that.closePayModal();
	            that.$vux.toast.show("支付失败 ",'middle');
	            // that.$router.push({name: 'index'});
	        },1000)
	      })
	    },
	
	
	    callpay(data){
	        var that = this;
			if (typeof WeixinJSBridge == "undefined"){
			    if( document.addEventListener ){
	                document.addEventListener('WeixinJSBridgeReady', 
	                that.jsApiCall(data), false);
			    }else if (document.attachEvent){
	                document.attachEvent('WeixinJSBridgeReady',  
	                that.jsApiCall(data)); 
	                document.attachEvent('onWeixinJSBridgeReady',  
	                that.jsApiCall(data)); 
			    }
			}else{
			    that.jsApiCall(data);
			}
	    },
	
	
	    jsApiCall(data){
	        var that = this;
			var new_data = JSON.parse(decodeURIComponent(data));     //此处很关键,
			WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				new_data,
				function(res){
	    			if(res.err_msg == "get_brand_wcpay_request:ok" ){
	    				 location.href = "{:url('/order_status')}";
			      		// 使用以上方式判断前端返回,微信团队郑重提示:
			            //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
		      		}else{
		      			alert("支付失败!");
		      		}
				}
			);
		}

使用cordova插件的写法:

// 支付宝支付
      aliPay(){
        var that = this;
        that.$axios.post('appAliPay/?oid=' + that.oid + '&price=' + that.amountPaid).then(response => {
          console.log(response.data);
          //return false;
          if (response.data.payInfo) {
            var payInfo = response.data.payInfo;
            cordova.plugins.alipay.payment(payInfo, function (result) {
              //alert('支付结果:' + result)
              that.$vux.loading.hide()
              that.$vux.toast.show(result,'middle');
              if(result.resultStatus == '9000'){
                that.$vux.toast.show('支付成功!','middle');
              }else if(result.resultStatus == '8000'){
                that.$vux.toast.show('正在处理中!','middle');
              }else if(result.resultStatus == '4000'){
                that.$vux.toast.show('订单支付失败!','middle');
              }else if(result.resultStatus == '6001!'){
                that.$vux.toast.show('用户中途取消','middle');
              }else if(result.resultStatus == '6002'){
                that.$vux.toast.show('网络连接出错!','middle');
              }else{
                that.$vux.toast.show('未知错误!','middle');
              }
              that.closePayModal();
              that.$router.replace({name: 'Order'});
            }, function (reason) {
              that.$vux.loading.hide()
              that.closePayModal();
              that.$vux.toast.show(reason.memo,'middle');
              that.$router.replace({name: 'Order'});
            });
          }else{
            //console.log("唤起支付失败");
            that.$vux.loading.hide()
            that.closePayModal();
            that.$vux.toast.show("支付失败",'middle');
            that.$router.replace({name: 'Order'});
          }
        }).catch((error) => {

        })
      },

      // 微信支付
      wePay(){
        var that = this;
        that.$axios.post('appWXPay/?oid=' + that.oid + '&price=' + that.amountPaid).then(response => {
          console.log(response.data)
          if (response.data.data.sign && response.data.data.prepayid){
            var params = {
              mch_id: response.data.data.partnerid, // merchant id
              prepay_id: response.data.data.prepayid, // prepay id returned from server
              nonce: response.data.data.noncestr, // nonce string returned from server
              timestamp: response.data.data.timestamp, // timestamp
              sign: response.data.data.sign, // signed string
            };
            //console.log("插件参数:");
            //console.log(params);
            Wechat.sendPaymentRequest(params, function (result) {
              console.log('支付结果:' + result)
              // alert('支付结果:' + result)
              that.$vux.loading.hide()
              that.closePayModal();
              that.$vux.toast.show(result,'middle');
              that.$router.push({name: 'Order'});
            }, function (reason) {
              //console.log('支付失败' + reason)
              that.$vux.loading.hide()
              that.closePayModal();
              that.$vux.toast.show("支付失败: " + reason,'middle');
              that.$router.push({name: 'Order'});
            });
          }else{
            that.$vux.loading.hide()
            that.closePayModal();
            that.$vux.toast.show("支付失败 ",'middle');
            that.$router.push({name: 'Order'});
          }
        }).catch((error) => {
          // alert(error)
            that.$vux.loading.hide()
            that.closePayModal();
            that.$vux.toast.show("支付失败 ",'middle');
            that.$router.push({name: 'Order'});
        })
      },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码片段:<view class="price_box_item"> <block v-for="(item,index) in priceData" :key="index" > <view class="item_con bg-image" :style="{'background-image':url(${OSSImgUrl}/images/20230403_newApp/box/offline_price_item_bg.png)}" @tap.stop="onSelectPrice(item, index)" > <view class="price_1">¥</view> <view class="price_2">{{item.price / 100}}</view> <image class="price_active" :src="${OSSImgUrl}/images/20230403_newApp/box/price_active.png" v-if="selectedPriceCode == item.priceCode" ></image> </view> </block> </view><!-- 支付组件 --> <confirm-order ref="offlinePay" :payData="payData" payType="offline" @onPay="onPay" ></confirm-order>// 支付组件 import ConfirmOrder from '@/components/confirm-order/confirm-order.vue';// 选择价位 onSelectPrice(item, index){ var that = this; uni.$u.throttle( ()=> { if(item.priceCode == this.selectedPriceCode) return this.selectedPriceCode = item.priceCode; this.payData.price = item.price / 100; console.log('this.payData.price', index) this.priceIndex = index; this.payData.name = item.price / 100 + '元双盲盒大礼包' },1000) }, 怎么通过选择不同的价位,选择对应的优惠券,优惠券在支付组件中,以下是组件中的优惠券代码片段:async mounted() { this.getAddressList(); this.getCouponList1(); }, computed:{ discountPrice(){ //优惠金额 let selectedCoupon = this.selectedCoupon; let price = this.payData.price; if(selectedCoupon.type === 1){ //折扣 return (price * ((100 - selectedCoupon.discount) / 100)).toFixed(2) }else if(selectedCoupon.type === 2){ //满减 return (selectedCoupon.subPrice / 100).toFixed(2) }else{ return 0.00 } } },
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值