API returned array item in an object. How to select?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
tunnelboy
New php-forum User
New php-forum User
Posts: 9
Joined: Wed Oct 21, 2015 5:15 pm

Wed Oct 21, 2015 5:31 pm

Ok, I don't yet get objects, but I'm slowly reading and learning. I am currently working on an API for Authorize.net using their PHP SDK.

Successful credit card response, no problem. Plenty of samples, works flawlessly. Declined card is another matter. Here is a snippet from their code.

Code: Select all

  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";
  }
It basically gets the response from the merchant. If ok, it spits out the Authorization code, and Transaction ID. That's fine. But you'll see if the card is declined or the wrong number of digits or whatever the reason, they just show "Invalid response". Of course their API does return the reason code, but I can't figure out how to display it. Here's a VAR_DUMP of the $tresponse object:

Code: Select all

object(net\authorize\api\contract\v1\TransactionResponseType)#204 (20) {
  ["responseCode":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(1) "3"
  ["rawResponseCode":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  NULL
  ["authCode":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(0) ""
  ["avsResultCode":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(1) "P"
  ["cvvResultCode":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(0) ""
  ["cavvResultCode":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(0) ""
  ["transId":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(1) "0"
  ["refTransID":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(0) ""
  ["transHash":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(32) "DD6B69DD8C11D36AB594C30268C38CC2"
  ["testRequest":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(1) "0"
  ["accountNumber":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(8) "XXXX3456"
  ["accountType":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  string(0) ""
  ["splitTenderId":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  NULL
  ["prePaidCard":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  NULL
  ["messages":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  NULL
  ["errors":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  array(1) {
    [0]=>
    object(net\authorize\api\contract\v1\TransactionResponseType\ErrorsAType\ErrorAType)#232 (2) {
      ["errorCode":"net\authorize\api\contract\v1\TransactionResponseType\ErrorsAType\ErrorAType":private]=>
      string(1) "6"
      ["errorText":"net\authorize\api\contract\v1\TransactionResponseType\ErrorsAType\ErrorAType":private]=>
      string(34) "The credit card number is invalid."
    }
  }
  ["splitTenderPayments":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  NULL
  ["userFields":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  NULL
  ["shipTo":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  NULL
  ["secureAcceptance":"net\authorize\api\contract\v1\TransactionResponseType":private]=>
  NULL
}
I want the errorText string in the array (errors). But how to get it? I tried about a dozen variations of :

echo "Credit Card Declined. Reason : " . $tresponse->geterrors[0]->errorText;

Nothing works. Any ideas? I have spent two nights and probably 4 hours on this! I have searched and read and just can't see what the problem is. Thanks!

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Wed Oct 21, 2015 6:01 pm

You've basically got the code right there. The first example you just listed. If you get a response code of 1 and $response isn't null it's valid. So put your code to retrieve and display the error text in the else

tunnelboy
New php-forum User
New php-forum User
Posts: 9
Joined: Wed Oct 21, 2015 5:15 pm

Wed Oct 21, 2015 8:07 pm

That's the problem. That's exactly what I did (see code below). Entering a made up card number (so it fails), it returns:

Credit Card Declined. Reason :

And that's it. $tresponse->geterrors[0]->errorText; is empty or I have the syntax wrong or something. You can see from the var_dump that there is a value in errorText.

Code: Select all

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 "<pre>";
 	var_dump($tresponse);
  	echo "</pre>";
  	echo "Credit Card Declined. Reason : " . $tresponse->geterrors[0]->errorText;
  }

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 315
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Thu Oct 22, 2015 9:50 am

I personally do this

Code: Select all

echo "<pre>" . print_r($tresponse, 1) . "</pre>\n";
instead of

Code: Select all

var_dump($tresponse);
IMO it makes it easier to debug, specially when dealing with objects.

tunnelboy
New php-forum User
New php-forum User
Posts: 9
Joined: Wed Oct 21, 2015 5:15 pm

Thu Oct 22, 2015 10:32 am

Thanks for that. I used both. That's why I had the <pre> tags left over in there.

Either way, I am still stumped on how to extract that "errorText" info. Here's a smaller output of the response (using print_r):
[transHash:net\authorize\api\contract\v1\TransactionResponseType:private] => DD6B69DD8C11D36AB594C30268C38CC2
[errors:net\authorize\api\contract\v1\TransactionResponseType:private] => Array
(
[0] => net\authorize\api\contract\v1\TransactionResponseType\ErrorsAType\ErrorAType Object
(
[errorCode:net\authorize\api\contract\v1\TransactionResponseType\ErrorsAType\ErrorAType:private] => 6
[errorText:net\authorize\api\contract\v1\TransactionResponseType\ErrorsAType\ErrorAType:private] => The credit card number is invalid.
)

)
[accountNumber:net\authorize\api\contract\v1\TransactionResponseType:private] => XXXX3456
echo "TH = ".$tresponse->gettransHash."<br />";
echo "Error Code = ".$tresponse->geterrors[0]->errorText."<br />"; # PROBLEM LINE
echo "AN = ".$tresponse->getaccountNumber."<br />";
Returns:

TH = DD6B69DD8C11D36AB594C30268C38CC2
Error Code =
AN = XXXX3456

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 315
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Thu Oct 22, 2015 1:30 pm

Well, just looking at it, it looks like private variables and you'll retrieve the using public methods to retrieve them. I guessing you didn't write this script?

$tresponse->geterrors[0]->errorText; is probably incorrect. Look for something like this

Code: Select all

public function getErrors() {
  return $this->getErrors; 
} 
It's probably not written exactly like that, but it's probably close enough to tell that's the function/method you should be looking for in order to access it.

tunnelboy
New php-forum User
New php-forum User
Posts: 9
Joined: Wed Oct 21, 2015 5:15 pm

Thu Oct 22, 2015 2:00 pm

Thanks so much for at least leading me in the right direction. I'm at the very beginning stages of learning objects and having a tough time understanding it.

No, this is taken from a very simple "Hello World" example at the merchant. http://developer.authorize.net/hello_world/

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Thu Oct 22, 2015 9:31 pm

For a quick crash course on PHP OOP check out Jesse Boyer's PHP OOP videos starting with : https://www.youtube.com/watch?v=hyXKjAs ... 1CD69ED950 Don't give up you can definitely do it.

Post Reply