检索客户来源时的条带响应

时间:2015-09-10 22:43:35

标签: php json stripe-payments

我正在尝试通过提供客户ID来检索特定客户的所有卡。根据{{​​3}},我期待以下回复:

Stripe\Collection JSON: {
  "object" => "list",
  "url" => "/v1/customers/cu_16jdAj2eZvKYlo2CbrCP4HRs/sources",
  "has_more" => false,
  "data" => [
    [0] => Stripe\Card JSON: {
      "id": "card_14bIFkou9GUI42eZvKYlo2Cs",
      "object": "card",
      "last4": "4242",
      "brand": "Visa",
      "funding": "credit",
      "exp_month": 11,
      "exp_year": 2018,
      "country": "US",
      "name": "callingsudhanshu@gmail.com",
      "address_line1": null,
      "address_line2": null,
      "address_city": null,
      "address_state": null,
      "address_zip": null,
      "address_country": null,
      "cvc_check": null,
      "address_line1_check": null,
      "address_zip_check": null,
      "tokenization_method": null,
      "dynamic_last4": null,
      "metadata": {
      },
      "customer": "cus_6Nb0wxYHigk2mX"
    }
    [1] => <Stripe\Card[...] ...>
    [2] => <Stripe\Card[...] ...>
  ]
}

这是我对Stripe的要求。试图从data中提取$card_list(即所有卡片组):

\Stripe\Stripe::setApiKey($this->stripe_sk);
$card_list = \Stripe\Customer::retrieve($customer_id)->sources->all(array(
    'object' => 'card'
));

// Return a list of all cards.
$card_list = $card_list->data;
return $card_list;

我想知道为什么来自$card_list的Stripe响应包含其他/意外数据。这就是我实际得到的东西:

array(1) {
  [0]=&gt;
  object(Stripe\Card)#292 (5) {
    ["_opts":protected]=&gt;
    object(Stripe\Util\RequestOptions)#294 (2) {
      ["headers"]=&gt;
      array(0) {
      }
      ["apiKey"]=&gt;
      string(32) "sk_test_d3..."
    }
    ["_values":protected]=&gt;
    array(23) {
      ["id"]=&gt;
      string(29) "card_16jLIwLBSNgilYpnoEiPhNhA"
      ["object"]=&gt;
      string(4) "card"
      ["last4"]=&gt;
      string(4) "4242"
      ["brand"]=&gt;
      string(4) "Visa"
      ["funding"]=&gt;
      string(6) "credit"
      ["exp_month"]=&gt;
      int(8)
      ["exp_year"]=&gt;
      int(2016)
      ["fingerprint"]=&gt;
      string(16) "T5RzXLIiXrF0nyqH"
      ["country"]=&gt;
      string(2) "US"
      ["name"]=&gt;
      NULL
      ["address_line1"]=&gt;
      NULL
      ["address_line2"]=&gt;
      NULL
      ["address_city"]=&gt;
      NULL
      ["address_state"]=&gt;
      NULL
      ["address_zip"]=&gt;
      NULL
      ["address_country"]=&gt;
      NULL
      ["cvc_check"]=&gt;
      string(4) "pass"
      ["address_line1_check"]=&gt;
      NULL
      ["address_zip_check"]=&gt;
      NULL
      ["tokenization_method"]=&gt;
      NULL
      ["dynamic_last4"]=&gt;
      NULL
      ["metadata"]=&gt;
      object(Stripe\AttachedObject)#261 (5) {
        ["_opts":protected]=&gt;
        object(Stripe\Util\RequestOptions)#294 (2) {
          ["headers"]=&gt;
          array(0) {
          }
          ["apiKey"]=&gt;
          string(32) "sk_test_d..."
        }
        ["_values":protected]=&gt;
        array(0) {
        }
        ["_unsavedValues":protected]=&gt;
        object(Stripe\Util\Set)#262 (1) {
          ["_elts":"Stripe\Util\Set":private]=&gt;
          array(0) {
          }
        }
        ["_transientValues":protected]=&gt;
        object(Stripe\Util\Set)#306 (1) {
          ["_elts":"Stripe\Util\Set":private]=&gt;
          array(0) {
          }
        }
        ["_retrieveOptions":protected]=&gt;
        array(0) {
        }
      }
      ["customer"]=&gt;
      string(18) "cus_6xCtshHPQiYSdI"
    }
    ["_unsavedValues":protected]=&gt;
    object(Stripe\Util\Set)#270 (1) {
      ["_elts":"Stripe\Util\Set":private]=&gt;
      array(0) {
      }
    }
    ["_transientValues":protected]=&gt;
    object(Stripe\Util\Set)#120 (1) {
      ["_elts":"Stripe\Util\Set":private]=&gt;
      array(0) {
      }
    }
    ["_retrieveOptions":protected]=&gt;
    array(0) {
    }
  }
}

希望有人可以解释响应的“扩展”版本,我正在这里。提前谢谢!

2 个答案:

答案 0 :(得分:4)

我几天前就陷入了这个问题,我已经设法使用这样的json_encode() API解决了这个问题(需要将此导出为我的AJAX返回)。

$cards = \Stripe\Customer::retrieve($_SESSION['stripe_customer_id'])->sources->all(array("object" => "card"));
$list = json_encode($cards->data);
echo($list);

希望这有帮助。

BTW:您的回复与var_dump()输出相似。

答案 1 :(得分:0)

如果要以数组形式规范化数据以轻松获取值,请添加__toArray();以获取数组中的响应。

例如: $cards->data->__toArray();