public async void PlaceOrder_Success() { var svc = CreateService(); PflOrderModel order = new PflOrderModel() { OrderCustomer = new PflCustomerModel() { FirstName = "Joe", LastName = "Schmoe", Address1 = "123 Main St", City = "Bozeman", State = "MT", PostalCode = "59715", CountryCode = "US", Phone = "1234567890", Email = "*****@*****.**" }, Items = new List <PflOrderItemModel>() { new PflOrderItemModel() { ItemSequenceNumber = 1, PartnerItemReference = "SomeRefID", ProductId = 9876, Quantity = 1 } }, Shipments = new List <PflShipmentModel>() { new PflShipmentModel() { ShipmentSequenceNumber = 1, FirstName = "Joe", LastName = "Schmoe", Address1 = "123 Main St", City = "Bozeman", State = "MT", PostalCode = "59715", CountryCode = "US", Phone = "1234567890", ShippingMethod = "FDXG", Email = "*****@*****.**" } }, ItemShipments = new List <PflItemShipmentModel>() { new PflItemShipmentModel() { ItemSequenceNumber = 1, ShipmentSequenceNumber = 1 } }, PartnerOrderReference = "SomePartnerOrderID" }; var orderNumber = await svc.PlaceOrderAsync(order); Assert.NotNull(orderNumber); Assert.True(orderNumber.Length > 0); }
/// <summary> /// Place an Order with the PFL Apis and return an Order Number /// </summary> /// <param name="order">Order to be placed</param> /// <returns>Order number that was placed successfully</returns> public async Task <string> PlaceOrderAsync(PflOrderModel order) { if (order == null) { throw new ArgumentNullException("order"); } try { _logger.LogDebug("Attempting to place order into PFL."); Uri pflUri = new Uri(new Uri(_pflLinkConfig.Url), "/orders?apikey=" + Uri.EscapeUriString(DataEncryption.DecryptData(_logger, _x509CertConfig, _pflLinkConfig.ApiKey))); var response = await CallApiAsync(pflUri, HttpMethod.Post, JsonConvert.SerializeObject(order)); if (response != null) { var orderResp = JsonConvert.DeserializeObject <PflOrderResponse>(response); if (orderResp.Meta.StatusCode != 200) { _logger.LogError("Error ststus returned from PFL Api. Details: {errors}", orderResp.Results.Errors); return(null); } else { _logger.LogInformation("Order #{order} placed with PFL", orderResp.Data.OrderNumber); return(orderResp.Data.OrderNumber); } } else { throw new Exception("Something bad happened. Did not receive response during Product Order call."); } } catch (Exception e) { _logger.LogError("Exception while placing order with PFL. Reason: {reason}", e.Message); throw e; } finally { _logger.LogDebug("Finished attempting to place order into PFL."); } }
public async Task <IActionResult> PlaceOrder([FromBody] List <PflOrderItemModel> items) { try { _logger.LogDebug("Attempting to place order"); if (items == null || items.Count == 0) { _logger.LogError("Placing an Order requires that there is at least one Item to be placed"); return(new BadRequestObjectResult("Missing list of order items")); } // Grab Customer Information from DB PflCustomerModel customer = GetCustomerFromIdentity(); if (customer == null) { _logger.LogError("Customer not defined. Order cannot be placed."); return(new BadRequestObjectResult("Customer cannot be found. Order will not be placed")); } PflShipmentModel shipment = new PflShipmentModel() { ShipmentSequenceNumber = 1, FirstName = customer.FirstName, LastName = customer.LastName, Address1 = customer.Address1, City = customer.City, CompanyName = customer.CompanyName, CountryCode = customer.CountryCode, Email = customer.Email, Phone = customer.Phone, PostalCode = customer.PostalCode, State = customer.State }; PflOrderModel order = new PflOrderModel() { Items = items, OrderCustomer = customer, Shipments = new List <PflShipmentModel>() { shipment } }; var orderNumber = await _pflApiSvc.PlaceOrderAsync(order); if (String.IsNullOrEmpty(orderNumber)) { string msg = "Order could not be placed successfully."; _logger.LogError(msg); return(new BadRequestObjectResult(msg)); } else { order.OrderNumber = orderNumber; } _logger.LogInformation("Successfully ordered {count} items. Order number is [{orderNumber}]", items.Count, orderNumber); return(new OkObjectResult(order)); } catch (Exception e) { _logger.LogError("Unable to process order due to exception. Message: {message}; StackTrace: {stacktrace}", e.Message, e.StackTrace); return(new BadRequestObjectResult("Unable to process order. " + e.Message)); } finally { _logger.LogDebug("Finished attempt to place order"); } }