Ejemplo n.º 1
0
        public virtual async Task <OrderManagementCapture> CaptureOrder(string orderId, int amount, string description, IOrderGroup orderGroup, IOrderForm orderForm, IPayment payment, IShipment shipment)
        {
            if (shipment == null)
            {
                throw new InvalidOperationException("Can't find correct shipment");
            }
            var lines        = shipment.LineItems.Select(l => FromLineItem(l, orderGroup.Currency)).ToList();
            var shippingInfo = new OrderManagementShippingInfo
            {
                // TODO shipping info
                ShippingMethod = OrderManagementShippingMethod.Own,
                TrackingNumber = shipment.ShipmentTrackingNumber
            };

            var captureData = new OrderManagementCreateCapture
            {
                CapturedAmount = amount,
                Description    = description,
                OrderLines     = lines,
                ShippingInfo   = new List <OrderManagementShippingInfo>()
                {
                    shippingInfo
                }
            };

            return(await _client.CreateAndFetchCapture(orderId, captureData).ConfigureAwait(false));
        }
        /// <summary>
        /// Create capture
        /// <a href="https://developers.klarna.com/api/#order-management-api-create-capture">https://developers.klarna.com/api/#order-management-api-create-capture</a>
        /// </summary>
        /// <param name="orderId">Id of order to create capture</param>
        /// <param name="capture">The <see cref="OrderManagementCapture"/> object</param>
        /// <returns>Object of <see cref="OrderManagementCapture"/> </returns>
        public async Task <OrderManagementCapture> CreateCapture(string orderId, OrderManagementCreateCapture capture)
        {
            var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures");

            OrderManagementCapture response = await Post <OrderManagementCapture>(url, capture);

            return(response);
        }
        /// <summary>
        /// Captures the order.
        /// </summary>
        public void CaptureOrder()
        {
            var username = "******";
            var password = "******";

            var klarna  = new Klarna(username, password, KlarnaEnvironment.TestingEurope);
            var orderId = "abc-abcdefg-abc";

            try
            {
                var captureData = new OrderManagementCreateCapture
                {
                    CapturedAmount = 10000,
                    Description    = "Full capture of the order",
                    OrderLines     = new List <OrderLine>()
                    {
                        new OrderLine
                        {
                            Type                = OrderLineType.physical,
                            Name                = "Foo",
                            Quantity            = 1,
                            UnitPrice           = 10000,
                            TaxRate             = 2500,
                            TotalAmount         = 10000,
                            TotalTaxAmount      = 2000,
                            TotalDiscountAmount = 0,
                        }
                    },
                    ShippingInfo = new List <OrderManagementShippingInfo>()
                    {
                        new OrderManagementShippingInfo
                        {
                            ShippingMethod        = OrderManagementShippingMethod.PickUpPoint,
                            ShippingCompany       = "PostNord",
                            TrackingUri           = "https://trackingpageofcarrier.com",
                            TrackingNumber        = "345...33",
                            ReturnTrackingUri     = "https://trackingpageofreturntracking.com",
                            ReturnTrackingNumber  = "675...575",
                            ReturnShippingCompany = "PostNord"
                        }
                    }
                };
                klarna.OrderManagement.CreateCapture(orderId, captureData).RunSynchronously();
            }
            catch (ApiException ex)
            {
                Console.WriteLine(ex.ErrorMessage.ErrorCode);
                Console.WriteLine(ex.ErrorMessage.ErrorMessages);
                Console.WriteLine(ex.ErrorMessage.CorrelationId);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 4
0
        public virtual async Task <OrderManagementCapture> CaptureOrder(string orderId, int amount, string description, IOrderGroup orderGroup, IOrderForm orderForm, IPayment payment)
        {
            var lines = orderForm.GetAllLineItems().Select(l => FromLineItem(l, orderGroup.Currency)).ToList();

            var captureData = new OrderManagementCreateCapture
            {
                CapturedAmount = amount,
                Description    = description,
                OrderLines     = lines
            };

            return(await _client.CreateAndFetchCapture(orderId, captureData).ConfigureAwait(false));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Captures the order.
        /// </summary>
        public void CaptureOrder()
        {
            var username = "******";
            var password = "******";

            var klarna  = new Klarna(username, password, KlarnaEnvironment.TestingEurope);
            var orderId = "abc-abcdefg-abc";

            try
            {
                var captureData = new OrderManagementCreateCapture
                {
                    CapturedAmount = 10000,
                    Description    = "Full capture of the order",
                    OrderLines     = new List <OrderLine>()
                    {
                        new OrderLine
                        {
                            Type                = OrderLineType.physical,
                            Name                = "Foo",
                            Quantity            = 1,
                            UnitPrice           = 10000,
                            TaxRate             = 2500,
                            TotalAmount         = 10000,
                            TotalTaxAmount      = 2000,
                            TotalDiscountAmount = 0,
                        }
                    },
                    ShippingInfo = new List <OrderManagementShippingInfo>()
                    {
                        new OrderManagementShippingInfo
                        {
                            ShippingMethod        = OrderManagementShippingMethod.PickUpPoint,
                            ShippingCompany       = "PostNord",
                            TrackingUri           = "https://trackingpageofcarrier.com",
                            TrackingNumber        = "345...33",
                            ReturnTrackingUri     = "https://trackingpageofreturntracking.com",
                            ReturnTrackingNumber  = "675...575",
                            ReturnShippingCompany = "PostNord"
                        }
                    }
                };
                klarna.OrderManagement.CreateCapture(orderId, captureData).RunSynchronously();
            }
            catch (AggregateException ae)
            {
                foreach (var e in ae.InnerExceptions)
                {
                    if (e is ApiException)
                    {
                        var apiException = (ApiException)e;
                        Console.WriteLine("Status code: " + apiException.StatusCode);
                        Console.WriteLine("Error: " + string.Join("; ", apiException.ErrorMessage.ErrorMessages));
                    }
                    else
                    {
                        // Rethrow any other exception or process it
                        Console.WriteLine(e.Message);
                        throw;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates capture and follow the Location header to fetch the data
        /// <a href="https://developers.klarna.com/api/#order-management-api-create-capture">
        ///     https://developers.klarna.com/api/#order-management-api-create-capture
        /// </a>
        /// </summary>
        /// <param name="orderId">Id of order to create capture</param>
        /// <param name="capture">The <see cref="OrderManagementCapture"/> object</param>
        /// <returns>Object of <see cref="OrderManagementCapture"/> </returns>
        public async Task <OrderManagementCapture> CreateAndFetchCapture(string orderId, OrderManagementCreateCapture capture)
        {
            var url      = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures");
            var response = new Ref <HttpResponseMessage>();

            await Post(url, capture, null, response).ConfigureAwait(false);

            var headers = response.Value.Headers;

            url = headers.Location.ToString();

            if (!string.IsNullOrEmpty(url))
            {
                return(await Get <OrderManagementCapture>(url).ConfigureAwait(false));
            }

            return(default(OrderManagementCapture));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates capture
        /// <a href="https://developers.klarna.com/api/#order-management-api-create-capture">
        ///     https://developers.klarna.com/api/#order-management-api-create-capture
        /// </a>
        /// </summary>
        /// <param name="orderId">Id of order to create capture</param>
        /// <param name="capture">The <see cref="OrderManagementCapture"/> object</param>
        /// <returns>Object of <see cref="OrderManagementCapture"/> </returns>
        public Task CreateCapture(string orderId, OrderManagementCreateCapture capture)
        {
            var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures");

            return(Post(url, capture));
        }