<?php

//-----------------------------------------------------------------
/**
* Purpose : Process the transaction for single purchase item
* You might want to do:
* Check if the mc_gross payment value is correct
* Check if the mc_currency code is correct (usually the same for all items)
* Check if txn_id has been used before
* Save the data to a database
* Send an email
*
* For physical goods, only check that the payment sent is at least the database value.
* Also only send a warning email if payment and currency values do not match
* For digital goods, check if database value is less than or equal to paypal value, 
* if not it you might want to exit.
*/
function process($paypal) {

    //We can get all the paypal data from the $paypal variable similar to the following and
    //then we can do whatever we want with that data. Such as insert into a database, send
    //an email, check the values and so on.

    $txn_id 		=	$paypal["txn_id"];
	$item_number 	=	$paypal["item_number"];	
	$item_name 		=	$paypal['item_name'];
	$payment_status =	$paypal['payment_status'];
	$mc_gross		=	$paypal["mc_gross"];
	$mc_currency	=	$paypal["mc_currency"];
	$receiver_email	=	$paypal["receiver_email"];
	$payer_email	=	$paypal["payer_email"];
	$quantity		=	$paypal["quantity"];

}
//-----------------------------------------------------------------
/**
* Purpose : Process the transaction for shopping cart for DIGITAL and PHYSICAL
* You might want to do:
* Check if the mc_gross payment value is correct for each product
* Check if the mc_currency code is correct (usually the same for all items)
* Check if txn_id has been used before
* Save the data to the database
* Send the email
*
* For physical goods, only check that the payment sent is at least the database value
* Also only send a warning email if payment and currency values don't match
* For digital goods, check if database value is less than or equal to paypal 
* value, if not it should exit and send an email
*/
function processcart($paypal, $paypal_cart) {

    //We can get details of the purchase from $paypal array:

	$txn_id 		= 	$paypal["txn_id"];
	$payment_status	=	$paypal["payment_status"];
	$mc_currency	=	$paypal["mc_currency"];
	$receiver_email	=	$paypal["receiver_email"];
	$payer_email	=	$paypal["payer_email"];

	$num_cart_items			=	$paypal['num_cart_items'];

    //Then we can get the details of the cart from the $paypal_cart array:

	for ($i=1; $i <= $num_cart_items; $i++) {
		
        $item_number 	= $paypal_cart[$i]['item_number_' . $i];
		$item_name 		= $paypal_cart[$i]['item_name_' . $i];
		$mc_gross 		= $paypal_cart[$i]['mc_gross_' . $i];
		$quantity 		= $paypal_cart[$i]['quantity_' . $i];

    }

    //We can prcoess these in whatever way we descide.  So we can 
    //chack the values, and save to the database.

}
//-----------------------------------------------------------------
/**
* Handles fee reversal in whatever way you choose
* mc_gross would be negative values in your database
*/
function processreversal($paypal) {

    //You can get the details of the reversal like this:

	$txn_id 		=	$paypal["txn_id"];
	$item_number 	=	$paypal["item_number"];	
	$item_name 		=	$paypal['item_name'];
	$payment_status =	$paypal['payment_status'];
	$mc_gross		=	$paypal["mc_gross"];
	$mc_fee			=	$paypal["mc_fee"];
	$mc_currency	=	$paypal["mc_currency"];
	$receiver_email	=	$paypal["receiver_email"];
	$payer_email	=	$paypal["payer_email"];
	$quantity		=	$paypal["quantity"];
	$currentdate 	= 	date("Y-m-d H:i:s");

    //You might want to update a database or send an email here.

}

?>