public MobileResponseWrapper Get(string memberId, string orderNumber, string paymentMethodName, Guid id)
        {
            var response = new MobileResponseWrapper
            {
                Data = new WechatQueryOrderResponseViewModel {
                    Status = false
                },
            };
            var paymentResponse = PaymentGatewayInvoker.CheckOrderStatus(paymentMethodName, orderNumber);

            if (paymentResponse != null && paymentResponse.Status == PaymentGatewayRecordStatusType.OrderSubmitted)
            {
                response = new MobileResponseWrapper
                {
                    Data = new WechatQueryOrderResponseViewModel {
                        Status = true
                    },
                };
                return(response);
            }
            if (paymentResponse != null && paymentResponse.Status == PaymentGatewayRecordStatusType.Unknown)
            {
                var approved = false;
                _mobileOrderProvider.CancelOrder(memberId, orderNumber, Thread.CurrentThread.CurrentCulture.Name, paymentMethodName,
                                                 ref approved, id);
                response = new MobileResponseWrapper
                {
                    Data = new WechatQueryOrderResponseViewModel {
                        Status = approved
                    },
                };
                return(response);
            }
            return(response);
        }
        public TransactionInfo GetTransactionData(string refId)
        {
            ValidateCaller();
            var transactionInfo = new TransactionInfo();

            if (string.IsNullOrEmpty(refId))
            {
                LoggerHelper.Error("PagoSeguro.GetTransactionData called with empty Order Number");
                transactionInfo.RefID = "Invalid RefId";
                return(transactionInfo);
            }

            LoggerHelper.Info(string.Format(TransactionInitiated, refId));
            SerializedOrderHolder order = OrderProvider.GetPaymentGatewayOrder(refId);

            if (null != order && null != order.Order)
            {
                PaymentGatewayInvoker.LogMessage(PaymentGatewayLogEntryType.Request, refId, order.DistributorId,
                                                 "PagoSeguroPaymentGateway", PaymentGatewayRecordStatusType.Unknown,
                                                 string.Format(TransactionInitiated, refId));
                var totals = (order.Order as Order_V01).Pricing as OrderTotals_V01;
                if (null != totals)
                {
                    transactionInfo.Monto = Convert.ToDouble(totals.AmountDue);
                    transactionInfo.RefID = refId;
                }
                else
                {
                    LoggerHelper.Error(string.Format(InvalidOrMissingOrderData, refId));
                    transactionInfo.RefID = "Invalid Data for RefId";
                }
            }
            else
            {
                LoggerHelper.Error(string.Format(InvalidOrMissingOrderNumber, refId));
                transactionInfo.RefID = "Invalid RefId";
            }

            return(transactionInfo);
        }
        private void SubmitOrder(string orderNumber)
        {
            string           distributorId = string.Empty;
            string           locale        = string.Empty;
            string           error         = string.Empty;
            MyHLShoppingCart shoppingCart  = null;
            var holder   = new SerializedOrderHolder();
            var response = new PagoSeguroPaymentGatewayResponse();

            response.OrderNumber = orderNumber;
            response.IsApproved  = true;

            if (OrderProvider.deSerializeAndSubmitOrder(response, out error, out holder))
            {
                locale = holder.Locale;
                if (!String.IsNullOrEmpty(holder.Email))
                {
                    //DistributorProvider.LoadDistributor(holder.DistributorId, holder.Token);
                    shoppingCart = ShoppingCartProvider.GetBasicShoppingCartFromService(holder.ShoppingCartId,
                                                                                        holder.DistributorId, locale);
                    shoppingCart.EmailAddress = holder.Email;
                    shoppingCart.Totals       = (holder.Order as Order_V01).Pricing as OrderTotals_V01;
                    EmailHelper.SendEmail(shoppingCart, holder.Order as Order_V01);
                }
                if (null != shoppingCart)
                {
                    ShoppingCartProvider.UpdateInventory(shoppingCart, shoppingCart.CountryCode, shoppingCart.Locale,
                                                         true);
                    shoppingCart.CloseCart();
                }
            }
            else
            {
                PaymentGatewayInvoker.LogMessage(PaymentGatewayLogEntryType.Error, orderNumber, distributorId,
                                                 "PagoSeguroPaymentGateway", PaymentGatewayRecordStatusType.InError,
                                                 error);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string Locale = CultureInfo.CurrentCulture.Name;

            _country = Locale.Substring(3, 2);

            MembershipUser <DistributorProfileModel> member = null;

            var    memberDefault = Membership.GetUser();
            string DistributorID = string.Empty;

            try
            {
                member        = (MembershipUser <DistributorProfileModel>)memberDefault;
                DistributorID = (member != null && member.Value != null) ? member.Value.Id : string.Empty;
            }
            catch (Exception ex)
            {
                LoggerHelper.Error(string.Format("PaymentGatewayManager Null member cast failed : {0} ", ex.Message));
            }

            if (!string.IsNullOrEmpty(DistributorID))
            {
                _sessionInfo = SessionInfo.GetSessionInfo(DistributorID, Locale);
            }

            if (!IsPostBack)
            {
                ProductsBase.RemoveDupCheckCookie();
                (Master as OrderingMaster).SetPageHeader(GetLocalResourceObject("PageResource1.Title") as string);
                PaymentGatewayResponse gatewayResponse = null;
                try
                {
                    gatewayResponse = PaymentGatewayResponse.Create();
                    if (null != gatewayResponse)
                    {
                        LoggerHelper.Warn(string.Format("PaymentGatewayRequest Splunk Log : Request {0}", LogFormData(Request.Form)));
                    }
                }
                catch (Exception ex)
                {
                    LoggerHelper.Error(string.Format("PaymentGatewayManager failed to create a Response: {0}", ex.Message));
                }

                if (gatewayResponse == null)
                {
                    MyHLShoppingCart ShoppingCart = (null != this._sessionInfo && null != this._sessionInfo.ShoppingCart) ? this._sessionInfo.ShoppingCart : ShoppingCartProvider.GetShoppingCart(DistributorID, Locale);
                    if (ShoppingCart != null)
                    {
                        try
                        {
                            PaymentGatewayInvoker invoker = PaymentGatewayInvoker.Create(HLConfigManager.Configurations.PaymentsConfiguration.PaymentGatewayPaymentMethods, (ShoppingCart.Totals as OrderTotals_V01).AmountDue);
                            invoker.Submit();
                        }
                        catch (ThreadAbortException)
                        {
                            //this just seems to come along with the redirect request. We don't want this in the logs
                        }
                        catch (Exception ex)
                        {
                            PaymentGatewayInvoker.LogBlindError(string.Format("Error occurred Invoking Payment Gateway {0}. The error is: {1}", HLConfigManager.Configurations.PaymentsConfiguration.PaymentGatewayInvoker, ex.Message));
                            if (HLConfigManager.Configurations.DOConfiguration.IsChina && _sessionInfo.StandAloneDonationError == standAloneDonation)
                            {
                                _sessionInfo.StandAloneDonationError = standAloneDonationerror;
                                Response.Redirect("~/Ordering/Donation.aspx");
                            }
                            else
                            {
                                Response.Redirect("~/Ordering/Checkout.aspx");
                            }
                        }
                    }
                }
                else
                {
                    if (gatewayResponse is PGHPaymentGatewayResponse && _sessionInfo != null)
                    {
                        Session[PaymentGatewayResponse.PGH_FPX_PaymentStatus] =
                            ((PGHPaymentGatewayResponse)gatewayResponse).PhgPaymentStatus;
                    }

                    if (gatewayResponse.IsReturning)
                    {
                        bool pending   = gatewayResponse.IsPendingTransaction;
                        bool cancelled = gatewayResponse.IsCancelled;
                        PaymentGatewayRecordStatusType gatewayResponseStatus = gatewayResponse.Status;
                        if (gatewayResponse.IsApproved)
                        {
                            if (!gatewayResponse.CanSubmitIfApproved && _sessionInfo != null)
                            {
                                _sessionInfo.OrderStatus = SubmitOrderStatus.OrderSubmitted;
                                _sessionInfo.OrderNumber = gatewayResponse.OrderNumber;
                                if (_sessionInfo.ShoppingCart != null)
                                {
                                    ShoppingCartProvider.UpdateShoppingCart(_sessionInfo.ShoppingCart,
                                                                            string.Empty,
                                                                            gatewayResponse.OrderNumber, DateTime.Now);
                                }
                                return;
                            }
                        }

                        if (gatewayResponseStatus == PaymentGatewayRecordStatusType.OrderSubmitted && _sessionInfo != null)
                        {
                            _sessionInfo.OrderStatus = SubmitOrderStatus.OrderSubmitted;
                            _sessionInfo.OrderNumber = gatewayResponse.OrderNumber;
                            ShoppingCartProvider.UpdateShoppingCart(_sessionInfo.ShoppingCart,
                                                                    string.Empty,
                                                                    gatewayResponse.OrderNumber, DateTime.Now);
                            return;
                        }
                        else if (pending && _sessionInfo != null)
                        {
                            _sessionInfo.OrderStatus = SubmitOrderStatus.Unknown;
                            if (_sessionInfo.ShoppingCart != null)
                            {
                                _sessionInfo.ShoppingCart.CloseCart();
                            }
                            Response.Redirect("~/Ordering/Catalog.aspx?ETO=FALSE");
                            return;
                        }
                        else if (cancelled && _sessionInfo != null)
                        {
                            _sessionInfo.OrderStatus = SubmitOrderStatus.Unknown;
                            Response.Redirect("~/Ordering/Checkout.aspx");
                            return;
                        }
                        else if (gatewayResponse.IsApproved)
                        {
                            //Let it fall through the natural flow...
                        }
                        else if (gatewayResponseStatus == PaymentGatewayRecordStatusType.Declined)
                        {
                            Session[PaymentGatewayResponse.PaymentGateWateSessionKey] = gatewayResponse;
                            if (HLConfigManager.Configurations.DOConfiguration.IsChina && _sessionInfo.StandAloneDonationError == standAloneDonation)
                            {
                                _sessionInfo.StandAloneDonationError = standAloneDonationerror;
                                Response.Redirect("~/Ordering/Donation.aspx");
                            }
                            else
                            {
                                if (_sessionInfo != null)
                                {
                                    _sessionInfo.OrderStatus = SubmitOrderStatus.OrderSubmitFailed;
                                    _sessionInfo.OrderNumber = gatewayResponse.OrderNumber;
                                }
                                Response.Redirect("~/Ordering/Checkout.aspx");
                            }

                            return;
                        }
                        else
                        {
                            if (_sessionInfo != null)
                            {
                                _sessionInfo.OrderStatus = SubmitOrderStatus.OrderSubmitFailed;
                                return;
                            }
                        }
                    }

                    //For PGH-Submitted orders
                    if (gatewayResponse is PGHPaymentGatewayResponse)
                    {
                        if (gatewayResponse.Status == PaymentGatewayRecordStatusType.OrderSubmitted && _sessionInfo != null)
                        {
                            // Timer1.Enabled = false;
                            _sessionInfo.OrderStatus = SubmitOrderStatus.OrderSubmitted;
                            _sessionInfo.OrderNumber = gatewayResponse.OrderNumber;
                            return;
                        }
                    }

                    if (member == null || member.Value == null) //Being posted to by a gateway
                    {
                        offLinePost     = true;
                        Timer1.Enabled  = false;
                        _customResponse = gatewayResponse.SpecialResponse;
                        bool approved = gatewayResponse.IsApproved;
                        if (approved)
                        {
                            if (gatewayResponse.CanSubmitIfApproved)
                            {
                                SubmitOrder(gatewayResponse);
                            }

                            return;
                        }
                    }

                    string orderNumber = gatewayResponse.OrderNumber;
                    if (null != _sessionInfo)
                    {
                        if (_sessionInfo.OrderStatus == SubmitOrderStatus.OrderSubmitted && orderNumber != _sessionInfo.OrderNumber)
                        {
                            _sessionInfo.OrderStatus = SubmitOrderStatus.Unknown;
                        }
                        if (_sessionInfo.OrderStatus == SubmitOrderStatus.Unknown)
                        {
                            if (gatewayResponse.IsApproved && gatewayResponse.CanSubmitIfApproved)
                            {
                                _sessionInfo.OrderStatus = SubmitOrderStatus.OrderBeingSubmitted;
                                if (string.IsNullOrEmpty(_sessionInfo.OrderMonthShortString))
                                {
                                    var orderMonth = new OrderMonth(_country);
                                    //orderMonth.ResolveOrderMonth();
                                    _sessionInfo.OrderMonthShortString = orderMonth.OrderMonthShortString;
                                    _sessionInfo.OrderMonthString      = orderMonth.OrderMonthString;
                                }
                                if (null == _sessionInfo.ShoppingCart)
                                {
                                    _sessionInfo.ShoppingCart = ShoppingCartProvider.GetShoppingCart(DistributorID, Locale);
                                }

                                (new AsyncSubmitOrderProvider()).AsyncSubmitOrder(gatewayResponse, _country, _sessionInfo);
                            }
                            else
                            {
                                if (gatewayResponse.Status == PaymentGatewayRecordStatusType.ApprovalPending && HLConfigManager.Configurations.PaymentsConfiguration.CanSubmitPending)
                                {
                                    if (null != gatewayResponse as PGHPaymentGatewayResponse)
                                    {
                                        if ((gatewayResponse as PGHPaymentGatewayResponse).OrderStatus == HL.PGH.Api.OrderStatus.GatewayWillSubmit)
                                        {
                                            _sessionInfo.OrderStatus = SubmitOrderStatus.OrderSubmittedProcessing;
                                            Response.Redirect("~/Ordering/Confirm.aspx?OrderNumber=" + orderNumber);
                                            return;
                                        }
                                    }
                                    _sessionInfo.OrderStatus = SubmitOrderStatus.OrderBeingSubmitted;
                                    if (string.IsNullOrEmpty(_sessionInfo.OrderMonthShortString))
                                    {
                                        var orderMonth = new OrderMonth(_country);
                                        _sessionInfo.OrderMonthShortString = orderMonth.OrderMonthShortString;
                                        _sessionInfo.OrderMonthString      = orderMonth.OrderMonthString;
                                    }
                                    if (null == _sessionInfo.ShoppingCart)
                                    {
                                        _sessionInfo.ShoppingCart = ShoppingCartProvider.GetShoppingCart(DistributorID, Locale);
                                    }
                                    (new AsyncSubmitOrderProvider()).AsyncSubmitOrder(gatewayResponse, _country, _sessionInfo);
                                }
                                else
                                {
                                    Session[PaymentGatewayResponse.PaymentGateWateSessionKey] = gatewayResponse;
                                    if (HLConfigManager.Configurations.DOConfiguration.IsChina && _sessionInfo.StandAloneDonationError == standAloneDonation)
                                    {
                                        _sessionInfo.StandAloneDonationError = standAloneDonationerror;
                                        Response.Redirect("~/Ordering/Donation.aspx");
                                    }
                                    else
                                    {
                                        Response.Redirect("~/Ordering/Checkout.aspx");
                                    }
                                    return;
                                }
                            }
                        }
                    }
                    else
                    {
                        lbSubmitOrderStatus.Text = string.Format((string)GetLocalResourceObject("PaymentGatewayOrderSubmitFailed"), orderNumber);
                        if (gatewayResponse != null && !string.IsNullOrEmpty(orderNumber))
                        {
                            var status = OrderProvider.GetPaymentGatewayRecordStatus(orderNumber);
                            if (status == PaymentGatewayRecordStatusType.Approved || status == PaymentGatewayRecordStatusType.OrderSubmitted)
                            {
                                SettleCustomerOrderPayment(orderNumber);
                                Response.Redirect("~/Ordering/Confirm.aspx?OrderNumber=" + orderNumber);
                            }
                        }
                        lbSubmitOrderStatus.Style.Add(HtmlTextWriterStyle.Color, "Red");
                        Timer1.Enabled = false;
                    }
                }
            }
        }