Exemple #1
0
        internal void ValidateCreatePaymentRequest(PaymentsCreateRequest contract)
        {
            if (!contract.FiatAmount.HasValue && !contract.TokensAmount.HasValue)
            {
                throw new ArgumentNullException(nameof(contract.FiatAmount), "Fiat or Token amount required");
            }

            if (string.IsNullOrWhiteSpace(contract.CustomerId))
            {
                throw new ArgumentNullException(nameof(contract.CustomerId));
            }

            if (string.IsNullOrWhiteSpace(contract.PartnerId))
            {
                throw new ArgumentNullException(nameof(contract.PartnerId));
            }

            if (string.IsNullOrWhiteSpace(contract.ExternalLocationId))
            {
                throw new ArgumentNullException(nameof(contract.ExternalLocationId));
            }

            if (!contract.TokensAmount.HasValue)
            {
                if (contract.FiatAmount.Value <= 0)
                {
                    throw new ArgumentException("FiatAmount must be a positive number");
                }

                if (string.IsNullOrWhiteSpace(contract.Currency))
                {
                    throw new ArgumentNullException(nameof(contract.Currency));
                }

                if (contract.Currency.Length > 20)
                {
                    throw new ArgumentException($"{nameof(contract.Currency)} must not be bigger than 20 characters");
                }
            }

            if (!contract.FiatAmount.HasValue)
            {
                if (contract.TokensAmount.Value <= 0)
                {
                    throw new ArgumentException("TokensAmount must be a positive number");
                }
            }

            if (contract.ExpirationTimeoutInSeconds.HasValue)
            {
                if (contract.ExpirationTimeoutInSeconds.Value <= 0)
                {
                    throw new ArgumentException("ExpirationTimeoutInSeconds must be a positive number");
                }
            }
        }
Exemple #2
0
        public async Task <PaymentsCreateResponse> CreatePaymentRequestAsync(PaymentsCreateRequest contract)
        {
            _log.Info("Creating payment request",
                      new { contract.CustomerId });

            ValidateCreatePaymentRequest(contract);

            LocationInfoResponse locationInfoResponse = null;

            var paymentRequestModel = _mapper.Map <PaymentRequestModel>(contract);

            if (!string.IsNullOrWhiteSpace(contract.ExternalLocationId))
            {
                locationInfoResponse = await _partnerManagementClient.Locations.GetByExternalId2Async(contract.ExternalLocationId);

                if (locationInfoResponse == null)
                {
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.LocationNotFound
                    });
                }

                paymentRequestModel.LocationId = locationInfoResponse.Id.ToString();
            }

            var partnerAndLocationStatus = await _partnerAndLocationHelper.ValidatePartnerInfo(contract.PartnerId, locationInfoResponse);

            if (partnerAndLocationStatus != PartnerAndLocationStatus.OK)
            {
                if (partnerAndLocationStatus == PartnerAndLocationStatus.PartnerNotFound)
                {
                    return new PaymentsCreateResponse {
                               Status = PaymentCreateStatus.PartnerNotFound
                    }
                }
                ;
                if (partnerAndLocationStatus == PartnerAndLocationStatus.LocationNotFound)
                {
                    return new PaymentsCreateResponse {
                               Status = PaymentCreateStatus.LocationNotFound
                    }
                }
                ;
            }

            if (contract.PaymentInfo != null)
            {
                var sendMessageResponse =
                    await _messagesService.SendMessageAsync(_mapper.Map <MessagesPostRequest>(contract));

                switch (sendMessageResponse.ErrorCode)
                {
                case MessagesErrorCode.CustomerIsBlocked:
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.CustomerIsBlocked
                    });

                case MessagesErrorCode.CustomerNotFound:
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.CustomerNotFound
                    });

                case MessagesErrorCode.LocationNotFound:
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.LocationNotFound
                    });

                case MessagesErrorCode.PartnerNotFound:
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.PartnerNotFound
                    });
                }

                paymentRequestModel.PartnerMessageId = sendMessageResponse.PartnerMessageId;
            }

            var responseModel = await _partnersPaymentsClient.Api.PartnerPaymentAsync(paymentRequestModel);

            var responseContract = new PaymentsCreateResponse()
            {
                PaymentRequestId = responseModel.PaymentRequestId, Status = PaymentCreateStatus.OK
            };

            if (responseModel.Error != PaymentRequestErrorCodes.None)
            {
                _log.Error(null, $"Received error code {responseModel.Error}", responseModel.PaymentRequestId);
                responseContract.Status = _mapper.Map <PaymentCreateStatus>(responseModel.Error);
            }

            //Only save if everything is ok and callback url is provided
            if (responseModel.Error == PaymentRequestErrorCodes.None &&
                !string.IsNullOrEmpty(contract.PaymentProcessedCallbackUrl))
            {
                await _paymentCallbackRepository.InsertAsync(new PaymentProcessedCallbackUrl
                {
                    PaymentRequestId = responseModel.PaymentRequestId,
                    RequestAuthToken = contract.RequestAuthToken,
                    Url = contract.PaymentProcessedCallbackUrl
                });
            }

            return(responseContract);
        }