使用Square Connect ChargeResponse对象时出现问题


Trouble working with Square Connect ChargeResponse object

我能够按照github上的示例成功地使用事务API进行收费。执行收费如下:

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

这是输出:

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
        )
)

我的问题是,虽然有些地方(比如这里)表明我应该从charge方法中取回一个数组,但我却得到了一个ChargeResponse对象。

在这个对象中是一个事务对象,它包含我想在事务完成后向客户显示的所有相关信息,但它是受保护的,因此尝试从这个返回的对象中回显事务id、created_at time或amount失败。

我确信我做错了什么,但我不知道如何从ChargeResponse对象中捕获属性,以便用它做有用的事情。

例如,我尝试过

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

但我得到的只是:

致命错误:无法访问受保护的属性

这甚至可能不是尝试这样做的正确方式,所以我完全愿意接受建议。

我设法弄清楚,必须使用对象中包含的getTransaction方法才能获得可用的属性形式。

$transaction = $result->getTransaction();

然后你可以得到你想要的属性:

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

我很生气,因为我在文档中没有看到这一点(事实上,在谷歌上搜索整个docs.connect.squareup.com并没有找到一个getTransaction)。当我试图使用其他一些破解工作将原始的ChargeResponse对象重新解析为数组时,我偶然发现了它。

不管怎样,很高兴这件事解决了。想把这个留给别人。

这将起作用。

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

我带着同样的问题来到这里,然后我有了一个顿悟的时刻。

以下是如何查找所有函数调用的方法,而不是指出一些函数调用。

只需查看您在解决方案中实现的方形库文件即可。在本例中,"Transaction.php"位于"model"文件夹中。

冲洗并重复所有其他对象(IE:Benders.php)。

我希望这能为一些人节省时间,因为我在弄清楚之前浪费了太多时间。

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'

如果有人在寻找带有square的代码时连接v2 API,而不是使用以下代码:

$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() );

如果通过composer安装square connect,则可以使用其预定义的功能。$data保存交易的请求

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

现在您将在$transactionId变量中拥有事务id。

$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() );