예제 #1
0
        public ActionResult PlaceOrder(string custId, [FromBody] IEnumerable <BasketItem> basketItems)
        {
            List <OrderLine> orderLines = new List <OrderLine>();

            foreach (var item in basketItems)
            {
                OrderLine orderLine = new OrderLine();

                orderLine.ProdId     = item.ProdId;
                orderLine.Item       = item.ProductBrand;
                orderLine.QtyOrdered = item.Qty;
                orderLine.Price      = item.Qty * item.Price;

                orderLines.Add(orderLine);
            }

            Order order = _orderRepository.ConfirmOrder(custId, orderLines);

            if (order.InvoiceId != "")
            {
                using (var message = new MailMessage())
                {
                    Customer customer = _customerRepository.GetCustomer(custId);

                    message.To.Add(new MailAddress(customer.EmailAddress, customer.Name));
                    message.From       = new MailAddress("*****@*****.**", "Food Webshop");
                    message.Subject    = "Thanks for your order!";
                    message.Body       = "<img src='https://morethankyounotes.com/wp-content/uploads/2017/02/Customer-Thank-You-Note-1.png'>";
                    message.IsBodyHtml = true;

                    string     invoiceTemplate = ".\\Documents\\Invoices\\Template\\InvoiceTemplate.pdf";
                    PdfCreator pdf             = new PdfCreator(invoiceTemplate, customer, order, orderLines);
                    string     invoiceDoc      = pdf.CreateInvoiceForOrder();
                    Attachment attachment      = new Attachment(invoiceDoc);
                    message.Attachments.Add(attachment);

                    using (var client = new SmtpClient("smtp.gmail.com"))
                    {
                        client.Port        = 587;
                        client.Credentials = new NetworkCredential("*****@*****.**", "700MB-80min");
                        client.EnableSsl   = true;
                        client.Send(message);
                    }
                }
            }


            return(Ok());
        }
예제 #2
0
        public ActionResult GetInvoiceOrder(int ordId)
        {
            var order    = _orderRepository.GetOrder(ordId);
            var customer = _customerRepository.GetCustomer(order.CustId);
            List <OrderLine> orderLines = new List <OrderLine>(_orderRepository.GetOrderLinesOrder(ordId));

            string     invoiceTemplate = ".\\Documents\\Invoices\\Template\\InvoiceTemplate.pdf";
            PdfCreator pdf             = new PdfCreator(invoiceTemplate, customer, order, orderLines);
            string     invoiceFile     = pdf.CreateInvoiceForOrder();

            FileStream stream = new FileStream(invoiceFile, FileMode.Open);

            byte[] fileBytes = new byte[stream.Length];

            stream.Read(fileBytes, 0, fileBytes.Length);
            stream.Close();

            return(File(
                       fileContents: fileBytes,
                       contentType: "application/pdf"));
        }