LoneWaffle
LoneWaffle

Reputation: 131

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.

Upvotes: 4

Views: 1507

Answers (6)

Jitendra
Jitendra

Reputation: 9

$result = $payments_api->createPayment($request_body);

print_r($result->getPayment()->getStatus());

print_r( $result->getPayment()->getId() );

print_r( $result->getPayment()->getReferenceId() );

print_r( $result->getPayment()->getAmountMoney()->getAmount() );

print_r( $result->getPayment()->getAmountMoney()->getCurrency() );

print_r( $result->getPayment()->getCreatedAt() );

Upvotes: 0

Nagendra Pantham
Nagendra Pantham

Reputation: 59

If you install square connect through composer then you can use its pre-defined functions. $data holds the request for transaction

$body = new \SquareConnect\Model\ChargeRequest($data);
$transaction = $transactions_api->charge($location_id, $body);
$transactionId = $transaction->getTransaction()->getId();

Now you will be having transaction id in $transactionId variable.

Upvotes: 0

Den Pat
Den Pat

Reputation: 1254

If anyone looking for code with square connects v2 API than using this:

$payments_api = new \SquareConnect\Api\PaymentsApi($api_client);
$result = $payments_api->createPayment($request_body);
echo '<br/>';

// to get ID.
print_r( $result->getPayment()->getId() );
echo '<br/>';

// to get Reference ID.
print_r( $result->getPayment()->getReferenceId() );
echo '<br/>';

// to get Amount.
print_r( $result->getPayment()->getAmountMoney()->getAmount() );
echo '<br/>';

// to get Currency.
print_r( $result->getPayment()->getAmountMoney()->getCurrency() );
echo '<br/>';

// to get CreatedAt datetime.
print_r( $result->getPayment()->getCreatedAt() );

Upvotes: 1

Peb0
Peb0

Reputation: 21

I came here with the same problem and then I had an aha moment.

Rather than point out a few of the function calls, here's how to find them all.

Simply look into the square library files that you have implemented in your solution. In this case "Transaction.php" found in the "model" folder.

Rinse and repeat for all the other objects (IE: Tenders.php).

I hope this saves time for some people because I wasted far too much before figuring it out.

Verbatim from Transaction.php

'id' => 'getId',
'location_id' => 'getLocationId',
'created_at' => 'getCreatedAt',
'tenders' => 'getTenders',
'refunds' => 'getRefunds',
'reference_id' => 'getReferenceId',
'product' => 'getProduct',
'client_id' => 'getClientId',
'shipping_address' => 'getShippingAddress',
'order_id' => 'getOrderId'

Upvotes: 2

user3195905
user3195905

Reputation: 101

This will work.

$transaction_id = $result->getTransaction()->getId();

Upvotes: 2

LoneWaffle
LoneWaffle

Reputation: 131

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.

Upvotes: 9

Related Questions