<?php
require_once('./config.php');
require_once(dirname(__FILE__) . '/lib/my-infusionsoft.php');

$logFile = dirname(__FILE__) . '/clickbank2infusion.log';

try {
	if (Debug) {
		$logEntry = sprintf("%s -- Received CB Notification!\r\n", date("c"));
		logEntry($logEntry);
		
		$logEntry = sprintf("HTTP Post Data:\r\n%s\r\n", print_r($_POST, true));
		logEntry($logEntry);
	}
	
	$ctransrole = $_POST['ctransrole'];
	if ($ctransrole != 'VENDOR') {
		echo '2';
//		die;
	}
	
	$infusion = new MyInfusionsoft(InfusionAppName, InfusionApiKey);
	$infusion->log = InfusionLog;

	$custName = isset($_POST['ccustname']) ? $_POST['ccustname'] : $_POST['ccustfullname'];
	$nameParts = explode(" ", $custName);
	$firstName = $nameParts[0];
	$lastName = ! empty($nameParts[2]) ? $nameParts[2] : $nameParts[1];
	$middleName = ! empty($nameParts[2]) ? $nameParts[1] : null;

	$contact['FirstName'] = $firstName;
	$contact['LastName'] = $lastName;
	$contact['MiddleName'] = $middleName;
	$contact['Email'] = $_POST['ccustemail'];

	$ctransaction = $_POST['ctransaction'];
	$cproditem = $_POST['cproditem'];
	$ctransaffiliate = $_POST['ctransaffiliate'];
	$ctransreceipt = $_POST['ctransreceipt'];
	
	$affiliateCustomField = "_cb${cproditem}aff";
	$receiptCustomField = "_cb${cproditem}receipt";
	
	$contact[$affiliateCustomField] = $ctransaffiliate;
	$contact[$receiptCustomField] = $ctransreceipt;
	
	if ($ctransaction == 'TEST') {
		if (array_key_exists($cproditem, $test)) {
			$infusion->addContact($contact, UpdateInfusion);
			$infusion->runActionSet($test[$cproditem]);
		}


	if ($ctransaction == 'SALE' || $ctransaction == 'SALE_TEST'|| $ctransaction == 'TEST_SALE') {
		if (array_key_exists($cproditem, $sale)) {
			$infusion->addContact($contact, UpdateInfusion);
			$infusion->runActionSet($sale[$cproditem]);
		}
	}


	if ($ctransaction == 'BILL') {
		if (array_key_exists($cproditem, $bill)) {
			$infusion->addContact($contact, UpdateInfusion);
			$infusion->runActionSet($bill[$cproditem]);
		}
	}


	 if ($ctransaction == 'RFND' || $ctransaction == 'INSF') {
		if (array_key_exists($cproditem, $rfnd)) {
			$infusion->addContact($contact, UpdateInfusion);
			$infusion->runActionSet($rfnd[$cproditem]);

}
	 if ($ctransaction == 'CGBK') {
		if (array_key_exists($cproditem, $cgbk)) {
			$infusion->addContact($contact, UpdateInfusion);
			$infusion->runActionSet($cgbk[$cproditem]);

	}



}

//		if (array_key_exists($cproditem, $rfnd)) {
//			$infusion_contact = $infusion->Contact();
				
//			if($conArray = $infusion_contact->find_one_by_email($contact['Email'])) {
//				$conArray[0]->run_action_sequence($rfnd[$cproditem]);
//			}
		}
	}
	 if ($ctransaction == 'CANCEL-REBILL') {
		if (array_key_exists($cproditem, $cancel)) {
			$infusion->addContact($contact, UpdateInfusion);
			$infusion->runActionSet($cancel[$cproditem]);

//		if (array_key_exists($cproditem, $cancel)) {
//			$infusion_contact = $infusion->Contact();
			
//			if($conArray = $infusion_contact->find_one_by_email($contact['Email'])) {
//				$conArray[0]->run_action_sequence($cancel[$cproditem]);
//			}			
		}
	}

	echo '1';
}
catch (Exception $e) {
	$logEntry = sprintf("%s -- ERROR!\r\n", date("c"));
	$logEntry .= sprintf("%s\r\n", $e->error);
	logEntry($logEntry);

	throw new Exception($e->error);
}

function logEntry($logEntry) {
	global $logFile;

	file_put_contents($logFile, $logEntry, FILE_APPEND);
}
