# Postback Security

You should verify the signature received in the postback to ensure that the call comes from our servers.

\
Signature parameter should match MD5 of `subId` `transactionId` `reward` `secret key`. You can find your `secret` of your placement settings. Here is a detailed page for getting your [API and Secret Key](/integrate-offerwall/required-information.md).

## Postback Examples (GET):

{% tabs %}
{% tab title="PHP" %}

```php
<?php
   $secret = "SECRET_KEY"; // Get your secret from placement settings

   $subId = isset($_GET['subId']) ? $_GET['subId'] : null;
   $transId = isset($_GET['transId']) ? $_GET['transId'] : null;
   $reward = isset($_GET['reward']) ? $_GET['reward'] : null;
   $signature = isset($_GET['signature']) ? $_GET['signature'] : null;

   // Validate Signature
   if(md5($subId . $transId . $reward . $secret) != $signature)
   {
       echo "ERROR: Signature doesn't match";
       return;
   }

   // Further processing can be done here
   echo "Signature is valid. Process the postback.";
?>
```

{% endtab %}

{% tab title="Python" %}

```python
from flask import Flask, request, jsonify
import hashlib

app = Flask(__name__)
secret = "SECRET_KEY"  # Get your secret from placement settings

@app.route('/postback', methods=['GET'])
def postback():
    subId = request.args.get('subId')
    transId = request.args.get('transId')
    reward = request.args.get('reward')
    signature = request.args.get('signature')

    # Validate Signature
    if hashlib.md5((subId + transId + reward + secret).encode()).hexdigest() != signature:
        return "ERROR: Signature doesn't match", 400

    # Further processing can be done here
    return "Signature is valid. Process the postback."

if __name__ == '__main__':
    app.run()
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
const express = require('express');
const crypto = require('crypto');
const app = express();
const port = 3000;
const secret = "SECRET_KEY"; // Get your secret from placement settings

app.get('/postback', (req, res) => {
    const { subId, transId, reward, signature } = req.query;

    // Validate Signature
    const hash = crypto.createHash('md5').update(subId + transId + reward + secret).digest('hex');
    if (hash !== signature) {
        res.status(400).send("ERROR: Signature doesn't match");
        return;
    }

    // Further processing can be done here
    res.send("Signature is valid. Process the postback.");
});

app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'sinatra'
require 'digest'

secret = "SECRET_KEY" # Get your secret from placement settings

get '/postback' do
  subId = params['subId']
  transId = params['transId']
  reward = params['reward']
  signature = params['signature']

  # Validate Signature
  if Digest::MD5.hexdigest(subId + transId + reward + secret) != signature
    halt 400, "ERROR: Signature doesn't match"
  end

  # Further processing can be done here
  "Signature is valid. Process the postback."
end
```

{% endtab %}

{% tab title="Java" %}

```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@RestController
public class PostbackController {

    private final String secret = "SECRET_KEY"; // Get your secret from placement settings

    @GetMapping("/postback")
    public String postback(@RequestParam String subId, @RequestParam String transId,
                           @RequestParam String reward, @RequestParam String signature) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            String data = subId + transId + reward + secret;
            md.update(data.getBytes());
            byte[] digest = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            if (!sb.toString().equals(signature)) {
                return "ERROR: Signature doesn't match";
            }
        } catch (NoSuchAlgorithmException e) {
            return "ERROR: Unable to process signature";
        }

        // Further processing can be done here
        return "Signature is valid. Process the postback.";
    }
}
```

{% endtab %}

{% tab title="C#" %}

```csharp
using Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography;
using System.Text;

[ApiController]
[Route("postback")]
public class PostbackController : ControllerBase
{
    private readonly string secret = "SECRET_KEY"; // Get your secret from placement settings

    [HttpGet]
    public IActionResult Postback(string subId, string transId, string reward, string signature)
    {
        using (var md5 = MD5.Create())
        {
            var input = Encoding.UTF8.GetBytes(subId + transId + reward + secret);
            var hashBytes = md5.ComputeHash(input);
            var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();

            if (hash != signature)
            {
                return BadRequest("ERROR: Signature doesn't match");
            }
        }

        // Further processing can be done here
        return Ok("Signature is valid. Process the postback.");
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
We have not included IP address validation in the examples above for certain reasons. We advise verifying the [whitelisted IP addresses](#ips-to-whitelist); however, it is entirely up to you to use this feature as it is optional and not mandatory.
{% endhint %}

{% hint style="info" %}
Don’t forget to check the transId against your database to ensure it doesn’t already exist.
{% endhint %}

Our servers wait for a response for a maximum time of 30 seconds before the timeout. In this case, postback will be marked as failed. Please, check if the transaction ID sent to you was already entered in your database, this will prevent to give twice the same amount of virtual currency to the user.

## **IPs to whitelist**

We will be sending the postbacks from any of the following IP address(es). Please make sure they are whitelisted if needed to be in your server.

{% hint style="info" %}
**195.35.39.220**\
**2a02:4780:b:1270:0:2b97:5732:1**\
**2a02:4780:b:1270:0:2b97:5732:2**\
**2a02:4780:b:1234::19**
{% endhint %}

## **Respond to Postback**

Our servers will expect your website to respond with "ok". If your postback doesn't return "ok" as response, postback will be marked as failed (even if postback was successfully called) and you will be able to resend it manually from Revtoo.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.revtoo.com/s2s-postback/postback-security.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
