Beispiel #1
0
        public async Task <IActionResult> Post(PaymentUrlDataRequest model)
        {
            if (string.IsNullOrWhiteSpace(model.AssetId))
            {
                model.AssetId = LykkeConstants.UsdAssetId;
            }

            var phoneNumberE164 = model.Phone.PreparePhoneNum().ToE164Number();
            var countryAsIso3   = _countryComponent.GetCountryIso3Code(model.Country);
            var pd = await _personalDataService.GetAsync(model.ClientId);

            CashInPaymentSystem paymentSystem;

            switch (model.DepositOption)
            {
            case DepositOption.BankCard:
                paymentSystem = CashInPaymentSystem.Fxpaygate;
                break;

            case DepositOption.Other:
                paymentSystem = CashInPaymentSystem.CreditVoucher;
                break;

            default:
                paymentSystem = CashInPaymentSystem.Unknown;
                break;
            }

            var transactionId = await _paymentUrlDataService.GenerateNewTransactionIdAsync();

            const string formatOfDateOfBirth = "yyyy-MM-dd";

            var info = OtherPaymentInfo.Create(
                model.FirstName,
                model.LastName,
                model.City,
                model.Zip,
                model.Address,
                countryAsIso3,
                model.Email,
                phoneNumberE164,
                pd.DateOfBirth?.ToString(formatOfDateOfBirth),
                model.OkUrl,
                model.FailUrl,
                model.CancelUrl)
                       .ToJson();

            var bankCardsFee = await _feeCalculatorClient.GetBankCardFees();

            var asset = await _assetsService.AssetGetAsync(model.AssetId);

            var feeAmount          = Math.Round(model.Amount * bankCardsFee.Percentage, 15);
            var feeAmountTruncated = feeAmount.TruncateDecimalPlaces(asset.Accuracy, true);

            var urlData = await _paymentUrlDataService.GetUrlDataAsync(
                paymentSystem.ToString(),
                transactionId,
                model.ClientId,
                model.Amount + feeAmountTruncated,
                model.AssetId,
                model.WalletId,
                countryAsIso3,
                info);

            await _paymentTransactionEventLogService.InsertPaymentTransactionEventLogAsync(new PaymentTransactionEventLog
            {
                PaymentTransactionId = transactionId,
                Message  = "Payment Url has created",
                DateTime = DateTime.UtcNow,
                TechData = urlData.PaymentUrl,
                Who      = model.ClientId
            });

            if (!string.IsNullOrEmpty(urlData.ErrorMessage))
            {
                await _log.WriteWarningAsync(nameof(PaymentUrlDataController), nameof(Post), model.ToJson(),
                                             urlData.ErrorMessage, DateTime.UtcNow);

                return(BadRequest(new
                {
                    message = urlData.ErrorMessage
                }));
            }

            await _paymentTransactionsService.InsertPaymentTransactionAsync(
                new PaymentTransaction
            {
                Amount           = model.Amount,
                Status           = PaymentStatus.Created,
                PaymentSystem    = paymentSystem,
                FeeAmount        = feeAmountTruncated,
                Id               = transactionId,
                ClientId         = model.ClientId,
                AssetId          = model.AssetId,
                DepositedAssetId = model.AssetId,
                WalletId         = model.WalletId,
                Created          = DateTime.UtcNow,
                Info             = info
            });

            await _paymentTransactionEventLogService.InsertPaymentTransactionEventLogAsync(new PaymentTransactionEventLog
            {
                PaymentTransactionId = transactionId,
                Message  = "Registered",
                DateTime = DateTime.UtcNow,
                TechData = string.Empty,
                Who      = model.ClientId
            });

            // mode=iframe is for Mobile version
            if (!string.IsNullOrWhiteSpace(urlData.PaymentUrl))
            {
                urlData.PaymentUrl = urlData.PaymentUrl
                                     + (urlData.PaymentUrl.Contains("?") ? "&" : "?")
                                     + "mode=iframe";
            }

            var result = new PaymentUrlDataResponse
            {
                Url       = urlData.PaymentUrl,
                OkUrl     = urlData.OkUrl,
                FailUrl   = urlData.FailUrl,
                CancelUrl = urlData.CancelUrl,
            };

            return(Ok(result));
        }
Beispiel #2
0
        public override async Task <PaymentUrlResponse> GetPaymentUrl(PaymentUrlRequest request, ServerCallContext context)
        {
            try
            {
                string errorMessage = string.Empty;
                ErrorDetails.Types.ErrorType?errorType = null;

                if (_supportedCurrencies.Any() && !_supportedCurrencies.Contains(request.Transaction.AssetId))
                {
                    errorMessage = $"Asset {request.Transaction.AssetId} is not supported";
                    errorType    = ErrorDetails.Types.ErrorType.CurrencyNotSupported;
                }

                if (_supportedCountries.Any() && !string.IsNullOrEmpty(request.Details.CountryIso3) && !_supportedCountries.Contains(request.Details.CountryIso3))
                {
                    errorMessage = $"Country {request.Details.CountryIso3} is not supported";
                    errorType    = ErrorDetails.Types.ErrorType.CountryNotSupported;
                }

                if (errorType != null)
                {
                    _log.Warning(errorMessage);

                    return(new PaymentUrlResponse
                    {
                        Error = new ErrorDetails
                        {
                            ErrorType = errorType.Value,
                            Message = errorMessage
                        }
                    });
                }

                var bankCardsFees = await _feeCalculatorClient.GetBankCardFees();

                var feeAmount   = Math.Round(request.Transaction.Amount * bankCardsFees.Percentage, 15);
                var totalAmount = (decimal)request.Transaction.Amount + (decimal)feeAmount;

                var url = await _link4PayApiService.GetPaymentUrlAsync(new CardPaymentRequest
                {
                    Merchant = new MerchantInfo {
                        CustomerId = request.Transaction.ExternalClientId, MerchantId = _link4PaySettings.ClientId
                    },
                    Transaction =
                        new TransactionInfo
                    {
                        TxnAmount    = totalAmount.ToString("F2"),
                        CurrencyCode = request.Transaction.AssetId,
                        TxnReference = request.Transaction.TransactionId,
                        Payout       = false
                    },
                    Customer = new CustomerInfo
                    {
                        BillingAddress = new AddressInfo
                        {
                            FirstName = request.Details?.FirstName,
                            LastName  = request.Details?.LastName,
                            EmailId   = request.Details?.Email,
                            MobileNo  = request.Details?.Phone
                        }
                    },
                    Url = new UrlInfo
                    {
                        SuccessUrl = $"{(string.IsNullOrEmpty(request.Urls?.OkUrl?.Trim()) ? _successUrl : request.Urls?.OkUrl)}?v=1",
                        FailUrl    = string.IsNullOrEmpty(request.Urls?.FailUrl?.Trim())
                            ? _failUrl
                            : request.Urls?.FailUrl,
                        CancelUrl = string.IsNullOrEmpty(request.Urls?.CancelUrl?.Trim())
                            ? _cancelUrl
                            : request.Urls?.CancelUrl
                    }
                });

                if (string.IsNullOrEmpty(url))
                {
                    return(new PaymentUrlResponse
                    {
                        Error = new ErrorDetails
                        {
                            ErrorType = ErrorDetails.Types.ErrorType.Unknown,
                            Message = "Error getting the payment url"
                        }
                    });
                }

                await _rawLogRepository.RegisterEventAsync(
                    RawLogEvent.Create("Payment Url has been created", request.ToJson()),
                    request.Transaction.ClientId);

                var info = OtherPaymentInfo.Create(
                    firstName: request.Details.FirstName,
                    lastName: request.Details.LastName,
                    city: string.Empty,
                    zip: string.Empty,
                    address: string.Empty,
                    country: request.Details.CountryIso3,
                    email: request.Details.Email,
                    contactPhone: request.Details.Phone,
                    dateOfBirth: string.Empty)
                           .ToJson();

                var pt = PaymentTransaction.Create(
                    request.Transaction.TransactionId,
                    CashInPaymentSystem.Link4Pay,
                    request.Transaction.ClientId,
                    request.Transaction.Amount,
                    feeAmount,
                    request.Transaction.AssetId,
                    null,
                    request.Transaction.AssetId,
                    info
                    );

                await Task.WhenAll(
                    _paymentTransactionsRepository.CreateAsync(pt),
                    _paymentTransactionEventsLog.WriteAsync(PaymentTransactionLogEvent.Create(request.Transaction.TransactionId, "",
                                                                                              "Registered", request.Transaction.ClientId)),
                    _paymentTransactionEventsLog.WriteAsync(PaymentTransactionLogEvent.Create(request.Transaction.TransactionId, url,
                                                                                              "Payment Url has created", request.Transaction.ClientId))
                    );

                return(new PaymentUrlResponse
                {
                    PaymentUrl = url,
                    OkUrl = $"{(string.IsNullOrEmpty(request.Urls?.OkUrl?.Trim()) ? _successUrl : request.Urls?.OkUrl)}?v=1",
                    FailUrl = string.IsNullOrEmpty(request.Urls?.FailUrl?.Trim())
                        ? _failUrl
                        : request.Urls?.FailUrl,
                    CancelUrl = string.IsNullOrEmpty(request.Urls?.CancelUrl?.Trim())
                        ? _cancelUrl
                        : request.Urls?.CancelUrl
                });
            }
            catch (Exception ex)
            {
                _log.Error(ex);

                return(new PaymentUrlResponse
                {
                    Error = new ErrorDetails
                    {
                        ErrorType = ErrorDetails.Types.ErrorType.Unknown,
                        Message = "Error getting the payment url"
                    }
                });
            }
        }