1

I am a beginner in working with JSON, so please bear with me. Basically, I am receiving this JSON array back from Stripe, which is great (and I don't believe there is any sensitive information in it as it's a test account with a bogus credit card number and no keys listed).

Basically, at this point, I would be happy with any method in obtaining the following single piece of data: current_period_end

Here is the array when I dd() it:

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" => "[email protected]"
              "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: []
}

Here's what it looks like with just echoing it out:

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": "[email protected]", "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" }

The above is probably enough to find me a simple solution to my problem. However, here is some more optional extra material that might help you finding me a solution if you are familiar with Laravel and Cashier

I'm able to pull out a couple of values from the JSON response I'm getting such as the value "last4" (last four digits of the credit card) by doing this:

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

and I am also able to get the "exp_year" value by doing something very similar:

    $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;

However, I have not been able to get the "current_period_end" value. Here is what I have tried:

    $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;

One reason for the trouble I'm having is likely that the JSON that gets returned to me gas the subscriptions" array is in a different location from the "sources" array. However, I have been banging my head against a wall for a while trying to figure out how to fix my code to access it, even though I am sure there is more than 1 simple solution to the above problem.

1 Answer 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": "[email protected]", "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);

Output:

1441426427

And finally the date you can obtain the date with this code:

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

Date:

2015-09-05

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for sorting out the cobwebs in my brain. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.