<?php
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];

// required for making tables easier
require_once( $this_file . 'wp-config.php' );

$aOptions = SofaShoppr::initOptions( false );
$chckoutpage = $aOptions[ 'chckoutpage' ];
$entrycurrency = $aOptions[ 'entrycurrency' ];
$taxpercentage = $aOptions[ 'taxpercentage' ];
if( $taxpercentage < 0 ) $taxpercentage = 0;
$shipinlandcost = 0;
if( stripslashes( strip_tags( $_GET[ 'hc' ] ) ) != '0.00' && !empty( $_GET[ 'hc' ] ) ) $shipinlandcost = stripslashes( strip_tags( $_GET[ 'hc' ] ) );

$paypal_mode = $aOptions[ 'paypal_mode' ];
$ppal = $aOptions[ 'ppal' ];

$return_fromPP = get_bloginfo( 'url' ) . '/?dc=pp';
$cancel_return_fromPP = get_permalink( (int)$chckoutpage );
$ping_ipn = get_bloginfo( 'template_directory' ) . '/gateways/paypal_ipn.php';

$custom_image_url = get_bloginfo( 'template_directory' ) . '/images/ppal_shoppr.png';
$back_button = __( "Back to ", "sofa_shoppr" ) . get_bloginfo( 'name' );
?>

<?php
// Include the paypal library
include_once ( TEMPLATEPATH . '/gateways/Paypal.php' );

// Create an instance of the paypal library
$myPaypal = new Paypal();

$myPaypal->addField( 'business', $ppal );
$myPaypal->addField( 'currency_code', $entrycurrency );
$myPaypal->addField( 'return', $return_fromPP );
$myPaypal->addField( 'cancel_return', $cancel_return_fromPP );
$myPaypal->addField( 'notify_url', $ping_ipn );
$myPaypal->addField( 'image_url', $custom_image_url );
$myPaypal->addField( 'cbt', $back_button );

// Specify the product information
if( isset( $_COOKIE[ 'sofashoppr' ] ) ) {
	
	$global_counter = 1;
	$items_sum = 0;
	$ppal_custom = '';
	
	foreach( $_COOKIE[ 'sofashoppr' ] as $name => $value ) {
		
		$ppal_custom .= intval( $name ) . ':' . $value . ','; // custom shit
		
		$item_info_extra_array = explode( ':', $value );
		$item_size = '';
		$item_color = '';
		
		if( $item_info_extra_array[ 2 ] != NULL ) $item_size  = $item_info_extra_array[ 2 ];
		if( $item_info_extra_array[ 3 ] != NULL ) $item_color = $item_info_extra_array[ 3 ];
		
		$p_o = get_post( intval( $name ) );
		$custom_fields = get_post_custom( intval( $name ) );
		$item_price = ( $item_info_extra_array[ 4 ] != NULL ) ? $item_info_extra_array[ 4 ] : $custom_fields[ 'price' ][ 0 ];
		
		if( $item_size != '' && $item_color == '' ) {
			$v = stripslashes( strip_tags( $p_o->post_title ) ) . ' | ' . __( "Size", "sofa_shoppr" ) . ': ' . $item_size;
			$myPaypal->addField( 'item_name_' . $global_counter, $v );
		} else if( $item_size == '' && $item_color != '' ) {
			$v = stripslashes( strip_tags( $p_o->post_title ) ) . ' | ' . __( "Color", "sofa_shoppr" ) . ': ' . $item_color;
			$myPaypal->addField( 'item_name_' . $global_counter, $v );
		} else if( $item_size != '' && $item_color != '' ) {
			$v = stripslashes( strip_tags( $p_o->post_title ) ) . ' | ' . __( "Size", "sofa_shoppr" ) . ': ' . $item_size . ' | ' . __( "Color", "sofa_shoppr" ) . ': ' . $item_color;
			$myPaypal->addField( 'item_name_' . $global_counter, $v );
		} else {
			$v = stripslashes( strip_tags( $p_o->post_title ) );
			$myPaypal->addField( 'item_name_' . $global_counter, $v );	
		}
		$myPaypal->addField( 'item_number_' . $global_counter, intval( $name ) );	
		$myPaypal->addField( 'quantity_' . $global_counter, $item_info_extra_array[ 1 ] );
		$myPaypal->addField( 'amount_' . $global_counter, number_format( $item_price, 2, '.', ',' ) );
	
		$items_sum = $items_sum + ( $item_price * $item_info_extra_array[ 1 ] );
		
		$global_counter ++;
		
	}
	
	$myPaypal->addField( 'custom', $ppal_custom );
	
	// tax and shipping
	if( $taxpercentage > 0 ) {
		$bare_tax = ( $items_sum * $taxpercentage ) / 100;
		$myPaypal->addField( 'tax_cart', number_format( $bare_tax, 2, '.', ',' ) );
		$items_sum = $items_sum + $bare_tax;
	}
	if( $shipinlandcost > 0 ) {
		$myPaypal->addField( 'handling_cart', number_format( $shipinlandcost, 2, '.', ',' ) );
		$items_sum = $items_sum + $shipinlandcost;
	}
	
}

// Enable test mode if needed
if( $paypal_mode == 'sandbox' ) $myPaypal->enableTestMode();

// Let's start the train!
$myPaypal->submitPayment();