<?php

define('INCLUDE_STEPS', 'pluginCallback');
require_once dirname(__FILE__) . '/../../../common/common.php';
require_once 'classes/class.gateway.plugin.php';

$GLOBALS['logger']->log(5, 'Quantum gateway callback invoked');

$cPlugin = new Plugin($_POST['invoice_num']);

// Verification step to ensure callback was made from the quantum gateway servers.
// Need to send browser headers or it won't work.
$response = curlRequest('https://www.quantumgateway.com/cgi/verify.php?transID='.$_POST['transID'].'&trans_result='.$_POST['trans_result'], false, false, true, true);

if (substr($GLOBALS['gSettingsArray']['ClientExec URL'],-1,1) == '/') {
    $signupURL = $GLOBALS['gSettingsArray']['ClientExec URL'].'signup.php';
} else {
    $signupURL = $GLOBALS['gSettingsArray']['ClientExec URL'].'/signup.php';
}

if (!preg_match('/ACCEPTED/i', $response)) {
    $GLOBALS['logger']->log(5, "Callback verification failed");
    $cPlugin->PaymentRejected('Callback verification failed');
    redirectPage("$signupURL?step=6&pass=0");
}

$GLOBALS['logger']->log(5, "Callback has been verified successfully");

if ($_POST['trans_result'] == 'APPROVED') {
    $cPlugin->PaymentAccepted($_POST['amount'],"Quantum Gateway payment of {$_POST['amount']} was accepted.", $_POST['transID']);
    redirectPage("$signupURL?step=6&pass=1");
} elseif ($_POST['trans_result'] == 'DECLINED') {
    $cPlugin->PaymentRejected("Quantum Gateway payment of {$_POST['amount']} was rejected.");
}

redirectPage("$signupURL?step=6&pass=0");

?>
