public async Task <IActionResult> ResolveMarkupByMerchant(string merchantId, string assetPairId)
        {
            merchantId  = Uri.UnescapeDataString(merchantId);
            assetPairId = Uri.UnescapeDataString(assetPairId);

            try
            {
                IMarkup markup = await _markupService.ResolveAsync(merchantId, assetPairId);

                return(Ok(Mapper.Map <MarkupResponse>(markup)));
            }
            catch (InvalidRowKeyValueException e)
            {
                _log.ErrorWithDetails(e, new
                {
                    e.Variable,
                    e.Value
                });

                return(BadRequest(ErrorResponse.Create(e.Message)));
            }
            catch (MarkupNotFoundException e)
            {
                _log.ErrorWithDetails(e, new
                {
                    e.MerchantId,
                    e.AssetPairId
                });

                return(NotFound(ErrorResponse.Create(e.Message)));
            }
        }
        public async Task <IReadOnlyList <string> > ResolvePaymentAsync(string merchantId, string settlementAssetId)
        {
            IReadOnlyList <string> assets = await ResolveAsync(merchantId, AssetAvailabilityType.Payment);

            var result = new List <string>();

            foreach (string assetId in assets)
            {
                string assetPairId = $"{assetId}{settlementAssetId}";

                try
                {
                    _markupService.ResolveAsync(merchantId, assetPairId).GetAwaiter().GetResult();

                    result.Add(assetId);
                }
                catch (MarkupNotFoundException)
                {
                }
            }

            return(result);
        }
示例#3
0
        public async Task <IActionResult> ResolveMarkupByMerchant(string merchantId, string assetPairId)
        {
            IMerchant merchant = await _merchantService.GetAsync(merchantId);

            if (merchant == null)
            {
                return(NotFound(ErrorResponse.Create("Couldn't find merchant")));
            }

            try
            {
                IMarkup markup = await _markupService.ResolveAsync(merchantId, assetPairId);

                return(Ok(Mapper.Map <MarkupResponse>(markup)));
            }
            catch (MarkupNotFoundException markupNotFoundEx)
            {
                await _log.WriteErrorAsync(nameof(MerchantsController), nameof(ResolveMarkupByMerchant), new
                {
                    markupNotFoundEx.MerchantId,
                    markupNotFoundEx.AssetPairId
                }.ToJson(), markupNotFoundEx);

                return(NotFound(ErrorResponse.Create(markupNotFoundEx.Message)));
            }
            catch (Exception ex)
            {
                await _log.WriteErrorAsync(nameof(MerchantsController), nameof(ResolveMarkupByMerchant), new
                {
                    merchantId,
                    assetPairId
                }.ToJson(), ex);

                throw;
            }
        }
示例#4
0
        private async Task <(string AssetPairId, decimal PaymentAmount, decimal Rate)> GetPaymentInfoAsync(string settlementAssetId, string paymentAssetId, decimal amount, string merchantId, RequestMarkup requestMarkup)
        {
            string lykkePaymentAssetId = await _lykkeAssetsResolver.GetLykkeId(paymentAssetId);

            string lykkeSettlementAssetId = await _lykkeAssetsResolver.GetLykkeId(settlementAssetId);

            string assetPairId = $"{paymentAssetId}{settlementAssetId}";

            IMarkup merchantMarkup;

            try
            {
                merchantMarkup = await _markupService.ResolveAsync(merchantId, assetPairId);
            }
            catch (MarkupNotFoundException e)
            {
                _log.ErrorWithDetails(e, new
                {
                    e.MerchantId,
                    e.AssetPairId
                });

                throw;
            }

            decimal paymentAmount, rate;

            try
            {
                paymentAmount = await _calculationService.GetAmountAsync(lykkePaymentAssetId,
                                                                         lykkeSettlementAssetId, amount, requestMarkup, merchantMarkup);

                rate = await _calculationService.GetRateAsync(lykkePaymentAssetId, lykkeSettlementAssetId,
                                                              requestMarkup.Percent, requestMarkup.Pips, merchantMarkup);
            }
            catch (MarketPriceZeroException e)
            {
                _log.ErrorWithDetails(e, new { e.PriceType });

                throw;
            }
            catch (UnexpectedAssetPairPriceMethodException e)
            {
                _log.ErrorWithDetails(e, new { e.PriceMethod });

                throw;
            }
            catch (ZeroRateException e)
            {
                _log.ErrorWithDetails(e, new
                {
                    lykkePaymentAssetId,
                    lykkeSettlementAssetId,
                    requestMarkup.Percent,
                    requestMarkup.Pips,
                    merchantMarkup
                });

                throw;
            }

            return(assetPairId, paymentAmount, rate);
        }
示例#5
0
        public async Task <IOrder> GetLatestOrCreateAsync(IPaymentRequest paymentRequest, bool force = false)
        {
            IReadOnlyList <IOrder> orders = await _orderRepository.GetAsync(paymentRequest.Id);

            IOrder latestOrder = orders.OrderByDescending(x => x.ExtendedDueDate).FirstOrDefault();

            var now = DateTime.UtcNow;

            if (latestOrder != null)
            {
                if (now < latestOrder.DueDate)
                {
                    return(latestOrder);
                }

                if (now < latestOrder.ExtendedDueDate && !force)
                {
                    return(latestOrder);
                }
            }

            IMerchant merchant = await _merchantRepository.GetAsync(paymentRequest.MerchantId);

            if (merchant == null)
            {
                throw new MerchantNotFoundException(paymentRequest.MerchantId);
            }

            string lykkePaymentAssetId = await _lykkeAssetsResolver.GetLykkeId(paymentRequest.PaymentAssetId);

            string lykkeSettlementAssetId = await _lykkeAssetsResolver.GetLykkeId(paymentRequest.SettlementAssetId);

            string assetPairId = $"{paymentRequest.PaymentAssetId}{paymentRequest.SettlementAssetId}";

            RequestMarkup requestMarkup = Mapper.Map <RequestMarkup>(paymentRequest);

            IMarkup merchantMarkup;

            try
            {
                merchantMarkup = await _markupService.ResolveAsync(merchant.Id, assetPairId);
            }
            catch (MarkupNotFoundException ex)
            {
                await _log.WriteErrorAsync(nameof(OrderService), nameof(GetLatestOrCreateAsync), new
                {
                    ex.MerchantId,
                    ex.AssetPairId
                }.ToJson(), ex);

                throw;
            }

            decimal paymentAmount, rate;

            try
            {
                paymentAmount = await _calculationService.GetAmountAsync(lykkePaymentAssetId,
                                                                         lykkeSettlementAssetId, paymentRequest.Amount, requestMarkup, merchantMarkup);

                rate = await _calculationService.GetRateAsync(lykkePaymentAssetId, lykkeSettlementAssetId,
                                                              requestMarkup.Percent, requestMarkup.Pips, merchantMarkup);
            }
            catch (MarketPriceZeroException priceZeroEx)
            {
                _log.WriteError(nameof(GetLatestOrCreateAsync), new { priceZeroEx.PriceType }, priceZeroEx);

                throw;
            }
            catch (UnexpectedAssetPairPriceMethodException assetPairEx)
            {
                _log.WriteError(nameof(GetLatestOrCreateAsync), new { assetPairEx.PriceMethod }, assetPairEx);

                throw;
            }

            var order = new Order
            {
                MerchantId       = paymentRequest.MerchantId,
                PaymentRequestId = paymentRequest.Id,
                AssetPairId      = assetPairId,
                SettlementAmount = paymentRequest.Amount,
                PaymentAmount    = paymentAmount,
                DueDate          = now.Add(_orderExpirationPeriods.Primary),
                ExtendedDueDate  = now.Add(_orderExpirationPeriods.Extended),
                CreatedDate      = now,
                ExchangeRate     = rate
            };

            IOrder createdOrder = await _orderRepository.InsertAsync(order);

            await _log.WriteInfoAsync(nameof(OrderService), nameof(GetLatestOrCreateAsync), order.ToJson(),
                                      "Order created.");

            return(createdOrder);
        }