Skip to content

Attach pdf invoice to completed orders’ emails in nopcommerce

October 5, 2012

As you know, when an order is completed, the user of any nopcommerce shop receives an email about this but that email does not contain any pdf invoice the user would like to see.

My approach to this situation was not to attach directly the pdf to the email because this require some headache (emails being saved in DB means you need to save pdf in DB which is ugly) but:

1. Include a link in the orderConfirmationEmail like this:

Please download your invoice from <a href=”http://www.myshop.com/orderdetails/pdfasguest/%Order.OrderId%”>here</a&gt;

(you do this in admin – messages template)

2. Add a new route for this link in RouterProvider.cs in //orders section

routes.MapLocalizedRoute("GetOrderPdfInvoiceAsGuest",
                           "orderdetails/pdfasguest/{orderId}",
                           new { controller = "Order", action = "GetPdfInvoiceAsGuest" },
                           new[] { "Nop.Web.Controllers" });

3. Add a method for this route in OrderController.cs

public ActionResult GetPdfInvoiceAsGuest(string orderId)
        {
            string orderIdDecoded = DecodeFrom64(orderId.ToString());

            var order = _orderService.GetOrderById(Int32.Parse(orderIdDecoded));
            if (order == null || order.Deleted)
                return new HttpUnauthorizedResult();

            string fileName = string.Format("order_{0}_{1}.pdf", order.OrderGuid, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
            string filePath = string.Format("{0}content\\files\\ExportImport\\{1}", this.Request.PhysicalApplicationPath, fileName);
            _pdfService.PrintOrderToPdf(order, _workContext.WorkingLanguage, filePath);
            var pdfBytes = System.IO.File.ReadAllBytes(filePath);
            return File(pdfBytes, "application/pdf", fileName);
        }

– as you notice, the order id in the link is decoded from base64 because I put orderId in the link to be base64 encoded to not be plain text easy-readable by a user (of couse, you can increase the security here if you want)

4. Last step, build Order.Id parameter for the link where rest of the parameters are built: MessageTokenProviders.cs, AddOrderTokes method:

 tokens.Add(new Token("Order.OrderId", EncodeTo64(order.Id.ToString())));

That’s all….

Hope to be helpfull for someone.

Advertisements

From → .NET, Diverse

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: