Callback mechanism

From Barion Documentation
Revision as of 14:11, 4 February 2021 by Geleim (talk | contribs) (→‎What NOT to do: This has been pretty muched cleared up above)
Jump to navigation Jump to search

Payment callback mechanism (aka. IPN)


Whenever a given payment's state changes, it is expected that the caller system and the Barion database are synchronized. This is accomplished by implementing the callback mechanism (referred to as "Instant Payment Notification" or "IPN" in some terminology) between the two systems.

The callback process

The event flow of the implemented callback mechanism is simple:

  1. The payment gets completed, cancelled, expired, refunded, taken under investigation, or released from investigation
  2. The Barion system sends an HTTP POST request to the merchant's system to the URL defined by the merchant (in the CallbackUrl parameter of the /v2/Payment/Start API call) with the payment's unique identifier, indicating that something has just happened, so the merchant's system should check the payment now
  3. The merchant's system sends a request to the /v2/Payment/GetPaymentState API endpoint with its POSKey and the received payment identifier
  4. Based on the response received, the merchant's system can determine what processing tasks should take place

Structure and processing of the callback request

The callback request contains one parameter, the payment identifier. This is sent in the paymentId field of the POST request body. The merchant's system must send an HTTP 200 OK response (with any content) in order for the callback to be considered successful. The timeout period for answering a callback request is 15 seconds.

If the Barion system does not get an HTTP 200 response, it retries sending the callback for a maximum of 5 times, with exponential back-off timing delay between tries:

  • 2 seconds
  • 6 seconds
  • 18 seconds
  • 54 seconds
  • 2 minutes 42 seconds

So the total time window allocated for successful callback is roughly 4 minutes. If the Barion system fails to get an HTTP 200 response after that, the callback is not sent and the merchant's system automatically gets an e-mail notification about the error.

Every time, when your system receives a callback request from the Barion you have to call the /v2/Payment/GetPaymentState API endpoint to find out the change. First, your system should check the payment's status and after the list of the payment's transactions. For example, if you requested a payment refund or when the reserved payment's time limit has expired the list of payment’s transactions will contain new e-money transactions. It's your system's responsibility to handle changes.

Security

Never rely on the callback function alone. When the callback URL is requested, your application must call the /v2/Payment/GetPaymentState Barion API to get the proper payment state.

Please refer to the Security Measures page for more information and a list of IP addresses.

IMPORTANT
The IP address of the callback request may change any time for security reasons.

Why you should use it

When a Barion Smart Gateway payment process takes place between the two systems, neither of them can rely purely on explicit user interaction to process things. In such situations, the Barion system must inform the merchant's system that the payment has changed in some way, without involving the user.

Here are some examples:

  • the payment is completed, but the user closed their browser or lose network connection, preventing them from returning to the webshop or application that redirected them to the Barion Smart Gateway
  • the user explicitly cancels the payment, but closes their browser instead of navigating back to the merchant
  • the payment is never completed and expires because of the time window limit
  • the payment is refunded

Implementing callback handling will save you time and pain tracking such "lost" payments. It also eases the troubleshooting with your customers, since you will always have valid data on your side.

Rate limiting

To prevent potential disruptions in our service caused by the abuse of the /v2/Payment/GetPaymentState API endpoint, we have introduced a rate-limiting feature to throttle excessive requests. Generally, you should rely on the callback mechanism whenever possible unless you have a specific reason not to, but if you want to manually get the details of a payment, here's a couple of things to bear in mind:

  • Make sure you do not send continuous requests for the same payment even if you need to get its current state manually without waiting for the callback to happen
  • Do not keep requesting for an indefinite time, even if your requests are throttled (e.g. if a problem occurs during the payment process, and the callback never happens, you shouldn't keep polling the API indefinitely) In this case the frequency is not the problem, but as time passes these orphan requests can cause a great load together

To avoid these situations, the following throttling logic is implemented on our API:

  • During a 5 seconds window we only accept the first 2 /v2/Payment/GetPaymentState requests with the same payment ID. This means that if you submit two requests less than 5 seconds apart, subsequent requests will be denied until 5 seconds pass after the initial successful request. These requests will return with an HTTP 429 "Too many requests" error.
  • We keep track of requests, and if requests with the same payment ID reach a certain limit during a specific timeframe, all further requests will return HTTP 429. The exact timeframe and limits are not disclosed to the public.

To reinforce the points above, we recommend using the callback mechanism described above, and calling the /v2/Payment/GetPaymentState API endpoint only when there's a reason to do so.