public async Task <MerchantAddressDto> Handle(AddMerchantAddressCommand request, CancellationToken cancellationToken)
        {
            var merchant = await _merchantRepository.GetAsync(request.MerchantCode);

            if (merchant is null)
            {
                throw new MerchantNotFoundException();
            }

            var merchantLocation = MerchantAddress.RegisterCreated(
                request.MerchantCode,
                request.MerchantName,
                request.Name,
                request.Address,
                request.Country,
                request.City,
                request.Town,
                request.District,
                request.Location,
                request.WorkingHours,
                request.ExtraInfo);

            merchantLocation.Activate();
            var locations = await _merchantLocationsRepository.GetAsync(request.MerchantCode, request.Name);

            if (locations != null)
            {
                throw new MerchantAddressAlreadyRegisteredException();
            }

            await _merchantLocationsRepository.AddAsync(merchantLocation);

            return(_mapper.Map <MerchantAddressDto>(merchantLocation));
        }
Exemplo n.º 2
0
        public async Task <Unit> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
        {
            if (request.OrderProducts is null || !request.OrderProducts.Any())
            {
                throw new ProductNotFoundException();
            }
            if (request.OrderProducts.Any(x => x.Quantity < 1))
            {
                throw new ProductItemShouldHaveAtLeastOneQuantityException();
            }

            var merchant = await _merchantRepository.GetAsync(request.MerchantCode);

            var merchantAddress = await _merchantLocationsRepository.GetAsync(request.MerchantCode, request.MerchantAddressCode);

            var address = new Address
            {
                Code        = merchantAddress.Code,
                FullAddress = merchantAddress.Location,
                City        = merchantAddress.City,
                Town        = merchantAddress.Town,
                District    = merchantAddress.District,
                Name        = merchantAddress.Name,
            };

            var order = Order.Create(merchant.Name, merchant.Code, request.FirstName, request.LastName, request.Phone,
                                     request.OrderNote, address, request.ReceiverAddress, request.OrderProducts);
            await _orderRepository.CreateAsync(order);

            return(Unit.Task.Result);
        }
        public async Task <Models.PaymentResultModel> Handle(
            ProcessPaymentRequestCommand command,
            CancellationToken cancellationToken = default)
        {
            var merchant = await _merchantRepository.GetAsync(command.MerchantId);

            if (merchant == null)
            {
                _logger.LogWarning("Merchant with {id} was not found", command.MerchantId);
                throw new Exception("Merchant was not found");
            }

            var shopperCard = new PaymentCard(
                command.CardNumber,
                command.CardHolderName,
                command.ExpiryMonth,
                command.ExpiryYear);

            var payment = new Payment(
                shopperCard,
                command.MerchantId,
                command.ChargeTotal,
                command.CurrencyCode);

            _paymentRepository.Add(payment);
            await _unitOfWork.CommitAsync();

            try
            {
                var transactionResult = await ProcessPaymentByAcquiringBank(command, merchant.Iban);

                if (transactionResult.Status == TransactionStatus.Accepted)
                {
                    payment.Succeed();
                }
                else
                {
                    payment.Fail();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Payment transaction with {id} failed to processed by acquiring bank", payment.Id);
                payment.Fail();
            }

            await _unitOfWork.CommitAsync();

            return(new PaymentResultModel
            {
                PaymentId = payment.Id,
                Status = payment.Status
            });
        }
        public async Task <SecurityErrorType> Post([FromBody] MerchantAuthRequest request)
        {
            if (request == null)
            {
                return(SecurityErrorType.MerchantUnknown);
            }

            var merchant = await _merchantRepository.GetAsync(request.MerchantId);

            if (merchant == null)
            {
                return(SecurityErrorType.MerchantUnknown);
            }

            return(_securityHelper.CheckRequest(request.StringToSign, merchant.MerchantId, request.Sign,
                                                merchant.PublicKey, merchant.ApiKey));
        }
        public async Task <MerchantAddressDto> Handle(UpdateMerchantAddressCommand request, CancellationToken cancellationToken)
        {
            var merchant = await _merchantRepository.GetAsync(request.MerchantCode);

            if (merchant is null)
            {
                throw new MerchantNotFoundException();
            }

            var merchantLocation = await _merchantAddressRepository.GetAsync(request.MerchantCode, request.MerchantAddressCode);

            if (merchantLocation is null)
            {
                throw new ActiveMerchantLocationNotFoundException();
            }

            merchantLocation.Update(request.Name, request.Address, request.City, request.Town, request.District,
                                    request.Location, request.WorkingHours, request.ExtraInfo);
            await _merchantAddressRepository.UpdateAsync(merchantLocation);

            return(_mapper.Map <MerchantAddressDto>(merchantLocation));
        }
Exemplo n.º 6
0
 public async Task <IReadOnlyList <IMerchant> > GetAsync()
 {
     return(await _merchantRepository.GetAsync());
 }
Exemplo n.º 7
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);
        }