<?php

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

class Plugin2checkoutCallback extends PluginCallback
{
    
    function processCallback()
    {
        // ignore blank browser requests
        if (!isset($_REQUEST['ce_invoice_num'])) {
            return;
        }
        
        //Get vars from post/get
        $lInvoiceID = $_REQUEST['ce_invoice_num'];
        $lInvoicePaid = $_REQUEST['x_2checked'];
        $lSignUp = $_REQUEST['signup'];
        $lErrorCode = "";
        $lPricePaid = $_REQUEST['x_amount'];
        $lOrderID = $_REQUEST['order_number'];

        //Create plug in class to interact with CE
        $cPlugin = new Plugin($lInvoiceID, $_GET['plugin'], $this->user, $this->customer);


        //Determine if payment was made & make sure that it was not made in demo mode
        if (($lInvoicePaid == 'Y' || $lInvoicePaid == 'K') && $_REQUEST['demo'] != 'Y') {
            $transaction = " 2checkout Payment of $lPricePaid was accepted (OrderID:".$lOrderID.")";
            $cPlugin->PaymentAccepted($lPricePaid, $transaction);
        } else {
            //if the payment was made in demo mode let's log it
            if($_REQUEST['demo']=='Y') {
                $transaction = " 2checkout Payment of $lPricePaid was accepted (OrderID:".$lOrderID.") (DEMO MODE)";
                $cPlugin->PaymentAccepted($lPricePaid, $transaction);
            } else {
                //if not in demo mode we will log the reason_response_code from 2co
                $lErrorCode = $_REQUEST['x_response_reason_code'];
                $transaction = "Payment rejected - Reason: ".$lErrorCode;
                $cPlugin->PaymentRejected($transaction);
            }
        }

        //Forward User needs to come after PaymentAccepted or PaymentRejected
        $cPlugin->ForwardUser($lSignUp, true);
    }
}

?>
