<?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()
    {
        // ignore blank browser requests
        if (!isset($_GET['invoice_num'])) {
            return;
        }
        
        $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(4, "Callback verification failed");
            $cPlugin->PaymentRejected('Callback verification failed');
        } else {
            $this->logger->log(4, "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']);
            } elseif ($_GET['trans_result'] == 'DECLINED') {
                $cPlugin->PaymentRejected("Quantum Gateway payment of {$_GET['amount']} was rejected.");
            }
        }

        $cPlugin->ForwardUser(1) ;
    }
}

?>
