<?php
require_once(dirname(__FILE__).'/../../config.php');

//*** get paypal payment details ***//

$paypalDetails = $db->Query("SELECT `ppc_payments_type_email` FROM ppc_payments_types where ppc_payments_type_id=?","i",1);
$dir = '/userAdvertiserAddFunds';
$USER_ID	=	$_SESSION[USER_SESSION]['0']['user_id'];


// Setup class
$p = new paypal_class( ); 				 // initiate an instance of the class.
$p -> admin_mail = EMAIL_ADD; 
//$p -> paypal_mail = PAYPAL_EMAIL_ADD;  // If set, class will verfy the receiver.
            
switch ($_GET['action']) {
    
   default:      // Process and order...

      // There should be no output at this point.  To process the POST data,
      // the submit_paypal_post() function will output all the HTML tags which
      // contains a FORM which is submited instantaneously using the BODY onload
      // attribute.  In other words, don't echo or printf anything when you're
      // going to be calling the submit_paypal_post() function.
      
      // adds or edits a "$p->add_field(key, value);" in following, which is what will be
	 		// sent to paypal as POST variables. Refer to PayPal HTML Variables:
	 		// https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_Appx_websitestandard_htmlvariables
 
      // This is where you would have your form validation  and all that jazz.
      // You would take your POST vars and load them into the class like below,
      // only using the POST values instead of constant string expressions.
      
      // setup a current URL variable for this script
			$this_script = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
      
      		$p->add_field('business', PAYPAL_EMAIL_ADD); //don't need add this item. if your set the $p -> paypal_mail.
			$p->add_field('return', HTTP_MASTER.'dashboard/advertiser/deposit-management/add-money?action=success');
			$p->add_field('cancel_return', HTTP_MASTER.'dashboard/advertiser/deposit-management/add-money?action=cancel');
			$p->add_field('notify_url', $this_script.'?action=ipn');
			$p->add_field('item_name', $LANG["WBS_PAYPAL_ITEM_NAME"]);
			$p->add_field('cmd', '_xclick');
			$p->add_field('rm', '2');	// Return method = POST
			$p->add_field('amount', $_POST['amount']);
			$p->add_field('mc_currency', PAYPAL_CURRENCY);
 			$p->add_field('custom', $USER_ID);
			$p->add_field('item_number', $USER_ID);


require_once(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/header.php');
echo '<link href="'.HTTP_SERVER_WEBSITE.'templates/'.WEBSITE_TEMPLATE.'/assets/css/dashboard.css" rel="stylesheet">';

		$p->submit_paypal_post($LANG); // submit the fields to paypal

require_once(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/footer.php');


		//$p->dump_fields();      // for debugging, output a table of all the fields
      break;
   
   case 'success':      // Order was successful...
   
      // This is where you would probably want to thank the user for their order
      // or what have you.  The order information at this point is in POST 
      // variables.  However, you don't want to "process" the order until you
      // get validation from the IPN.  That's where you would have the code to
      // email an admin, update the database with payment status, activate a
      // membership, etc.  
 
      echo "<html><head><title>Success</title></head><body><h3>Thank you for your order.</h3>";
      foreach ($_POST as $key => $value) { echo "$key: $value<br>"; }
      echo "</body></html>";
      
      // You could also simply re-direct them to another page, or your own 
      // order status page which presents the user with the status of their
      // order based on a database (which can be modified with the IPN code 
      // below).
      
      break;
      
   case 'cancel':       // Order was canceled...

      // The order was canceled before being completed.
 
      echo "<html><head><title>Canceled</title></head><body><h3>The order was canceled.</h3>";
      echo "</body></html>";
      
      break;
      
   case 'ipn':          // Paypal is calling page for IPN validation...
   
      // It's important to remember that paypal calling this script.  There
      // is no output here.  This is where you validate the IPN data and if it's
      // valid, update your database to signify that the user has payed.  If
      // you try and use an echo or printf function here it's not going to do you
      // a bit of good.  This is on the "backend".  That is why, by default, the
      // class logs all IPN data to a text file.
      
      if ($p->validate_ipn()) {
          
         // Payment has been recieved and IPN is verified.  This is where you
         // update your database to activate or process the order, or setup
         // the database with the user's order details, email an administrator,
         // etc.  You can access a slew of information via the ipn_data() array.
  
         // Check the paypal documentation for specifics on what information
         // is available in the IPN POST variables.  Basically, all the POST vars
         // which paypal sends, which we send back for validation, are now stored
         // in the ipn_data() array.
  
         // For this example, we'll just email ourselves ALL the data.



/*
	Deposit Types
	1 : paypal
	2 : credit card
	3 : bank transfer
*/

$adv_deposits_user_id	=	$prepare->sanitize( $_POST['custom'], 'input' );
$adv_deposits_type		=	'1';
$adv_deposits_email		=	$prepare->sanitize( $_POST['payer_email'], 'input' );
$adv_deposits_amount	=	$prepare->sanitize( $_POST['payment_gross'], 'input' );
$adv_deposits_details	=	'';
$adv_deposits_status	=	'1';
$adv_deposits_date		=	strtotime( "now" );


//*** INSERT INTO DEPOSITS ***//
$insert		=	$db->NoQuery("INSERT INTO advertisers_deposits VALUES(NULL,?,?,?,?,?,?,?)","sssssss",$adv_deposits_user_id,$adv_deposits_type,$adv_deposits_email,$adv_deposits_amount,$adv_deposits_details,$adv_deposits_date,$adv_deposits_status);
$deposit_id	=	$db->LastID();

//** UPDATE USER DEPOSIT MONEY **//
$update = $db->NoQuery("UPDATE advertisers_profiles SET adv_profile_deposits=adv_profile_deposits+".$adv_deposits_amount." where adv_profile_user_id=?","s",$adv_deposits_user_id);


foreach ($p->ipn_data as $key => $value) { $content .= "\n$key: $value"; }
$adv_deposits_details = ( serialize( $p->ipn_data ) );

$update = $db->NoQuery("UPDATE advertisers_deposits SET adv_deposits_details=? where adv_deposits_id=?","si",$adv_deposits_details,$deposit_id);

				 $subject = 'Instant Payment Notification - Recieved Payment';
				 $p->send_report ( $subject );
      } else {
				 $subject = 'Instant Payment Notification - Payment Fail';
			 	 $p->send_report ( $subject );
    	}
      break;
 }



?>