Error handling when creating payment

In PHP; I’m using this line to charge customer:
$api_response = $client->getPaymentsApi()->createPayment($body);

To get the error message (in proper format so I can save it) I use:

$errors = print_r($api_response->getErrors(), true);
$errors = preg_replace(’/[^a-z]/i’,’’,$errors);

As results, in one case I received this error message:

ArraySquareModelsErrorObjectcategorySquareModelsErrorprivatePAYMENTMETHODERRORcodeSquareModelsErrorprivateTRANSACTIONLIMITdetailSquareModelsErrorprivateAuthorizationerrorTRANSACTIONLIMITfieldSquareModelsErrorprivate

My first question is, what does this error message mean?

My second question is, I need to get one no special character error message & one sentence so I can display this to customer. How do I get this in PHP?
Instead of me having to troubleshoot each time I just want to be able to display directly to customer what the error is but worried the message might have a quote or double quote and mess something up :S
I don’t want an array result, just one sentence error message in response to display to customer

Any help? :confused:
Thank you.

:wave: That error you provided should also have detail which will have the details of why the error was thrown. For example the error you got means:

The card issuer has determined the payment amount is either too high or too low. The API returns the error code mostly for credit cards (for example, the card reached the credit limit). However, sometimes the issuer bank can indicate the error for debit or prepaid cards (for example, card has insufficient funds).

You can display the detail of the error messages to your customer when payments fails so they can make any adjustments as needed for a successful payment. For more please see Error Handling. :slightly_smiling_face:

I understand.
The issue is how to extract the message in PHP I can’t figure out.
Will this code work? $errors = print_r($api_response->getErrors(), true) ?
I mean will this give me a message (not array) of the error message?

Thanks.

I understand.
The issue is how to extract the message in PHP I can’t figure out.
Will this code work? $errors = print_r($api_response->getErrors(), true) ?
I mean will this give me a message (not array) of the error message?

Thanks.