Who am I?
Hello everyone, my name is Houssem, API Architect at Moneris, Canada’s leading payment processor.1.
I am here today to show you how we can use Moneris APIs and Postman Flows to design intuitive solutions for some of our everyday developer tasks.
What are we looking to solve?
Let’s say, in another life, we are the technology department of an online store. For the purpose of this example, our stakeholders would like to setup an online payment process that allows customers a same-day cancellation grace period until 11 PM.
What are we looking to build?
Looking at what’s in The Postman API Network, we discovered Moneris.
They recently launched a new set of RESTful APIs that are secure and easy to set up, with awesome customer support!
We will use the Moneris Two-Step payment flow to:
- Perform a temporary hold on the customer’s credit card (called “Authorization”).
- Complete/capture the purchases that were not cancelled (also known as “Void”).
Now to present this process to business owners/stakeholders, we need something that is engaging and visual.
Enter Postman Flows! This tool allows us to effectively illustrate and demonstrate the API flow for better understanding.
We will create a Flow that:
- Retrieves all Moneris Payments performed during the same day.
- Complete all Payments that are in the
AUTHORIZED
state.
The flow will be triggered daily at 11 PM using a Postman monitor.
Step 0: Fork the latest collection of Moneris APIs
Postman Flows runs on the platform’s top collections. Let’s get started by forking the latest collection of Moneris APIs as well as the Moneris Sandbox Environment (Moneris API’s testing environment).
Of course, we also explored the Moneris Payment API and discovered that it supports OAuth 2.0 Client Credentials.
So we navigated to the Moneris API Developer Portal, where we signed up for a test merchant account. As part of the process, we obtained a Merchant ID, along with a pair of Client ID And Client Secret credentials.
The process was simple and straightforward!
Step 1: Generate Access Token
Using the Open API specification provided by Moneris, another team seamlessly integrated their order management system to create Pre-Authorizations.
Now, it’s our turn to retrieve those payments using the List Payments API call. The API supports cursor pagination through a cursor
and limit
query parameters. It also supports filtering Payments by creation time, using the created_from
and created_to
parameters.
To implement this in Flows, let’s start by getting an OAuth 2.0 Access Token:
-
First, we will drag the Start output connector and select the Send Request block.
a. We can then select the Moneris API collection that we forked earlier and navigate to Access Token Generation.
b. In the “Add Environment” box, let’s selectSandbox
. SinceclientId
andclientSecret
are environment variables, Flows will automatically populate the required parameters for us. It’s that intuitive!
-
Since we plan to use the Access Token for future calls, we need to extract it using a Select block before saving it in a variable using the Create Variable block.
-
We can now start running the flow!
Step 2: Retrieve Payments
Now that we’ve generated an access token, let’s proceed to retrieve the payment data.
-
From Access Token request block, drag another success connector and select “Send Request”. Configure it to use the List Payments API.
-
To enable cursor pagination and handle Bearer tokens, we need to update the collection to include variables for the cursor and set the authorization to Bearer Token, using the token as a collection variable.
a. Optionally we can set created_from and created_to parameters to filter payments by a specific date range. By default, the API fetches payments created on the same day.
-
To implement continuous looping over the pages, we will make use of the If block to evaluate whether
next
property is set.
a. To fetch the next page, we will connect the Then output to an Evaluate block to get thecursor
from thenext
link property.
b. Inside the Evaluate block, we will set the code toTypeScript
. Then, we can add the necessary code to extract thecursor
value from the next link’squery string
.
c. Next, we will connect output to the input of another Send List Payments Request block.
-
To make the process a loop, let’s connect the success output of the List Payments block to the previous If block.
-
To prepare for the next step, we will connect both the Then output and the Else output of the flow to a Select block that is set to
body.data
to get the payments list.
-
We can now run the program to execute the workflow!
Step 3: Complete Authorized Payments
Completing the payments is our pièce de résistance. To get started,
-
Let’s filter the Authorized Payments by looping through the Select block’s output from the previous step, using an Evaluate block that filters by
paymentStatus
=AUTHORIZED
.
-
To loop over the list, we connect the Authorized Payments Evaluate block output to a For block.
-
We will then connect the Item’s output to a Send Request block, which will execute the Complete Payment request.
-
Similarly to other calls, we will adapt the
paymentId
path parameter to be a collection variable and connect the For loop to it.
Step 4: Creating a Webhook trigger
Now that we have created a flow that works, how do we trigger it on demand from outside the app? The Webhook trigger will allow us to call the flow using an HTTP request.
- On the bottom left corner of the flows screen, there is a section called Applications.
We can click on the Create button for the Webhook option, which will generate a HTTP link that we can use to call the flow whenever we need it. - We can now grab the generated Webhook link and test it using Postman. We just need to send a request to the link and we should see the flow spring into action. Pretty neat, right?
Step 5: Automating the flow using Postman Monitors
For our final step, we will make use of Postman Monitors to call our Completion Flow every night at 11 PM.
-
First, we need to convert the Flow’s Webhook trigger into a Postman Collection. Let’s call it Moneris API - Automation
-
Once the collection is ready, we can go ahead and set up a Monitor:
a. In the Collection box, we will select Moneris API – Automation.
b. In the Environment dropdown, let’s choose the Sandbox option.
c. We can then set this monitor to run on a weekly timer, every day at 11 PM.
d. And finally, we will just need to click “Create Monitor” to complete the setup.