public void LogTransaction(OrderPart 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, OrderPart 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(OrderPart order, EditOrderVM editModel)
        {
            CustomerPart customer = _customerService.GetCustomer(order.CustomerId);
            AddressPart shipAddressPart = _customerService.GetShippingAddress(customer.Id, order.Id);
            AddressPart custAddressPart = _customerService.GetInvoiceAddress(customer.Id);

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

            string shipAddress = shipAddressPart == null ? "(same as Invoice Address)" : shipAddressPart.Address;
            string shipAddress2 = shipAddressPart == null ? string.Empty : shipAddressPart.City + " " + shipAddressPart.State + " " + shipAddressPart.Postcode;
            string shipCountry = shipAddressPart == null ? string.Empty : 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: null, // Convert the whole thing to use Part instead of Record
                //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 PaymentRequest(OrderPart order)
 {
     Order = order;
 }
 public EditOrderVM(OrderPart order)
 {
     Id = order.Id;
     Status = order.Status;
 }
        public void UpdateOrderStatus(OrderPart 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, OrderPart 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, OrderPart order)
 {
     int lineNumber = 0;
     foreach (var item in order.Details)
         AddItem(pr, ++lineNumber, item);
 }
        private void SetShippingAddress(PaypalRequest pr, OrderPart order)
        {
            bool addrOverride = true;
            CustomerPart customer = _customerService.GetCustomer(order.CustomerId);

            // determine which address to use
            var address = _customerService.GetShippingAddress(order.CustomerId, order.Id);
            if (address == null || String.IsNullOrWhiteSpace(address.Address))
                address = _customerService.GetInvoiceAddress(order.CustomerId);
            if (address == null || String.IsNullOrWhiteSpace(address.Address))
                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, OrderPart 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, OrderPart 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;
        }
Exemple #12
0
        private void SaveDetails(OrderPart part)
        {
            if (part.Id <= 0) {
                return;
            }

            var oldDetails = _orderDetailsRepository.Fetch(d => d.OrderId == part.Id);
            foreach (var detail in part.Details.Where(d => !oldDetails.Where(od => od.Id == d.Id).Any())) {
                // New details
                var newRecord = detail.Record;
                newRecord.OrderId = part.Id;
                _orderDetailsRepository.Create(newRecord);
            }
            foreach (var detail in part.Details.Join(oldDetails, d => d.Id, od => od.Id, (d, od) => new { updated = d, stored = od})) {
                // Updated details
                if (detail.updated.Quantity <= 0) {
                    _orderDetailsRepository.Delete(detail.stored);
                }
                else {
                    _orderDetailsRepository.Update(detail.updated.Record);
                }
            }
            foreach (var removed in oldDetails.Where(od => !part.Details.Where(d => d.Id == od.Id).Any())) {
                // Removed details
                _orderDetailsRepository.Delete(removed);
            }
        }
Exemple #13
0
 private static decimal BuildOrderTotal(OrderPart part)
 {
     return part.ContentItem.Parts.Select(p => p as IOrderSubTotal != null ? (p as IOrderSubTotal).SubTotal : 0).Sum();
 }
 public void OrderToContact(OrderPart order)
 {
     // tutto in try catch perchè viene scatenato appena finito il pagamento e quindi non posso permettermi di annullare la transazione
     try {
         // recupero il contatto
         var currentUser = _orchardServices.WorkContext.CurrentUser;
         var ContactList = new List <ContentItem>();
         if (currentUser != null)
         {
             var contactpart = _communicationService.GetContactFromUser(currentUser.Id);
             if (contactpart == null)   // non dovrebbe mai succedere (inserito nel caso cambiassimo la logica già implementata)
             {
                 _communicationService.UserToContact(currentUser);
                 contactpart = _communicationService.GetContactFromUser(currentUser.Id);
             }
             ContactList.Add(contactpart.ContentItem);
         }
         else
         {
             var contacts = _communicationService.GetContactsFromMail(order.CustomerEmail);
             if (contacts.Count > 0)
             {
                 ContactList = contacts;
             }
             else
             {
                 var newcontact = _orchardServices.ContentManager.Create("CommunicationContact", VersionOptions.Draft);
                 ((dynamic)newcontact).CommunicationContactPart.Master = false;
                 ContactList.Add(newcontact);
             }
         }
         foreach (var contactItem in ContactList)
         {
             // nel caso in cui una sincro fallisce continua con
             try {
                 StoreAddress(order.BillingAddress, "BillingAddress", contactItem);
             }
             catch (Exception ex) {
                 _Logger.Error("OrderToContact -> BillingAddress -> order id= " + order.Id.ToString() + " Error: " + ex.Message);
             }
             try {
                 StoreAddress(order.ShippingAddress, "ShippingAddress", contactItem);
             }
             catch (Exception ex) {
                 _Logger.Error("OrderToContact -> ShippingAddress -> order id= " + order.Id.ToString() + " Error: " + ex.Message);
             }
             try {
                 _communicationService.AddEmailToContact(order.CustomerEmail, contactItem);
             }
             catch (Exception ex) {
                 _Logger.Error("OrderToContact -> AddEmailToContact -> order id= " + order.Id.ToString() + " Error: " + ex.Message);
             }
             try { // non sovrascrivo se dato già presente
                 _communicationService.AddSmsToContact((order.CustomerPhone + ' ').Split(' ')[0], (order.CustomerPhone + ' ').Split(' ')[1], contactItem, false);
             }
             catch (Exception ex) {
                 _Logger.Error("OrderToContact -> AddSmsToContact -> order id= " + order.Id.ToString() + " Error: " + ex.Message);
             }
         }
     }
     catch (Exception myex) {
         _Logger.Error("OrderToContact -> order id= " + order.Id.ToString() + " Error: " + myex.Message);
     }
 }
Exemple #15
0
        public async Task <IActionResult> Create(OrderPart orderPart)
        {
            await _ordersPartsRepository.Create(orderPart);

            return(RedirectToAction("Index", "Order"));
        }
Exemple #16
0
 public EditOrderVM(OrderPart order)
 {
     Id     = order.Id;
     Status = order.Status;
 }