public OrderRecord CreateOrder(int customerId, IEnumerable<ShoppingCartItem> items) { if (items == null) throw new ArgumentNullException("items"); // Convert to an array to avoid re-running the enumerable var itemsArray = items.ToArray(); if (!itemsArray.Any()) throw new ArgumentException("Creating an order with 0 items is not supported", "items"); var order = new OrderRecord { //CreatedAt = _dateTimeService.Now, CreatedAt = DateTime.Now, CustomerId = customerId, Status = OrderStatus.New }; _orderRepository.Create(order); // add the shipping charges (if any) //var shippingProductPart = _contentManager.Get<ShippingProductPart>(_webShopSettings.ShippingProductRecord.Id); //if (shippingProductPart != null) //{ // var shippingProduct = shippingProductPart.As<ProductPart>(); // if (shippingProductPart != null) // { // var detail = new OrderDetailRecord // { // OrderRecord_Id = order.Id, // ProductPartRecord_Id = shippingProduct.Record.Id, // Quantity = 1, // UnitPrice = shippingProduct.UnitPrice, // GSTRate = .1m // }; // _orderDetailRepository.Create(detail); // order.Details.Add(detail); // } //} // Get all products in one shot, so we can add the product reference to each order detail //var productIds = itemsArray.Select(x => x.ProductId).ToArray(); //var products = _productRepository.Fetch(x => productIds.Contains(x.Id)).ToArray(); // Create an order detail for each item foreach (var item in itemsArray) { // This is not very fast but it is flexible and gathers the info we need // NOTE the use of a dynamic type below so we don't have to take a dependency on the // external Cascade.ArtStock module for the ArtworkPart. var product = _contentManager.Get<ProductPart>(item.ProductId); var description = _contentManager.GetItemMetadata(product).DisplayText; if (product.ContentItem.Parts.Any(p => p.TypeDefinition.Name == "Artwork")) { dynamic part = product ; description += " (" + part.ArtworkPart.ArtistRecord.Name + ")"; } else description += " (" + product.ContentItem.TypeDefinition.Name + ")"; var detail = new OrderDetailRecord { OrderRecord_Id = order.Id, ProductPartRecord_Id = product.Id, Quantity = item.Quantity, UnitPrice = product.UnitPrice, GSTRate = .1m, Sku = product.Sku, Description = description }; _orderDetailRepository.Create(detail); order.Details.Add(detail); } order.UpdateTotals(); return order; }
private HttpWebRequest BuildSetRequest(PaypalExpressPart paypalExpressPart, OrderRecord order) { // Create the web request HttpWebRequest request = WebRequest.Create(paypalExpressPart.ApiUrl) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // Ensure order totals are up to date order.UpdateTotals(); var pr = new PaypalRequest(paypalExpressPart, "SetExpressCheckout"); pr.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale"); pr.Add("PAYMENTREQUEST_0_AMT", order.SubTotal.ToString("F2")); // before shipping and tax pr.Add("PAYMENTREQUEST_0_ITEMAMT", order.SubTotal.ToString("F2")); // including shipping and tax pr.Add("PAYMENTREQUEST_0_CURRENCYCODE", paypalExpressPart.Currency); pr.Add("returnUrl", paypalExpressPart.SuccessUrl); pr.Add("cancelUrl", paypalExpressPart.CancelUrl); // order details AddAllItems(pr, order); // Set Address SetShippingAddress(pr, order); // format the data pr.SetData(request); return request; }