Reply
Highlighted
Member
Posts: 7
Registered: ‎12-19-2017
Accepted Solution

Add Shipping Address to API

I am using  the SDK to charge a credit card. I can't find a php example that shows adding the shipping address. So I added the $customerShippingAddress object to the below code along with $transactionRequestType->setShipTo($customerShipppingAddress) inside another object. This does not work. It gives me error:

 

PHP Catchable fatal error: Argument 1 passed to net\authorize\api\contract\v1\TransactionRequestType::setShipTo() must be an instance of net\authorize\api\contract\v1\NameAndAddressType, null given, called in /public_html/subdir/store/authnetOrig.php on line 64 and defined in /public_html/subdir/store/vendor/authorizenet/authorizenet/lib/net/authorize/api/contract/v1/TransactionRequestType.php on line 664

 

Can someone tell me how to add a shipping address? This is the code I'm using:

 

require_once "vendor/autoload.php";
//require_once "constants.php";
define('ANET_LOGIN_ID', '#########');
define('ANET_TRANSACTION_KEY', '################');
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
function chargeCreditCard($arr_data = []) {
extract($arr_data);
/* Create a merchantAuthenticationType object with authentication details
retrieved from the constants file */
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(ANET_LOGIN_ID);
$merchantAuthentication->setTransactionKey(ANET_TRANSACTION_KEY);
// Set the transaction's refId
$refId = 'ref' . time();
// Create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber($card_number);
$creditCard->setExpirationDate($exp_date);
$creditCard->setCardCode($card_code);
// Add the payment data to a paymentType object
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);
// Create order information
$order = new AnetAPI\OrderType();
$order->setInvoiceNumber(mt_rand(10000, 99999)); //generate random invoice number
$order->setDescription($product);
// Set the customer's Bill To address
$customerAddress = new AnetAPI\CustomerAddressType();
$customerAddress->setFirstName($first_name);
$customerAddress->setLastName($last_name);
//$customerAddress->setCompany("Souveniropolis");
$customerAddress->setAddress($address);
$customerAddress->setCity($city);
$customerAddress->setState($state);
$customerAddress->setZip($zip);
$customerAddress->setCountry($country);

// Create a customer shipping address This is the object that I added
$customerShippingAddress = new AnetAPI\CustomerAddressType();
$customerShippingAddress->setFirstName("James");
$customerShippingAddress->setLastName("White");
//$customerShippingAddress->setCompany("Addresses R Us");
$customerShippingAddress->setAddress(rand() . " North Spring Street");
$customerShippingAddress->setCity("Toms River");
$customerShippingAddress->setState("NJ");
$customerShippingAddress->setZip("08753");
$customerShippingAddress->setCountry("USA");

 

// Set the customer's identifying information
$customerData = new AnetAPI\CustomerDataType();
$customerData->setType("individual");
$customerData->setId(mt_rand(10000, 99999)); //try to set unique id here
$customerData->setEmail($email);
// Create a TransactionRequestType object and add the previous objects to it
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");
$transactionRequestType->setAmount($amount);
$transactionRequestType->setOrder($order);
$transactionRequestType->setPayment($paymentOne);
$transactionRequestType->setBillTo($customerAddress);


The following is line 64 as referenced in the error message:
$transactionRequestType->setShipTo($customerShipppingAddress);

$transactionRequestType->setCustomer($customerData);
// Assemble the complete transaction request
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setTransactionRequest($transactionRequestType);
// Create the controller and get the response
$controller = new AnetController\CreateTransactionController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); //Use SANDBOX or PRODUCTION
if ($response != null) {
// Check to see if the API request was successfully received and acted upon
if ($response->getMessages()->getResultCode() == 'Ok') {
// Since the API request was successful, look for a transaction response
// and parse it to display the results of authorizing the card
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null) {
echo " Successfully created transaction with Transaction ID: " . $tresponse->getTransId() . "\n";
echo " Transaction Response Code: " . $tresponse->getResponseCode() . "\n";
echo " Message Code: " . $tresponse->getMessages()[0]->getCode() . "\n";
echo " Auth Code: " . $tresponse->getAuthCode() . "\n";
echo " Description: " . $tresponse->getMessages()[0]->getDescription() . "\n";
} else {
echo "Transaction Failed \n";
if ($tresponse->getErrors() != null) {
echo " Error Code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
echo " Error Message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
}
}
// Or, print errors if the API request wasn't successful
} else {
echo "Transaction Failed \n";
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getErrors() != null) {
echo " Error Code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
echo " Error Message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
} else {
echo " Error Code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
echo " Error Message : " . $response->getMessages()->getMessage()[0]->getText() . "\n";
}
}
} else {
echo "No response returned \n";
}
return $response;
}
$arr_user_info = [
'card_number' => '4111111111111111',
'exp_date' => '2020-12',
'card_code' => '123',
'product' => 'Test Plugin',
'first_name' => 'Sam',
'last_name' => 'Jose',
'address' => '101 main street',
'city' => 'Pecan Springs',
'state' => 'TX',
'zip' => '44628',
'country' => 'USA',
'email' => 'sam@test.com',
'amount' => 15,
];
$response = chargeCreditCard($arr_user_info);


Accepted Solutions
Solution
Accepted by topic author dean
‎12-20-2017 04:25 PM
Authorize.Net Expert
Posts: 75
Registered: ‎07-27-2016

Re: Add Shipping Address to API

Hi @dean

 

The argument passed to setShipTo() must be of type CustomerAddressType.

 

There is a small typo in your code and as a result you are getting this error.

You have defined the new object $customerShippingAddress of correct type but you are not sending the same object to setShipTo(), instead you are sending customerShipppingAddress (with an extra p).

View solution in original post


All Replies
Solution
Accepted by topic author dean
‎12-20-2017 04:25 PM
Authorize.Net Expert
Posts: 75
Registered: ‎07-27-2016

Re: Add Shipping Address to API

Hi @dean

 

The argument passed to setShipTo() must be of type CustomerAddressType.

 

There is a small typo in your code and as a result you are getting this error.

You have defined the new object $customerShippingAddress of correct type but you are not sending the same object to setShipTo(), instead you are sending customerShipppingAddress (with an extra p).

Member
Posts: 7
Registered: ‎12-19-2017

Re: Add Shipping Address to API

Thank you!......that was the problem.