Paynow Integration Part 5 - Checking for payments Introduction
Tags: webdev, payments, paynow
Published: 2023-09-24 05:58:33

Once you have requested for a transaction to take place the next steps includes

- Checking transaction status
- Giving a service or a product to the client


![Paynow checking all methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oek9qxavyzu43xf0zih.png)

For checking transaction you can either use

1. Polling
2. Webhooks
3. Hybrid

## Method 1: Polling


![Paynow payment checking using polling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k81izupa3p00ofpnfldu.png)

In the polling method we are checking the status of the transaction at regular intervals.

For example we could check the transaction status every 10 or 15 seconds. If the transaction was successful or unsuccessful we take the appropriate action.

### Advantages

- It provides precise control over when data is retrieved or actions are taken.
- Easier to implement compared to event-driven approaches.
- Helps ensure predictable system behavior as data is collected at fixed intervals.
- Suitable for devices and systems that do not support interrupts or events for example phones, gadgets that cant serve webhook endpoints on the internet.

### Disadvantages

- Consumes system resources and can lead to inefficiency, especially with frequent polling.
- May introduce latency or delays in response time, as data is collected only at specified intervals.
- In cases where data rarely changes, continuous polling can waste resources. Imagine spamming Paynow every second with "Whats the status of my transaction?" .....thats annoying

## Method 2: Webhooks

![Paynow payment checking using webhooks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgfzgo25xs0cwduoxqsk.png)


In the webhooks method, Paynow gives us real-time updates or notifications.

These updates or notification contains the status of our transaction.

Its like ordering junk food at your favorate restaurant, you are given an order number and told to wait for order. 5 minutes later you are told......"Order #202 is ready!"

### Advantages

- Provides instant updates when events occur, reducing latency.
- Only triggers when events happen, minimizing unnecessary requests or data transfer. No more spamming "Whats the status of my transaction?"
- Well-suited for handling a large volume of events or notifications.

### Disadvantages

- Implementation can be more complex than polling, requiring webhook endpoints and event handling logic.
- Requires careful handling to prevent potential security risks, such as unauthorized access or abuse. Imagine a malicious user updating their transaction request to get free stuff
- Dependent on the stability and availability of the webhook provider's infrastructure.
- Handling and troubleshooting failed webhook deliveries can be challenging.

## Method 3: Hybrid


![Paynow payment using hybrid method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6e4xz2prkvujm6tzpc09.png)

In the hybrid approach we use both polling and webhooks inorder to achieve the best of both world. Of course the has its short-comings.

For example after submitting a transaction we can quickly check for its status using polling. If it succeeds quickly we move on. If it takes time to complete we leave it to the webhook to handle that

```javascript
/**
 *
 * order.js
 *
 * **/

function update_transaction(trans_id, data) {
  // Update transaction
  // Send a message to the frontend using websockets
}

function checkout(user_id, order) {
  // create a payment order and send to paynow
  // Check the status using polling
  // If anything changes call update_transaction
  // After a minute or two exit the function
}

/**
 *
 * orders.routes.js
 *
 * **/

app.post('/payment-updates', (req, res) => {
  // Confirm if its paynow
  // Retrieve transaction id
  // Call update_transaction
})
```

Another way could be to using polling to add an extra layer of confirmation after recieving a successful transaction update from a webhook.

I am making this up. Use whatever works with you.

Thats it guys, in the next tutorials

![A joke](https://media.makeameme.org/created/and-thats-the-83902ebdaf.jpg)