PHP Array Elements

83 Views Asked by At

I am trying to process a credit card (Bambora/Beanstream), and when there is an error, pick out the response code and message out of the Exception array. Here is my code:

try {

    $result = $beanstream->payments()->makeCardPayment($payment_data, TRUE);

} catch (\Beanstream\Exception $e) {

    //handle exception
    print_r($e);

}

The error exception output ($e) begins with:

Beanstream\ApiException Object ( [_message:protected] => Invalid Card Number [_code:protected] => 52 [message:protected] => Invalid Card Number [string:Exception:private] => [code:protected] => 52...

I am simply trying to store "message" and "code" into variables. Thank you.

2

There are 2 best solutions below

0
Ilkin Alibayli On

It looks like an object, not like an array.

Just try to get values from the object:

$message = $e->getMessage();
$code = $e->getCode();
0
Alexandre Tranchant On

When an error occured, an exception is thrown.

Exception are objects and their structures are explained in php documentation

try {
    $result = $beanstream->payments()->makeCardPayment($payment_data, TRUE);
} catch (\Beanstream\Exception $e) {
    //handle exception
    $message = $e->getMessage();
    $code = $e->getCode();
}