<?php
/*****************************************************************************************************/
/*  CALL BACK CODE FROM PAYSYSTEMS : This code needs to be called from your processor upon completion */
/*****************************************************************************************************/
// $cPlugin->GetPluginVariable takes queries in the form of plugin_[pluginname]_[variable]

include_once("../../../classes/class.gateway.plugin.php");
$body = "REQUEST[option1] = ".$_REQUEST['option1']."\n";


//Get vars from post/get
list($tInvoiceID,$tIsRecurring,$tGenerateInvoice) = explode("_", $_REQUEST['option1']);

$body .= "INVOICE: $tInvoiceID\n";
$body .= "ISRECURRING: $tIsRecurring\n";
$body .= "GENERATEINVOICE: $tGenerateInvoice\n";
$body .= "ORDERID: ".$_REQUEST['orderid']."\n";
$body .= "PRICEPAID: ".$_REQUEST['amount']."\n";
$body .= "TRANSSTATUS: ".$_REQUEST['cc_status']."\n";

//mail("alberto@newedge.com","Paysystems msg from: ".$_SERVER['SERVER_NAME'],$body);

$tSignUp = $_REQUEST['option2'];  // return of optional variables needs to be 1 or 0, 1 if a signup transaction
$tTransStatus = $_REQUEST['cc_status'];
$lPricePaid = $_REQUEST['amount'];  //For Future Use
$tOrderID = $_REQUEST['orderid'];

//determine if this is the first payment for this invoice

//Create plug in class to interact with CE
$cPlugin = new Plugin($tInvoiceID);

//$cPlugin->IsPaid() - check to determine if an invoice with this invoice id
//has already been entered into the system and marked paid
//if so then this is a recurring payment using paysystmes recurring engine
if ($cPlugin->IsPaid()==0){
    //Determine if payment was made
    $transaction = " Paysystems Payment of $lPricePaid was accepted (OrderID:".$tOrderID.")";
    $cPlugin->PaymentAccepted($lPricePaid,$transaction);
}else if ( ($cPlugin->IsPaid()==1) && $tGenerateInvoice){
    $transaction = " Recurred Payment of $lPricePaid was accepted via Paysystems - Original Invoice: $tInvoiceID (OrderID:".$tOrderID.")";
    $invoice = $cPlugin->GenerateNewInvoice($lPricePaid,$transaction);
}else{
    $transaction = " Recurred Payment of $lPricePaid was accepted via Paysystems - Original Invoice: $tInvoiceID (OrderID:".$tOrderID.")";
    $cPlugin->PaymentAccepted($lPricePaid,$transaction);
}

//Forward User needs to come after PaymentAccepted or PaymentRejected
$cPlugin->ForwardUser($tSignUp);
?>