cancel
Showing results for 
Search instead for 
Did you mean: 

Adding a new line item - SDK - PHP

I'm having a hard time adding a new line item using the SDK (PHP). An error gets generated.

 

 

 

<?php

require 'apps/authorizenet/sdk/vendor/autoload.php';

use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;

define("AUTHORIZENET_LOG_FILE","phplog");

// Common setup for API credentials
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName("********");
$merchantAuthentication->setTransactionKey("**************");
$refId = 'ref' . time();

// Create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber("4111111111111111");
$creditCard->setExpirationDate( "2038-12");
$creditCard->setCardCode("513");
$paymentCreditCard = new AnetAPI\PaymentType();
$paymentCreditCard->setCreditCard($creditCard);

// Create the Bill To info
$billto = new AnetAPI\CustomerAddressType();
$billto->setFirstName("Ellen");
$billto->setLastName("Johnson");
$billto->setCompany("Souveniropolis");
$billto->setAddress("14 Main Street");
$billto->setCity("Pecan Springs");
$billto->setState("TX");
$billto->setZip("44628");
$billto->setCountry("USA");
$billto->setPhoneNumber("310-867-5309");
//$billto->setEmail("chingone@wtf.com");

 

// LineItem - NOT WORKING
$lineItem = new AnetAPI\LineItemType();
$lineItem->setItemId("123");
$lineItem->setName("Donation");
$lineItem->setDescription("Ellen Johnson donated $50.00");
$lineItem_Array[] = $lineItem;

 

// Create a transaction
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");
$transactionRequestType->setAmount(187.66);
$transactionRequestType->setBillTo($billto);
$transactionRequestType->setPayment($paymentCreditCard);
$transactionRequestType->setLineItems($lineItem_Array);

$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId( $refId);
$request->setTransactionRequest($transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
//$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);

if ($response != null)
{
$tresponse = $response->getTransactionResponse();
if (($tresponse != null) && ($tresponse->getResponseCode()=="1"))
{
echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
echo "Charge Credit Card TRANS ID : " . $tresponse->getTransId() . "\n";
}
else
{
echo "Charge Credit Card ERROR : Invalid response :\n";
}
}
else
{
echo "Charge Credit Card Null response returned";
}
?>

 

 

Any help is appriciated.

Bandsaw000o0
Member
1 ACCEPTED SOLUTION

Accepted Solutions

My script worked as per Providencray's solution from the above comments, but just only with a single change:

 

//$lineItem->setUnitPrice("$50.00")

To

$lineItem->setUnitPrice(50.00); //Allowed only a decimal value upto 2 decimal points.

View solution in original post

3 REPLIES 3

Hello @Bandsaw000o0

 

May I suggest opening an issue in GitHub for our PHP Sample Code.  This will notify the developers working on this directly so they can address.

 

Richard

RichardH
Administrator Administrator
Administrator

Not sure if you figured it out but I also had the same issue today. I was able to figure it out by dumping the $lineItem_Array. If you look it is passing all this info

array(1) { [0]=> object(net\authorize\api\contract\v1\LineItemType)#8 (6) { ["itemId":"net\authorize\api\contract\v1\LineItemType":private]=> string(3) "123" ["name":"net\authorize\api\contract\v1\LineItemType":private]=> string(8) "Donation" ["description":"net\authorize\api\contract\v1\LineItemType":private]=> string(28) "Ellen Johnson donated $50.00" ["quantity":"net\authorize\api\contract\v1\LineItemType":private]=> NULL ["unitPrice":"net\authorize\api\contract\v1\LineItemType":private]=> NULL ["taxable":"net\authorize\api\contract\v1\LineItemType":private]=> NULL } }

If you set those variables it should will work. After making the following changes it processed the transaction.

$lineItem = new AnetAPI\LineItemType();
$lineItem->setItemId("123");
$lineItem->setName("Donation");
$lineItem->setDescription("Ellen Johnson donated $50.00");
$lineItem->setQuantity(1);
$lineItem->setUnitPrice("$50.00");
$lineItem->setTaxable(0); // 1 Yes 0 for no
$lineItem_Array[] = $lineItem;
providencray
Member

My script worked as per Providencray's solution from the above comments, but just only with a single change:

 

//$lineItem->setUnitPrice("$50.00")

To

$lineItem->setUnitPrice(50.00); //Allowed only a decimal value upto 2 decimal points.