public void LogTransaction(OrderRecord order, PaypalExpressResult result)
 {
     TransactionRecord transaction = new TransactionRecord { 
         Token = result.Token, 
         Ack = result.Ack, 
         Method = result.Method, 
         OrderRecord_Id=order.Id, 
         DateTime=DateTime.Now,
         Timestamp=result.Timestamp,
         ErrorCodes=result.ErrorCodes,
         ShortMessages=result.ShortMessages,
         LongMessages=result.LongMessages
     };
     _paypalTransactions.Create(transaction);
 }
        /// <summary>
        /// Call the Paypal SetExpressCheckout API (https://www.x.com/developers/paypal/documentation-tools/api/setexpresscheckout-api-operation-nvp)
        /// </summary>
        /// <param name="paypalExpressPart">part</param>
        /// <param name="order">WebShop order</param>
        /// <returns>result containing token and ack</returns>
        public PaypalExpressResult SetExpressCheckout(PaypalExpressPart paypalExpressPart, OrderRecord order)
        {
            PaypalExpressResult result = new PaypalExpressResult { Method = "SetExpressCheckout" };
            HttpWebRequest request = BuildSetRequest(paypalExpressPart, order);

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                string[] lines = GetResponseLines(response);
                result.Ack = GetValue(lines, "Ack").FirstOrDefault();
                result.Token = GetValue(lines, "Token").FirstOrDefault();
                result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault();
                if (string.Compare(result.Ack, "success", true) != 0)
                    ExtractErrors(lines, result);
            }

            return result;
        }
        private dynamic BuildModel(OrderRecord order, EditOrderVM editModel)
        {
            CustomerPart customer = _customerService.GetCustomer(order.CustomerId);
            AddressPart shipAddressPart = _customerService.GetShippingAddress(customer.Id);
            AddressPart custAddressPart = _customerService.GetAddress(customer.Id, "InvoiceAddress");

            string shipName = string.Empty;
            if (!string.IsNullOrWhiteSpace(shipAddressPart.Name))
                shipName = shipAddressPart.Name;

            string shipAddress = shipAddressPart.Address;
            string shipAddress2 = shipAddressPart.City + " " + shipAddressPart.State + " " + shipAddressPart.Postcode;
            string shipCountry = shipAddressPart.Country;

            string custAddress = custAddressPart.Address;
            string custAddress2 = custAddressPart.City + " " + custAddressPart.State + " " + custAddressPart.Postcode;
            string custCountry = custAddressPart.Country;

            return Shape.Order(
                Order: order,
                CustomerName: customer.FullName,
                CustomerAddress1: custAddress,
                CustomerAddress2: custAddress2,
                CustomerCountry: custCountry,
                ShippingName: shipName,
                ShippingAddress1: shipAddress,
                ShippingAddress2: shipAddress2,
                ShippingCountry : shipCountry,
                Details: order.Details.Select( detail =>
                    Shape.Detail
                    (
                        Sku: detail.Sku,
                        Price: detail.UnitPrice,
                        Quantity: detail.Quantity,
                        Total: detail.Total,
                        Description: detail.Description
                    )).ToArray(),
                EditModel: editModel
            );
        }
        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;
        }
        public void UpdateOrderStatus(OrderRecord order, PaymentResponse paymentResponse)
        {
            OrderStatus orderStatus;

            switch (paymentResponse.Status)
            {
                case PaymentResponseStatus.Success:
                    orderStatus = OrderStatus.Paid;
                    break;
                default:
                    orderStatus = OrderStatus.Cancelled;
                    break;
            }

            if (order.Status == orderStatus)
                return;

            order.Status = orderStatus;
            order.PaymentServiceProviderResponse = paymentResponse.ResponseText;
            order.PaymentReference = paymentResponse.PaymentReference;

            switch (order.Status)
            {
                case OrderStatus.Paid:
                    order.PaidAt = DateTime.Now;
                    break;
                case OrderStatus.Completed:
                    order.CompletedAt = DateTime.Now;
                    break;
                case OrderStatus.Cancelled:
                    order.CancelledAt = DateTime.Now;
                    break;
            }
        }
        /// <summary>
        /// Call the Paypal DoExpressCheckoutPayment API (https://www.x.com/developers/paypal/documentation-tools/api/doexpresscheckoutpayment-api-operation-nvp)
        /// </summary>
        /// <param name="paypalExpressPart">part</param>
        /// <param name="order">Webshop order</param>
        /// <param name="token">Paypal token</param>
        /// <param name="payerId">Paypal PayerId</param>
        /// <returns>result containing token and ack</returns>
        public PaypalExpressResult DoExpressCheckoutPayment(PaypalExpressPart paypalExpressPart, OrderRecord order, string token, string payerId)
        {
            PaypalExpressResult result = new PaypalExpressResult { Method = "DoExpressCheckoutPayment" };
            HttpWebRequest request = BuildExpressCheckoutPaymentRequest(paypalExpressPart, order, token, payerId);

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                string[] lines = GetResponseLines(response);
                result.Ack = GetValue(lines, "Ack").FirstOrDefault();
                result.Token = GetValue(lines, "Token").FirstOrDefault();
                result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault();

                // TODO: parse out useful information about the payer, address, etc
                // and store somewhere: update the order? add to PaypalTransaction? customer?

                if (string.Compare(result.Ack, "success", true) != 0)
                    ExtractErrors(lines, result);
            }

            return result;

        }
 private void AddAllItems(PaypalRequest pr, OrderRecord order)
 {
     int lineNumber = 0;
     foreach (var item in order.Details)
         AddItem(pr, ++lineNumber, item);
 }
        private void SetShippingAddress(PaypalRequest pr, OrderRecord order)
        {
            bool addrOverride = true;
            CustomerPart customer = _customerService.GetCustomer(order.CustomerId);

            // determine which address to use
            var address = _customerService.GetAddress(order.CustomerId, "ShippingAddress");
            if (address == null)
                address = _customerService.GetAddress(order.CustomerId, "InvoiceAddress");
            if (address == null)
                addrOverride = false;

            pr.Add("ADDROVERRIDE", addrOverride ? "1" : "0");
            pr.Add("NOSHIPPING", "0");
            if (addrOverride)
            {
                if (string.IsNullOrWhiteSpace(address.Name))
                    pr.Add("PAYMENTREQUEST_0_SHIPTONAME", customer.FirstName + " " + customer.LastName);
                else
                    pr.Add("PAYMENTREQUEST_0_SHIPTONAME", address.Name);
                pr.Add("PAYMENTREQUEST_0_SHIPTOSTREET", address.Address);
                pr.Add("PAYMENTREQUEST_0_SHIPTOCITY", address.City);
                pr.Add("PAYMENTREQUEST_0_SHIPTOSTATE", address.State);
                pr.Add("PAYMENTREQUEST_0_SHIPTOZIP", address.Postcode);
                pr.Add("PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE", address.CountryCode);

                // Set email
                UserPart user = customer.As<UserPart>();
                pr.Add("PAYMENTREQUEST_0_EMAIL", user.Email);
            }
        }
        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;
        }
        private HttpWebRequest BuildExpressCheckoutPaymentRequest(PaypalExpressPart paypalExpressPart, OrderRecord order, string token, string payerId)
        {
            // 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";

            // Create a paypal request
            PaypalRequest pr = new PaypalRequest(paypalExpressPart, "DoExpressCheckoutPayment", token);

            // Add data
            pr.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale");
            pr.Add("PAYERID", payerId);
            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);

            // order details
            AddAllItems(pr, order);

            // format the request with data from the paypal request
            pr.SetData(request);

            return request;
        }
 public PaymentRequest(OrderRecord order)
 {
     Order = order;
 }
Example #12
0
 public EditOrderVM(OrderRecord order)
 {
     Id = order.Id;
     Status = order.Status;
 }