예제 #1
0
        protected void DoValidate()
        {
            PayConfiguration    config     = PayConfiguration.GetConfig();
            NameValueCollection parameters = new NameValueCollection();

            parameters.Add(HttpContext.Current.Request.QueryString);
            parameters.Add(HttpContext.Current.Request.Form);
            string gatewayData = parameters[Globals.GATEWAY_KEY];

            if (string.IsNullOrEmpty(gatewayData))
            {
                Core.Globals.WriteText(new System.Text.StringBuilder(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " gatewaydatanotfound GATEWAYDATA IS NULL"));
                this.ResponseStatus(false, "gatewaydatanotfound");
                return;
            }
            parameters.Remove(Globals.GATEWAY_KEY);

            //获取GetwayData特殊Base64数据
            GetwayDatas = Globals.DecodeData4Url(gatewayData).Split(new[] { '|' }, System.StringSplitOptions.None);
            if (GetwayDatas.Length < 1)
            {
                Core.Globals.WriteText(new System.Text.StringBuilder(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " gatewaydatanotfound GetwayDatas.Length < 1"));
                this.ResponseStatus(false, "gatewaydatanotfound");
                return;
            }
            this.GatewayName = GetwayDatas[0].ToLower();
            GatewayProvider provider = config.Providers[this.GatewayName] as GatewayProvider;

            if (provider == null)
            {
                this.ResponseStatus(false, "gatewaynotfound");
                Core.Globals.WriteText(new System.Text.StringBuilder(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " gatewaydatanotfound PROVIDER IS NULL"));
                return;
            }
            if (string.IsNullOrWhiteSpace(provider.NotifyType))
            {
                this.ResponseStatus(false, "notifytypenotfound");
                Core.Globals.WriteText(new System.Text.StringBuilder(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " gatewaydatanotfound PROVIDER.NOTIFYTYPE IS NULL"));
                return;
            }

            if (provider.UseNotifyMode)     //DONE: 使用差异通知模式 BEN NEW MODE 20131016
            {
                this.Notify = NotifyQuery.Instance(provider.NotifyType, parameters,
                                                   _isNotify ? NotifyMode.Notify : NotifyMode.Callback);
            }
            else                            //回调和通知均使用同一种模式
            {
                this.Notify = NotifyQuery.Instance(provider.NotifyType, parameters);
            }
            if (this._isNotify)
            {
                this.Notify.ReturnUrl = Globals.FullPath(string.Format(Option.ReturnUrl, gatewayData));
            }

            #region 加载企业ID
            if (Components.MvcApplication.IsAutoConn)
            {
                Common.CallContextHelper.SetAutoTag(Common.Globals.SafeLong(GetwayDatas[2], 0));
            }
            #endregion

            #region 测试模式
            if (Globals.IsPaymentTestMode)
            {
                this._orderId = parameters["out_trade_no"];
            }
            #endregion
            else
            {
                this._orderId = this.Notify.GetOrderId();
            }

            this.Order = Option.GetOrderInfo(this._orderId);
            if (this.Order == null)
            {
                Core.Globals.WriteText(new System.Text.StringBuilder(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " GETORDERINFO IS NULL| ORDERID:" + this._orderId));
                this.ResponseStatus(false, "ordernotfound");
                return;
            }

            #region 测试模式
            if (Globals.IsPaymentTestMode)
            {
                this._amount = Globals.SafeDecimal(parameters["total_fee"], -1);
            }
            #endregion
            else
            {
                this._amount = this.Notify.GetOrderAmount() == decimal.Zero
                    ? this.Order.Amount
                    : this.Notify.GetOrderAmount();
            }

            //设置支付网关生成的订单ID
            this.Order.GatewayOrderId = this.Notify.GetGatewayOrderId();
            PaymentMode = this.GetPaymentMode(this.Order.PaymentTypeId);
            if (PaymentMode == null)
            {
                Core.Globals.WriteText(new System.Text.StringBuilder(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " PAYMENTMODE IS NULL| PaymentTypeId:" + this.Order.PaymentTypeId));
                this.ResponseStatus(false, "gatewaynotfound");
                return;
            }

            if (this.Order.PaymentStatus == PaymentStatus.Prepaid)
            {
                this.ResponseStatus(true, "success");
            }
            else
            {
                #region 测试模式
                //DONE: 测试模式埋点
                if (Globals.IsPaymentTestMode)
                {
                    string sign = HttpContext.Current.Request.QueryString["sign"];
                    if (string.IsNullOrWhiteSpace(sign))
                    {
                        this.ResponseStatus(false, "<TestMode> no sign");
                    }

                    System.Text.StringBuilder url = new System.Text.StringBuilder(
                        Globals.FullPath(string.Format(Option.ReturnUrl, gatewayData)));
                    url.AppendFormat("&out_trade_no={0}", this._orderId);
                    url.AppendFormat("&total_fee={0}", this._amount);

                    if (sign != Globals.GetMd5(System.Text.Encoding.UTF8, url.ToString()))
                    {
                        this.ResponseStatus(false, "<TestMode> Unauthorized sign");
                    }

                    //效验通过
                    PaidToSite();
                    return;
                }
                #endregion

                PayeeInfo payee = new PayeeInfo
                {
                    EmailAddress  = PaymentMode.EmailAddress,
                    Partner       = PaymentMode.Partner,
                    Password      = PaymentMode.Password,
                    PrimaryKey    = PaymentMode.SecretKey,
                    SecondKey     = PaymentMode.SecondKey,
                    SellerAccount = PaymentMode.MerchantCode
                };
                GatewayInfo getway = new GatewayInfo
                {
                    Data      = gatewayData,
                    DataList  = GetwayDatas.ToList(),
                    ReturnUrl = Option.ReturnUrl,
                    NotifyUrl = Option.NotifyUrl
                };
                this.Notify.NotifyVerifyFaild  += new NotifyEventHandler(this.notify_NotifyVerifyFaild);
                this.Notify.PaidToIntermediary += new NotifyEventHandler(this.notify_PaidToIntermediary);
                this.Notify.PaidToMerchant     += new NotifyEventHandler(this.notify_PaidToMerchant);

                this.Notify.VerifyNotify(0x7530, payee, getway);
            }
        }
예제 #2
0
        public virtual void ProcessRequest(HttpContext context)
        {
            //Safe
            if (!VerifySendPayment(context))
            {
                return;
            }

            //订单ID字符串
            string orderIdStr = string.Empty;

            //获取全部订单ID
            string[] orderIds = OrderProcessor.GetQueryString4OrderIds(context.Request, out orderIdStr);

            //订单ID NULL ERROR返回首页
            if (orderIds == null || orderIds.Length < 1)
            {
                //Add ErrorLog..
                HttpContext.Current.Response.Redirect("~/");
                return;
            }

            //合并支付 订单支付信息以第一份订单为主
            T orderInfo = Option.GetOrderInfo(orderIds[0]);

            if (orderInfo == null)
            {
                return;
            }

            //计算订单支付金额
            decimal totalMoney = this.GetOrderTotalMoney(orderIds, orderInfo);

            if (totalMoney < 0)
            {
                return;
            }

            if (orderInfo.PaymentStatus != PaymentStatus.NotYet)
            {
                //订单已支付
                context.Response.Write(
                    HttpContext.GetGlobalResourceObject("Resources", "IDS_ErrorMessage_SentPayment").ToString());
                return;
            }

            PaymentModeInfo paymentMode = GetPaymentMode(orderInfo);

            if (paymentMode == null || string.IsNullOrWhiteSpace(paymentMode.Gateway))
            {
                //订单历史的支付方式不存在
                context.Response.Write(
                    HttpContext.GetGlobalResourceObject("Resources", "IDS_ErrorMessage_NoPayment").ToString());
                return;
            }

            string getwayName = paymentMode.Gateway.ToLower();

            //获取支付网关
            GatewayProvider provider =
                PayConfiguration.GetConfig().Providers[getwayName] as GatewayProvider;

            if (provider == null)
            {
                return;
            }

            //支付网关
            GatewayInfo gatewayInfo = this.GetGateway(getwayName);

            //交易信息
            TradeInfo tradeInfo = this.GetTrade(orderIdStr, totalMoney, orderInfo);

            #region 测试模式
            //DONE: 测试模式埋点
            if (Globals.IsPaymentTestMode && !Globals.ExcludeGateway.Contains(getwayName.ToLower()))
            {
                System.Text.StringBuilder url = new System.Text.StringBuilder(gatewayInfo.ReturnUrl);
                url.AppendFormat("&out_trade_no={0}", tradeInfo.OrderId);
                url.AppendFormat("&total_fee={0}", tradeInfo.TotalMoney);
                url.AppendFormat("&sign={0}", Globals.GetMd5(System.Text.Encoding.UTF8, url.ToString()));
                HttpContext.Current.Response.Redirect(
                    gatewayInfo.ReturnUrl.Contains("?")
                        ? url.ToString()
                        : url.ToString().Replace("&out_trade_no", "?out_trade_no"), true);
                return;
            }
            #endregion

            #region 发送支付请求
            //发送支付请求
            PaymentRequest paymentRequest = PaymentRequest.Instance(
                provider.RequestType,
                this.GetPayee(paymentMode),
                gatewayInfo,
                tradeInfo
                );
            if (paymentRequest == null)
            {
                return;
            }
            paymentRequest.SendRequest();
            #endregion
        }