Knowledge BaseCustom Payment Module Example: DocData

Custom Payment Module Example: DocData

Creating the Cluster Steps

Because implementations of PSP can be different it is impossible to make everything configurable. Fortunately, because of the pipeline system it is very easy to add custom code to the payment flow. We can do this by creating pipeline steps. Pipeline steps can be added anywhere in the pipeline process. If we take a look at the requirements for DocData we will need the following steps:
  • Retrieving customer data from the system (DocDataGetCustomerStep);
  • Creating a new payment cluster (DocDataCreateClusterStep);
  • Getting the status from DocData (DocDataGetClusterStatusStep);
  • Extra: Debugging DocData Step (DocDataConfirmationDebug).
In this example we will show parts of how to create the 'DocDataCreateClusterStep'.
 
We start by creating a new class and creating a step like described in the 'Create a custom pipeline step' chapter in the 'How to' section. For DocData it is required that before we redirect the user
 
First, we need make the URL that we need to call in order to create a cluster configurable. We can do this without much effort by adding a new value to the payment module configuration like:
 
<add name="PostUrl" value="https://test.tripledeal.com/ps/com.tripledeal.paymentservice.servlets.PaymentService?command=new_payment_cluster" isRequestParameter="false" /> 
 
We then get this value in the code in the following way:
 
var postUrl = entity.Configuration.Where(c => c.Name.ToUpper() == "POSTURL");
 
After we get the post URL we will need to get a list of value we are going to send, we can get them the same way as other configuration values. Lastly we will make a request to the 'postUrl'; we can use the 'SanaWebRequest' class found in the Sana Commerce framework for this, like:
 
SanaWebRequest request = new SanaWebRequest();
NameValueCollection parameters = CreateRequestParameters(entity);
XmlDocument doc = Request.CreateXmlRequest(postUrl.First().Value, parameters, true);
          
Using this class we should make a request for XML output and parse the returned XML. The 'CreateRequestParameters' returns a list of parameters we will send to the request.
 
We get the returned cluster key with the following code:
 
XmlNode keyNode = requestDoc.SelectSingleNode("new_payment_cluster/key");
            if (keyNode != null)
            {
                ModifyRequestConfiguration("payment_cluster_key", keyNode.Attributes["value"].InnerText, entity);
                PaymentLog.Current.Add(entity.Order, string.Format("DocDataCreateClusterStep - Created a new cluster in DocData with the ID of {0}. The following parameters where used in the create cluster reqest {1}. The XML returned was {2}.", keyNode.Attributes["value"].InnerText, flatParameters, keyNode.InnerXml), false);
            }
            else
            {
                string message = string.Format("DocDataCreateClusterStep - There was an error in the create cluster call. Refer to the returned XML for the error details: {0}.", requestDoc.InnerXml);
                PaymentLog.Current.Add(entity.Order, message, true);
                throw new SanaBusinessException(message);
            }
 
If we have a valid key we will store the key with the order in the field collection like:
 
entity.Order["PaymentInformation"] = …
 
At the end of the pipeline the order will be saved automatically, so we do not have to save it during any of the payment steps. Of course all changes done to the order will be persisted for the steps executed after the step.
 
The same can be done with the other steps by implementing the logic needed in the same way. 
Knowledge BaseCustom Payment Module Example: DocData