<?php

require_once 'classes/PluginCallback.php';
require_once 'modules/billing/models/class.gateway.plugin.php';
require_once 'newedge/classes/NE_Network.php';

class PluginQuantumCallback extends PluginCallback
{
    
    function processCallback()
    {
        $cPlugin = new Plugin($_GET['invoice_num'], "", $this->user, $this->customer);

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

        if (!preg_match('/ACCEPTED/i', $response)) {
            $this->logger->log(5, "Callback verification failed");
            $cPlugin->PaymentRejected('Callback verification failed');
            NE_Lib::redirectPage("signup.php?step=6&pass=0");
        }

        $this->logger->log(5, "Callback has been verified successfully");

        if ($_GET['trans_result'] == 'APPROVED') {
            $cPlugin->setTransactionID($_GET['transID']);
            $cPlugin->setAmount($_GET['amount']);
            $cPlugin->setAction('charge');
            $cPlugin->PaymentAccepted($_GET['amount'], "Quantum Gateway payment of {$_GET['amount']} was accepted.", $_GET['transID']);
            NE_Lib::redirectPage("signup.php?step=6&pass=1");
        } elseif ($_GET['trans_result'] == 'DECLINED') {
            $cPlugin->PaymentRejected("Quantum Gateway payment of {$_GET['amount']} was rejected.");
        }

        NE_Lib::redirectPage("signup.php?step=6&pass=0");
    }
}

?>
