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.
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.
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="hfToken" runat="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 { get; set; }
public string api_token { get; set; }
public string checkout_id { get; set; }
public string environment { get; set; }
public string action { get; set; }
public string ticket { get; set; }
}
public class MonerisPreloadRequest
{
public string store_id { get; set; }
public string api_token { get; set; }
public string checkout_id { get; set; }
public string txn_total { get; set; }
public string environment { get; set; }
public string action { get; set; }
public string order_no { get; set; }
public CartItems cart { get; set; }
public class CartItems
{
public List<CartItem> items { get; set; }
}
public class CartItem
{
public string url { get; set; }
public string description { get; set; }
public string product_code { get; set; }
public decimal unit_cost { get; set; }
public int quantity { get; set; }
}
}
public class PreloadResponseItem
{
public string success { get; set; }
public string ticket { get; set; }
public object error { get; set; }
}
public class MonerisPreloadResponse
{
public PreloadResponseItem response { get; set; }
}
public class _3dSecure
{
public string decision_origin { get; set; }
public string result { get; set; }
public string condition { get; set; }
public string status { get; set; }
public string code { get; set; }
public string details { get; set; }
}
public class Avs
{
public string decision_origin { get; set; }
public string result { get; set; }
public string condition { get; set; }
public string status { get; set; }
public string code { get; set; }
public string details { get; set; }
}
public class Cc
{
public string first6last4 { get; set; }
public string expiry { get; set; }
public string cardholder { get; set; }
public string order_no { get; set; }
public object cust_id { get; set; }
public string transaction_no { get; set; }
public string reference_no { get; set; }
public string transaction_code { get; set; }
public string transaction_type { get; set; }
public string transaction_date_time { get; set; }
public string corporate_card { get; set; }
public string amount { get; set; }
public string response_code { get; set; }
public string iso_response_code { get; set; }
public string approval_code { get; set; }
public string card_type { get; set; }
public object dynamic_descriptor { get; set; }
public object invoice_number { get; set; }
public object customer_code { get; set; }
public string eci { get; set; }
public string cvd_result_code { get; set; }
public object avs_result_code { get; set; }
public object cavv_result_code { get; set; }
public string expiry_date { get; set; }
public object recur_success { get; set; }
public string issuer_id { get; set; }
public string is_debit { get; set; }
public string ecr_no { get; set; }
public string batch_no { get; set; }
public string sequence_no { get; set; }
public string result { get; set; }
public Tokenize tokenize { get; set; }
public Fraud fraud { get; set; }
}
public class CustInfo
{
public object first_name { get; set; }
public object last_name { get; set; }
public object phone { get; set; }
public object email { get; set; }
}
public class Cvd
{
public string decision_origin { get; set; }
public string result { get; set; }
public string condition { get; set; }
public string status { get; set; }
public string code { get; set; }
public string details { get; set; }
}
public class Details
{
public string responseCode { get; set; }
public string message { get; set; }
public string receiptID { get; set; }
public string result { get; set; }
public string score { get; set; }
public string transactionID { get; set; }
}
public class Fraud
{
public Cvd cvd { get; set; }
public Avs avs { get; set; }
[JsonProperty("3d_secure")]
public _3dSecure _3d_secure { get; set; }
public Kount kount { get; set; }
}
public class Kount
{
public string decision_origin { get; set; }
public string result { get; set; }
public string condition { get; set; }
public string status { get; set; }
public string code { get; set; }
public Details details { get; set; }
}
public class Receipt
{
public string result { get; set; }
public Cc cc { get; set; }
}
public class PaymentRequest
{
public string txn_total { get; set; }
public CustInfo cust_info { get; set; }
public Shipping shipping { get; set; }
public object billing { get; set; }
public string cc_total { get; set; }
public string pay_by_token { get; set; }
public Cc cc { get; set; }
public string ticket { get; set; }
public object cust_id { get; set; }
public object dynamic_descriptor { get; set; }
public string order_no { get; set; }
public string issuer_id { get; set; }
public string eci { get; set; }
}
public class Response
{
public string success { get; set; }
public PaymentRequest request { get; set; }
public Receipt receipt { get; set; }
}
public class MonerisReceiptResponse
{
public Response response { get; set; }
}
public class Shipping
{
public object address_1 { get; set; }
public object address_2 { get; set; }
public object city { get; set; }
public object country { get; set; }
public object province { get; set; }
public object postal_code { get; set; }
}
public class Tokenize
{
public string success { get; set; }
public string first4last4 { get; set; }
public string datakey { get; set; }
public string status { get; set; }
public string message { get; set; }
}
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.
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.
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
Transaction Status
Payment Receipt
Downloads