Ejemplo n.º 1
0
 public ServerApiCall(UiSettings settings)
 {
     Users         = new UserApiClient(settings);
     Subscriptions = new SubscriptionApiClient(settings);
     Orders        = new OrderApiClient(settings);
     EntityChanges = new EntityChangesApiClient(settings);
 }
        public void RemoveAll_Should_Return_Empty(string id)
        {
            // Arrange
            var client = new OrderApiClient(_factory.CreateClient());

            // Act
            Should.NotThrow(async() => { await client.Basket.RemoveAll(new Guid(id)); });
        }
        public void Basket_Delete_Should_Return_Right_Data(string id)
        {
            // Arrange
            var client = new OrderApiClient(_factory.CreateClient());

            // Act
            Should.NotThrow(async() => { await client.Basket.Delete(new Guid(id)); });
        }
Ejemplo n.º 4
0
        public void it_cancel_reservation()
        {
            var orderApiClient = new OrderApiClient(Int32.Parse(TestConfig.MerchantId), TestConfig.SharedSecret, TestConfig.Locale, false);

            var cancelResult = orderApiClient.CancelReservation("1226560000");

            cancelResult.ShouldBeEquivalentTo(true);
        }
        public async Task ItemIncrementDecrement_Should_Remove_Quantity(string id)
        {
            // Arrange
            var client = new OrderApiClient(_factory.CreateClient());
            // Act
            var response = await client.Item.IncreaseDecreaseItem(new Guid(id), new Guid(id), -1);

            // Assert
            response.Quantity.ShouldBe(0);
        }
        public async Task GetItems_Should_Return_Right_Data(string id)
        {
            // Arrange
            var client = new OrderApiClient(_factory.CreateClient());
            // Act
            var response = await client.Item.GetItems(new Guid(id));

            // Assert
            response.Count().ShouldBe(1);
        }
        public async Task Basket_Get_Should_Return_Right_Data(string id)
        {
            // Arrange
            var client = new OrderApiClient(_factory.CreateClient());
            // Act
            var response = await client.Basket.Get(new Guid(id));

            // Assert
            response.BasketId.ShouldBe(new Guid(id));
        }
        public async Task Basket_Create_Should_Return_Right_Data()
        {
            // Arrange
            var client = new OrderApiClient(_factory.CreateClient());
            // Act
            var response = await client.Basket.Create(new CreateBasketRequest
            {
                Email = "*****@*****.**"
            });

            response.ShouldNotBeEmpty();
        }
        public async Task AddItem_Should_Add_New_Item(string id)
        {
            // Arrange
            var client = new OrderApiClient(_factory.CreateClient());
            // Act
            var response = await client.Item.AddItem(new Guid(id), new CreateItemBasketRequest
            {
                ItemDescription = "test",
                ItemCode        = "code10",
                Quantity        = 34
            });

            response.ShouldNotBeEmpty();
        }
Ejemplo n.º 10
0
        public override bool ProcessPayment(Payment payment, ref string message)
        {
            Logger.Debug("Klarna checkout gateway. Processing Payment ....");
            VerifyConfiguration();

            var orderGroup      = payment.Parent.Parent;
            var transactionType = payment.TransactionType.ToUpper();

            var orderApiClient = new OrderApiClient(Int32.Parse(KlarnaSettings.MerchantId), KlarnaSettings.Secret, KlarnaSettings.CurrentLocale, KlarnaSettings.IsProduction);

            try
            {
                switch (transactionType)
                {
                case "CAPTURE":
                {
                    var reservation = GetReservation(payment);

                    if (string.IsNullOrEmpty(reservation))
                    {
                        var errorMessage =
                            "CAPTURE operation KlarnaCheckoutPaymentGateway failed. Metafield 'Reservation' on KlarnaPayment is empty.";
                        Logger.Error(errorMessage);
                        throw new Exception(errorMessage);
                    }

                    var orderForm = orderGroup.OrderForms[0];

                    // We will include cart items (in case of partial shipment)
                    var shipment  = orderForm.Shipments[0];         // only 1 shipment is valid
                    var cartItems = orderForm.LineItems.Select(item => item.ToCartItem(true)).ToList();
                    cartItems.AddRange(shipment.ToCartItems(true));

                    var purchaseOrder = (orderGroup as PurchaseOrder);

                    if (purchaseOrder == null)
                    {
                        var errorMessage =
                            "CAPTURE operation KlarnaCheckoutPaymentGateway failed. Uanble to cast orderGroup to PurchaseOrder.";
                        Logger.Error(errorMessage);
                        throw new Exception(errorMessage);
                    }


                    var trackingNr = purchaseOrder.TrackingNumber;

                    string infoMsg = string.Format("KlarnaCheckoutPaymentGateway: Activating reservation {0}. Transaction id: {1}. Tracking number: {2}.",
                                                   reservation, payment.TransactionID, trackingNr);
                    Logger.Debug(infoMsg);

                    var response = orderApiClient.Activate(reservation, payment.TransactionID, trackingNr, cartItems);
                    payment.Status = response.IsSuccess ? PaymentStatus.Processed.ToString() : PaymentStatus.Failed.ToString();
                    if (response.IsSuccess)
                    {
                        orderGroup.OrderNotes.Add(new OrderNote
                            {
                                Title  = "Invoice number",
                                Detail = response.InvoiceNumber
                            });

                        // we need to save invoice number incase of refunds later
                        purchaseOrder[MetadataConstants.InvoiceId] = response.InvoiceNumber;
                        orderGroup.AcceptChanges();
                        PostProcessPayment.PostCapture(response, payment);
                    }
                    else
                    {
                        PostProcessPayment.PostCapture(response, payment);
                        Logger.Error(string.Format("Capture failed for order {0} with reservation {1}. Error message: {2}",
                                                   trackingNr, reservation, response.ErrorMessage));
                        throw new Exception(response.ErrorMessage);
                    }

                    return(response.IsSuccess);
                }

                case "VOID":
                {
                    var reservation = GetReservation(payment);

                    if (string.IsNullOrEmpty(reservation))
                    {
                        var errorMessage =
                            "VOID operation KlarnaCheckoutPaymentGateway failed. Metafield 'Reservation' on KlarnaPayment is empty.";
                        Logger.Error(errorMessage);
                        throw new Exception(errorMessage);
                    }

                    Logger.Debug(string.Format("Cancel reservation called with reservation {0}. Transaction id is {1}.", reservation, payment.TransactionID));

                    var cancelResult = orderApiClient.CancelReservation(reservation);
                    if (cancelResult.IsSuccess)
                    {
                        orderGroup.Status = OrderStatus.Cancelled.ToString();
                        payment.Status    = PaymentStatus.Processed.ToString();
                    }
                    else
                    {
                        payment.Status = PaymentStatus.Failed.ToString();
                    }

                    orderGroup.AcceptChanges();
                    PostProcessPayment.PostAnnul(cancelResult.IsSuccess, payment);

                    if (cancelResult.IsSuccess == false)
                    {
                        var errorMessage = string.Format("VOID operation KlarnaCheckoutPaymentGateway failed. Error is {0}.", cancelResult.ErrorMessage);
                        Logger.Error(errorMessage);
                        throw new Exception(cancelResult.ErrorMessage);
                    }

                    return(cancelResult.IsSuccess);
                }

                case "CREDIT":
                {
                    var purchaseOrder = orderGroup as PurchaseOrder;
                    if (purchaseOrder != null)
                    {
                        var invoiceNumber = GetInvoiceId(purchaseOrder);

                        if (string.IsNullOrEmpty(invoiceNumber))
                        {
                            var errorMessage =
                                "CREDIT operation on KlarnaCheckoutPaymentGateway failed. Metafield 'InvoiceNumber' is empty.";
                            Logger.Error(errorMessage);
                            throw new Exception(errorMessage);
                        }

                        var returnFormToProcess =
                            purchaseOrder.ReturnOrderForms.FirstOrDefault(
                                p => p.Status == ReturnFormStatus.AwaitingCompletion.ToString() && p.Total == payment.Amount);

                        if (returnFormToProcess == null)
                        {
                            payment.Status = PaymentStatus.Failed.ToString();
                            PostProcessPayment.PostCredit(new RefundResponse()
                                {
                                    IsSuccess = false, ErrorMessage = "No return forms to process."
                                }, payment);
                            return(false);
                        }

                        // Determine if this is full refund, in that case we will call CreditInvoice
                        // If payment.Amount = captured amount then do full refund
                        var capturedAmount = orderGroup.OrderForms[0].Payments
                                             .Where(p => p.TransactionType == "Capture" & p.Status == PaymentStatus.Processed.ToString())
                                             .Sum(p => p.Amount);

                        var result = new RefundResponse();

                        if (capturedAmount == payment.Amount)         // full refund
                        {
                            result = orderApiClient.CreditInvoice(invoiceNumber);
                        }
                        else
                        {
                            var returnItems = returnFormToProcess.LineItems.Select(item => item.ToCartItem()).ToList();
                            // if shipment is part of returnForm, then we will return shipping cost as well
                            var shipment = returnFormToProcess.Shipments[0];
                            if (shipment != null && shipment.ShippingTotal > 0)
                            {
                                returnItems.AddRange(shipment.ToCartItems());
                            }

                            result = orderApiClient.HandleRefund(invoiceNumber, returnItems);
                        }

                        payment.Status = result.IsSuccess ? PaymentStatus.Processed.ToString() : PaymentStatus.Failed.ToString();
                        orderGroup.AcceptChanges();

                        PostProcessPayment.PostCredit(result, payment);

                        if (result.IsSuccess == false)
                        {
                            Logger.Error(result.ErrorMessage);
                            throw new Exception(result.ErrorMessage);
                        }


                        return(result.IsSuccess);
                    }
                    return(false);
                }
                }
            }
            catch (Exception exception)
            {
                Logger.Error("Process payment failed with error: " + exception.Message, exception);
                throw;
            }

            return(true);
        }
Ejemplo n.º 11
0
 public HomeController(AppDbContext appContext, OrderApiClient orderApiClient)
 {
     this.appContext     = appContext;
     this.orderApiClient = orderApiClient;
 }