Exemple #1
0
        private async Task <Money18> ConvertVoucherPriceToTokensAsync(Guid customerId, Guid spendRuleId, decimal price)
        {
            var conversionRequest = new ConvertAmountBySpendRuleRequest
            {
                CustomerId   = customerId,
                Amount       = price,
                FromCurrency = _settingsService.GetBaseCurrencyCode(),
                ToCurrency   = _settingsService.GetTokenCurrencyCode(),
                SpendRuleId  = spendRuleId
            };

            var conversionResponse =
                await _eligibilityEngineClient.ConversionRate.GetAmountBySpendRuleAsync(conversionRequest);

            if (conversionResponse.ErrorCode != EligibilityEngineErrors.None)
            {
                _log.Warning("An error occurred while converting amount.",
                             context: $"request: {conversionRequest.ToJson()}; error: {conversionResponse.ErrorCode}");

                throw new InvalidConversionException();
            }

            return(conversionResponse.Amount);
        }
Exemple #2
0
        public async Task <PaymentTransfersErrorCodes> PaymentTransferAsync(PaymentTransferDto paymentTransfer)
        {
            #region Validation

            if (!await CheckIfCustomerExists(paymentTransfer.CustomerId))
            {
                return(PaymentTransfersErrorCodes.CustomerDoesNotExist);
            }

            if (await CheckIfCustomerWalletIsBlocked(paymentTransfer.CustomerId))
            {
                return(PaymentTransfersErrorCodes.CustomerWalletBlocked);
            }

            var isCampaignIdGuid = Guid.TryParse(paymentTransfer.SpendRuleId, out var campaignIdAsGuid);
            if (!isCampaignIdGuid)
            {
                return(PaymentTransfersErrorCodes.InvalidSpendRuleId);
            }

            var campaignResult = await _campaignClient.BurnRules.GetByIdAsync(campaignIdAsGuid);

            if (campaignResult.ErrorCode == CampaignServiceErrorCodes.EntityNotFound)
            {
                return(PaymentTransfersErrorCodes.SpendRuleNotFound);
            }

            if (campaignResult.Vertical != Vertical.RealEstate)
            {
                return(PaymentTransfersErrorCodes.InvalidVerticalInSpendRule);
            }

            //We can have either amount in Tokens or in Fiat
            if (paymentTransfer.AmountInFiat != null && paymentTransfer.AmountInTokens != null)
            {
                return(PaymentTransfersErrorCodes.CannotPassBothFiatAndTokensAmount);
            }

            //Fiat or Tokens Amount must be provided
            if (paymentTransfer.AmountInFiat == null && paymentTransfer.AmountInTokens == null)
            {
                return(PaymentTransfersErrorCodes.EitherFiatOrTokensAmountShouldBePassed);
            }

            if (paymentTransfer.AmountInTokens != null && paymentTransfer.AmountInTokens <= 0)
            {
                return(PaymentTransfersErrorCodes.InvalidTokensAmount);
            }

            if (paymentTransfer.AmountInFiat != null && paymentTransfer.AmountInFiat <= 0)
            {
                return(PaymentTransfersErrorCodes.InvalidFiatAmount);
            }

            #endregion

            var conversionRequest = new ConvertAmountBySpendRuleRequest
            {
                CustomerId   = Guid.Parse(paymentTransfer.CustomerId),
                Amount       = paymentTransfer.AmountInTokens ?? paymentTransfer.AmountInFiat.Value,
                FromCurrency = paymentTransfer.AmountInTokens.HasValue ? _settingsService.GetTokenCurrencyCode() : paymentTransfer.Currency,
                ToCurrency   = paymentTransfer.AmountInTokens.HasValue ? paymentTransfer.Currency : _settingsService.GetTokenCurrencyCode(),
                SpendRuleId  = campaignIdAsGuid
            };

            var conversionResponse = await _eligibilityEngineClient.ConversionRate.GetAmountBySpendRuleAsync(conversionRequest);

            if (conversionResponse.ErrorCode != EligibilityEngineErrors.None)
            {
                _log.Warning("Invalid amount conversion when trying to add payment transfer", context: conversionRequest);
                return(PaymentTransfersErrorCodes.InvalidAmountConversion);
            }

            if (paymentTransfer.AmountInFiat == null)
            {
                paymentTransfer.AmountInFiat = (decimal)conversionResponse.Amount;
            }
            else
            {
                paymentTransfer.AmountInTokens = conversionResponse.Amount;
            }

            var transferId         = Guid.NewGuid();
            var transferIdAsString = transferId.ToString();
            var receiptNumber      = await GenerateReceiptNumberAsync();

            paymentTransfer.TransferId    = transferIdAsString;
            paymentTransfer.ReceiptNumber = receiptNumber;
            paymentTransfer.InvoiceId     = receiptNumber;
            paymentTransfer.Timestamp     = DateTime.UtcNow;

            var encodedData = EncodePaymentTransferData(paymentTransfer.SpendRuleId, receiptNumber, transferIdAsString);

            var pbfTransferResponse = await _pbfClient.GenericTransfersApi.GenericTransferAsync(new GenericTransferRequestModel
            {
                Amount           = paymentTransfer.AmountInTokens.Value,
                AdditionalData   = encodedData,
                RecipientAddress = _settingsService.GetPaymentTransfersAddress(),
                SenderCustomerId = paymentTransfer.CustomerId,
                TransferId       = transferIdAsString
            });

            if (pbfTransferResponse.Error == TransferError.SenderWalletMissing)
            {
                return(PaymentTransfersErrorCodes.CustomerWalletDoesNotExist);
            }

            if (pbfTransferResponse.Error == TransferError.NotEnoughFunds)
            {
                return(PaymentTransfersErrorCodes.NotEnoughFunds);
            }

            await _paymentTransfersRepository.AddAsync(paymentTransfer);

            return(PaymentTransfersErrorCodes.None);
        }
        public async Task <ConvertAmountBySpendRuleResponse> GetAmountBySpendRuleAsync(ConvertAmountBySpendRuleRequest request)
        {
            if (request.FromCurrency.ToLower() == request.ToCurrency.ToLower())
            {
                return(new ConvertAmountBySpendRuleResponse
                {
                    ConversionSource = ConversionSource.BurnRule,
                    Amount = request.Amount,
                    UsedRate = 1
                });
            }

            AmountConvertBySpendRuleResponse result;

            try
            {
                result = await _conversionRateService.GetAmountBySpendRuleAsync(
                    request.SpendRuleId, request.CustomerId, request.FromCurrency, request.ToCurrency, request.Amount);
            }
            catch (BurnRuleNotFoundException e)
            {
                _log.Info(e.Message, context: request);

                return(new ConvertAmountBySpendRuleResponse
                {
                    ErrorCode = EligibilityEngineErrors.SpendRuleNotFound,
                    ErrorMessage = e.Message
                });
            }
            catch (ConversionRateNotFoundException e)
            {
                _log.Info(e.Message, context: request);

                return(new ConvertAmountBySpendRuleResponse
                {
                    ErrorCode = EligibilityEngineErrors.ConversionRateNotFound,
                    ErrorMessage = e.Message
                });
            }

            return(new ConvertAmountBySpendRuleResponse
            {
                Amount = result.Amount,
                CurrencyCode = result.CurrencyCode,
                UsedRate = result.UsedRate,
                ConversionSource = Mapper.Map <ConversionSource>(result.ConversionSource),
                SpendRuleId = result.SpendRuleId
            });
        }