Ejemplo n.º 1
0
        public async Task ProcessPayment(PaymentDto paymentDto)
        {
            try
            {
                var payment = new Payment
                {
                    Amount           = paymentDto.Amount,
                    CardHolder       = paymentDto.CardHolder,
                    CreditCardNumber = paymentDto.CreditCardNumber,
                    ExpirationDate   = paymentDto.ExpirationDate,
                    PaymentState     = new PaymentState
                    {
                        PaymentStatus = PaymentStatus.Pending,
                    },
                    SecurityCode = paymentDto.SecurityCode,
                };
                await _paymentRepository.InsertAsync(payment);

                await _unitOfWork.SaveChangesAsync();

                IPaymentGateway paymentGateway = PaymentServiceHelper.GetPaymentGateway(payment.Amount);
                var             isProcessed    = await paymentGateway.ProcessPaymentAsync(paymentDto);

                payment.PaymentState.PaymentStatus = isProcessed ? PaymentStatus.Processed : PaymentStatus.Failed;
                await _unitOfWork.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task HandleAsync(PostedOrder @event)
        {
            try
            {
                _logger.LogError($"We received the following order : {@event.OrderId}");
                Thread.Sleep(10000);
                var result = await _gateway.ProcessPaymentAsync(true);

                var payment = new Payment(@event);
                payment = _paymentRepository.Create(payment);
                if (result)
                {
                    await _busClient.PublishAsync(new PaymentAccepted()
                    {
                        Amount    = (double)payment.Amount,
                        OrderId   = payment.OrderId,
                        PaymentId = payment.Id,
                        Type      = PaymentType.CreditCard
                    });
                }
                else
                {
                    await _busClient.PublishAsync(new PaymentRejected()
                    {
                        OrderId = payment.OrderId,
                        Code    = "401",
                        Reason  = "The payment cannot be processed right now",
                        Amount  = (double)@event.Total,
                    });
                }
            }
            catch (ArgumentNullException exception)
            {
                _logger.LogError($"There was an error publishing : {exception.Message}");
                await _busClient.PublishAsync(new PaymentRejected()
                {
                    OrderId = @event.OrderId,
                    Code    = "400",
                    Amount  = (double)@event.Total,
                    Reason  = exception.Message,
                });
            }
        }
Ejemplo n.º 3
0
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='payment'>
 /// </param>
 public static Payment ProcessPayment(this IPaymentGateway operations, ProcessPaymentRequest payment = default(ProcessPaymentRequest))
 {
     return(operations.ProcessPaymentAsync(payment).GetAwaiter().GetResult());
 }