In this article I will explain with an example, how to implement Moneris Checkout in ASP.Net using C# and VB.Net.
Moneris Checkout gives e-commerce merchants a simple and secure way to process payments by integrating a Moneris-hosted payment module into the merchant checkout page.
 
 

System and Skills Requirements

In order to integrate with Moneris Checkout as a merchant, you must have knowledge of the following.
1. JavaScript
2. JSON
3. Server-side programming
 
 

Moneris Checkout Transaction Process Flow

For understanding the Process flow of Moneris Checkout, please refer below diagram.
Implement Moneris Checkout in ASP.Net
 
 

Steps for Integrating Moneris Checkout

Following are the steps for integrating the Moneris Checkout.
1. Configuring Moneris Checkout in Merchant Resource Center
2. Preparing the Client-Side Checkout Page
3. Implementing Server-to-Server Logic
 
 

1. Configuring Moneris Checkout in Merchant Resource Center

The first step is to configure the Moneris Checkout in the Moneris Merchant Resource Center (MRC).
For testing purpose, you need to create a test configuration in the testing MRC.
Note: Once the solution is ready to be deployed for production, you need to create a new configuration for the production environment in the Merchant Resource Center (MRC).
 
After the configuration is completed, the checkout ID i.e. the key is generated and used within the Preload Request in order to identify the specific Moneris Checkout configuration.
In order to get the checkout ID, please follow the below steps:
1. Log into the Merchant Resource Center using the following URLs (according to stage of development) with Username, Store ID and Password.

Testing – https://esqa.moneris.com/mpg

Production – https://www3.moneris.com/mpg

2. In the Admin menu, select Moneris Checkout Config.
3. Then, click on the Create Profile button.
4. Follow the on-screen steps to complete the configuration.
For more information, please refer the URL developer.moneris.com.
 
 

2. Preparing Client-Side Checkout Page

In order to prepare client-side checkout page for interacting with Moneris Checkout, you need to follow the below steps.
1. First, inherit the Moneris Checkout JavaScript library in the page.
Testing:
<script src="https://gatewayt.moneris.com/chkt/js/chkt_v1.00.js"></script>
 
Production:
<script src="https://gatewayt.moneris.com/chkt/js/chkt_v1.00.js"></script>
 
2. Add the following HTML DIV in the page.
<div id="monerisCheckout"></div>
 
 

3. Implementing Server-to-Server Logic

The Preload request is the means by which a Moneris Checkout instance is securely generated at transaction time. It involves a server-to-server post using the JSON format Preload Request.
The response to the Preload request returns a ticket number which uniquely identifies the instance and must be passed in the JavaScript request in order to display the Moneris Checkout page in the browser.
Note: The ticket number expiration time is set to 30 minutes.
 
For server implementation, following Moneris Checkout URLs will be used depending on the development stage.
 
 

Integrating Moneris Checkout

HTML Markup

The HTML markup consists of following element and controls.
div – For displaying checkout page.
HiddenField – For storing the ticket (checkout ID).
<div id="monerisCheckout"></div>
<asp:HiddenField ID="hfTokenrunat="server" /> 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Text;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
 
VB.Net
Imports System.Text
Imports System.Net
Imports System.Net.Http
Imports Newtonsoft.Json
 

Property Classes

The classes consist of following properties.
C#
public class MonerisReceiptRequest
{
    public string store_id { getset; }
    public string api_token { getset; }
    public string checkout_id { getset; }
    public string environment { getset; }
    public string action { getset; }
    public string ticket { getset; }
}
 
public class MonerisPreloadRequest
{
    public string store_id { getset; }
    public string api_token { getset; }
    public string checkout_id { getset; }
    public string txn_total { getset; }
    public string environment { getset; }
    public string action { getset; }
    public string order_no { getset; }
    public CartItems cart { getset; }
 
    public class CartItems
    {
        public List<CartItem> items { getset; }
    }
 
    public class CartItem
    {
        public string url { getset; }
        public string description { getset; }
        public string product_code { getset; }
        public decimal unit_cost { getset; }
        public int quantity { getset; }
    }
}
 
public class PreloadResponseItem
{
    public string success { getset; }
    public string ticket { getset; }
    public object error { getset; }
}
 
public class MonerisPreloadResponse
{
    public PreloadResponseItem response { getset; }
}
 
public class _3dSecure
{
    public string decision_origin { getset; }
    public string result { getset; }
    public string condition { getset; }
    public string status { getset; }
    public string code { getset; }
    public string details { getset; }
}
 
public class Avs
{
    public string decision_origin { getset; }
    public string result { getset; }
    public string condition { getset; }
    public string status { getset; }
    public string code { getset; }
    public string details { getset; }
}
 
public class Cc
{
    public string first6last4 { getset; }
    public string expiry { getset; }
    public string cardholder { getset; }
    public string order_no { getset; }
    public object cust_id { getset; }
    public string transaction_no  { getset; }
    public string reference_no { getset; }
    public string transaction_code { getset; }
    public string transaction_type { getset; }
    public string transaction_date_time { getset; }
    public string corporate_card { getset; }
    public string amount { getset; }
    public string response_code { getset; }
    public string iso_response_code { getset; }
    public string approval_code { getset; }
    public string card_type { getset; }
    public object dynamic_descriptor { getset; }
    public object invoice_number { getset; }
    public object customer_code { getset; }
    public string eci { getset; }
    public string cvd_result_code { getset; }
    public object avs_result_code { getset; }
    public object cavv_result_code { getset; }
    public string expiry_date { getset; }
    public object recur_success { getset; }
    public string issuer_id { getset; }
    public string is_debit { getset; }
    public string ecr_no { getset; }
    public string batch_no { getset; }
    public string sequence_no { getset; }
    public string result { getset; }
    public Tokenize tokenize { getset; }
    public Fraud fraud { getset; }
}
 
public class CustInfo
{
    public object first_name { getset; }
    public object last_name { getset; }
    public object phone { getset; }
    public object email { getset; }
}
 
public class Cvd
{
    public string decision_origin { getset; }
    public string result { getset; }
    public string condition { getset; }
    public string status { getset; }
    public string code { getset; }
    public string details { getset; }
}
 
public class Details
{
    public string responseCode { getset; }
    public string message { getset; }
    public string receiptID { getset; }
    public string result { getset; }
    public string score { getset; }
    public string transactionID { getset; }
}
 
public class Fraud
{
    public Cvd cvd { getset; }
    public Avs avs { getset; }
 
    [JsonProperty("3d_secure")]
    public _3dSecure _3d_secure { getset; }
    public Kount kount { getset; }
}
 
public class Kount
{
    public string decision_origin { getset; }
    public string result { getset; }
    public string condition { getset; }
    public string status { getset; }
    public string code { getset; }
    public Details details { getset; }
}
 
public class Receipt
{
    public string result { getset; }
    public Cc cc { getset; }
}
 
public class PaymentRequest
{
    public string txn_total { getset; }
    public CustInfo cust_info { getset; }
    public Shipping shipping { getset; }
    public object billing { getset; }
    public string cc_total { getset; }
    public string pay_by_token { getset; }
    public Cc cc { getset; }
    public string ticket { getset; }
    public object cust_id { getset; }
    public object dynamic_descriptor  { getset; }
    public string order_no { getset; }
    public string issuer_id { getset; }
    public string eci { getset; }
}
 
public class Response
{
    public string success { getset; }
    public PaymentRequest request { getset; }
    public Receipt receipt { getset; }
}
 
public class MonerisReceiptResponse
{
    public Response response { getset; }
}
 
public class Shipping
{
    public object address_1 { getset; }
    public object address_2 { getset; }
    public object city { getset; }
    public object country { getset; }
    public object province { getset; }
    public object postal_code { getset; }
}
 
public class Tokenize
{
    public string success { getset; }
    public string first4last4 { getset; }
    public string datakey { getset; }
    public string status { getset; }
    public string message { getset; }
}
 
VB.Net
Public Class MonerisReceiptRequest
    Public Property store_id As String
    Public Property api_token As String
    Public Property checkout_id As String
    Public Property environment As String
    Public Property action As String
    Public Property ticket As String
End Class
 
Public Class MonerisPreloadRequest
    Public Property store_id As String
    Public Property api_token As String
    Public Property checkout_id As String
    Public Property txn_total As String
    Public Property environment As String
    Public Property action As String
    Public Property order_no As String
    Public Property cart As CartItems
 
    Public Class CartItems
        Public Property items As List(Of CartItem)
    End Class
 
    Public Class CartItem
        Public Property url As String
        Public Property description As String
        Public Property product_code As String
        Public Property unit_cost As Decimal
        Public Property quantity As Integer
    End Class
End Class
 
Public Class PreloadResponseItem
    Public Property success As String
    Public Property ticket As String
    Public Property [error] As Object
End Class
 
Public Class MonerisPreloadResponse
    Public Property response As PreloadResponseItem
End Class
 
Public Class _3dSecure
    Public Property decision_orig in As String
    Public Property result As String
    Public Property condition As String
    Public Property status As String
    Public Property code As String
    Public Property details As String
End Class
 
Public Class Avs
    Public Property decision_orig in As String
    Public Property result As String
    Public Property condition As String
    Public Property status As String
    Public Property code As String
    Public Property details As String
End Class
 
Public Class Cc
    Public Property first6last4 As String
    Public Property expiry As String
    Public Property cardholder As String
    Public Property order_no As String
    Public Property cust_id As Object
    Public Property transaction_no As String
    Public Property reference_no As String
    Public Property transaction_code As String
    Public Property transaction_type As String
    Public Property transaction_date_time As String
    Public Property corporate_card As String
    Public Property amount As String
    Public Property response_code As String
    Public Property iso_response_code As String
    Public Property approval_code As String
    Public Property card_type As String
    Public Property dynamic_descriptor As Object
    Public Property invoice_number As Object
    Public Property customer_code As Object
    Public Property eci As String
    Public Property cvd_result_code As String
    Public Property avs_result_code As Object
    Public Property cavv_result_code As Object
    Public Property expiry_date As String
    Public Property recur_success As Object
    Public Property issuer_id As String
    Public Property is_debit As String
    Public Property ecr_no As String
    Public Property batch_no As String
    Public Property sequence_no As String
    Public Property result As String
    Public Property tokenize As Tokenize
    Public Property fraud As Fraud
End Class
 
Public Class CustInfo
    Public Property first_name As Object
    Public Property last_name As Object
    Public Property phone As Object
    Public Property email As Object
End Class
 
Public Class Cvd
    Public Property decision_orig in As String
    Public Property result As String
    Public Property condition As String
    Public Property status As String
    Public Property code As String
    Public Property details As String
End Class
 
Public Class Details
    Public Property responseCode As String
    Public Property message As String
    Public Property receiptID As String
    Public Property result As String
    Public Property score As String
    Public Property transactionID As String
End Class
 
Public Class Fraud
    Public Property cvd As Cvd
    Public Property avs As Avs
    <JsonProperty("3d_secure")>
    Public Property _3d_secure As _3dSecure
    Public Property kount As Kount
End Class
 
Public Class Kount
    Public Property decision_orig in As String
    Public Property result As String
    Public Property condition As String
    Public Property status As String
    Public Property code As String
    Public Property details As Details
End Class
 
Public Class Receipt
    Public Property result As String
    Public Property cc As Cc
End Class
 
Public Class PaymentRequest
    Public Property txn_total As String
    Public Property cust_info As CustInfo
    Public Property shipping As Shipping
    Public Property billing As Object
    Public Property cc_total As String
    Public Property pay_by_token As String
    Public Property cc As Cc
    Public Property ticket As String
    Public Property cust_id As Object
    Public Property dynamic_descriptor As Object
    Public Property order_no As String
    Public Property issuer_id As String
    Public Property eci As String
End Class
 
Public Class Response
    Public Property success As String
    Public Property request As PaymentRequest
    Public Property receipt As Receipt
End Class
 
Public Class MonerisReceiptResponse
    Public Property response As Response
End Class
 
Public Class Shipping
    Public Property address_1 As Object
    Public Property address_2 As Object
    Public Property city As Object
    Public Property country As Object
    Public Property province As Object
    Public Property postal_code As Object
End Class
 
Public Class Tokenize
    Public Property success As String
    Public Property first4last4 As String
    Public Property datakey As String
    Public Property status As String
    Public Property message As String
End Class
 
 

Sending Request to Checkout URL for setting the token (client ID)

Inside the Page_Load event handler, the MonerisPreloadRequest class object is created and its properties are set based on the Moneris Merchant Resource Center (MRC) configuration.
Here, the action is specified as preload.
Then, HttpClient class object is created and the HttpRequestMessage class object is created which creates and sends an HTTP POST request to the Moneris checkout URL with JSON data by serializing the MonerisPreloadRequest object using SendAsync method of HttpClient class object.
Note: For more details on HttpClient, please refer below article Using TLS1.2 with HttpClient in C# and VB.Net.
 
Next, a check is performed if the response IsSuccessStatusCode is success, the response is de-serialisszed to MonerisPreloadResponse class object.
Finally, a check is performed if the success property is true then, the received ticket is set into the hidden field.
 

Receiving Payment Receipt

When the Checkout button is clicked, the MonerisReceiptRequest class object is created and its properties are set based on the Moneris Merchant Resource Center (MRC) configuration.
Here, the action is specified as receipt.
Then, HttpClient class object is created and the HttpResponseMessage class object is created which creates and sends an HTTP POST request to the Moneris checkout URL with JSON data by serializing the MonerisReceiptRequest object using PostAsync method of HttpClient class object.
Note: For more details on HttpClient, please refer below article Using TLS1.2 with HttpClient in C# and VB.Net.
 
Next, a check is performed if the response IsSuccessStatusCode is success, the response is de-serialized to MonerisReceiptResponse class object.
After that, a check is performed if the result is “a” then, the receipt was approved, and the Receipt object is assigned to the receipt variable.
And if the result is “d” which indicates that the receipt was declined, and again the Receipt object is assigned to the receipt variable.
C#
protected void Page_Load(object  sender,EventArgs e)
{
    string storeId "<StoreId>";
    string apiToken "<APIToken>";
    string checkoutId "<CheckoutId>";
    string environment = "qa";
    string url "https://gatewayt.moneris.com/chkt/request/request.php";
 
    if (!this.IsPostBack)
    {
        MonerisPreloadRequest preloadRequest = new MonerisPreloadRequest
        {
            store_id storeId,
            api_token apiToken,
            checkout_id checkoutId,
            environment = environment,
            txn_total "452.00",
            action = "preload"
        };
 
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        using (HttpClient client = new HttpClient())
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
            request.Content = new StringContent(JsonConvert.SerializeObject(preloadRequest), Encoding.UTF8, "application/json");
            HttpResponseMessage response = client.SendAsync(request).Result;
            if (response.IsSuccessStatusCode)
            {
                MonerisPreloadResponse preloadResponse JsonConvert.DeserializeObject<MonerisPreloadResponse>(response.Content.ReadAsStringAsync().Result);
                if (preloadResponse.response.success == "true")
                {
                    hfToken.Value preloadResponse.response.ticket;
                }
            }
        }
    }
    else
    {
        MonerisReceiptRequest receiptRequest = new MonerisReceiptRequest()
        {
            store_id storeId,
            api_token apiToken,
            checkout_id checkoutId,
            environment = environment,
            action = "receipt",
            ticket = hfToken.Value
        };
 
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        using (HttpClien  client = new HttpClient())
        {
            StringContent stringContent = new StringContent(JsonConvert.SerializeObject(receiptRequest));
            HttpResponseMessage response = client.PostAsync(url, stringContent).Result;
            if (response.IsSuccessStatusCode)
            {
                MonerisReceiptResponse monerisReceiptResponse JsonConvert.DeserializeObject<MonerisReceiptResponse>(response.Content.ReadAsStringAsync().Result);
                if (monerisReceiptResponse.response.receipt.result == "a")
                {
                    // Payment Approved.
                    Receipt receipt  monerisReceiptResponse.response.receipt;
                }
                if (monerisReceiptResponse.response.receipt.result == "d")
                {
                    // Payment Declined.
                    Receipt receipt  monerisReceiptResponse.response.receipt;
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim storeId As String "<StoreId>"
    Dim apiToken As String "<APIToken>"
    Dim checkoutId As String "<CheckoutId>"
    Dim environment As String "qa"
    Dim url As String "https://gatewayt.moneris.com/chkt/request/request.php"
 
    If Not Me.IsPostBack Then
        Dim preloadRequest As MonerisPreloadRequest = New MonerisPreloadRequest With {
            .store_id storeId,
            .api_token apiToken,
            .checkout_id checkoutId,
            .environment = environment,
            .txn_total "452.00",
            .action = "preload"
        }
 
        ServicePointManager.Expect100Continue = True
        ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
        Using client As HttpClient = New HttpClient()
            Dim request As HttpRequestMessage = New HttpRequestMessage(HttpMethod.Post, url)
            request.Content = New StringContent(JsonConvert.SerializeObject(preloadRequest), Encoding.UTF8, "application/json")
            Dim response As HttpResponseMessage client.SendAsync(request).Result
            If response.IsSuccessStatusCode Then
                Dim preloadResponse As MonerisPreloadResponse JsonConvert.DeserializeObject(Of MonerisPreloadResponse)(response.Content.ReadAsStringAsync().Result)
                If preloadResponse.response.success "true" Then
                    hfToken.Value preloadResponse.response.ticket
                End If
            End If
        End Using
    Else
        Dim receiptRequest As MonerisReceiptRequest = New MonerisReceiptRequest() With {
            .store_id storeId,
            .api_token apiToken,
            .checkout_id checkoutId,
            .environment = environment,
            .action = "receipt",
            .ticket = hfToken.Value
        }
 
        ServicePointManager.Expect100Continue = True
        ServicePointManager.SecurityProtocol = CType(3072,SecurityProtocolType)
        Using client As HttpClient = New HttpClient()
            Dim stringContent As StringContent = New StringContent(JsonConvert.SerializeObject(receiptRequest))
            Dim response As HttpResponseMessage client.PostAsync(url,stringContent).Result
            If response.IsSuccessStatusCode Then
                Dim monerisReceiptResponse As MonerisReceiptResponse JsonConvert.DeserializeObject(Of MonerisReceiptResponse)(response.Content.ReadAsStringAsync().Result)
                If monerisReceiptResponse.response.receipt.result "a" Then
                    ' Payment Approved.
                    Dim receipt As Receipt monerisReceiptResponse.response.receipt
                End If
                If monerisReceiptResponse.response.receipt.result "d" Then
                    ' Payment Declined.
                    Dim receipt As Receipt monerisReceiptResponse.response.receipt
                End If
            End If
        End Using
    End If
End Sub
 

JavaScript for set Checkout in HTML DIV and display Moneris Checkout page in the browser

Inside the window onload event handler, monerisCheckout is initialized and following properties are set.
setMode – It specifies the environment. Here it is specified as qa i.e. testing environment.
setCheckoutDiv – It specifies the element to which the checkout form will be displayed.s
startCheckout – It specifies the token i.e. checkout ID. In this case the value of Hidden Field is specified.
 

Submitting the Checkout Form

When the Checkout button is clicked, the monerisCheckout setCallback function is triggered and the form is submitted.
 
 

Screenshots

Checkout Form

Implement Moneris Checkout in ASP.Net
 

Transaction Status

Implement Moneris Checkout in ASP.Net
 

Payment Receipt

Implement Moneris Checkout in ASP.Net
 
 

Downloads