<?php 
require_once('../../../../configuration.php');
require_once('../../../../functions.php');
require_once('../../../../dbhandle.php');

$selectsecret = mysql_query("SELECT * FROM Web_Gateways WHERE phpname = 'g2apay' ");
$selectsecretre = mysql_fetch_array($selectsecret);
$secrethash = $selectsecretre['data3'];
					
$transactionId = $_GET['transactionId'];
$userOrderId = $_GET['userOrderId'];
$email = $_GET['email'];
$amount = $_GET['amount'];
$currency = $_GET['currency'];
$status = $_GET['status'];
$orderCreatedAt = $_GET['orderCreatedAt'];
$orderCompleteAt = $_GET['orderCompleteAt'];
$refundedAmount = $_GET['refundedAmount'];
$hash = $_GET['hash']; // generated at payment process >> must be equal with $hash2use
$ApiSecret = $secrethash;

$hash2encode = $transactionId . $userOrderId . $amount . $ApiSecret;
$hash2use = hash('sha256', $hash2encode);

if($status == 'complete') {
if($hash2use == $hash) {
	$bigtext = $userOrderId;
	$urlst = explode("|", $bigtext);
	$acctype = $urlst[1];
	$taxees = $urlst[2];
	$usernamea = $urlst[3];
	$g2aemail = $email;
	if($acctype == 'publisher') {
		$traceuser = mysql_query("SELECT * FROM Accounts_Classic_Publishers WHERE g2aemail = '$g2aemail' ");
	}
	if($acctype == 'advertiser') {
		$traceuser = mysql_query("SELECT * FROM Accounts_Classic_Advertisers WHERE g2aemail = '$g2aemail' ");
	}
	$traceuserresult = mysql_fetch_array($traceuser);
	$balance = $traceuserresult['balance'];
	$username = $traceuserresult['username'];
	$balancenew = $balance + ($amount - $taxees);
	if($username == $usernamea) {
		if($acctype == 'publisher') {
			mysql_query("UPDATE Accounts_Classic_Publishers SET balance='$balancenew' WHERE username='$username'");
		}
		if($acctype == 'advertiser') {					
			mysql_query("UPDATE Accounts_Classic_Advertisers SET balance='$balancenew' WHERE username='$username'");
		}
	}
}else {
	//ERROR >> Add payment to payment with errors
}
}

?>