2016-04-30 56 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

您可以請您設置您的代碼格式,並詳細闡述爲什麼您認爲這回答了這個問題? –