Ejemplo n.º 1
0
        public async Task <CommandHandlingResult> Handle(CashInCommand command, IEventPublisher eventPublisher)
        {
            var tx = await _paymentTransactionsRepository.GetByTransactionIdAsync(command.TransactionId);

            if (tx != null && (tx.Status == PaymentStatus.NotifyDeclined || tx.Status == PaymentStatus.NotifyProcessed || tx.Status == PaymentStatus.Processing))
            {
                return(CommandHandlingResult.Ok());
            }

            var transactionStatus = await _link4PayApiService.GetTransactionInfoAsync(command.TransactionId);

            if (!string.IsNullOrEmpty(transactionStatus.Card?.CardHash) && transactionStatus.OriginalTxnStatus == TransactionStatus.Successful)
            {
                await _paymentTransactionsRepository.SaveCardHashAsync(command.TransactionId, transactionStatus.Card.CardHash);

                if (tx != null)
                {
                    var evt = new CreditCardUsedEvent
                    {
                        ClientId     = tx.ClientId,
                        OrderId      = command.TransactionId,
                        CardHash     = transactionStatus.Card.CardHash,
                        CardNumber   = transactionStatus.Card.CardNo,
                        CustomerName = transactionStatus.Card.CardHolderName
                    };

                    eventPublisher.PublishEvent(evt);
                }
                else
                {
                    _log.Warning("CreditCardUsedEvent is not sent!", context: new { tranasactionId = transactionStatus.TxnReference }.ToJson());
                }
            }

            switch (transactionStatus.OriginalTxnStatus)
            {
            case TransactionStatus.Successful:
                tx = await _paymentTransactionsRepository.StartProcessingTransactionAsync(command.TransactionId);

                if (tx != null)     // initial status
                {
                    eventPublisher.PublishEvent(new ProcessingStartedEvent
                    {
                        OrderId = command.TransactionId
                    });
                }

                return(CommandHandlingResult.Ok());

            case TransactionStatus.Failed:
                await _paymentTransactionsRepository.SetStatusAsync(command.TransactionId, PaymentStatus.NotifyDeclined);

                await _paymentTransactionEventsLog.WriteAsync(
                    PaymentTransactionLogEvent.Create(
                        command.TransactionId, command.Request, "Declined by Payment status from payment system", nameof(CashInCommand)));

                return(CommandHandlingResult.Ok());

            default:
                return(new CommandHandlingResult {
                    Retry = true, RetryDelay = (long)TimeSpan.FromMinutes(1).TotalMilliseconds
                });
            }
        }
        public async Task <IActionResult> TransactionInfo(string transactionId)
        {
            var data = await _link4PayApiService.GetTransactionInfoAsync(transactionId);

            return(Ok(data));
        }