Try our conversational search powered by Generative AI!

Richly Chheuy
Aug 25, 2015
  3265
(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
From Procrastination to Proficiency: Navigating Your Journey to Web Experimentation Certification

Hey there, Optimizely enthusiasts!   Join me in celebrating a milestone – I'm officially a certified web experimentation expert! It's an exhilarati...

Silvio Pacitto | May 17, 2024

GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024

The Experimentation Process

This blog is part of the series -   Unlocking the Power of Experimentation: A Marketer's Insight. Welcome back, to another insightful journey into...

Holly Quilter | May 16, 2024

Azure AI Language – Sentiment Analysis in Optimizely CMS

In the following article, I showcase how sentiment analysis, which is part of the Azure AI Language service, can be used to detect the sentiment of...

Anil Patel | May 15, 2024 | Syndicated blog