1

我是使用 JSON 的初学者,所以请多多包涵。基本上,我从 Stripe 收到了这个 JSON 数组,这很棒(而且我不相信其中有任何敏感信息,因为它是一个带有虚假信用卡号且没有列出密钥的测试帐户)。

基本上,在这一点上,我对获取以下单个数据的任何方法都很满意:current_period_end

这是我 dd() 时的数组:

Customer {#270 ▼
  #_opts: RequestOptions {#225 ▶}
  #_values: array:15 [▼
    "id" => "cus_6slA9S24KBk8qK"
    "object" => "customer"
    "created" => 1440817503
    "livemode" => false
    "description" => null
    "email" => null
    "shipping" => null
    "delinquent" => false
    "metadata" => AttachedObject {#276 ▶}
    "subscriptions" => Collection {#257 ▼
      #_opts: RequestOptions {#225 ▶}
      #_values: array:5 [▼
        "object" => "list"
        "total_count" => 1
        "has_more" => false
        "url" => "/v1/customers/cus_6slA9S24KBk8qK/subscriptions"
        "data" => array:1 [▼
          0 => Subscription {#249 ▼
            #_opts: RequestOptions {#225 ▶}
            #_values: array:18 [▼
              "id" => "sub_6slAE9UNlCLi24"
              "plan" => Plan {#293 ▶}
              "object" => "subscription"
              "start" => 1440817504
              "status" => "trialing"
              "customer" => "cus_6slA9S24KBk8qK"
              "cancel_at_period_end" => false
              "current_period_start" => 1440817504
              "current_period_end" => 1441422304
              "ended_at" => null
              "trial_start" => 1440817504
              "trial_end" => 1441422304
              "canceled_at" => null
              "quantity" => 1
              "application_fee_percent" => null
              "discount" => null
              "tax_percent" => null
              "metadata" => AttachedObject {#255 ▶}
            ]
            #_unsavedValues: Set {#294 ▶}
            #_transientValues: Set {#289 ▶}
            #_retrieveOptions: []
          }
        ]
      ]
      #_unsavedValues: Set {#280 ▶}
      #_transientValues: Set {#272 ▶}
      #_retrieveOptions: []
    }
    "discount" => null
    "account_balance" => 0
    "currency" => "usd"
    "sources" => Collection {#260 ▼
      #_opts: RequestOptions {#225 ▶}
      #_values: array:5 [▼
        "object" => "list"
        "total_count" => 1
        "has_more" => false
        "url" => "/v1/customers/cus_6slA9S24KBk8qK/sources"
        "data" => array:1 [▼
          0 => Card {#322 ▼
            #_opts: RequestOptions {#225 ▶}
            #_values: array:23 [▼
              "id" => "card_16ezeJKTaUqqkEkTmcBfksKh"
              "object" => "card"
              "last4" => "4242"
              "brand" => "Visa"
              "funding" => "credit"
              "exp_month" => 10
              "exp_year" => 2017
              "fingerprint" => "Y1GhThZ3NCmHOdTv"
              "country" => "US"
              "name" => "blah@yahoo.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" => AttachedObject {#335 ▼
                #_opts: RequestOptions {#225 ▶}
                #_values: []
                #_unsavedValues: Set {#339 ▶}
                #_transientValues: Set {#340 ▶}
                #_retrieveOptions: []
              }
              "customer" => "cus_6slA9S24KBk8qK"
            ]
            #_unsavedValues: Set {#336 ▶}
            #_transientValues: Set {#337 ▶}
            #_retrieveOptions: []
          }
        ]
      ]
      #_unsavedValues: Set {#321 ▶}
      #_transientValues: Set {#332 ▶}
      #_retrieveOptions: []
    }
    "default_source" => "card_16ezeJKTaUqqkEkTmcBfksKh"
  ]
  #_unsavedValues: Set {#269 ▶}
  #_transientValues: Set {#288 ▶}
  #_retrieveOptions: []
}

这就是它的样子:

Stripe\Customer JSON: { "id": "cus_6smHAG99OncrSm", "object": "customer", "created": 1440821625, "livemode": false, "description": null, "email": null, "shipping": null, "delinquent": false, "metadata": [], "subscriptions": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/subscriptions", "data": [ { "id": "sub_6smHbsmTA6JhrP", "plan": { "id": "yearly", "interval": "year", "name": "yearly", "created": 1439229255, "amount": 5000, "currency": "usd", "object": "plan", "livemode": false, "interval_count": 1, "trial_period_days": 7, "metadata": [], "statement_descriptor": "Degree Tracker yearly" }, "object": "subscription", "start": 1440821627, "status": "trialing", "customer": "cus_6smHAG99OncrSm", "cancel_at_period_end": false, "current_period_start": 1440821627, "current_period_end": 1441426427, "ended_at": null, "trial_start": 1440821627, "trial_end": 1441426427, "canceled_at": null, "quantity": 1, "application_fee_percent": null, "discount": null, "tax_percent": null, "metadata": [] } ] }, "discount": null, "account_balance": 0, "currency": "usd", "sources": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/sources", "data": [ { "id": "card_16f0ioKTaUqqkEkT7osN0knY", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 10, "exp_year": 2017, "fingerprint": "Y1GhThZ3NCmHOdTv", "country": "US", "name": "blah@yahoo.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_6smHAG99OncrSm" } ] }, "default_source": "card_16f0ioKTaUqqkEkT7osN0knY" }

以上可能足以为我找到解决问题的简单方法。但是,如果您熟悉 Laravel 和 Cashier ,这里有一些更多可选的额外材料可能会帮助您找到我的解决方案

通过执行以下操作,我可以从收到的 JSON 响应中提取几个值,例如值“last4”(信用卡的最后四位数字):

    $this_customer = \Stripe\Customer::retrieve($user->stripe_id);  
    $last_four = $this_customer->sources->retrieve($this_customer->default_source)->last4;

我也可以通过做一些非常相似的事情来获得“exp_year”值:

    $this_customer = \Stripe\Customer::retrieve($user->stripe_id);  //This is the first billing date
    $expiration_year = $this_customer->sources->retrieve($this_customer->default_source)->exp_year;

但是,我无法获得“current_period_end”值。这是我尝试过的:

    $this_customer = \Stripe\Customer::retrieve($user->stripe_id);  //This is the first billing date
    $expiration_year = $this_customer->subscriptions->retrieve($this_customer->default_source)->current_period_end;

我遇到麻烦的一个原因可能是返回给我的 JSON 加油订阅“数组与“源”数组位于不同的位置。但是,我已经把头撞到墙上一段时间了试图弄清楚如何修复我的代码以访问它,即使我确信上述问题有不止 1 个简单的解决方案。

4

1 回答 1

3
<?php

$json = '{ "id": "cus_6smHAG99OncrSm", "object": "customer", "created": 1440821625, "livemode": false, "description": null, "email": null, "shipping": null, "delinquent": false, "metadata": [], "subscriptions": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/subscriptions", "data": [ { "id": "sub_6smHbsmTA6JhrP", "plan": { "id": "yearly", "interval": "year", "name": "yearly", "created": 1439229255, "amount": 5000, "currency": "usd", "object": "plan", "livemode": false, "interval_count": 1, "trial_period_days": 7, "metadata": [], "statement_descriptor": "Degree Tracker yearly" }, "object": "subscription", "start": 1440821627, "status": "trialing", "customer": "cus_6smHAG99OncrSm", "cancel_at_period_end": false, "current_period_start": 1440821627, "current_period_end": 1441426427, "ended_at": null, "trial_start": 1440821627, "trial_end": 1441426427, "canceled_at": null, "quantity": 1, "application_fee_percent": null, "discount": null, "tax_percent": null, "metadata": [] } ] }, "discount": null, "account_balance": 0, "currency": "usd", "sources": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/sources", "data": [ { "id": "card_16f0ioKTaUqqkEkT7osN0knY", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 10, "exp_year": 2017, "fingerprint": "Y1GhThZ3NCmHOdTv", "country": "US", "name": "blah@yahoo.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_6smHAG99OncrSm" } ] }, "default_source": "card_16f0ioKTaUqqkEkT7osN0knY" }';

$data = json_decode($json, true);

$current_period_end = $data['subscriptions']['data'][0]['current_period_end'];

print_r($current_period_end);

输出:

1441426427

最后是您可以使用此代码获取日期的日期:

echo date('Y-m-d', $current_period_end);

日期:

2015-09-05

于 2015-08-29T04:38:07.017 回答