Jump to content

Rewriting an array


mallen

Recommended Posts

I am working with a API and need to change the format of the data I send. I need this

 

// Line Items
$i = 1;
foreach($Order->Cart->contents as $Item) {
$_['x_line_item'][] = ($i++)."<|>".substr($Item->name,0,31)."<|>".((sizeof($Item->options) > 1)?"     (".substr($Item->option->label,0,253).")":"")."<|>".(int)$Item->quantity."<|>".number_format($Item->unitprice,$this->precision,'.','')."<|>".(($Item->tax)?"Y":"N");
		}

 Converted to this:

                 'lineItems' => array(
                    0 => array(
                        'itemId' => '1',
                        'name' => $Item->name,
                        'quantity' => $Item->quantity,
                        'unitPrice' => $Item->unitprice
                    ),
                    1 => array(
                        'itemId' => '2',
                        'name' => $Item->name,
                        'quantity' => $Item->quantity,
                        'unitPrice' => $Item->unitprice
                    )

I tried it like this but I don't have it correct:

                $i = 1;
		foreach($Order->Cart->contents as $Item) {
			'lineItems' = ($i++).

			"<|>".substr($Item->name,0,31).
			
			"<|>".(int)$Item->quantity.
			"<|>".number_format($Item->unitPrice,$this->precision,'.','');
			}

 

Link to comment
Share on other sites

It needs to look like this when its sent.

 

 $xml = new AuthnetXML(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetXML::USE_DEVELOPMENT_SERVER);
    $xml->createCustomerProfileTransactionRequest(array(
        'transaction' => array(
            'profileTransAuthCapture' => array(
                'amount' => '10.95',
                'tax' => array(
                    'amount' => '1.00',
                    'name' => 'state sales tax',
                    'description' => 'your state sales tax'
                ),
                'shipping' => array(
                    'amount' => '2.00',
                    'name' => 'ground based shipping',
                    'description' => 'Ground based 5 to 10 day shipping'
                ),
                'lineItems' => array(  ////////this is the part I need from here
                    0 => array(
                        'itemId' => '1',
                        'name' => 'widget',
                        'quantity' => '5',
                        'unitPrice' => '10.00'
                    ),
                    1 => array(
                        'itemId' => '2',
                        'name' => 'another widget',
                        'quantity' => '20',
                        'unitPrice' => '10.00'
                    )
                ), /////////////////to here
                'customerProfileId' => '345',
                'customerPaymentProfileId' => '675',
                'customerShippingAddressId' => '453',
                'order' => array(
                    'invoiceNumber' => 'INV000001',
                    'description' => 'description of transaction',
                    'purchaseOrderNumber' => '000001'
                ),
                'taxExempt' => 'false',
                'recurringBilling' => 'false',
                'cardCode' => '000'
            )
        ),
        'extraOptions' => '<![CDATA[x_customer_ip=100.0.0.1]]>'
    ));
Edited by mallen
Link to comment
Share on other sites

Like this? I need the commas and closing brackets in the correct place.

 $xml = new AuthnetXML(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetXML::USE_DEVELOPMENT_SERVER);
    $xml->createCustomerProfileTransactionRequest(array(
        'transaction' => array(
            'profileTransAuthCapture' => array(
                'amount' => '10.95',
                'tax' => array(
                    'amount' => '1.00',
                    'name' => 'state sales tax',
                    'description' => 'your state sales tax'
                ),
                'shipping' => array(
                    'amount' => '2.00',
                    'name' => 'ground based shipping',
                    'description' => 'Ground based 5 to 10 day shipping'
                ),
                 
                $i = 1;
		foreach($Order->Cart->contents as $Item) {
    		$lineItems[] = array (
       		'itemId' => $i++,
        	'name' => $Item->name,
        	'quantity' => $Item->quantity,
        	'unitPrice' => $Item->unitprice
    );
}  
		$whatYouWant = 'lineItems = ' . var_export($lineItems, 1);
                'customerProfileId' => '345',
                'customerPaymentProfileId' => '675',
                'customerShippingAddressId' => '453',
                'order' => array(
                    'invoiceNumber' => 'INV000001',
                    'description' => 'description of transaction',
                    'purchaseOrderNumber' => '000001'
                ),
                'taxExempt' => 'false',
                'recurringBilling' => 'false',
                'cardCode' => '000'
            )
        ),
        'extraOptions' => '<![CDATA[x_customer_ip=100.0.0.1]]>'
    ));


Link to comment
Share on other sites

  $i = 1;
		foreach($Order->Cart->contents as $Item) {
    		$lineItems[] = array (
       		'itemId' => $i++,
        	'name' => $Item->name,
        	'quantity' => $Item->quantity,
        	'unitPrice' => $Item->unitprice
    );
} 
 $whatIWant = 'lineItems = ' . var_export($lineItems, 1)
 
 $xml = new AuthnetXML(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetXML::USE_DEVELOPMENT_SERVER);
    $xml->createCustomerProfileTransactionRequest(array(
        'transaction' => array(
            'profileTransAuthCapture' => array(
                'amount' => '10.95',
                'tax' => array(
                    'amount' => '1.00',
                    'name' => 'state sales tax',
                    'description' => 'your state sales tax'
                ),
                'shipping' => array(
                    'amount' => '2.00',
                    'name' => 'ground based shipping',
                    'description' => 'Ground based 5 to 10 day shipping'
                ),
                 
            	echo $whatIWant;
                 ),

                'customerProfileId' => '345',
                'customerPaymentProfileId' => '675',
                'customerShippingAddressId' => '453',
                'order' => array(
                    'invoiceNumber' => 'INV000001',
                    'description' => 'description of transaction',
                    'purchaseOrderNumber' => '000001'
                ),
                'taxExempt' => 'false',
                'recurringBilling' => 'false',
                'cardCode' => '000'
            )
        ),
        'extraOptions' => '<![CDATA[x_customer_ip=100.0.0.1]]>'
    ));


Like this?

Link to comment
Share on other sites

I edited this line adding  \   ran and echoed the results outside of the main script and it looked correct. So that's a start.

 $whatIWant = '\'lineItems\' = ' . var_export($lineItems, 1) ;

I am getting an error when I process the script saying text is not allowed. It seems to not like the echo$whatIWant inthe middle of the script.

Edited by mallen
Link to comment
Share on other sites


<?xml version="1.0"?>
<createCustomerProfileTransactionRequest>
<merchantAuthentication>
<name>xxx</name>
<transactionKey>xxx</transactionKey>
</merchantAuthentication>
<transaction>'lineItems' = array (
0 =>
array (
'itemId' => 1,
'name' => 'Some Widget',
'quantity' => '1',
'unitPrice' => 1,
),
)</transaction>


The error is : The element 'transaction' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' cannot contain text.

 

Link to comment
Share on other sites


<?xml version="1.0"?>
<createCustomerProfileTransactionRequest>
<merchantAuthentication>
<name>xxx</name>
<transactionKey>xxx</transactionKey>
</merchantAuthentication>
<transaction>'lineItems' = array (
0 =>
array (
'itemId' => 1,
'name' => 'Some Widget',
'quantity' => '1',
'unitPrice' => 1,
),
)</transaction>


The error is : The element 'transaction' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' cannot contain text.

 

Link to comment
Share on other sites

I would do as Barrand suggested yet add the lineItems key to it.

 

 

 

$i = 1;
foreach($Order->Cart->contents as $Item) {
    $lineItems['lineItems'][] = array (
   'itemId' => $i++,
   'name' => $Item->name,
   'quantity' => $Item->quantity,
   'unitPrice' => $Item->unitprice
   );
}

$whatYouWant = var_export($lineItems, 1);
 
Edited by Zane
Link to comment
Share on other sites

Sorry maybe my previous post was not clear. Its returning the XML and showing that line items. Its just the formatting of the > that I need fixed.

 

<?xml version="1.0"?>
<createCustomerProfileTransactionRequest>
  <merchantAuthentication>
    <name>xxx</name>
    <transactionKey>xxx</transactionKey>
  </merchantAuthentication>
  <transaction>'lineItems' = array (
  0 => 
  array (
    'itemId' => 1,
    'name' => 'Some Widget',
    'quantity' => '1',
    'unitPrice' => 1,
  ),
)</transaction>

Should be returned as:

<?xml version="1.0"?>
<createCustomerProfileTransactionRequest>
  <merchantAuthentication>
    <name>xxx</name>
    <transactionKey>xxx</transactionKey>
  </merchantAuthentication>
  <transaction>'lineItems' = array (
  0 => array (
    'itemId' => 1,
    'name' => 'Some Widget',
    'quantity' => '1',
    'unitPrice' => 1,
  ),
)</transaction>
Edited by mallen
Link to comment
Share on other sites

Yes it wants it just like this. Sorry had a couple typos. See post #3 for what it should look like for the items. I have used a hard coded version before and have successfully submitted the XML.

 <transaction>'lineItems' = array (
  0 => array (
    'itemId' => '1',
    'name' => 'Some Widget',
    'quantity' => '1',
    'unitPrice' => '1',
  ),

Edited by mallen
Link to comment
Share on other sites

I tried something like this

 

    $i = 1;
    foreach($Order->Cart->contents as $Item) {
        $lineItems[] = array (
            'lineItem' => array(
                'itemId' => $i++,
                'name' => $Item->name,
                'quantity' => $Item->quantity,
                'unitPrice' => number_format($Item->unitprice,$this->precision,'.','')//format to 1.00
             )
        );
    }
     
    $xml->createCustomerProfileTransactionRequest(array(
            'transaction' => array(
                'profileTransAuthCapture' => array(
                    'amount' => $Order->Cart->Totals->total
               ),
           'lineItems' => $lineItems, //changed this line
           'customerProfileId' => '135',
           'customerPaymentProfileId' => '186',
           'customerShippingAddressId' => '120',
           'order' => array(
               'invoiceNumber' => 'IN8',
               'description' => 'description of transaction',
               'purchaseOrderNumber' => 'PO4'
           ),
           'taxExempt' => 'false',
           'recurringBilling' => 'false',
           'cardCode' => '000'
       )
    ); 

But it outputs this. There should only be one opening <lineItems>  and one closing </lineItems>  I am getting cose.

 

<lineItems>
        <lineItem>
          <itemId>1</itemId>
          <name>widget name</name>
          <quantity>3</quantity>
          <unitPrice>5.00</unitPrice>
        </lineItem>
      </lineItems>
      <lineItems>
        <lineItem>
          <itemId>2</itemId>
          <name>widget 2 name</name>
          <quantity>4</quantity>
          <unitPrice>6.00</unitPrice>
        </lineItem>
      </lineItems>  
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.