【问题标题】:Trouble working with Square Connect ChargeResponse object使用 Square Connect ChargeResponse 对象时出现问题
【发布时间】:2016-08-25 23:31:35
【问题描述】:

我可以按照 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
        )

)

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

在此对象中是一个交易对象,其中包含我想在交易完成后向客户显示的所有相关信息,但它是受保护的,因此尝试从该对象中回显交易 id、created_at 时间或金额返回对象失败。

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

例如,我试过了

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

但我得到的只是:

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

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

【问题讨论】:

  • 编辑了问题以包含输出。
  • 第一个建议给出了关于无法访问受保护属性的相同错误,第二个和第三个建议不解析为有效的 php。

标签: php square-connect square


【解决方案1】:
$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() );

【讨论】:

  • 你应该添加一些解释。这是怎么回事?这如何回答这个问题?
【解决方案2】:

如果您通过 composer 安装 square connect,那么您可以使用其预定义的功能。 $data 持有交易请求

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

现在您将在 $transactionId 变量中获得交易 ID。

【讨论】:

    【解决方案3】:

    如果有人在寻找使用 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() );
    

    【讨论】:

      【解决方案4】:

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

      这里不指出几个函数调用,而是如何找到它们。

      只需查看您在解决方案中实现的方形库文件。 在这种情况下,“模型”文件夹中的“Transaction.php”。

      冲洗并重复所有其他对象(IE:Tenders.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'
      

      【讨论】:

        【解决方案5】:

        这会起作用。

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

        【讨论】:

        • 您能否格式化您的代码,并更详细地说明您为什么认为这可以回答问题?
        【解决方案6】:

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

        $transaction = $result->getTransaction();
        

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

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

        我很生气,因为我在文档中的任何地方都没有遇到这个问题(事实上,谷歌搜索整个 docs.connect.squareup.com 并没有找到对 getTransaction 的单一引用)。当我尝试使用其他一些 hack 作业将原始 ChargeResponse 对象重新解析为数组时,我不得不偶然发现它。

        无论如何,很高兴这件事得到了解决。想把这个留给其他人。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-10
        • 1970-01-01
        • 2021-08-23
        • 2011-01-08
        • 2010-12-30
        • 2011-11-13
        • 1970-01-01
        相关资源
        最近更新 更多