/// <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)); }
public ActionResult PaymentInfo(CheckoutPaymentInfoViewModel viewModel) { StoreFrontConfiguration config = CurrentStoreFrontConfigOrThrow; Cart cart = config.StoreFront.GetCart(Session.SessionID, CurrentUserProfileOrNull); if (!cart.CartIsValidForCheckout(this)) { return(RedirectToAction("Index", "Cart")); } if (!cart.StatusStartedCheckout) { return(RedirectToAction("Index")); } if (!cart.StatusSelectedLogInOrGuest) { return(RedirectToAction("LogInOrGuest")); } cart = cart.ValidateCartAndSave(this); if (!cart.StatusCompletedDeliveryInfo) { return(RedirectToAction("DeliveryInfo")); } if (!cart.StatusSelectedDeliveryMethod) { return(RedirectToAction("DeliveryMethod")); } if (config.CheckoutPaymentInfoWebForm != null) { FormProcessorExtensions.ValidateFields(this, config.CheckoutPaymentInfoWebForm); } if (ModelState.IsValid) { WebFormResponse webFormResponse = cart.PaymentInfoProcessWebForm(this); if (config.PaymentMethod_PayPal_Enabled) { return(Payment_PayPalStartPayment(viewModel, webFormResponse)); } //payment with pay after order/no automated processing CartPaymentInfo cartPaymentInfo = null; if (cart.CartPaymentInfo == null) { cartPaymentInfo = GStoreDb.CartPaymentInfos.Create(); cartPaymentInfo.SetDefaults(CurrentUserProfileOrNull); cartPaymentInfo.Client = CurrentClientOrThrow; cartPaymentInfo.ClientId = cartPaymentInfo.Client.ClientId; cartPaymentInfo.StoreFront = CurrentStoreFrontOrThrow; cartPaymentInfo.StoreFrontId = cartPaymentInfo.StoreFront.StoreFrontId; cartPaymentInfo.StartDateTimeUtc = DateTime.UtcNow.AddMinutes(-1); cartPaymentInfo.EndDateTimeUtc = DateTime.UtcNow.AddYears(100); cartPaymentInfo.Cart = cart; cartPaymentInfo.CartId = cart.CartId; if (webFormResponse != null) { cartPaymentInfo.WebFormResponseId = webFormResponse.WebFormResponseId; } cartPaymentInfo = GStoreDb.CartPaymentInfos.Add(cartPaymentInfo); } else { cartPaymentInfo = cart.CartPaymentInfo; if (webFormResponse != null) { cartPaymentInfo.WebFormResponseId = webFormResponse.WebFormResponseId; } cartPaymentInfo = GStoreDb.CartPaymentInfos.Update(cartPaymentInfo); } //add/remove/etc cart.StatusPaymentInfoConfirmed = true; GStoreDb.Carts.Update(cart); GStoreDb.SaveChanges(); GStoreDb.LogUserActionEvent(HttpContext, RouteData, this, UserActionCategoryEnum.Checkout, UserActionActionEnum.Checkout_ConfirmedPaymentInfo, "", true, cartId: cart.CartId); return(RedirectToAction("ConfirmOrder")); } viewModel.UpdateForRepost(config, cart, RouteData.Action()); return(View("PaymentInfo", viewModel)); }