Ejemplo n.º 1
0
        /// <summary>
        /// Starts a PayPal payment and returns a redirect result to PayPal (or payment info page if an error occurs)
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        protected ActionResult Payment_PayPalStartPayment(CheckoutPaymentInfoViewModel viewModel, WebFormResponse webFormResponse)
        {
            StoreFrontConfiguration storeFrontConfig = CurrentStoreFrontConfigOrThrow;
            Cart cart = storeFrontConfig.StoreFront.GetCart(Session.SessionID, CurrentUserProfileOrNull);

            Uri returnUri = new Uri(Url.Action("PayPalAccountConfirmed", null, null, Request.Url.Scheme));
            Uri cancelUri = new Uri(Url.Action("PayPalCanceled", null, null, Request.Url.Scheme));

            PayPalPaymentClient paypalClient = new PayPalPaymentClient();

            PayPalPaymentData response;
            try
            {
                response = paypalClient.StartPayPalPayment(storeFrontConfig, cart, returnUri, cancelUri);
            }
            catch(PayPalExceptionOAuthFailed exOAuth)
            {
                string message = "Sorry, this store's configuration for PayPal OAuth is not operational. Please contact us for other payment options."
                    + (exOAuth.IsSandbox ? "\nError in Sandbox Config." : "\nError in Live Config");
                AddUserMessage("PayPal Error", message, UserMessageType.Danger);

                if (CurrentUserProfileOrNull != null && CurrentUserProfileOrThrow.AspNetIdentityUserIsInRoleSystemAdmin())
                {
                    string adminMessage = exOAuth.ToString()
                        + "\n\nHTTP Response:\n" + exOAuth.ResponseString
                        + "\n\nHTTP Headers:\n" + exOAuth.ResponseHeaders;
                    AddUserMessage("PayPal Error (admin info)", "Error " + adminMessage, UserMessageType.Danger);
                }

                return RedirectToAction("PaymentInfo");
            }
            catch(PayPalExceptionCreatePaymentFailed exPaymentFailed)
            {
                string message = "Sorry, there was an error sending your order to PayPal for payment. Please contact us for other payment options."
                    + (exPaymentFailed.IsSandbox ? "\nError in Sandbox." : "\nError in Live Site.");

                AddUserMessage("PayPal Error", message, UserMessageType.Danger);

                if (CurrentUserProfileOrNull != null && CurrentUserProfileOrThrow.AspNetIdentityUserIsInRoleSystemAdmin())
                {
                    string adminMessage = exPaymentFailed.ToString()
                        + "\n\nHTTP Response:\n" + exPaymentFailed.ResponseString
                        + "\n\nHTTP Headers:\n" + exPaymentFailed.ResponseHeaders;
                    AddUserMessage("PayPal Error (admin info)", "Error " + adminMessage, UserMessageType.Danger);
                }

                return RedirectToAction("PaymentInfo");
            }
            catch (Exception ex)
            {
                string message = "Sorry, there was an error starting starting your order with PayPal. Please contact us for other payment options.";
                AddUserMessage("PayPal Error", message, UserMessageType.Danger);

                if (CurrentUserProfileOrNull != null && CurrentUserProfileOrThrow.AspNetIdentityUserIsInRoleSystemAdmin())
                {
                    string adminMessage = "Exception: " + ex.ToString();
                    AddUserMessage("PayPal Error (admin info)", "Error " + adminMessage, UserMessageType.Danger);
                }
                return RedirectToAction("PaymentInfo");
            }

            CartPaymentInfo cartPaymentInfo = cart.CartPaymentInfo;
            if (cartPaymentInfo == null)
            {
                cartPaymentInfo = GStoreDb.CartPaymentInfos.Create();
                cartPaymentInfo.SetFromPayPalResponse(cart, response);
                if (webFormResponse != null)
                {
                    cartPaymentInfo.WebFormResponseId = webFormResponse.WebFormResponseId;
                }
                cartPaymentInfo = GStoreDb.CartPaymentInfos.Add(cartPaymentInfo);
            }
            else
            {
                cartPaymentInfo.SetFromPayPalResponse(cart, response);
                if (webFormResponse != null)
                {
                    cartPaymentInfo.WebFormResponseId = webFormResponse.WebFormResponseId;
                }
                cartPaymentInfo = GStoreDb.CartPaymentInfos.Update(cartPaymentInfo);
            }

            GStoreDb.SaveChanges();

            cart.CartPaymentInfoId = cartPaymentInfo.CartPaymentInfoId;
            cart.StatusPaymentInfoConfirmed = false;
            cart = GStoreDb.Carts.Update(cart);
            GStoreDb.SaveChanges();

            PayPalLinkData confirmLink = response.links.Where(l => l.rel == "approval_url").SingleOrDefault();
            if (string.IsNullOrEmpty(confirmLink.href))
            {
                string message = "Sorry, there was an error getting your order info from PayPal. Please contact us for other payment options.";
                AddUserMessage("PayPal Error", message, UserMessageType.Danger);
                if (CurrentUserProfileOrNull != null && CurrentUserProfileOrThrow.AspNetIdentityUserIsInRoleSystemAdmin())
                {
                    string adminMessage = "PayPal Response parse error. Cannot find link with method: approval_url";
                    AddUserMessage("PayPal Error (admin info)", "Error " + adminMessage, UserMessageType.Danger);
                }
                return RedirectToAction("PaymentInfo");
            }

            return Redirect(confirmLink.href);
        }
Ejemplo n.º 2
0
 public CheckoutWebFormInfo(CheckoutViewModelBase checkoutViewModel, WebForm webForm, WebFormResponse webFormResponse)
 {
     this.CheckoutViewModel = checkoutViewModel;
     this.WebForm = webForm;
     this.WebFormResponse = webFormResponse;
 }