<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: POST.GATEWAY.PHP
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

if ( $_SERVER['REQUEST_METHOD'] == 'POST' || $_SERVER['REQUEST_METHOD'] == 'post' )
{
	require_once( '../../../includes/config.inc.php' );

	/* system controller */
	require_once( RL_INC . 'control.inc.php' );

	$reefless -> loadClass( 'Cache' );
	$reefless -> loadClass( 'CCAvenue', null, 'ccAvenue' );

	/* load system configurations */
	$config = $rlConfig -> allConfig();

	/* save params to log */
	if( $GLOBALS['config']['ccavenue_test_mode'] )
	{
		$file = fopen( 'response.log', 'a' );

		if ( $file )
		{                      
			$line = "\n\n" . date( 'Y.m.d H:i:s' ) . ":\n";
		    fwrite( $file, $line );

			foreach( $_REQUEST as $p_key => $p_val )
			{                 
				$line = "{$_SERVER['REQUEST_METHOD']}: {$p_key} => {$p_val}\n";
    			fwrite( $file, $line );
			}

			fclose( $file );
		}
	}
 
 	$txn_id = $_POST['Order_Id'];
	$txn_checksum = $_POST['Checksum'];
	$total = $_POST['Amount'];
	$prefix = '';

	if ( $txn_id && $txn_checksum )
	{
		define( 'RL_LANG_CODE', $lang_code );

		$seo_base = RL_URL_HOME;
		$seo_base .= $lang_code == $config['lang'] ? '' : $lang_code . '/';
		
		$lang = $rlLang -> getLangBySide( 'frontEnd', RL_LANG_CODE );
		$GLOBALS['lang'] = $lang;

		$items = $rlCCAvenue -> getPaymentDetails( $txn_id );

		if ( $items )
		{
			$plan_id = $items[0];
			$item_id = $items[1];
			$account_id = $items[2];
			$crypted_sum = $items[3];
			$callback_class = $items[4];
			$callback_method = $items[5];
			$cancel_url = $items[6];
			$success_url = $items[7];
			$lang_code = $items[8];
			$plugin = $items[9];
		}
		else
		{
			$errors = true;
			$rlDebug -> logger( "ccAvenue: Items is empty or can't execute mysql query;" );
		}

		$checksum = $rlCCAvenue -> verifyChecksum( $config['ccavenue_merchant_id'], $txn_id, $total, $_POST['AuthDesc'], $config['ccavenue_working_key'], $txn_checksum );
        //echo $checksum . " -- " . $txn_checksum; exit;
		if ( !$checksum )
		{
			$errors = true;
			$rlDebug -> logger( "ccAvenue: CheckSum is failed;" );
		}
		
		if ( empty( $item_id ) || empty( $total ) )
		{
			$errors = true;
			$rlDebug -> logger( "ccAvenue: Item ID or Total is empty;" );
		}

		if ( $_POST['AuthDesc'] == 'N' ) 
		{            
			if ( $GLOBALS['config']['ccavenue_test_mode'] && $checksum )
			{
				/* continue */
			}	
			else
			{                 
		    	$errors = true;
				$rlDebug -> logger( "ccAvenue: Your transaction was declined;" );
			}
		}
		elseif( $_POST['AuthDesc'] == 'B' )
		{
			$prefix = ' (PENDING)';	
		}

		if ( !$errors ) 
		{
			$reefless -> loadClass( str_replace( 'rl', '', $callback_class ), null, $plugin );
			$$callback_class -> $callback_method( $item_id, $plan_id, $account_id, $txn_id, 'ccAvenue' . $prefix, $total );

			/* clear tmp transaction */
			if ( !$GLOBALS['config']['ccavenue_test_mode'] )
			{
				$sql = "DELETE FROM `" . RL_DBPREFIX . "ccavenue_transactions` WHERE `Txn_ID` = '{$txn_id}' LIMIT 1";
				$rlDb -> query( $sql );
			}
			else
			{
				$sql = "UPDATE `" . RL_DBPREFIX . "ccavenue_transactions` SET `Status` = 'completed' WHERE `Txn_ID` = '{$txn_id}' LIMIT 1";
				$rlDb -> query( $sql );
			}

			$reefless -> redirect( false, str_replace( '&amp;', '&', $success_url ) );
		}
		else
		{
			if ( $cancel_url )
			{
				$reefless -> redirect( false, str_replace( '&amp;', '&', $cancel_url ) );
			}	
		}
	}

	$reefless -> redirect( false, RL_URL_HOME );
}
exit;