public Task <ProcessRejectedPaymentResponse> Handle(ProcessRejectedPaymentRequest request, CancellationToken cancellationToken = default)
        {
            var merchantKey = _merchantEncryptionKeyGetter.Key(request.Merchant.Id);

            var paymentRecord = new PaymentRecord
            {
                Id     = _guid.NewGuid().ToString(),
                Source = new PaymentRecord.PaymentSource
                {
                    CardExpiryEncrypted = _encrypter.EncryptUtf8(request.Source.CardExpiry, merchantKey),
                    CardNumberEncrypted = _encrypter.EncryptUtf8(request.Source.CardNumber, merchantKey),
                    CvvEncrypted        = _encrypter.EncryptUtf8(request.Source.Cvv, merchantKey)
                },
                Recipient = new PaymentRecord.PaymentRecipient
                {
                    AccountNumberEncrypted = _encrypter.EncryptUtf8(request.Recipient.AccountNumber, merchantKey),
                    SortCodeEncrypted      = _encrypter.EncryptUtf8(request.Recipient.SortCode, merchantKey),
                },
                Currency      = request.Currency,
                Status        = PaymentStatus.Rejected,
                Amount        = request.Amount,
                CreatedAt     = _dateTime.UtcNow(),
                MerchantId    = request.Merchant.Id,
                FailureReason = request.BankResponse.FailureReason,
            };

            _paymentRecordCreator.Add(paymentRecord);

            return(Task.FromResult(new ProcessRejectedPaymentResponse
            {
                Id = paymentRecord.Id
            }));
        }
Exemple #2
0
        public Task <ApiResponse <GetPaymentByIdResponse> > Handle(GetPaymentByIdRequest request, CancellationToken cancellationToken = default)
        {
            var merchantId = _merchantContext.GetMerchantId();

            var merchantKey = _encryptionKeys.Key(merchantId);

            var paymentRecord = _paymentRecordReader
                                .PaymentRecords
                                .Where(record => record.MerchantId == _merchantContext.GetMerchantId())
                                .Where(record => record.Id == request.Id)
                                .Select(b => new
            {
                b.Id,
                DecryptedCvv           = _decrypter.DecryptUtf8(b.Source.CvvEncrypted, merchantKey),
                DecryptedCardExpiry    = _decrypter.DecryptUtf8(b.Source.CardExpiryEncrypted, merchantKey),
                DecryptedCardNumber    = _decrypter.DecryptUtf8(b.Source.CardNumberEncrypted, merchantKey),
                DecryptedSortCode      = _decrypter.DecryptUtf8(b.Recipient.SortCodeEncrypted, merchantKey),
                DecryptedAccountNumber = _decrypter.DecryptUtf8(b.Recipient.AccountNumberEncrypted, merchantKey),
                b.FailureReason,
                b.Amount,
                b.Status,
                b.CreatedAt,
                b.Currency
            })
                                .Select(record => new GetPaymentByIdResponse
            {
                Id     = record.Id,
                Source = new GetPaymentByIdResponse.PaymentSource
                {
                    Cvv        = record.DecryptedCvv.Mask(3, 0, 'X'),
                    CardExpiry = record.DecryptedCardExpiry,
                    CardNumber = record.DecryptedCardNumber.Mask(12, 0, 'X'),
                },
                Recipient = new GetPaymentByIdResponse.PaymentRecipient
                {
                    SortCode      = record.DecryptedSortCode.Mask(4, 0, 'X'),
                    AccountNumber = record.DecryptedAccountNumber.Mask(6, 0, 'X')
                },
                Details = string.IsNullOrEmpty(record.FailureReason) ? null : new GetPaymentByIdResponse.BankResponse
                {
                    FailureReason = record.FailureReason
                },
                Amount    = record.Amount,
                Status    = record.Status,
                CreatedAt = record.CreatedAt,
                Currency  = record.Currency,
            }).FirstOrDefault();

            if (paymentRecord == null)
            {
                return(Task.FromResult(ApiResponse <GetPaymentByIdResponse> .Fail(StatusCodes.Status404NotFound,
                                                                                  "NOT_FOUND",
                                                                                  "Payment record not found")));
            }

            return(Task.FromResult(ApiResponse <GetPaymentByIdResponse> .Success(StatusCodes.Status200OK, paymentRecord)));
        }