示例#1
0
        public async Task <IActionResult> CreateInvoice([FromBody] ViewModel.CreateInvoiceRequest request)
        {
            try
            {
                // to enable idempotency.
                var invoice = await _invoiceRepository.GetInvoiceByTripAsync(request.TripId);

                if (invoice != null)
                {
                    return(CreatedAtAction(nameof(GetInvoice), new { invoiceId = invoice.InvoiceId }, invoice.InvoiceId));
                }

                invoice = new Domain.Invoice.Model.Invoice(
                    request.PaymentMethod.Id,
                    request.TripId,
                    request.Duration,
                    request.Distance,
                    request.TripStatus.Id);

                await _invoiceRepository.AddInvoiceAsync(invoice);

                // integration with external payment system.
                if (Equals(invoice.PaymentMethod, PaymentMethod.CreditCard) && invoice.Total > 0)
                {
                    await _paymentService.PerformPayment(invoice, request.UserId);
                }

                return(CreatedAtAction(nameof(GetInvoice), new { invoiceId = invoice.InvoiceId }, invoice.InvoiceId));
            }
            finally
            {
                _invoiceRepository.Dispose();
            }
        }
        public async Task Handle(TripFinishedIntegrationEvent @event)
        {
            var invoice = await _invoiceRepository.GetInvoiceByTripAsync(@event.TripId);

            if (invoice != null)
            {
                return;
            }

            try
            {
                invoice = new Domain.Invoice.Model.Invoice(
                    @event.PaymentMethod.Id,
                    @event.TripId,
                    @event.Duration,
                    @event.Distance,
                    TripStatus.Finished.Id);

                await _invoiceRepository.AddInvoiceAsync(invoice);

                // integration with external payment system.
                if (Equals(invoice.PaymentMethod, PaymentMethod.CreditCard) && invoice.Total > 0)
                {
                    await _paymentService.PerformPayment(invoice, @event.UserId);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error trying to perform tge payment the Trip: {@event.TripId}", ex);
            }
        }
        public async Task Handle(TripCancelledIntegrationEvent @event)
        {
            _logger.LogInformation($"Trip {@event.TripId} has been cancelled.");

            var invoice = await _invoiceRepository.GetInvoiceByTripAsync(@event.TripId);

            if (invoice != null)
            {
                return;
            }

            try
            {
                invoice = new Domain.Invoice.Model.Invoice(
                    @event.PaymentMethod.Id,
                    @event.TripId,
                    @event.Duration,
                    0,
                    TripStatus.Cancelled.Id);

                await _invoiceRepository.AddInvoiceAsync(invoice);

                _logger.LogInformation($"Invoice {invoice.InvoiceId} created.");

                // integration with external payment system.
                if (Equals(invoice.PaymentMethod, PaymentMethod.CreditCard) && invoice.Total > 0)
                {
                    await _paymentService.PerformPayment(invoice, @event.UserId);

                    _logger.LogInformation($"Payment for invoice {invoice.InvoiceId} has been processed.");
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error trying to perform the payment. Trip: {@event.TripId}", ex);
            }
            finally
            {
                _invoiceRepository.Dispose();
            }
        }
        public async Task <IActionResult> CreateTrip([FromBody] ViewModel.CreateInvoiceRequest request)
        {
            // to enable idempotency.
            var invoice = await _invoiceRepository.GetInvoiceByTripAsync(request.TripId);

            if (invoice != null)
            {
                return(Created(HttpContext.Request.GetUri().AbsoluteUri, invoice.InvoiceId));
            }

            invoice = new Domain.Invoice.Model.Invoice(
                request.PaymentMethod.Id,
                request.TripId,
                request.Duration,
                request.Distance,
                request.TripStatus.Id);

            await _invoiceRepository.AddInvoiceAsync(invoice);

            return(Created(HttpContext.Request.GetUri().AbsoluteUri, invoice.InvoiceId));
        }
示例#5
0
        public async Task Handle(TripFinishedIntegrationEvent @event)
        {
            var invoice = await _invoiceRepository.GetInvoiceByTripAsync(@event.TripId);

            if (invoice != null)
            {
                return;
            }

            invoice = new Domain.Invoice.Model.Invoice(
                @event.PaymentMethod.Id,
                @event.TripId,
                @event.Duration,
                @event.Distance,
                TripStatus.Finished.Id);

            await _invoiceRepository.AddInvoiceAsync(invoice);

            // integration with external payment system.
            if (Equals(invoice.PaymentMethod, PaymentMethod.CreditCard) && invoice.Total > 0)
            {
            }
        }