コード例 #1
0
        public async Task <ProcessPaymentResult> ProcessAsync(IPaymentRequest paymentRequest)
        {
            var unprocessedPayment   = _paymentsService.CreateUnprocessedPayment(paymentRequest);
            var acquiringBankRequest = await _acquiringBank.CreateRequestAsync(paymentRequest);

            _logger.LogInformation("Begun processing payment {PaymentId}.", unprocessedPayment.Id);

            IAcquiringBankResponse acquiringBankResponse = null;

            try
            {
                acquiringBankResponse = await _acquiringBank.ProcessPaymentAsync(acquiringBankRequest);

                _logger.LogInformation("The acquiring bank has processed the payment {PaymentId} without error.",
                                       unprocessedPayment.Id);
            }
            catch (Exception exception)
            {
                _logger.LogError(
                    "Exception occured whilst the acquiring bank was processing payment {PaymentId}. {Exception}",
                    unprocessedPayment.Id, exception);
            }

            var processedPayment = _paymentsService.CreateProcessedPayment(unprocessedPayment, acquiringBankResponse);
            var payment          = CreatePayment(processedPayment);

            await _paymentsRepository.SavePaymentAsync(payment);

            _logger.LogInformation("Payment {PaymentId} has successfully been processed.", processedPayment.Id);

            return(new ProcessPaymentResult
            {
                Payment = processedPayment
            });
        }