<?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 = stripslashes( strip_tags( $_POST[ 'co_shipping_cost' ] ) );

$checkout_mode = $aOptions[ 'checkout_mode' ];
$checkout_vendor = $aOptions[ 'checkout_vendor' ];

$return_fromCO = get_bloginfo( 'url' );
$ping_ipn = get_bloginfo( 'template_directory' ) . '/gateways/2co_ipn.php';

$curr_timestamp = time();
$invoice_rnd_num = __( "Invoice", "sofa_shoppr" ) . '-' . $curr_timestamp;
$description_string = __( "Webshop", "sofa_shoppr" ) . ' ' . get_bloginfo( 'name' );

// Include the paypal library
include_once ( TEMPLATEPATH . '/gateways/TwoCo.php' );

// Create an instance of the authorize.net library
$my2CO = new TwoCo();

// Specify your 2CheckOut vendor id
$my2CO->addField( 'sid', $checkout_vendor );

// Specify the order information
$my2CO->addField( 'cart_order_id', $invoice_rnd_num );

// 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 ];
		
		$my2CO->addField( 'c_tangible_' . $global_counter, 'Y' );
		$my2CO->addField( 'c_name_' . $global_counter, substr( stripslashes( strip_tags( $p_o->post_title ) ), 0, 128 ) );
		
		if( $item_size != '' && $item_color == '' ) {
			$v = __( "Size", "sofa_shoppr" ) . ': ' . $item_size . ' | ' . __( "Price", "sofa_shoppr" ) . ': ' . number_format( $item_price, 2, '.', ',' );
			$my2CO->addField( 'c_description_' . $global_counter, $v );
		} else if( $item_size == '' && $item_color != '' ) {
			$v = __( "Color", "sofa_shoppr" ) . ': ' . $item_color . ' | ' . __( "Price", "sofa_shoppr" ) . ': ' . number_format( $item_price, 2, '.', ',' );
			$my2CO->addField( 'c_description_' . $global_counter, $v );
		} else if( $item_size != '' && $item_color != '' ) {
			$v = __( "Size", "sofa_shoppr" ) . ': ' . $item_size . ' | ' . __( "Color", "sofa_shoppr" ) . ': ' . $item_color . ' | ' . __( "Price", "sofa_shoppr" ) . ': ' . number_format( $item_price, 2, '.', ',' );
			$my2CO->addField( 'c_description_' . $global_counter, $v );
		} else {
			$v = __( "Price", "sofa_shoppr" ) . ': ' . number_format( $item_price, 2, '.', ',' );
			$my2CO->addField( 'c_description_' . $global_counter, $v );	
		}
		$my2CO->addField( 'c_prod_' . $global_counter, intval( $name ) . ',' . $item_info_extra_array[ 1 ] );	
		$my2CO->addField( 'c_price_' . $global_counter, number_format( $item_price, 2, '.', ',' ) );
	
		$items_sum = $items_sum + ( $item_price * $item_info_extra_array[ 1 ] );
		
		$global_counter ++;
		
	}
	
	// tax and shipping
	if( $taxpercentage > 0 ) {
		$bare_tax = ( $items_sum * $taxpercentage ) / 100;
		$my2CO->addField( 'tax', number_format( $bare_tax, 2, '.', ',' ) );
		$items_sum = $items_sum + $bare_tax;
	}
	if( $shipinlandcost > 0 ) {
		$my2CO->addField( 'sh_cost', number_format( $shipinlandcost, 2, '.', ',' ) );
		$items_sum = $items_sum + $shipinlandcost;
	}
	
	$my2CO->addField( 'total', number_format( $items_sum, 2, '.', '' ) );
	
}

// Specify the url where 2CO will send the IPN
$my2CO->addField( 'id_type', 1 );
$my2CO->addField( 'order_number', $curr_timestamp );
$my2CO->addField( 'pay_method', 'CC' );
$my2CO->addField( 'skip_landing', 0 );
$my2CO->addField( 'merchant_order_id', $curr_timestamp );
$my2CO->addField( 'x_Receipt_Link_URL', $ping_ipn );
$my2CO->addField( 'tco_currency', $entrycurrency );
$my2CO->addField( 'custom', $ppal_custom );

// pickup shipping info
$ship_name = stripslashes( strip_tags( $_POST[ 'ship_name' ] ) );
$ship_street_address = stripslashes( strip_tags( $_POST[ 'ship_street_address' ] ) );
$ship_state = stripslashes( strip_tags( $_POST[ 'ship_state' ] ) );
$ship_city = stripslashes( strip_tags( $_POST[ 'ship_city' ] ) );
$ship_zip = stripslashes( strip_tags( $_POST[ 'ship_zip' ] ) );
$ship_country = stripslashes( strip_tags( $_POST[ 'ship_country' ] ) );
$my2CO->addField( 'ship_name', $ship_name );
$my2CO->addField( 'ship_street_address', $ship_street_address );
$my2CO->addField( 'ship_state', $ship_state );
$my2CO->addField( 'ship_city', $ship_city );
$my2CO->addField( 'ship_zip', $ship_zip );
$my2CO->addField( 'ship_country', $ship_country );

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

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