Try our conversational search powered by Generative AI!

Richly Chheuy
Aug 25, 2015
  3249
(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 and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024