SaaS CMS has officially launched! Learn more now.

Richly Chheuy
Aug 25, 2015
  3298
(4 votes)

PayPalPayment Serialization for Sending Email Through Commerce Template Service

This may be helpful for developers who are looking to serialize the PayPalPayment class - which stores information such as the PayPal token and order number - using Mediachase.Commerce.Engine.Template.Providers.XslTemplateProvider. We received such a question in Developer Support, so I wanted to share one solution provided by our Commerce product team.

General steps

  • Create your own TemplateProvider that inherits from XslTemplateProvider
  • Add a settable property that takes a Type array
  • Override the Process method (see current source code below) and make sure that the line instantiating the XmlSerializer
  •                
     XmlSerializer serializer = new XmlSerializer(contextObject.GetType());


  • Instead uses the constructor that takes a type array as the second parameter (this will be the extended types – PayPalPayment in this case)
  • Before calling into the Process method (from the TemplateService.Process call), make sure that the new type array property has been set (the template provider can be accessed thru “new TemplateSevice().Provider…” since the backing variable is static)
  • Update configuration file to use your custom TemplateProcessor

Code Example

        

public override string Process(string template, CultureInfo culture, System.Collections.IDictionary context)

        {

            // 1. Serialize all context variables into XML           

            MemoryStream stream = new MemoryStream();

 

            // Start creating xml document

            XmlWriterSettings xmlFileSettings = new XmlWriterSettings();

            xmlFileSettings.Indent = true;

            XmlWriter exportWriter = XmlWriter.Create(stream, xmlFileSettings);

 

            // Start the Xml Document

            exportWriter.WriteStartDocument();

 

            exportWriter.WriteStartElement("ContextDoc", "");

 

           // Cycle through dictionary

 

            foreach (string key in context.Keys)

            {

                object contextObject = context[key];

                XmlSerializer serializer = new XmlSerializer(contextObject.GetType());

                serializer.Serialize(exportWriter, contextObject);

            }

 

            exportWriter.WriteEndElement(); // End of ContextDoc

            exportWriter.WriteEndDocument();

            exportWriter.Close(); // Close the XmlWriter Stream

 

            stream.Position = 0;

 

            // 2. Locate XSL Template

            XslCompiledTransform xslt = new XslCompiledTransform();

 

            string path = String.Format(TemplateSource, culture.Name, template);

            string specificPath = String.Empty;

            // Check default path if language specific one doesn't exist

            if (!File.Exists(path))

            {

                specificPath = path;

                path = String.Format(TemplateSource, "Default", template);

            }

 

            // Generate exception if path doesn't exist

            if (!File.Exists(path))

            {

                throw new ProviderException(String.Format("The template was not found at the default path \"{0}\" nor at the specific path \"{1}\". Please either modify settings in web.config for XSL Provider or create an xsl template in the path specified.", path, specificPath));

            }

 

            // Load otherwise

            xslt.Load(path);

 

            // 3. Transform Content

            stream.Position = 0;

            XPathDocument pathDoc = new XPathDocument(stream);

            MemoryStream outputStream = new MemoryStream();

            StringWriter writer = new StringWriter();

            xslt.Transform(pathDoc, null, writer);

 

            // Return contents

            return writer.ToString();

        }

Aug 25, 2015

Comments

Richly Chheuy
Richly Chheuy Aug 25, 2015 11:06 PM

Noting based on feedback from a partner developer that you may run into a potential null issue. Workaround is to call TemplateService.Process first before setting the new TemplateService().Provider.

Please login to comment.
Latest blogs
Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024