狼丶宇先生的日志

Thank for your coming.

浅谈微信公众号开发之微信支付的坑

2017-10-19 22:35:03




        

    /**
	 * 获取支付签名
	 * 
	 * */
	function getPayAutograph(sendJson,payType,orderId,jwt,dpsBillingId){
		//获取用户地址列表地址
		var jwt = common.getCookie('jwtToken');
		common.initToast(orderSubmitData.languageData.takeOut.user_paying);
		service.getPayTypeAutograph(payType,orderId,dpsBillingId,jwt).then(function(res){			
			var result = JSON.parse(res);		
			if (result.code == '200') {
				switch(payType){
					case 'P_WECHAT': //微信
						weiXinPay(result.data,orderId);break;
					case 'P_ALIPAY': //支付宝
						aliPay(result.data.payInfo);break;						
					case 'P_DPS': //纽币支付
						dpsPay(result.data.redirectUrl);break;
					default:
						return common.createDialog(orderSubmitData.languageData.failed);
				}
			}else if(result.code == '401'){
					weui.confirm(result.msg, {
					    buttons: [{
					        label: orderSubmitData.languageData.yes,
					        type: 'primary',
					        onClick: function(){
					        	window.location.href = '../../pages/index.html';
					        }
					    }]
					});
			}else{
				common.createDialog(result.msg);
			}
		});
	}
	  

	/**
	 * 微信支付
	 * @param payInfo{String} 支付签名
	 * @param orderId {Number} 订单ID
	 * */
	function weiXinPay(payInfo,orderId){
		if(!payInfo){
			return false;
		}
		
		var appId = payInfo.appid;
		var timeStamp = payInfo.timestamp;
		var nonceStr = payInfo.noncestr;
		var _package = payInfo.package;
		var prepayid = payInfo.prepayid;
		var paySign = payInfo.sign;
			//支付请求数据配置
		var weiXinPay = {
				"appId":appId,     //公众号名称,由商户传入     
           		"timeStamp":timeStamp,         //时间戳,自1970年以来的秒数     
           		"nonceStr":nonceStr, //随机串     
       			"package":'prepay_id='+prepayid,     
           		"signType":"MD5",         //微信签名方式:     
           		"paySign":paySign, //微信签名 ,
			};
		callPay(weiXinPay,orderId);
	}
	
	/**
	 * 唤起支付控件
	 * @param weiXinPay {JSON} 请求数据
	 * @param orderId {Number} 订单ID
	 * */
	function callPay(weiXinPay,orderId){
		if (typeof WeixinJSBridge == "undefined"){
				common.createDialog('WeixinJSBridge undefined');
		   if( document.addEventListener ){
		       	document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
		   }else if (document.attachEvent){
		       	document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
		       	document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		   }
		}else{
		   onBridgeReady(weiXinPay,orderId);
		}
	}
	
	/**
	 * 支付控件
	 * @param weiXinPay {JSON} 请求数据
	 * @param orderId {Number} 订单ID
	 * */
	function onBridgeReady(weiXinPay,orderId){
	    //上面接口已经获取接口需要的数据,
	   WeixinJSBridge.invoke(
	       'getBrandWCPayRequest',weiXinPay, function(res){
	            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
	            	//支付成功
	            	weui.confirm(orderSubmitData.languageData.takeOut.pay_success, {
					    buttons: [{
					        label: orderSubmitData.languageData.yes,
					        type: 'primary',
					        onClick: function(){
					        	var shopId = common.getRegExp('shopId');
					        	//支付成功跳转到订单详情页
								window.location.href = './takeout_order_detail.html#shopId=' + shopId + '&orderId='+orderId+'&from=list';
					        }
					    }]
					});
	            } else if(res.err_msg == "get_brand_wcpay_request:cancel") {
	            	//支付取消
	            	common.createDialog(orderSubmitData.languageData.takeOut.pay_cancel);
	            } else if(res.err_msg == "get_brand_wcpay_request:fail") {
	            	//支付失败
	            	common.createDialog(orderSubmitData.languageData.takeOut.pay_failed);
	            }
	        }
	    );
	}	


作者:admin 分类:前端 浏览:549 评论:0