<?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.00';
if( stripslashes( strip_tags( $_GET[ 'hc' ] ) ) != '0.00' && !empty( $_GET[ 'hc' ] ) ) $shipinlandcost = stripslashes( strip_tags( $_GET[ 'hc' ] ) );

$authorize_mode = $aOptions[ 'authorize_mode' ];
$authorize_user = $aOptions[ 'authorize_user' ];
$authorize_secret_key = $aOptions[ 'authorize_secret_key' ];

$return_fromAN = get_bloginfo( 'url' );
$ping_ipn = get_bloginfo( 'template_directory' ) . '/gateways/authorize_ipn.php';

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

$custom_image_url = get_bloginfo( 'template_directory' ) . '/images/ppal_shoppr.png';
// Include the library
include_once ( TEMPLATEPATH . '/gateways/Authorize.php' );

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

// Specify your authorize.net login and secret
$myAuthorize->setUserInfo($authorize_user, $authorize_secret_key);

// Specify the url where authorize.net will send the user on success/failure
$myAuthorize->addField('x_receipt_link_method', 'LINK');
$myAuthorize->addField('x_receipt_link_text', __( "RETURN TO OUR WEBSHOP", "sofa_shoppr" ));
$myAuthorize->addField('x_receipt_link_URL', $return_fromAN);
$myAuthorize->addField('x_logo_url', $custom_image_url);
$myAuthorize->addField('x_email_customer', 1);
$myAuthorize->addField('x_method', 'CC');
$myAuthorize->addField('x_delim_data', 'FALSE');

// Specify the url where authorize.net will send the IPN
$myAuthorize->addField('x_Relay_URL', $ping_ipn);

// 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 ];
		
		$pre_calc_total = number_format( $item_price, 2, '.', ',' ) . ' x ' . $item_info_extra_array[ 1 ];
		$calc_total = (int)$item_price * (int)$item_info_extra_array[ 1 ];
		
		//$clean_single_quote = str_replace( "'", "", $p_o->post_title );
		$reduced_item_name = substr( stripslashes( strip_tags( $p_o->post_title ) ), 0, 31 );
		if( $item_size != '' && $item_color == '' ) {
			$van = intval( $name ) . '<|>' . $reduced_item_name . '<|>' . __( "Size", "sofa_shoppr" ) . ': ' . $item_size . '<|>' . $item_info_extra_array[ 1 ] . '<|>' . number_format( $item_price, 2, '.', ',' ) . '<|>Y';
			$myAuthorize->addField( 'x_line_item-' . $global_counter, $van );
		} else if( $item_size == '' && $item_color != '' ) {
			$van = intval( $name ) . '<|>' . $reduced_item_name . '<|>' . __( "Color", "sofa_shoppr" ) . ': ' . $item_color . '<|>' . $item_info_extra_array[ 1 ] . '<|>' . number_format( $item_price, 2, '.', ',' ) . '<|>Y';
			$myAuthorize->addField( 'x_line_item-' . $global_counter, $van );
		} else if( $item_size != '' && $item_color != '' ) {
			$van = intval( $name ) . '<|>' . $reduced_item_name . '<|>' . __( "Size", "sofa_shoppr" ) . ': ' . $item_size . ', ' . __( "Color", "sofa_shoppr" ) . ': ' . $item_color . '<|>' . $item_info_extra_array[ 1 ] . '<|>' . number_format( $item_price, 2, '.', ',' ) . '<|>Y';
			$myAuthorize->addField( 'x_line_item-' . $global_counter, $van );
		} else {
			$van = intval( $name ) . '<|>' . $reduced_item_name . '<|>x<|>' . $item_info_extra_array[ 1 ] . '<|>' . number_format( $item_price, 2, '.', ',' ) . '<|>Y';
			$myAuthorize->addField( 'x_line_item-' . $global_counter, $van );
		}
	
		$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;
		$myAuthorize->addField( 'x_tax', number_format( $bare_tax, 2, '.', ',' ) );
		$items_sum = $items_sum + $bare_tax;
	}
	if( $shipinlandcost > 0 ) {
		$myAuthorize->addField( 'x_duty', number_format( $shipinlandcost, 2, '.', ',' ) );
		$items_sum = $items_sum + $shipinlandcost;
	}
	
	$myAuthorize->addField( 'custom_internal_code', $ppal_custom );
	
}


$myAuthorize->addField('x_Description', $description_string);
$myAuthorize->addField('x_Invoice_num', $invoice_rnd_num);
$myAuthorize->addField('x_Cust_ID', $customer_rnd_num);
$myAuthorize->addField('x_Amount', number_format( $items_sum, 2, '.', ',' ));

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

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