Exemplo n.º 1
0
        public void it_gets_checkout_snippet(
            List <CartItem> cartItems,
            ShippingItem shippingItem)
        {
            var sut   = new CheckoutClient(TestConfig.OrderBaseUri, TestConfig.MerchantId, TestConfig.SharedSecret);
            var items = new List <ICartItem>();

            items.AddRange(cartItems);
            items.Add(shippingItem);

            var testUrl = "http://www.mysite.com";

            var checkoutUris = new CheckoutUris(
                new Uri(testUrl), new Uri(testUrl), new Uri(testUrl), new Uri(testUrl));

            var response = sut.Checkout(items, Locale.Norway, checkoutUris);

            response.Snippet.Should().NotBeNullOrWhiteSpace();
            response.Location.AbsoluteUri.Should().StartWith(TestConfig.OrderBaseUri.ToString());
        }
Exemplo n.º 2
0
        public CheckoutResponse Checkout(IEnumerable <ICartItem> cartItems, Locale locale, CheckoutUris checkoutUris, string orderId = null, ShippingAddress address = null)
        {
            if (cartItems == null)
            {
                throw new ArgumentNullException("cartItems");
            }
            if (locale == null)
            {
                throw new ArgumentNullException("locale");
            }
            if (checkoutUris == null)
            {
                throw new ArgumentNullException("checkoutUris");
            }

            var   connector = Connector.Create(SharedSecret, OrderBaseUri);
            Order order     = null;

            bool orderExists = string.IsNullOrEmpty(orderId) == false;

            if (orderExists)
            {
                try
                {
                    // try to get existing order
                    order = new Order(connector, orderId)
                    {
                        ContentType = ContentType
                    };

                    order.Fetch();
                }
                catch
                {
                    //throws exception if cannot find order - we'll create a new one
                    orderExists = false;
                }
            }

            if (orderExists == false)
            {
                var merchant = new Merchant(
                    MerchantId,
                    SharedSecret,
                    checkoutUris.Checkout,
                    checkoutUris.Confirmation,
                    checkoutUris.Push,
                    checkoutUris.Terms,
                    checkoutUris.Validation);
                var cart    = new Cart(cartItems);
                var options = new Options(AllowSeparateShippingAddress);
                var gui     = new Gui(DisableAutoFocus);
                if (ColorOptions != null)
                {
                    options.ColorOptions = ColorOptions;
                }
                var data = new OrderData(merchant, cart, locale, options, gui, address);


                order = new Order(connector)
                {
                    ContentType = ContentType
                };
                order.Create(data.ToDictionary());
                order.Fetch();
            }
            else
            {
                // updating cart will only work for order with status "checkout_incomplete"
                var cart       = new Cart(cartItems);
                var updateData = new Dictionary <string, object> {
                    { "cart", cart.ToDictionary() }
                };

                var status = order.GetStringField("status");

                if (!status.Equals(OrderStatus.InComplete, StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new Exception("Cannot change order that has status " + status);
                }

                order.Update(updateData);
            }

            return(new CheckoutResponse(order.GetStringField("id"), order.Location, order.GetSnippet(), order.GetStringField("status")));
        }