Example #1
0
        private static string ProcessPayment(Order order)
        {
            PaymentGateway gateway      = new PaymentGateway();
            string         customerName = string.Format("{0} {1}", order.Customer.FirstName, order.Customer.LastName);
            string         response     = gateway.Authorise(order.getTotalValue(), customerName, order.PaymentCard.CardNumber, order.PaymentCard.CVV, order.PaymentCard.ExpiryDate);

            return(response);
        }
Example #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
                    })
                });
            }
        }
Example #3
0
        public ActionResult Edit([Bind(Include = "OrderId,CustomerId,DateCreated,DateDispatched,PaymentCardId")] Order order)
        {
            if (ModelState.IsValid)
            {
                // User the OrderId to repopulate order data from the database
                order.OrderItems  = db.OrderItems.Where(oi => oi.OrderId == order.OrderId).ToList <OrderItem>();
                order.PaymentCard = db.PaymentCards.Where(pc => pc.PaymentCardId == order.PaymentCardId).First <PaymentCard>();
                order.Customer    = db.Customers.Where(c => c.CustomerId == order.CustomerId).First <Customer>();

                // Work out the amount for the order

                decimal orderTotal = 0.00m;

                foreach (OrderItem item in order.OrderItems)
                {
                    orderTotal += item.getPrice();
                }

                // Authorise the payment for the order

                PaymentGateway gateway      = new PaymentGateway();
                string         customerName = string.Format("{0} {1}", order.Customer.FirstName, order.Customer.LastName);

                string response = gateway.Authorise(orderTotal, customerName, order.PaymentCard.CardNumber, order.PaymentCard.CVV, order.PaymentCard.ExpiryDate);

                // If the payment fails, forward the user to confirmation with an error message

                if (response.Equals("PAYMENT FAILURE"))
                {
                    return(RedirectToAction("Confirm", new { id = order.OrderId, outcome = "failure" }));
                }
                else
                {
                    // The order was successful so email the warehouse

                    MailSender mailSender = new MailSender();

                    string toEmailWarehouse      = "*****@*****.**";
                    string fromEmailWarehouse    = "*****@*****.**";
                    string emailSubjectWarehouse = "New Order";

                    StringBuilder sb = new StringBuilder();

                    sb.Append("A new order has been received:" + Environment.NewLine);

                    foreach (OrderItem item in order.OrderItems)
                    {
                        sb.Append(string.Format("Item name: {0} - Price: {1} - Quantity: {2} - Total Cost: {3}{4}",
                                                item.Product.Name,
                                                item.Product.Price,
                                                item.Quantity,
                                                item.getPrice(),
                                                Environment.NewLine));
                    }

                    string emailBodyWarehouse = sb.ToString();

                    if (!mailSender.SendMail(toEmailWarehouse, fromEmailWarehouse, emailSubjectWarehouse, emailBodyWarehouse))
                    {
                        return(RedirectToAction("Confirm", new { id = order.OrderId, outcome = "failure" }));
                    }

                    // Then email the customer (note - going to have to add a customer class...)

                    string toEmailCustomer      = order.Customer.Email;
                    string fromEmailCustomer    = "*****@*****.**";
                    string emailSubjectCustomer = "Thanks for placing an order with us";

                    sb.Clear();

                    sb.Append("Thankyou for placing an order with DaveCo. Your order details are:" + Environment.NewLine);

                    foreach (OrderItem item in order.OrderItems)
                    {
                        sb.Append(string.Format("Item name: {0} - Price: {1} - Quantity: {2} - Total Cost: {3}{4}",
                                                item.Product.Name,
                                                item.Product.Price,
                                                item.Quantity,
                                                item.getPrice(),
                                                Environment.NewLine));
                    }

                    string emailBodyCustomer = sb.ToString();

                    if (!mailSender.SendMail(toEmailCustomer, fromEmailCustomer, emailSubjectCustomer, emailBodyCustomer))
                    {
                        return(RedirectToAction("Confirm", new { id = order.OrderId, outcome = "failure" }));
                    }

                    // Then last but not least, update the order details in the database

                    db.Entry(order).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("Confirm", new { id = order.OrderId, outcome = "success" }));
                }
            }
            ViewBag.PaymentCardId = new SelectList(db.PaymentCards, "PaymentCardId", "CardNumber", order.PaymentCardId);
            return(View(order));
        }