Exemple #1
0
        private void DoPayPalExpressCeckout()
        {
            PayPalExpressGateway gateway
                = new PayPalExpressGateway(
                      commerceConfig.PayPalAPIUsername,
                      commerceConfig.PayPalAPIPassword,
                      commerceConfig.PayPalAPISignature,
                      commerceConfig.PayPalStandardEmailAddress);

            gateway.UseTestMode = commerceConfig.PaymentGatewayUseTestMode;

            gateway.MerchantCartId = cart.CartGuid.ToString();
            gateway.ChargeTotal    = cart.OrderTotal;

            string siteRoot = SiteUtils.GetNavigationSiteRoot();

            gateway.ReturnUrl = siteRoot + "/Services/PayPalReturnHandler.ashx";
            gateway.CancelUrl = siteRoot + Request.RawUrl;
            //Currency currency = new Currency(store.DefaultCurrencyId);
            //gateway.CurrencyCode = currency.Code;
            gateway.CurrencyCode     = siteSettings.GetCurrency().Code;
            gateway.OrderDescription = store.Name + " " + WebStoreResources.OrderHeading;

            gateway.BuyerEmail       = cart.OrderInfo.CustomerEmail;
            gateway.ShipToFirstName  = cart.OrderInfo.DeliveryFirstName;
            gateway.ShipToLastName   = cart.OrderInfo.DeliveryLastName;
            gateway.ShipToAddress    = cart.OrderInfo.DeliveryAddress1;
            gateway.ShipToAddress2   = cart.OrderInfo.DeliveryAddress2;
            gateway.ShipToCity       = cart.OrderInfo.DeliveryCity;
            gateway.ShipToState      = cart.OrderInfo.DeliveryState;
            gateway.ShipToCountry    = cart.OrderInfo.DeliveryCountry;
            gateway.ShipToPostalCode = cart.OrderInfo.DeliveryPostalCode;
            gateway.ShipToPhone      = cart.OrderInfo.CustomerTelephoneDay;

            // this tells paypal to use the shipping address we pass in
            // rather than what the customer has on file
            // when we implement shippable products we'll do shipping calculations before
            // sending the user to paypal
            //gateway.OverrideShippingAddress = true;

            //commented out the above, we want user to be able to populate shipping info from their paypal account

            bool executed = gateway.CallSetExpressCheckout();

            if (executed)
            {
                //TODO: log the raw response
                if (gateway.PayPalExpressUrl.Length > 0)
                {
                    // record the gateway.PayPalToken
                    PayPalLog payPalLog = new PayPalLog();
                    payPalLog.RawResponse  = gateway.RawResponse;
                    payPalLog.ProviderName = "WebStorePayPalHandler";
                    payPalLog.ReturnUrl    = siteRoot + Request.RawUrl;
                    payPalLog.Token        = HttpUtility.UrlDecode(gateway.PayPalToken);
                    payPalLog.RequestType  = "SetExpressCheckout";

                    cart.SerializeCartOffers();
                    payPalLog.SerializedObject = SerializationHelper.SerializeToString(cart);

                    payPalLog.CartGuid  = cart.CartGuid;
                    payPalLog.SiteGuid  = store.SiteGuid;
                    payPalLog.StoreGuid = store.Guid;
                    payPalLog.UserGuid  = cart.UserGuid;

                    payPalLog.Save();

                    Response.Redirect(gateway.PayPalExpressUrl);
                }
            }
        }
        public override string HandleRequestAndReturnUrlForRedirect(
            HttpContext context,
            string payPalToken,
            string payPalPayerId,
            PayPalLog setExpressCheckoutLog)
        {
            string redirectUrl = string.Empty;

            if ((payPalToken == null) || (payPalToken.Length == 0))
            {
                log.Error("WebStorePayPalReturnHandler received empty payPalToken");
                return(redirectUrl);
            }

            if (setExpressCheckoutLog == null)
            {
                log.Error("WebStorePayPalReturnHandler received null setExpressCheckoutLog for payPalToken " + payPalToken);
                return(redirectUrl);
            }

            if (setExpressCheckoutLog.SerializedObject.Length == 0)
            {
                log.Error("WebStorePayPalReturnHandler cart was not previously serialized for payPalToken " + payPalToken);
                return(redirectUrl);
            }

            if (setExpressCheckoutLog.CreatedUtc.AddHours(4) < DateTime.UtcNow)
            {
                log.Error("payPalToken " + payPalToken + " was more than 4 hours old, it should expire after 3 hours ");
                return(redirectUrl);
            }

            CommerceConfiguration commerceConfig = SiteUtils.GetCommerceConfig();

            PayPalExpressGateway gateway
                = new PayPalExpressGateway(
                      commerceConfig.PayPalAPIUsername,
                      commerceConfig.PayPalAPIPassword,
                      commerceConfig.PayPalAPISignature,
                      commerceConfig.PayPalStandardEmailAddress);

            gateway.UseTestMode   = commerceConfig.PaymentGatewayUseTestMode;
            gateway.PayPalToken   = payPalToken;
            gateway.PayPalPayerId = payPalPayerId;


            Cart savedCart = (Cart)SerializationHelper.DeserializeFromString(typeof(Cart), setExpressCheckoutLog.SerializedObject);

            savedCart.DeSerializeCartOffers();

            string siteRoot = SiteUtils.GetNavigationSiteRoot();

            gateway.MerchantCartId = savedCart.CartGuid.ToString();
            gateway.ChargeTotal    = savedCart.OrderTotal;
            gateway.ReturnUrl      = siteRoot + "/Services/PayPalReturnHandler.ashx";
            gateway.CancelUrl      = siteRoot;
            //gateway.PayPalPayerId = payPalPayerId;

            gateway.CallGetExpressCheckoutDetails();


            PayPalLog payPalLog = new PayPalLog();

            payPalLog.ProviderName     = WebStorePayPalReturnHandler.ProviderName;
            payPalLog.SerializedObject = setExpressCheckoutLog.SerializedObject;
            payPalLog.ReturnUrl        = setExpressCheckoutLog.ReturnUrl;
            payPalLog.RawResponse      = gateway.RawResponse;
            payPalLog.TransactionId    = gateway.TransactionId;
            payPalLog.CurrencyCode     = gateway.CurrencyCode;
            // TODO: add versions to gateways
            //log.ApiVersion = gateway.
            payPalLog.CartGuid = savedCart.CartGuid;

            Store store = new Store(savedCart.StoreGuid);

            payPalLog.Token       = payPalToken;
            payPalLog.PayerId     = payPalPayerId;
            payPalLog.RequestType = "GetExpressCheckoutDetails";
            payPalLog.SiteGuid    = store.SiteGuid;
            payPalLog.StoreGuid   = store.Guid;
            payPalLog.UserGuid    = savedCart.UserGuid;

            // update the order with customer shipping info
            savedCart.OrderInfo.DeliveryCompany    = gateway.ShipToCompanyName;
            savedCart.OrderInfo.DeliveryAddress1   = gateway.ShipToAddress;
            savedCart.OrderInfo.DeliveryAddress2   = gateway.ShipToAddress2;
            savedCart.OrderInfo.DeliveryCity       = gateway.ShipToCity;
            savedCart.OrderInfo.DeliveryFirstName  = gateway.ShipToFirstName;
            savedCart.OrderInfo.DeliveryLastName   = gateway.ShipToLastName;
            savedCart.OrderInfo.DeliveryPostalCode = gateway.ShipToPostalCode;
            savedCart.OrderInfo.DeliveryState      = gateway.ShipToState;
            savedCart.OrderInfo.DeliveryCountry    = gateway.ShipToCountry;

            //Note that PayPal only returns a phone number if your Merchant accounts is configured to require the
            // buyer to provide it.
            if (gateway.ShipToPhone.Length > 0)
            {
                savedCart.OrderInfo.CustomerTelephoneDay = gateway.ShipToPhone;
            }

            if (gateway.BuyerEmail.Length > 0)
            {
                savedCart.OrderInfo.CustomerEmail = gateway.BuyerEmail;
            }

            // if customer and billing aren't populated already, user was anonymous when checkout began, make them the same as shipping
            //if (savedCart.UserGuid == Guid.Empty)
            //{
            //2013-12-23 since all we get is shipping address this can be considered as the same thing as billing address for paypal purposes so always use it
            // especially because we may need to calculate tax for express checkout
            // based on the address provided by paypal
            savedCart.CopyShippingToBilling();
            savedCart.CopyShippingToCustomer();
            //}

            GeoCountry country = new GeoCountry(savedCart.OrderInfo.DeliveryCountry);
            GeoZone    taxZone = GeoZone.GetByCode(country.Guid, savedCart.OrderInfo.DeliveryState);

            savedCart.OrderInfo.TaxZoneGuid = taxZone.Guid;

            savedCart.OrderInfo.Save();

            // refresh totals to calculate tax or shipping now that we have an address
            savedCart.RefreshTotals();
            savedCart.Save();

            savedCart.SerializeCartOffers();
            payPalLog.SerializedObject = SerializationHelper.SerializeToString(savedCart);

            payPalLog.Save();

            if (gateway.Response == PaymentGatewayResponse.Error)
            {
                redirectUrl = siteRoot + "/WebStore/PayPalGatewayError.aspx?plog=" + payPalLog.RowGuid.ToString();
                return(redirectUrl);
            }

            if (gateway.PayPalPayerId.Length == 0)
            {
                redirectUrl = siteRoot + "/WebStore/PayPalGatewayError.aspx?plog=" + payPalLog.RowGuid.ToString();
                return(redirectUrl);
            }


            int pageId = -1;

            List <PageModule> pageModules = PageModule.GetPageModulesByModule(store.ModuleId);

            foreach (PageModule pm in pageModules)
            {
                // use first pageid found, really a store should only
                // be on one page
                pageId = pm.PageId;
                break;
            }

            // after the CallGetExpressCheckoutDetails
            // we have the option of directing to a final review page before
            // calling CallDoExpressCheckoutPayment

            redirectUrl = siteRoot +
                          "/WebStore/PayPalExpressCheckout.aspx?pageid="
                          + pageId.ToString(CultureInfo.InvariantCulture)
                          + "&mid=" + store.ModuleId.ToString(CultureInfo.InvariantCulture)
                          + "&plog=" + payPalLog.RowGuid.ToString();

            return(redirectUrl);
        }
Exemple #3
0
        private void DoPayPalExpressCeckout()
        {
            string siteRoot = SiteUtils.GetNavigationSiteRoot();

            PayPalExpressGateway gateway = new PayPalExpressGateway(
                commerceConfig.PayPalAPIUsername,
                commerceConfig.PayPalAPIPassword,
                commerceConfig.PayPalAPISignature,
                commerceConfig.PayPalStandardEmailAddress
                )
            {
                UseTestMode      = commerceConfig.PaymentGatewayUseTestMode,
                MerchantCartId   = cart.CartGuid.ToString(),
                ChargeTotal      = cart.OrderTotal,
                ReturnUrl        = siteRoot + "/Services/PayPalReturnHandler.ashx",
                CancelUrl        = siteRoot + Request.RawUrl,
                CurrencyCode     = siteSettings.GetCurrency().Code,
                OrderDescription = store.Name + " " + WebStoreResources.OrderHeading,
                BuyerEmail       = cart.OrderInfo.CustomerEmail,
                ShipToFirstName  = cart.OrderInfo.DeliveryFirstName,
                ShipToLastName   = cart.OrderInfo.DeliveryLastName,
                ShipToAddress    = cart.OrderInfo.DeliveryAddress1,
                ShipToAddress2   = cart.OrderInfo.DeliveryAddress2,
                ShipToCity       = cart.OrderInfo.DeliveryCity,
                ShipToState      = cart.OrderInfo.DeliveryState,
                ShipToCountry    = cart.OrderInfo.DeliveryCountry,
                ShipToPostalCode = cart.OrderInfo.DeliveryPostalCode,
                ShipToPhone      = cart.OrderInfo.CustomerTelephoneDay
            };

            // this tells paypal to use the shipping address we pass in
            // rather than what the customer has on file
            // when we implement shippable products we'll do shipping calculations before
            // sending the user to paypal
            //gateway.OverrideShippingAddress = true;

            //commented out the above, we want user to be able to populate shipping info from their paypal account

            bool executed = gateway.CallSetExpressCheckout();

            if (executed)
            {
                //TODO: log the raw response
                if (gateway.PayPalExpressUrl.Length > 0)
                {
                    cart.SerializeCartOffers();

                    // record the gateway.PayPalToken
                    PayPalLog payPalLog = new PayPalLog
                    {
                        RawResponse      = gateway.RawResponse,
                        ProviderName     = WebStorePayPalReturnHandler.ProviderName,
                        ReturnUrl        = siteRoot + Request.RawUrl,
                        Token            = HttpUtility.UrlDecode(gateway.PayPalToken),
                        RequestType      = "SetExpressCheckout",
                        SerializedObject = SerializationHelper.SerializeToString(cart),
                        CartGuid         = cart.CartGuid,
                        SiteGuid         = store.SiteGuid,
                        StoreGuid        = store.Guid,
                        UserGuid         = cart.UserGuid
                    };

                    payPalLog.Save();

                    Response.Redirect(gateway.PayPalExpressUrl);
                }
                else
                {
                    if (commerceConfig.PaymentGatewayUseTestMode)
                    {
                        lblMessage.Text = gateway.RawResponse;
                    }
                }
            }
            else
            {
                lblMessage.Text = WebStoreResources.TransactionNotInitiatedMessage;

                if (gateway.LastExecutionException != null)
                {
                    if (commerceConfig.PaymentGatewayUseTestMode)
                    {
                        lblMessage.Text = gateway.LastExecutionException.ToString();
                    }
                }
                else
                {
                    if (commerceConfig.PaymentGatewayUseTestMode)
                    {
                        lblMessage.Text = gateway.RawResponse;
                    }
                }
            }
        }
        void btnMakePayment_Click(object sender, EventArgs e)
        {
            PayPalExpressGateway gateway
                = new PayPalExpressGateway(
                      commerceConfig.PayPalAPIUsername,
                      commerceConfig.PayPalAPIPassword,
                      commerceConfig.PayPalAPISignature,
                      commerceConfig.PayPalStandardEmailAddress);

            gateway.UseTestMode   = commerceConfig.PaymentGatewayUseTestMode;
            gateway.PayPalToken   = checkoutDetailsLog.Token;
            gateway.PayPalPayerId = checkoutDetailsLog.PayerId;

            gateway.MerchantCartId = cart.CartGuid.ToString();
            gateway.ChargeTotal    = cart.OrderTotal;
            gateway.ReturnUrl      = SiteRoot + "/Services/PayPalReturnHandler.ashx";
            gateway.CancelUrl      = SiteUtils.GetCurrentPageUrl();
            gateway.CurrencyCode   = siteSettings.GetCurrency().Code;

            // **** here's where the payment is requested ******
            bool executed = gateway.CallDoExpressCheckoutPayment();

            PayPalLog payPalLog = new PayPalLog();

            payPalLog.RequestType      = "DoExpressCheckoutPayment";
            payPalLog.ProviderName     = WebStorePayPalReturnHandler.ProviderName;
            payPalLog.SerializedObject = checkoutDetailsLog.SerializedObject;
            payPalLog.ReturnUrl        = checkoutDetailsLog.ReturnUrl;
            payPalLog.RawResponse      = gateway.RawResponse;

            payPalLog.TransactionId = gateway.TransactionId;
            payPalLog.PaymentType   = gateway.PayPalPaymentType;
            payPalLog.PaymentStatus = gateway.PayPalPaymentStatus;
            payPalLog.PendingReason = gateway.PayPalPendingReason;
            payPalLog.ReasonCode    = gateway.ReasonCode;
            payPalLog.PayPalAmt     = gateway.ChargeTotal;
            payPalLog.FeeAmt        = gateway.PayPalFeeAmount;
            payPalLog.SettleAmt     = gateway.PayPalSettlementAmount;
            payPalLog.TaxAmt        = gateway.PayPalTaxTotal;

            payPalLog.Token        = gateway.PayPalToken;
            payPalLog.PayerId      = gateway.PayPalPayerId;
            payPalLog.RequestType  = "DoExpressCheckoutPayment";
            payPalLog.SiteGuid     = store.SiteGuid;
            payPalLog.StoreGuid    = store.Guid;
            payPalLog.CartGuid     = cart.CartGuid;
            payPalLog.UserGuid     = cart.UserGuid;
            payPalLog.CartTotal    = cart.OrderTotal;
            payPalLog.CurrencyCode = gateway.CurrencyCode;

            if (gateway.PayPalExchangeRate.Length > 0)
            {
                payPalLog.ExchangeRate = decimal.Parse(gateway.PayPalExchangeRate);
            }


            payPalLog.Save();

            if (!executed)
            {
                lblMessage.Text = WebStoreResources.TransactionNotInitiatedMessage;

                if (gateway.LastExecutionException != null)
                {
                    log.Error("ExpressCheckout gateway error", gateway.LastExecutionException);

                    if (commerceConfig.PaymentGatewayUseTestMode)
                    {
                        lblMessage.Text = gateway.LastExecutionException.ToString();
                    }
                }
                else
                {
                    if (commerceConfig.PaymentGatewayUseTestMode)
                    {
                        lblMessage.Text = gateway.RawResponse;
                    }
                }

                return;
            }

            string redirectUrl = string.Empty;

            if (gateway.TransactionId.Length == 0)
            {
                // TODO: redirect where?
                redirectUrl = SiteRoot + "/WebStore/PayPalGatewayError.aspx?plog=" + payPalLog.RowGuid.ToString();
                Response.Redirect(redirectUrl);
            }


            Guid orderStatusGuid;

            if (payPalLog.PaymentStatus == "Completed")
            {
                orderStatusGuid = OrderStatus.OrderStatusFulfillableGuid;
            }
            else
            {
                orderStatusGuid = OrderStatus.OrderStatusReceivedGuid;
            }


            Order order = Order.CreateOrder(
                store,
                cart,
                payPalLog.RawResponse,
                payPalLog.TransactionId,
                string.Empty,
                siteSettings.GetCurrency().Code,
                "PayPal",
                orderStatusGuid);

            StoreHelper.ClearCartCookie(cart.StoreGuid);


            // send confirmation email
            // paypal sends an order confirmation so no need

            // redirect to order details
            redirectUrl = SiteRoot +
                          "/WebStore/OrderDetail.aspx?pageid="
                          + PageId.ToString(CultureInfo.InvariantCulture)
                          + "&mid=" + store.ModuleId.ToString(CultureInfo.InvariantCulture)
                          + "&orderid=" + order.OrderGuid.ToString();

            Response.Redirect(redirectUrl);
        }