/// <summary> /// Adds the payment method specified in App.config to the cart and creates an order from the cart. /// If the payment method is unable to be added to the cart then an exception will occur. /// </summary> /// <param name="cartId">Cart to create order from</param> /// <returns>Order ID created in Magento.</returns> public int CreateOrderForCart(int cartId) { var paymentMethod = new CartAddPaymentMethodResource(cartId, ConfigReader.MagentoPaymentMethod); if (_magentoCartController.GetPaymenMethods(cartId).Any(x => x.code == ConfigReader.MagentoPaymentMethod)) { _magentoCartController.AddPaymentMethod(cartId, paymentMethod); return(_magentoCartController.CreateOrder(cartId, paymentMethod)); } else { throw new Exception(string.Format("Unable to create Order for cart {0}. " + "\nNo payment method matching {1} found for cart. " + "\nEnsure that Magento_PaymentMethod is valid in App.config", cartId, ConfigReader.MagentoPaymentMethod)); } }
/// <summary> /// Creates an Order in Magento from a cart with a specified payment method /// </summary> /// <param name="cartId">cart to add payment to</param> /// <param name="paymentMethod">Payment method to add</param> /// <returns>Order ID of order created</returns> public int CreateOrder(int cartId, CartAddPaymentMethodResource paymentMethod) { if (paymentMethod == null) { throw new ArgumentNullException(nameof(paymentMethod)); } var endpoint = UrlFormatter.MagentoCreateAnOrderUrl(cartId); var client = new RestClient(endpoint); var request = new RestRequest(Method.PUT); request.AddHeader("Authorization", string.Format("Bearer {0}", AuthToken)); request.AddHeader("Content-Type", "application/json"); request.AddJsonBody(paymentMethod); var response = client.Execute(request); //Ensure we get the right code CheckStatusCode(response.StatusCode); return(JsonConvert.DeserializeObject <int>(response.Content)); }