2016-04-30 53 views
2

我能够使用github上的示例,使用事务API成功收费。执行收费看起来是这样的:使用Square Connect ChargeResponse对象工作时遇到的问题

$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)表明,我应该得到一个数组从后充电方法,我反而得到ChargeResponse对象。

在这个对象中是一个事务对象,它包含了所有我想在事务完成后向客户显示的相关信息,但它是受保护的,所以试图回显事务ID,created_at时间或金额这个返回的对象失败。

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

举例来说,我已经试过

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

,但我得到的是:

致命错误:无法访问受保护的财产

这甚至可能不尝试一些正确的方法像这样,所以我完全接受建议。

+0

编辑该问题以包含输出。 –

+0

第一个建议给出了关于不能访问受保护属性的相同错误,第二个和第三个建议不解析为有效的php。 –

回答

7

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

$transaction = $result->getTransaction(); 

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

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

我相当恼火,我没碰到过的文档中的任何地方这来(实际上是谷歌搜索整个docs.connect.squareup.com不会提供getTransaction的单个引用)。当我尝试使用其他黑客工作将原始ChargeResponse对象重新分解为数组时,我不得不绊倒它。

无论如何,很高兴这个问题得到解决。想在这里为他人留下这个。

+0

这个getTransaction函数记录在Square Connect SDK中https://github.com/square/connect-php-sdk/blob/master/lib/Model/ChargeResponse.php#L136 你是对的,虽然有实际使用SDK的例子还不多。 – Joe

+0

这将做同样的事情:$ result ['transaction'] - > getId(); – Rossitten

+0

这就是我的工作! –

0

这将工作 $ transaction_id = $ result-> getTransaction() - > getId();

+0

您可以请您设置您的代码格式,并详细阐述为什么您认为这回答了这个问题? –