<?php

require_once TIAOP_PLUGIN_DIR . "includes/tiaop_db.php";
$ip_address = tiaop_get_user_ip();
if (isset($_REQUEST["btnSavePurchase"])) {
    $test_results = "IP Address: " . $ip_address;
    $test_results = $test_results . "<br />Database Time: " . tiaop_get_mysql_time();
    $test_results = $test_results . "<br />Creating Saved Purchase.";
    tiaop_save_purchase($ip_address, "1.00", "Test Purchase Description", "Test Purchase Reference", true);
    $test_results = $test_results . "<br />Check Saved Purchase Table.";
}
if (isset($_REQUEST["btnDeletePurchase"])) {
    $test_results = "IP Address: " . $ip_address;
    $test_results = $test_results . "<br />Deleting Saved Purchase.";
    tiaop_delete_current_ip();
    tiaop_purge_history();
    $test_results = $test_results . "<br />Check Saved Purchase Table.";
}
if (isset($_REQUEST["btnProcessPurchase"])) {
    $test_results = "IP Address: " . $ip_address;
    $test_results = $test_results . "<br />Affiliate ID: 1";
    $test_results = $test_results . "<br />Visit ID: 2";
    require_once TIAOP_PLUGIN_DIR . "includes/tiaop_processing.php";
    tiaop_process_saved_purchases(true);
    $test_results = $test_results . "<br />Check History Table.";
}
?>
<div style="margin-top:10px">
	<form method="post" action="">
		<table class="form-table">
			<tr>
function tiaop_paypal_notify($false, $args)
{
    // Delete any expired purchases
    tiaop_delete_expired();
    // Don't proceed if the notification arguments weren't passed
    if (!$args) {
        if (WP_DEBUG === true) {
            error_log("tiaop_paypal_notify: second argument set empty");
        }
        return;
    }
    // Don't proceed if the paypal array isn't available
    if (!$args["paypal"]) {
        if (WP_DEBUG === true) {
            error_log("tiaop_paypal_notify: paypal array not found");
        }
        return;
    }
    // Show the available args in the debug file
    if (WP_DEBUG === true) {
        error_log("tiaop_paypal_notify: args value = ");
        if (is_array($args) || is_object($args)) {
            error_log(print_r($args, true));
        } else {
            error_log($args);
        }
    }
    // Get the referral information
    $affiliateId = affiliate_wp()->tracking->get_affiliate_id();
    $amount = $args["paypal"]["mc_gross"];
    $description = $args["paypal"]["transaction_subject"];
    $reference = $args["paypal"]["payer_email"];
    $payerIpAddress = $args["paypal"]["option_selection2"];
    // If the affiliateId wasn't found using get_affiliate_id(), save to try later
    if (empty($affiliateId)) {
        if (WP_DEBUG === true) {
            error_log("tiaop_paypal_notify: affiliate_wp()->tracking->get_affiliate_id() did not obtain affiliateId, storing in database to try later");
        }
        // Save purchase
        tiaop_save_purchase($payerIpAddress, $amount, $description, $reference);
        return;
    }
    // If the affiliateId wasn't found, log and return
    if (empty($affiliateId)) {
        if (WP_DEBUG === true) {
            error_log("tiaop_paypal_notify: affiliate id was not found");
        }
        return;
    }
    // If the affiliateId isn't valid, log and return
    if (!affiliate_wp()->tracking->is_valid_affiliate($affiliate_id)) {
        if (WP_DEBUG === true) {
            error_log("tiaop_paypal_notify: affiliate id was not valid");
        }
        return;
    }
    // Get the visit id
    $visitId = affiliate_wp()->tracking->get_visit_id();
    // Call the function that adds the referral
    tiaop_add_referral($affiliateId, $amount, $description, $reference, "", $visitId);
}