Skip to content

Commit

Permalink
Merge pull request #196 from HotcakesCommerce/Issues/Issue-112
Browse files Browse the repository at this point in the history
PR #112 : CI: Server cannot append header after HTTP headers have been sent
  • Loading branch information
Will Strohl authored Jun 1, 2019
2 parents 33e0509 + 5cb6fd7 commit b688b0f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 64 deletions.
62 changes: 35 additions & 27 deletions Website/DesktopModules/Hotcakes/Core/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void CheckFreeItems(CartViewModel model)

if (freeItem != null)
{
var ids = freeItem.Value.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
var ids = freeItem.Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

if (ids.Any())
{
Expand Down Expand Up @@ -121,7 +121,7 @@ private void CheckFreeItems(CartViewModel model)
if (flag != null)
{
freePromotions =
flag.Value.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
flag.Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => long.Parse(s))
.ToList();
}
Expand Down Expand Up @@ -222,12 +222,12 @@ private bool CheckForStockOnItems(CartViewModel model)
return false;
}

public void ForwardToCheckout(CartViewModel model)
public RedirectResult ForwardToCheckout(CartViewModel model)
{
if (Request["paypalexpress"] != null && Request["paypalexpress"] == "true")
{
ForwardToPayPalExpress(model);
return;
return null;
}

var c = new OrderTaskContext
Expand All @@ -238,7 +238,7 @@ public void ForwardToCheckout(CartViewModel model)

if (Workflow.RunByName(c, WorkflowNames.VerifyOrderSize))
{
Response.Redirect(Url.RouteHccUrl(HccRoute.Checkout, null, Uri.UriSchemeHttps));
return Redirect(Url.RouteHccUrl(HccRoute.Checkout, null, Uri.UriSchemeHttps));
}
else
{
Expand All @@ -259,6 +259,7 @@ public void ForwardToCheckout(CartViewModel model)
FlashFailure(Localization.GetString("CheckoutFailed"));
}
}
return null;
}

public void ForwardToPayPalExpress(CartViewModel model)
Expand All @@ -271,7 +272,7 @@ public void ForwardToPayPalExpress(CartViewModel model)
};

var checkoutFailed = false;

if (!Workflow.RunByName(c, WorkflowNames.VerifyOrderSize))
{
checkoutFailed = true;
Expand Down Expand Up @@ -350,7 +351,11 @@ public ActionResult IndexPost()
{
if (CheckForStockOnItems(model))
{
ForwardToCheckout(model);
var redirectUrl = ForwardToCheckout(model);
if (redirectUrl != null)
{
return Redirect(redirectUrl.Url);
}
}
}
else
Expand Down Expand Up @@ -472,7 +477,7 @@ public ActionResult RemoveCoupon()

private CartViewModel IndexSetup()
{
var model = new CartViewModel {KeepShoppingUrl = GetKeepShoppingLocation()};
var model = new CartViewModel { KeepShoppingUrl = GetKeepShoppingLocation() };
SetPayPalVisibility(model);
return model;
}
Expand All @@ -494,20 +499,20 @@ private void SetPayPalVisibility(CartViewModel model)
model.PayPalExpressAvailable = enabledMethods.Any(m => m.MethodId == PaymentMethods.PaypalExpressId);
}

private void HandleActionParams()
private RedirectResult HandleActionParams()
{
//1. AddSKU - this is the sku that should be added to the cart automatically
//2. AddSKUQTY - this is the quantity of the provided sku that should be added.
//3. CouponCode - the coupon that should automatically be applied, if valid.
//4. RedirectToCheckout=True - redirects the user automatically to the checkout.
if (ModuleContext == null || ModuleContext.Configuration.DesktopModule.ModuleName != "Hotcakes.Cart")
return;
return null;

var clearCart = (string) RouteData.Values["ClearCart"];
var addSKU = (string) RouteData.Values["AddSKU"];
var addSKUQTY = (string) RouteData.Values["AddSKUQTY"];
var CouponCode = (string) RouteData.Values["CouponCode"];
var RedirectToCheckout = (string) RouteData.Values["RedirectToCheckout"];
var clearCart = (string)RouteData.Values["ClearCart"];
var addSKU = (string)RouteData.Values["AddSKU"];
var addSKUQTY = (string)RouteData.Values["AddSKUQTY"];
var CouponCode = (string)RouteData.Values["CouponCode"];
var RedirectToCheckout = (string)RouteData.Values["RedirectToCheckout"];

if (!string.IsNullOrWhiteSpace(addSKU)
|| !string.IsNullOrWhiteSpace(addSKUQTY)
Expand All @@ -528,13 +533,13 @@ private void HandleActionParams()
if (!string.IsNullOrWhiteSpace(addSKUQTY))
SKUQTYs = addSKUQTY.Split(',');
if (addSKU == null)
return;
return null;
if (SKUQTYs == null)
SKUQTYs = new string[SKUs.Length];
if (SKUQTYs != null && SKUs.Length != SKUQTYs.Length)
{
FlashFailure(Localization.GetString("ParamsCountMismatch"));
return;
return null;
}

var errorsPresent = false;
Expand Down Expand Up @@ -579,15 +584,17 @@ private void HandleActionParams()
if (redirect)
Response.Redirect(Url.RouteHccUrl(HccRoute.Checkout));
}
Response.Redirect(Url.RouteHccUrl(HccRoute.Cart));
return Redirect(Url.RouteHccUrl(HccRoute.Cart));

}
return null;
}

private void CheckForQuickAdd()
private RedirectResult CheckForQuickAdd()
{
if ((bool) (RouteData.Values["MiniCart"] ?? false))
if ((bool)(RouteData.Values["MiniCart"] ?? false))
{
return;
return null;
}

if (Request.QueryString["quickaddid"] != null
Expand All @@ -612,7 +619,7 @@ private void CheckForQuickAdd()
}
AddSingleProduct(prod, quantity);
}
Response.Redirect(Url.RouteHccUrl(HccRoute.Cart));
return Redirect(Url.RouteHccUrl(HccRoute.Cart));
}
else if (Request.QueryString["quickaddsku"] != null)
{
Expand All @@ -631,7 +638,7 @@ private void CheckForQuickAdd()
}
AddSingleProduct(prod, quantity);
}
Response.Redirect(Url.RouteHccUrl(HccRoute.Cart));
return Redirect(Url.RouteHccUrl(HccRoute.Cart));
}
else if (Request.QueryString["multi"] != null)
{
Expand Down Expand Up @@ -660,8 +667,9 @@ private void CheckForQuickAdd()
}
}
}
Response.Redirect(Url.RouteHccUrl(HccRoute.Cart));
return Redirect(Url.RouteHccUrl(HccRoute.Cart));
}
return null;
}

private void LoadCart(CartViewModel model)
Expand Down Expand Up @@ -691,10 +699,10 @@ private void LoadCart(CartViewModel model)
product.ImageFileSmall,
lineItem.VariantId, Request.IsSecureConnection),
LinkUrl = UrlRewriter.BuildUrlForProduct(product,
new {lineItem.OrderBvin, LineItemId = lineItem.Id}),
new { lineItem.OrderBvin, LineItemId = lineItem.Id }),
HasDiscounts = lineItem.HasAnyDiscounts
};

model.LineItems.Add(ci);
}
else
Expand Down Expand Up @@ -729,7 +737,7 @@ private void ValidateOrderCoupons()
.ToList();

var promotionCoupons =
couponQulifications.SelectMany(q => ((OrderHasCoupon) q).CurrentCoupons()).ToList();
couponQulifications.SelectMany(q => ((OrderHasCoupon)q).CurrentCoupons()).ToList();
var isMet = promotionCoupons.Contains(coupon);
if (!bestMatchIsMet && isMet)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public partial class CheckoutController : BaseStoreController
protected bool ShowConfirmation
{
get { return false; }
}
}

protected bool IsOrderConfirmed
{
get { return Request.Params["hcOrderConfirmed"] == "true"; }
}
}

#endregion

Expand All @@ -87,9 +87,9 @@ public ActionResult Index()
{
var model = LoadCheckoutModel();
HccApp.AnalyticsService.RegisterEvent(HccApp.CurrentCustomerId, ActionTypes.GoToChekout, null);
VerifySessionError(model);
VerifySessionError(model);
RenderErrorSummary(model);
CheckFreeItems(model);
CheckFreeItems(model);

return View(model);
}
Expand All @@ -109,10 +109,14 @@ public ActionResult IndexPost()
{
if (ValidateOrder(model))
{
AddPaymentInfoTransaction(model);
HccApp.OrderServices.Orders.Update(model.CurrentOrder);
AddPaymentInfoTransaction(model);
HccApp.OrderServices.Orders.Update(model.CurrentOrder);

ProcessOrder(model);
var redirectUrl = ProcessOrder(model);
if (redirectUrl != null)
{
return Redirect(redirectUrl.RouteValues.Values.First().ToString());
}
}
}
else
Expand Down Expand Up @@ -318,7 +322,7 @@ public ActionResult AddNewGiftCard(FormCollection form)
if (gcBalance.CurrentValue > 0 && gcBalance.Success)
{
var toApply = Math.Min(gcBalance.CurrentValue, totalDue);
payManager.GiftCardAddInfo(new GiftCardData {CardNumber = cardNumber}, toApply);
payManager.GiftCardAddInfo(new GiftCardData { CardNumber = cardNumber }, toApply);
}
else
{
Expand Down Expand Up @@ -390,7 +394,7 @@ private CheckoutViewModel LoadCheckoutModel()
if (CurrentCart == null || CurrentCart.Items == null || CurrentCart.Items.Count == 0)
Response.Redirect(Url.RouteHccUrl(HccRoute.Cart));

var model = new CheckoutViewModel {CurrentOrder = CurrentCart};
var model = new CheckoutViewModel { CurrentOrder = CurrentCart };

LoadCurrentCustomer(model);
LoadGiftCards(model);
Expand All @@ -411,15 +415,15 @@ private CheckoutViewModel LoadCheckoutModel()
return model;
}

private void VerifySessionError(CheckoutViewModel model)
{
var sessionError = SessionManager.GetSessionObject("SessionError_" + model.CurrentOrder.bvin);
if (sessionError != null && sessionError.ToString() == bool.TrueString)
{
model.Violations.Add(new RuleViolation("", "", "Whoops! Your previous session expired. We're sorry, but your previous order can't be placed, and you'll need to checkout again."));
SessionManager.SetSessionObject("SessionError_" + model.CurrentOrder.bvin, "");
}
}
private void VerifySessionError(CheckoutViewModel model)
{
var sessionError = SessionManager.GetSessionObject("SessionError_" + model.CurrentOrder.bvin);
if (sessionError != null && sessionError.ToString() == bool.TrueString)
{
model.Violations.Add(new RuleViolation("", "", "Whoops! Your previous session expired. We're sorry, but your previous order can't be placed, and you'll need to checkout again."));
SessionManager.SetSessionObject("SessionError_" + model.CurrentOrder.bvin, "");
}
}

private void LoadCurrentCustomer(CheckoutViewModel model)
{
Expand Down Expand Up @@ -794,18 +798,18 @@ private void LoadPaymentFromForm(CheckoutViewModel model)
}
else
{
if (ShowConfirmation)
{
SessionManager.CardSecurityCode = cardSecurityCode;
}
if (ShowConfirmation)
{
SessionManager.CardSecurityCode = cardSecurityCode;
}
}
payModel.DataCreditCard.SecurityCode = cardSecurityCode;

payModel.NoPaymentNeeded = (order.TotalGrandAfterStoreCredits(HccApp.OrderServices) <= 0) &&
!order.IsRecurring;

//Add Payment info only if form is valid and order needs to be processed
//AddPaymentInfoTransaction(model);
//AddPaymentInfoTransaction(model);
}

private void AddPaymentInfoTransaction(CheckoutViewModel model)
Expand Down Expand Up @@ -1045,13 +1049,13 @@ private List<RuleViolation> ValidatePayment(CheckoutViewModel model)
return violations;
}
}
// We haven't return anything so nothing is selected.
// Try CC as default payment method
if (model.PaymentViewModel.DataCreditCard.CardNumber.Length > 12)
{
model.PaymentViewModel.SelectedMethodId = PaymentMethods.CreditCardId;
return ValidateCreditCard(model);
}
// We haven't return anything so nothing is selected.
// Try CC as default payment method
if (model.PaymentViewModel.DataCreditCard.CardNumber.Length > 12)
{
model.PaymentViewModel.SelectedMethodId = PaymentMethods.CreditCardId;
return ValidateCreditCard(model);
}

// nothing selected, trial of cc failed
violations.Add(new RuleViolation("Payment Method", string.Empty, Localization.GetString("SelectPaymentMethod"), string.Empty));
Expand Down Expand Up @@ -1101,7 +1105,7 @@ private List<RuleViolation> ValidateCreditCard(CheckoutViewModel model)
return violations;
}

private void ProcessOrder(CheckoutViewModel model)
private RedirectToRouteResult ProcessOrder(CheckoutViewModel model)
{
// Save as Order
var c = new OrderTaskContext
Expand Down Expand Up @@ -1143,14 +1147,14 @@ private void ProcessOrder(CheckoutViewModel model)
var tempOrder = HccApp.OrderServices.Orders.FindForCurrentStore(model.CurrentOrder.bvin);
HccApp.CurrentRequestContext.IntegrationEvents.OrderReceived(tempOrder, HccApp);
SessionManager.AnalyticsOrderId = model.CurrentOrder.bvin;
Response.Redirect(Url.RouteHccUrl(HccRoute.Checkout,
new {action = "receipt", id = model.CurrentOrder.bvin}));
return RedirectToAction(Url.RouteHccUrl(HccRoute.Checkout,
new { action = "receipt", id = model.CurrentOrder.bvin }));
}
else
{
// Redirect to Payment Error
SessionManager.SetCurrentPaymentPendingCartId(HccApp.CurrentStore, model.CurrentOrder.bvin);
Response.Redirect(Url.RouteHccUrl(HccRoute.Checkout, new {action = "paymenterror"}));
return RedirectToAction(Url.RouteHccUrl(HccRoute.Checkout, new { action = "paymenterror" }));
}
}
else
Expand All @@ -1170,6 +1174,7 @@ private void ProcessOrder(CheckoutViewModel model)
}
}
}
return null;
}

private void RenderAnalytics(Order o)
Expand Down Expand Up @@ -1262,7 +1267,7 @@ private void PopulateAddress(Address address, FormCollection form, string prefix
address.RegionBvin = region.Abbreviation;
}
}

private void RenderErrorSummary(CheckoutViewModel model)
{
foreach (var v in model.Violations)
Expand All @@ -1281,7 +1286,7 @@ private void CheckFreeItems(CheckoutViewModel model)

if (freeItem != null)
{
var ids = freeItem.Value.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
var ids = freeItem.Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

if (ids.Any())
{
Expand All @@ -1297,4 +1302,4 @@ private void CheckFreeItems(CheckoutViewModel model)

#endregion
}
}
}

0 comments on commit b688b0f

Please sign in to comment.