Exemplo n.º 1
0
        private static void AdjustPriceAndTaxes(Currency currency, ILineItem lineItem,
            DinteroOrderLine dinteroItem, IOrderAddress orderAddress)
        {
            var amount = lineItem.GetExtendedPrice(currency).Amount;
            double vat = 0;

            var entryDto = CatalogContext.Current.GetCatalogEntryDto(lineItem.Code,
                new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryFull));
            if (entryDto.CatalogEntry.Count > 0)
            {
                CatalogEntryDto.VariationRow[] variationRows = entryDto.CatalogEntry[0].GetVariationRows();
                if (variationRows.Length > 0)
                {
                    var taxCategory = CatalogTaxManager.GetTaxCategoryNameById(variationRows[0].TaxCategoryId);
                    var taxes = OrderContext.Current.GetTaxes(Guid.Empty, taxCategory,
                        Thread.CurrentThread.CurrentCulture.Name, orderAddress).ToList();

                    foreach (var tax in taxes)
                    {
                        if (tax.TaxType == TaxType.SalesTax)
                        {
                            vat = tax.Percentage;
                        }
                    }
                }
            }

            dinteroItem.Amount = CurrencyHelper.CurrencyToInt(amount, currency.CurrencyCode);
            dinteroItem.Vat = Convert.ToInt32(vat);
            dinteroItem.VatAmount = GetVatAmount(amount, vat, currency.CurrencyCode);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Refund transaction
        /// </summary>
        /// <param name="payment"></param>
        /// <param name="returnForms"></param>
        /// <param name="currency"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public TransactionResult RefundTransaction(IPayment payment, IEnumerable<IOrderForm> returnForms,
            Currency currency, string accessToken)
        {
            var result = new TransactionResult();

            if (!Configuration.IsValid())
            {
                throw new Exception("Dintero configuration is not valid!");
            }

            var url = DinteroAPIUrlHelper.GetTransactionRefundUrl(payment.TransactionID);

            if (!string.IsNullOrWhiteSpace(url))
            {
                try
                {
                    var transaction = GetTransactionDetails(payment.TransactionID, accessToken);

                    if (transaction == null)
                    {
                        throw new Exception("Dintero transaction can't be loaded!");
                    }

                    var request = new DinteroRefundRequest
                    {
                        Amount = CurrencyHelper.CurrencyToInt(payment.Amount, currency.CurrencyCode),
                        Reason = "Refund"
                    };

                    var returnForm = GetCurrentReturnForm(returnForms, transaction);

                    if (returnForm != null)
                    {
                        request.Items = ConvertRefundOrderLineItems(returnForm, transaction, currency);
                    }

                    result = SendTransactionRequest(url, "Bearer", accessToken, request,
                        new List<string> {"PARTIALLY_REFUNDED", "PARTIALLY_CAPTURED_REFUNDED", "REFUNDED"});
                }
                catch (Exception e)
                {
                    Logger.Error("An error occurred during refunding transaction.", e);
                    throw;
                }

            }

            return result;
        }
Exemplo n.º 3
0
        private static List<DinteroOrderLine> ConvertOrderLineItems(IEnumerable<IOrderForm> orderForms,
            Currency currency)
        {
            var items = new List<DinteroOrderLine>();
            var index = 0;
            var marketId = ServiceLocator.Current.GetInstance<ICurrentMarket>().GetCurrentMarket().MarketId.Value;
            var shippingMethods = ShippingManager.GetShippingMethodsByMarket(marketId, false);
            foreach (var orderForm in orderForms)
            {
                foreach (var s in orderForm.Shipments)
                {
                    foreach (var item in s.LineItems.Select((value, i) => new {Index = i, Value = value}))
                    {
                        //index = item.Index + 1;
                        items.Add(TransformLineItem(currency, item.Value, s.ShippingAddress, index));
                        index++;
                    }

                    var shipment = (Shipment) s;
                    var shipping = shippingMethods.ShippingMethod.FirstOrDefault(x => x.ShippingMethodId == shipment.ShippingMethodId);
                    if (shipping != null)
                    {
                        items.Add(new DinteroOrderLine
                        {
                            Id = shipment.Id.ToString(),
                            Groups = new List<DinteroOrderLineGroup>(),
                            LineId = index.ToString(),
                            Description = shipping.DisplayName,
                            Quantity = 1,
                            Amount = CurrencyHelper.CurrencyToInt(shipment.ShippingTotal, currency.CurrencyCode),
                            VatAmount = Convert.ToInt32(shipment.ShippingTax * 100),
                            Vat = shipment.ShippingTotal - shipment.ShippingTax == 0? 0: Convert.ToInt32((shipment.ShippingTax/(shipment.ShippingTotal - shipment.ShippingTax)) * 100)
                        });
                    }

                    index++;
                }
            }

            return items;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Capture transaction
        /// </summary>
        /// <param name="payment"></param>
        /// <param name="purchaseOrder"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public TransactionResult CaptureTransaction(IPayment payment, IPurchaseOrder purchaseOrder, string accessToken, bool skipItems = false)
        {
            var result = new TransactionResult();

            if (!Configuration.IsValid())
            {
                throw new Exception("Dintero configuration is not valid!");
            }

            var url = DinteroAPIUrlHelper.GetTransactionCaptureUrl(payment.TransactionID);

            if (!string.IsNullOrWhiteSpace(url))
            {
                try
                {
                    var request = new DinteroCaptureRequest
                    {
                        Amount = CurrencyHelper.CurrencyToInt(payment.Amount, purchaseOrder.Currency.CurrencyCode),
                        CaptureReference = purchaseOrder.OrderNumber,
                        Items = new List<DinteroOrderLine>()
                    };

                    if (!skipItems)
                    {
                        request.Items = ConvertOrderLineItems(purchaseOrder.Forms, purchaseOrder.Currency);
                    }

                    result = SendTransactionRequest(url, "Bearer", accessToken, request, new List<string> {"CAPTURED"});
                }
                catch (Exception e)
                {
                    Logger.Error("An error occurred during capturing transaction. ", e);
                    throw;
                }

            }

            return result;
        }
Exemplo n.º 5
0
 private static int GetVatAmount(decimal amount, double vat, string currencyCode)
 {
     return CurrencyHelper.CurrencyToInt(Convert.ToDouble(amount) * vat / (100 + vat), currencyCode);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Create Dintero session
        /// </summary>
        /// <param name="payment"></param>
        /// <param name="currentCart"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public DinteroCreateSessionResponse CreateCheckoutSession(IPayment payment, ICart currentCart,
            string accessToken, string trackingNumber = "")
        {
            DinteroCreateSessionResponse sessionData = null;

            if (Configuration.IsValid() && !string.IsNullOrWhiteSpace(accessToken))
            {
                var url = DinteroAPIUrlHelper.GetNewSessionUrl();

                if (!Configuration.IsValid())
                {
                    throw new Exception("Dintero configuration is not valid!");
                }

                try
                {
                    var orderForm = currentCart.Forms.FirstOrDefault(f => f.Payments.Contains(payment));

                    if (orderForm != null)
                    {
                        var shippingAddress = orderForm.Shipments.First().ShippingAddress;

                        var orderNumber = string.IsNullOrEmpty(trackingNumber) ? _orderNumberGenerator.GenerateOrderNumber(currentCart): trackingNumber;

                        var request = new DinteroCreateSessionRequest
                        {
                            UrlSetting =
                                new DinteroUrlSetting
                                {
                                    ReturnUrl =
                                        UriUtil.GetUrlFromStartPageReferenceProperty("DinteroPaymentPage", true),
                                    CallbackUrl =
                                        UriUtil.GetUrlFromStartPageReferenceProperty("DinteroPaymentPage", true)
                                },
                            Customer = new DinteroCustomer
                            {
                                Email = payment.BillingAddress.Email,
                                PhoneNumber = payment.BillingAddress.DaytimePhoneNumber
                            },
                            Order = new DinteroOrder
                            {
                                Amount =
                                    CurrencyHelper.CurrencyToInt(payment.Amount, currentCart.Currency.CurrencyCode),
                                Currency = currentCart.Currency.CurrencyCode,
                                MerchantReference = orderNumber,
                                BillingAddress =
                                    new DinteroAddress
                                    {
                                        FirstName = payment.BillingAddress.FirstName,
                                        LastName = payment.BillingAddress.LastName,
                                        AddressLine =
                                            $"{payment.BillingAddress.Line1} {payment.BillingAddress.Line2}",
                                        PostalCode = payment.BillingAddress.PostalCode,
                                        PostalPlace = payment.BillingAddress.City,
                                        Country = payment.BillingAddress.CountryCode
                                    },
                                ShippingAddress = new DinteroAddress
                                {
                                    FirstName = shippingAddress.FirstName,
                                    LastName = shippingAddress.LastName,
                                    AddressLine = $"{shippingAddress.Line1} {shippingAddress.Line2}",
                                    PostalCode = shippingAddress.PostalCode,
                                    PostalPlace = shippingAddress.City,
                                    Country = shippingAddress.CountryCode
                                },
                                Items = ConvertOrderLineItems(currentCart.Forms, currentCart.Currency),
                                PartialPayment = false,
                                Store = new DinteroStore
                                {
                                    Id = currentCart != null && currentCart.GetFirstShipment() != null ? currentCart.GetFirstShipment().WarehouseCode: ""
                                }
                            },
                            ProfileId = Configuration.ProfileId
                        };

                        request.Order.VatAmount = request.Order.Items.Sum(item => item.VatAmount);
                        request.Order.PartialPayment = request.Order.Amount != request.Order.Items.Sum(item => item.Amount);

                        sessionData = SendRequest<DinteroCreateSessionResponse>(url, accessToken, "Bearer", request);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("An error occurred during initializing payment session.", e);
                    throw;
                }

            }

            return sessionData;
        }