<?php

	include "library.php";

		// this is the latest payment code, 19/07/17
		
		// sample URL request to this form: amt=2.60&cc=USD&cm=133&item_name=Academic%20Dating%20donation&st=Completed&tx=19029293UE0786729
		
		
	$cxn = start_code( true );

	$cxn_common = db_common_login();

	

	$payment_status = val( $_GET, "st" );

	$id = val( $_GET, "cm" );
	
	$amt = val( $_GET, "amt" );

	$currency = val( $_GET, "cc" );
		
	$transaction_id = val( $_GET, "tx" );
	
	$date_created = date("Y-m-d H:i:s");

	$today_date = date_create( date("Y-m-d") );
	
	$today_plus_367_days = clone $today_date;
		
	date_add( $today_plus_367_days, date_interval_create_from_date_string( '367 days' ));
    $today_plus_367_days_str = $today_plus_367_days->format( "Y-m-d" );		


	page_header( "Academic Dating", $cxn, $cxn_common );


	if ($id != "" && ($payment_status == "Completed" || $payment_status == "Processed"))
	{
    	echo "<br><br>Thank you for purchasing a Paid membership.<br><br>";
    	
    	echo "Your payment has been processed and your new membership is active.<br><br>";
    	
    	echo "A receipt for your payment will be emailed to you.<br><br>";
    	
    	echo "Your transaction ID is ".$transaction_id."<br><br><br>";
	
	    
		$query = "SELECT * FROM profile WHERE id = ".$id;
		
		$result = run_query( $query, $cxn );
	
		$data = get_row( $result );

		$query = "UPDATE profile SET membership_type = 'paid', premium_membership_expiry = '".$today_plus_367_days_str."' WHERE id = ".$id;		

		$result = run_query( $query, $cxn );
		
    	send_email( "Academic Dating", "noreply@academicdating.net", "mark.mcilroy@outlook.com", "Academic Dating", "noreply@academicdating.net", "Academic Dating premium membership received", "", "", $cxn, $cxn_common, "live" );
	}
	else
	{
    	echo "<br>Your payment could not be processed. Please contact site administration.<br><br>";
	}


	if ($amt == "")
		$amt = "0";


	$query = "INSERT INTO payment_notification_log (member_id, payment_status, type, currency, amount, transaction_id, date_created) VALUES (".$id.", '".$payment_status."', 'paid membership purchase', '".$currency."', ".$amt.", '".$transaction_id."', '".$date_created."')";		
	$result = run_query( $query, $cxn );


	echo show_home_link();

?>