private async Task ValidateOrderAsync(HSOrderWorksheet worksheet, OrderCloudIntegrationsCreditCardPayment payment, string userToken) { Require.That( !worksheet.Order.IsSubmitted, new ErrorCode("OrderSubmit.AlreadySubmitted", 400, "Order has already been submitted") ); var shipMethodsWithoutSelections = worksheet.ShipEstimateResponse.ShipEstimates.Where(estimate => estimate.SelectedShipMethodID == null); Require.That( shipMethodsWithoutSelections.Count() == 0, new ErrorCode("OrderSubmit.MissingShippingSelections", 400, "All shipments on an order must have a selection"), shipMethodsWithoutSelections ); var standardLines = worksheet.LineItems.Where(li => li.Product.xp.ProductType != ProductType.PurchaseOrder); Require.That( !standardLines.Any() || payment != null, new ErrorCode("OrderSubmit.MissingPayment", 400, "Order contains standard line items and must include credit card payment details"), standardLines ); var lineItemsInactive = await GetInactiveLineItems(worksheet, userToken); Require.That( !lineItemsInactive.Any(), new ErrorCode("OrderSubmit.InvalidProducts", 400, "Order contains line items for products that are inactive"), lineItemsInactive ); try { // ordercloud validates the same stuff that would be checked on order submit await _oc.Orders.ValidateAsync(OrderDirection.Incoming, worksheet.Order.ID); } catch (OrderCloudException ex) { // this error is expected and will be resolved before oc order submit call happens // in a non-seb flow this could be removed because we'd auth the payment which would mark it as accepted // before it even hits the submit endpoint var errors = ex.Errors.Where(ex => ex.ErrorCode != "Order.CannotSubmitWithUnaccceptedPayments"); if (errors.Any()) { throw new OrderCloudIntegrationException(new ApiError { ErrorCode = "OrderSubmit.OrderCloudValidationError", Message = "Failed ordercloud validation, see Data for details", Data = errors }); } } }
private string GetMerchantID(OrderCloudIntegrationsCreditCardPayment payment) { string merchantID; if (payment.Currency == "USD") { merchantID = _settings.CardConnectSettings.UsdMerchantID; } else if (payment.Currency == "CAD") { merchantID = _settings.CardConnectSettings.CadMerchantID; } else { merchantID = _settings.CardConnectSettings.EurMerchantID; } return(merchantID); }
public async Task <Payment> Post([FromBody] OrderCloudIntegrationsCreditCardPayment payment) { string merchantID; if (payment.Currency == "USD") { merchantID = _settings.CardConnectSettings.UsdMerchantID; } else if (payment.Currency == "CAD") { merchantID = _settings.CardConnectSettings.CadMerchantID; } else { merchantID = _settings.CardConnectSettings.EurMerchantID; } return(await _card.AuthorizePayment(payment, VerifiedUserContext.AccessToken, merchantID)); }
private async Task ValidateOrderAsync(HSOrderWorksheet worksheet, OrderCloudIntegrationsCreditCardPayment payment, string userToken) { Require.That( !worksheet.Order.IsSubmitted, new ErrorCode("OrderSubmit.AlreadySubmitted", 400, "Order has already been submitted") ); var shipMethodsWithoutSelections = worksheet?.ShipEstimateResponse?.ShipEstimates?.Where(estimate => estimate.SelectedShipMethodID == null); Require.That( worksheet?.ShipEstimateResponse != null && shipMethodsWithoutSelections.Count() == 0, new ErrorCode("OrderSubmit.MissingShippingSelections", 400, "All shipments on an order must have a selection"), shipMethodsWithoutSelections ); Require.That( !worksheet.LineItems.Any() || payment != null, new ErrorCode("OrderSubmit.MissingPayment", 400, "Order contains standard line items and must include credit card payment details"), worksheet.LineItems ); var lineItemsInactive = await GetInactiveLineItems(worksheet, userToken); Require.That( !lineItemsInactive.Any(), new ErrorCode("OrderSubmit.InvalidProducts", 400, "Order contains line items for products that are inactive"), lineItemsInactive ); try { // ordercloud validates the same stuff that would be checked on order submit await _oc.Orders.ValidateAsync(OrderDirection.Incoming, worksheet.Order.ID); } catch (OrderCloudException ex) { // credit card payments aren't accepted yet, so ignore this error for now // we'll accept the payment once the credit card auth goes through (before order submit) var errors = ex.Errors.Where(ex => ex.ErrorCode != "Order.CannotSubmitWithUnaccceptedPayments"); if (errors.Any()) { throw new CatalystBaseException("OrderSubmit.OrderCloudValidationError", 400, "Failed ordercloud validation, see Data for details", errors); } } }
public async Task <HSOrder> Submit(OrderDirection direction, string orderID, [FromBody] OrderCloudIntegrationsCreditCardPayment payment) { return(await _orderSubmitCommand.SubmitOrderAsync(orderID, direction, payment, UserContext.AccessToken)); }
public async Task <HSOrder> SubmitOrderAsync(string orderID, OrderDirection direction, OrderCloudIntegrationsCreditCardPayment payment, string userToken) { var worksheet = await _oc.IntegrationEvents.GetWorksheetAsync <HSOrderWorksheet>(OrderDirection.Incoming, orderID); await ValidateOrderAsync(worksheet, payment, userToken); var incrementedOrderID = await IncrementOrderAsync(worksheet); if (worksheet.LineItems.Any(li => li.Product.xp.ProductType != ProductType.PurchaseOrder)) { payment.OrderID = incrementedOrderID; await _card.AuthorizePayment(payment, userToken, GetMerchantID(payment)); } try { return(await WithRetry().ExecuteAsync(() => _oc.Orders.SubmitAsync <HSOrder>(direction, incrementedOrderID, userToken))); } catch (Exception) { await _card.VoidPaymentAsync(incrementedOrderID, userToken); throw; } }
public async Task <HSOrder> SubmitOrderAsync(string orderID, OrderDirection direction, OrderCloudIntegrationsCreditCardPayment payment, string userToken) { var worksheet = await _oc.IntegrationEvents.GetWorksheetAsync <HSOrderWorksheet>(OrderDirection.Incoming, orderID); await ValidateOrderAsync(worksheet, payment, userToken); var incrementedOrderID = await IncrementOrderAsync(worksheet); // If Credit Card info is null, payment is a Purchase Order, thus skip CC validation if (payment.CreditCardDetails != null || payment.CreditCardID != null) { payment.OrderID = incrementedOrderID; await _card.AuthorizePayment(payment, userToken, GetMerchantID(payment)); } try { return(await _oc.Orders.SubmitAsync <HSOrder>(direction, incrementedOrderID, userToken)); } catch (Exception) { await _card.VoidPaymentAsync(incrementedOrderID, userToken); throw; } }