<?php

$email_notifications = $C->EMAIL_NOTIFICATION_PAYPAL;

function sender_mail($subject, $message, $email) {
//	$subject = "New Payment";
	
	foreach($_POST as $name=>$value) {
		$message .= "<br>".$name.": ".$value;
	}
	send_mail_phpmailer($email, $subject, $message);		
}

function Array2Str($kvsep, $entrysep, $a) {
	$str = "";
	foreach ($a as $k=>$v) {
		$str .= "{$k}{$kvsep}{$v}{$entrysep}";
	}
	return $str;
}

$req = 'cmd=_notify-validate';
$fullipnA = array();
 
foreach ($_POST as $key => $value)
{
	$fullipnA[$key] = $value; 
	$encodedvalue = urlencode(stripslashes($value));
	$req .= "&$key=$encodedvalue";
}

$fullipn = Array2Str(" : ", "\n", $fullipnA);

$url ='https://www.paypal.com/cgi-bin/webscr';	

$curl_result=$curl_err='';
$fp = curl_init();
curl_setopt($fp, CURLOPT_URL,$url);
curl_setopt($fp, CURLOPT_RETURNTRANSFER,1);
curl_setopt($fp, CURLOPT_POST, 1);
curl_setopt($fp, CURLOPT_POSTFIELDS, $req);
curl_setopt($fp, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($req)));
curl_setopt($fp, CURLOPT_HEADER , 0); 
curl_setopt($fp, CURLOPT_VERBOSE, 1);
curl_setopt($fp, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($fp, CURLOPT_TIMEOUT, 30);
 
$response = curl_exec($fp);
$curl_err = curl_error($fp);
curl_close($fp);
 
// Variables received by Paypal
$payment_status = $_POST['payment_status'];
 
if (strcmp ($response, "VERIFIED") == 0) {
	// Check the status of the order
	if ($payment_status != "Completed")	{		
		sender_mail('Payment declined', "The payment was not accepted by paypal - Payment Status: $payment_status", $email_notifications);
		exit;
	}
	
	// Variables received by Paypal
	$item_name = $_POST['item_name'];
	$item_number = $_POST['item_number'];
	$payment_status = $_POST['payment_status'];
	$payment_amount = $_POST['mc_gross'];
	$payment_currency = $_POST['mc_currency'];
	$txn_id = $_POST['txn_id'];
	$receiver_email = $_POST['receiver_email'];
	$quantity = $_POST['quantity'];
	$payer_email = $_POST['payer_email'];
	$txn_type = $_POST['txn_type'];
	$pending_reason = $_POST['pending_reason'];
	$payment_type = $_POST['payment_type'];
	$custom = $_POST['custom'];
	$invoice =$_POST['invoice'];
	
	// Buyer information
	$first_name=$_POST['first_name'];
	$last_name=$_POST['last_name'];
	$address_name=$_POST['address_name'];
	$address_country=$_POST['address_country'];
	$address_country_code=$_POST['address_country_code'];
	$address_zip=$_POST['address_zip'];
	$address_state=$_POST['address_state'];
	$address_city=$_POST['address_city'];
	$address_street=$_POST['address_street'];

	sender_mail('New payment was successfully', "New payment was successfully recieved from $payer_email", $email_notifications);

	$infoActivation = explode(":", $custom);
	$codeAd = $infoActivation[0];
	$codePlan = $infoActivation[1];
	
	$this->db1->query("INSERT INTO my_ads_paypal (whendate, item_name, item_number, quantity, payment_status, payment_amount, payment_currency,  payer_email, payment_type, custom, invoice, first_name, last_name, address_name, address_country, address_country_code, address_zip, address_state, address_city, address_street) VALUES ('".time()."','$item_name','$item_number',$quantity,'$payment_status', '$payment_amount', '$payment_currency', '$payer_email', '$payment_type', '$custom', '$invoice', '$first_name', '$last_name', '$address_name', '$address_country', '$address_country_code', '$address_zip', '$address_state', '$address_city', '$address_street')");

	$idRegPaypal = $this->db1->insert_id();
	$idAd = $this->db2->fetch_field("SELECT idad FROM my_ads WHERE code='".$codeAd."' LIMIT 1");
	$idPlan = $this->db2->fetch_field("SELECT idplan FROM my_ads_plans WHERE code='".$codePlan."' LIMIT 1");
	
	$this->db2->query("INSERT INTO my_ads_plans_assigned SET idad=".$idAd.", idplan=".$idPlan.", assignedby=2, idassigner=".$idRegPaypal.", whendate='".time()."'");	

	$r = $this->db2->query("SELECT * FROM my_ads_plans WHERE idplan=".$idPlan." LIMIT 1");
	
	if ($obj = $this->db2->fetch_object($r)) {
		$typeaction = $obj->typeaction;
		$quantity = $obj->quantity;
		if ($typeaction == 1) {
			$this->db2->query("UPDATE my_ads SET typeaction=1, last_assignedby=2, impressions_available=".$quantity.", clicks_available=0, num_impressions=0, num_impressions_unique=0, num_clicks=0, num_clicks_unique=0, available=1 WHERE idad=".$idAd);
		}
		if ($typeaction == 2) {
			$this->db2->query("UPDATE my_ads SET typeaction=2, last_assignedby=2, impressions_available=0, clicks_available=".$quantity.", num_impressions=0, num_impressions_unique=0, num_clicks=0, num_clicks_unique=0, available=1 WHERE idad=".$idAd);
		}
	}
 
  } else { 	
  	 //the transaction is invalid I can NOT charge the client. 
	 sender_mail('Invalid payment', "Invalid payment - $fullipn", $email_notifications);
}
?>