Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble working with Square Connect ChargeResponse object

I'm able to successfully charge using the transaction API following the example on github. Executing the charge looks like this:

$result = $transaction_api->charge($access_token, $location_id, $request_body);
echo "<pre>";
print_r($result);
echo "</pre>";

Here's the output:

SquareConnect\Model\ChargeResponse Object
(
    [errors:protected] => 
    [transaction:protected] => SquareConnect\Model\Transaction Object
        (
            [id:protected] => REMOVED FROM POST
            [location_id:protected] => REMOVED FROM POST
            [created_at:protected] => 2016-04-30T23:42:33Z
            [tenders:protected] => Array
                (
                    [0] => SquareConnect\Model\Tender Object
                        (
                            [id:protected] => REMOVED FROM POST
                            [location_id:protected] => REMOVED FROM POST
                            [transaction_id:protected] => 02d1d965-51fd-5023-68f5-0fcd148a263b
                            [created_at:protected] => 2016-04-30T23:42:33Z
                            [note:protected] => Online Transaction
                            [amount_money:protected] => SquareConnect\Model\Money Object
                                (
                                    [amount:protected] => 6000
                                    [currency:protected] => USD
                                )

                            [processing_fee_money:protected] => 
                            [customer_id:protected] => 
                            [type:protected] => CARD
                            [card_details:protected] => SquareConnect\Model\TenderCardDetails Object
                                (
                                    [status:protected] => CAPTURED
                                    [card:protected] => SquareConnect\Model\Card Object
                                        (
                                            [id:protected] => 
                                            [card_brand:protected] => VISA
                                            [last_4:protected] => 5858
                                            [exp_month:protected] => 
                                            [exp_year:protected] => 
                                            [cardholder_name:protected] => 
                                            [billing_address:protected] => 
                                        )

                                    [entry_method:protected] => KEYED
                                )

                            [cash_details:protected] => 
                        )

                )

            [refunds:protected] => 
            [reference_id:protected] => 
            [product:protected] => EXTERNAL_API
        )

)

My problem is that, while some places (such as here) indicate that I'm supposed to get an array back from the charge method, I instead get a ChargeResponse object.

Within this object is a transaction object that contains all of the relevant information that I want to display to the customer once the transaction is complete, but it's protected, so trying to echo a transaction id, created_at time, or amount from this returned object fails.

I'm certain I'm doing something wrong, but I'm lost as to how to capture properties from the ChargeResponse object so that I can do useful things with it.

For instance, I've tried

echo($result->transaction['id']);

but all I get is:

Fatal error: Cannot access protected property

This may not even be the right way to attempt something like this, so I'm completely open to suggestions.

like image 398
LoneWaffle Avatar asked Dec 07 '22 22:12

LoneWaffle


1 Answers

I managed to figure out that one must use the getTransaction method that's included in the object to get a usable form of the properties.

$transaction = $result->getTransaction();

Then you can just get properties out that you want:

$transactionID = $transaction["tenders"][0]["transaction_id"];

I'm rather annoyed that I didn't come across this anywhere in the documentation (in fact a google search of the entire docs.connect.squareup.com doesn't turn up a single reference to getTransaction). I had to stumble upon it when I was trying to reparse the original ChargeResponse object into an array using some other hack job.

Anyway, glad this is resolved. Wanted to leave this here for others.

like image 196
LoneWaffle Avatar answered Dec 10 '22 11:12

LoneWaffle