Ejemplo n.º 1
0
        public async Task<string> Authorise(Payment payment)
        {
            // failure rate of 10%
            if (_random.NextDouble() < 0.1)
                throw new PaymentFailureException(string.Format("{0}\r\n{1}\r\n{2}",
                    payment.PaymentMethod,
                    payment.Id,
                    payment.Amount
            ));

            return Guid.NewGuid().ToString("N"); // returns transaction id

        }
Ejemplo n.º 2
0
        public async Task<IEnumerable<Event>> ProcessAsync(Event evnt)
        {
            var orderAccepted = evnt.GetBody<OrderAccepted>();
            var order =  await _orderRepo.GetAsync(orderAccepted.OrderId);

            Trace.TraceInformation("PaymentActor received OrderAccepted " + order.Id);

            // if order is cancelled no further processing
            if (order.IsCancelled)
            {
                Trace.TraceInformation("Order was cancelled.");
                return new Event[0];                
            }

            var payment = new Payment()
            {
                Amount   = order.TotalPrice,
                OrderId = order.Id,
                PaymentMethod = order.PaymentMethod
            };

            try
            {
                var transactionId = await _paymentGateway.Authorise(payment);
                Trace.TraceInformation("Order transaction authorised: " + transactionId);

                payment.TransactionId = transactionId;
                await _paymentRepo.InsertAsync(payment);
                Trace.TraceInformation("Payment success");

                return new[]
                {
                    new Event(new PaymentAuthorised()
                    {
                        OrderId = order.Id,
                        PaymentId = payment.Id
                    })
                };
            }
            catch (AggregateException aggregateException)
            {
                var paymentFailureException = aggregateException.InnerExceptions.OfType<PaymentFailureException>()
                    .FirstOrDefault();
                if (paymentFailureException != null)
                {
                    Trace.TraceInformation("Payment failed");

                    Trace.TraceWarning(paymentFailureException.ToString());
                    return new[]
                    {
                        new Event(new PaymentFailed()
                        {
                            OrderId = order.Id
                        })                                    
                    };            
                }
                else
                {
                    throw;
                }

            }
            catch (PaymentFailureException paymentFailureException)
            {
                Trace.TraceInformation("Payment failed");
                Trace.TraceWarning(paymentFailureException.ToString());
                return new[]
                {
                    new Event(new PaymentFailed()
                    {
                        OrderId = order.Id
                    })                                    
                };

            }
            
        }
Ejemplo n.º 3
0
 public bool IsFradulent(Payment payment, Customer customer)
 {
     // 1% is fraudulant
     return _random.NextDouble() < 0.01;
 }