<?php

// jCart v1.3
// http://conceptlogic.com/jcart/

// This file is called when any button on the checkout page (PayPal checkout, update, or empty) is clicked

$xml=simplexml_load_file("../config/config.xml");
include('../config/config.php');
// Include jcart before session start
include_once('jcart.php');

$config = $jcart->config;

// The update and empty buttons are displayed when javascript is disabled 
// Re-display the cart if the visitor has clicked either button
if (isset($_POST['jcartUpdateCart']) || isset($_POST['jcartEmpty'])) {

	// Update the cart
	if ($_POST['jcartUpdateCart']) {
		if ($jcart->update_cart() !== true)	{
			$_SESSION['quantityError'] = true;
		}
	}

	// Empty the cart
	if ($_POST['jcartEmpty']) {
		$jcart->empty_cart();
	}

	// Redirect back to the checkout page
	$protocol = 'http://';
	if (!empty($_SERVER['HTTPS'])) {
		$protocol = 'https://';
	}

	header('Location: ' . $protocol . $_SERVER['HTTP_HOST'] . $config['checkoutPath']);
	exit;
}

// The visitor has clicked the PayPal checkout button
else {

	require_once "../../../plugins/PagSeguroLibrary/PagSeguroLibrary.php";
	
    // Instantiate a new payment request
    $paymentRequest = new PagSeguroPaymentRequest();

    // Sets the currency
    $paymentRequest->setCurrency("BRL");
		
	foreach ($jcart->get_contents() as $item) {
		$paymentRequest->addItem($item['id'], $item['name'], 1, $item['price']);
	}
	
    $paymentRequest->setReference("NovoCurso");

	//$ddd = substr($_SESSION['telAluno'], 1, -10);
	//$tel = str_replace("-", "", substr($_SESSION['telAluno'], 4));    
    
    $paymentRequest->setSender(
	    'Pagamento aluno',
	    $_SESSION['emailAluno'],
	    '11',
	    '56273440',
	    'CPF',
	    $_SESSION['cpfAluno']
    );    
    	
    $paymentRequest->setRedirectUrl(SITE."notification.php");

    $paymentRequest->addParameter('notificationURL', SITE."notification.php");
		
	$jcart->empty_cart();

    $credentials = new PagSeguroAccountCredentials(PAGSEGURO, TOKEN); 
    
    $url = $paymentRequest->register($credentials);

	header("Location: $url");


}

?>