Ejemplo n.º 1
0
        public static Outcome PlaceOrder(ILogger logger, Config config, Order order, bool validate = true)
        {
            const string OAUTHERROR =
                "An unexpected OAUTH validation error occured!  Please contact support for help in resolving this issue.";

            Contract.Requires(logger != null);
            Contract.Requires(config != null);
            Contract.Requires(order != null);

            if (validate)
            {
                config.Validate();
                order.Validate();
            }

            logger.Log(LogLevel.Info, "Placing Order (OrderId: {0})", order.OrderId);

            var oauth = new OAuthHelper(logger, config);

            var accessToken = oauth.GetAccessToken();

            if (accessToken == null)
            {
                logger.Log(LogLevel.Error, OAUTHERROR);

                throw new Exception(OAUTHERROR);
            }

            var context = new Context(accessToken, order.OrderId);

            var payment = GetPayment(logger, config, context, order, validate);

            var outcome = new Outcome()
            {
                CreatedOn = DateTime.Parse(payment.create_time),
                UpdatedOn = DateTime.Parse(payment.update_time),
                State = (OrderState)Enum.Parse(typeof(OrderState), payment.state, true),
                PaymentId = payment.id
            };

            logger.Log(LogLevel.Info, "OrderId: {0}, Outcome: {1}, PaymentId: {2}",
                order.OrderId, outcome.State, outcome.PaymentId);

            return outcome;
        }
Ejemplo n.º 2
0
        public static Order Parse(XElement element, bool validate = true)
        {
            var order = new Order();

            var initial = element.Element("initial");

            order.FirstName = element.Element("firstName").Value;
            order.Initial = initial == null ? (char?)null : ((string)initial)[0];
            order.LastName = element.Element("lastName").Value;
            order.Email = element.Element("email").Value;
            order.Phone = element.Element("phone").Value;
            order.Country = element.Element("country").Value;
            order.Address1 = element.Element("address1").Value;
            order.Address2 = element.Element("address2").Value;
            order.City = element.Element("city").Value;
            order.State = element.Element("state").Value;
            order.PostalCode = element.Element("postalCode").Value;
            order.CardNumber = element.Element("cardNumber").Value;
            order.CVV2 = (int)element.Element("cvv2");
            order.ExpireMonth = (int)element.Element("expireMonth");
            order.ExpireYear = (int)element.Element("expireYear");

            foreach (var detail in element.Element("details").Elements("detail"))
            {
                var shipping = detail.Element("shipping");
                var tax = detail.Element("tax");

                order.Details.Add(new Detail()
                {
                    SKU = detail.Element("sku").Value,
                    Name = detail.Element("name").Value,
                    Price = (double)detail.Element("price"),
                    Shipping = shipping == null ? 0.0 : (double)shipping,
                    Tax = shipping == null ? 0.0 : (double)tax,
                    Quantity = (int)detail.Element("quantity")
                });
            }

            if (validate)
                order.Validate();

            return order;
        }
Ejemplo n.º 3
0
        private static Payment GetPayment(ILogger logger, Config config,
            Context context, Order order, bool validate)
        {
            var billingAddress = new Address()
            {
                city = order.City,
                country_code = order.Country,
                line1 = order.Address1,
                postal_code = order.PostalCode,
                state = order.State,
            };

            var creditCard = new CreditCard()
            {
                billing_address = billingAddress,
                cvv2 = order.CVV2.ToString(),
                expire_month = order.ExpireMonth.ToString("00"),
                expire_year = order.ExpireYear.ToString(),
                first_name = order.FirstName,
                last_name = order.LastName,
                number = order.CardNumber,
                type = validate ? order.CardNumber.GetCardType().ToString().ToLower() : "visa"
            };

            var transactions = new List<Transaction>();

            var appInfo = new AppInfo();

            foreach (var thing in order.Details)
            {
                var transaction = new Transaction()
                {
                    amount = new Amount()
                    {
                        currency = "USD",
                        total = thing.Total.ToString("N2"),
                        details = new AmountDetails()
                        {
                            shipping = thing.Shipping.ToString("N2"),
                            subtotal = thing.SubTotal.ToString("N2"),
                            tax = thing.Tax.ToString("N2")
                        }
                    },
                    description = string.Format("Order: {0}, Product: {1}",
                        order.OrderId, appInfo.GetTitle())
                };

                transactions.Add(transaction);
            }

            var payment = new Payment()
            {
                intent = "sale",
                payer = new Payer()
                {
                    funding_instruments = new List<FundingInstrument>()
                    {
                        new FundingInstrument()
                        {
                            credit_card = creditCard
                        }
                    },
                    payment_method = "credit_card"
                },
                transactions = transactions
            };

            return JsonConvert.DeserializeObject<Payment>(
                payment.Create(logger, config, context).ToJson());
        }