<?php
//Encryption key you set for the app in dashboard
$encryption_key = 'qwerty';
//data1 and data2 sent as GET parameters
//$data1 = $_GET['data1'];
//$data2 = $_GET['data2'];
$data1 = "02A87383F97008D2F8898DA81A39EDAF";
$data2 = "839EA3731891B24D8025F447747F097664DB4F6A1B88351796EFF7234BE99B21A8E6176F7515C0E4520E898BE97F1B50D48F9979DE6B96822CA9E4DAB6094076E71A2898DC22632FEE638F611334B08557964783B9F98C10D2B841C2182ED5F707F569143431847800EDA5C0D2FFE8BC2550D4F16850F57DBE4315404D10BA295D724A30BB3B2FB6FB72F74624819D5F89AEFB73E8C8EE0D61E8224E749C6BC8";

//Decrypting data1 and data2
$key = pack("H*" , hash('sha256', $encryption_key));
$iv = pack("H*" , $data1);
$encrypted = pack("H*" , $data2);

if(function_exists('mdecrypt_generic')) {
  //Mcrypt
  $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'', MCRYPT_MODE_CBC, '');
  mcrypt_generic_init($td, $key, $iv);
  $decrypted = mdecrypt_generic($td, $encrypted);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
  $strPad = ord($decrypted[strlen($decrypted)-1]);
  $decrypted = substr($decrypted, 0, -$strPad);
} else {
  //OpenSSL
  $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
}

parse_str($decrypted, $output);

//User ID set using Appodeal.getUserSettings(this).setUserId("User#123") method in your app
$userId = $output['user_id'];
//Reward amount
$amount = $output['amount'];
//Reward currency
$currency = $output['currency'];
//Unique impression ID in the UUID form
$impressionId = $output['impression_id'];
//Timestamp of the impression
$timestamp = $output['timestamp'];
//Hash of the data used for validity confirmation
$hash = $output['hash'];

//Hash of the data calculation
$hashString = sha1("user_id=" . $userId . "&amount=" . $amount . "&currency=" . $currency . "&impression_id=" . $impressionId . "&timestamp=" . $timestamp);

//If hashes match impression is valid
if (strcasecmp($hash, $hashString) == 0) {
  //Validate amount and currency
  //Check impression_id for uniqueness
  //Add funds to user balance
  echo "Funds added";
}
?>
