cancel
Showing results for 
Search instead for 
Did you mean: 

x_line_item integration in PHP

I don't know where I found the original sample code but this is what I am using to submit payaments and generate a detailed receipt for the customer.

 

    $customer                          = (object) array();
    $customer->first_name              = tools::get_field_value("bill_first_name");
    $customer->last_name               = tools::get_field_value("bill_last_name");
    $customer->address                 = tools::get_field_value("bill_address_1");
    $customer->city                    = tools::get_field_value("bill_city");
    $customer->state                   = $this->system->get_state_code(tools::get_field_value("bill_state_id"));
    $customer->zip                     = tools::get_field_value("bill_zip_code");
    $customer->phone                   = tools::get_field_value("phone_home");
    $customer->email                   = tools::get_field_value("email_address");
    $customer->customer_ip             = tools::get_client_ip();
    $shipping_info                     = (object) array();
    $shipping_info->ship_to_first_name = tools::get_field_value("ship_first_name");
    $shipping_info->ship_to_last_name  = tools::get_field_value("ship_last_name");
    $shipping_info->ship_to_address    = tools::get_field_value("ship_address_1");
    $shipping_info->ship_to_city       = tools::get_field_value("ship_city");
    $shipping_info->ship_to_state      = $this->system->get_state_code(tools::get_field_value("ship_state_id"));
    $shipping_info->ship_to_zip        = tools::get_field_value("ship_zip_code");
    $sale                              = new AuthorizeNetAIM;
    $sale->amount                      = tools::get_field_value("order_total");
    $sale->address                     = tools::get_field_value("bill_address_1");
    $sale->zip                         = tools::get_field_value("bill_zip_code");
    $sale->card_num                    = tools::get_field_value("cc_number");
    $sale->exp_date                    = tools::get_field_value("cc_exp_month") . "/" . tools::get_field_value("cc_exp_year");
    $sale->card_code                   = tools::get_field_value("cc_cvv2");
    $sale->description                 = $company_name_no_space . "com Sales Order: " . tools::get_field_value("order_id");
    $sale->setFields($shipping_info);
    $sale->setFields($customer);
    $response = $sale->authorizeAndCapture();

 

Everything works great, but I can't find any information on how to integrate line item detail.  The x_line_item information in the AIM PDF is useless for this implementation.  Any ideas how I should go about this?

 

Thanks,

 

vijer
Member
16 REPLIES 16

Um .. I understand that there is an interface and sample API files. I am mainly saying that you have to possibly filter your Item names and descriptions so they have the correct string length and no special characters. Thank you for your 2 cents though.

Well, that's one way to address the problem, but the most efficient way in my opinion is to avoid it entirely by using the API and letting it deal with any XML formatting issues. Why anyone would want to mess with XML when there's a nice clean API layer on top, I don't know.

can someone paste the addLineItem() function code? I see it in the markdown file but i can't find it in the class itself... What the heck is the function doing?

Took me about 5 seconds to find it in the AuthorizeNetAIM.php file in the lib folder:

 

    public function addLineItem($item_id, $item_name, $item_description, $item_quantity, $item_unit_price, $item_taxable)
    {
        $line_item = "";
        $delimiter = "";
        foreach (func_get_args() as $key => $value) {
            $line_item .= $delimiter . $value;
            $delimiter = "<|>";
        }
        $this->_additional_line_items[] = $line_item;
    }

 This is a rather inefficient way to say that you want to put together id, name, descriptioon, quantity, price, taxable, separated by <|>. Then those are in turn added to the post string as follows:

 

foreach ($this->_additional_line_items as $key => $value) {
    $this->_post_string .= "x_line_item=" . urlencode($value) . "&";
}

 

Use something like this:

 

	private function _authN_addLineItem(&$to,$item) {
	
		//wrapper for authNet's crappy addLineItem
		
		for ($i=0,$c=count($item);$i<$c;$i++) {
			$item[$i] = substr($item[$i],0,30);
		}
		
		if (empty($to->line_item)) $to->line_item = implode('<|>',$item);
		else {
			list ($item_id, $item_name, $item_desc, $item_quantity, $item_unit_price, $item_taxable) = $item;
			$to->addLineItem($item_id, $item_name, $item_desc, $item_quantity, $item_unit_price, $item_taxable);
		}
	
	}

 

It'll cover the dumb $auth->line_item format and strip your strings to 30 chars.

That empty() won't work. Do this instead:

 

private function _authN_addLineItem(&$to,$item) {

	//wrapper for authNet's crappy addLineItem
	
	if (!isset($this->_authN_addLineItem_hash)) $this->_authN_addLineItem_hash = array();

	for ($i=0,$c=count($item);$i<$c;$i++) {
		$item[$i] = substr($item[$i],0,30);
	}
	
	$id = spl_object_hash($to);
	if (empty($this->_authN_addLineItem_hash[$id])) {
		$to->line_item = implode('<|>',$item);
		$this->_authN_addLineItem_hash[$id] = true;
	} else {
		list ($item_id, $item_name, $item_desc, $item_quantity, $item_unit_price, $item_taxable) = $item;
		$to->addLineItem($item_id, $item_name, $item_desc, $item_quantity, $item_unit_price, $item_taxable);
	}

}

 

Thanks for posting the answer to this issue.  I was having the same problems and your post was just what I needed! Thanks again