How do I access the invoice data returned?

285 views
Skip to first unread message

Chris Greene

unread,
Oct 21, 2014, 3:45:00 PM10/21/14
to api-d...@lists.stripe.com
I have a curious situation; I cannot get my data to pass to the front end, although it's set on the back end. For example, using Stripe's API

 $data = \Stripe_Invoice::all(array(
                    "customer" => $user->customer_id
                ));


If I debug data, (debug($data)) I get this: 

    object(Stripe_List) {[protected] _apiKey => 'sk_test_wdsdfa3asfa3afR2lG2'[protected] _values => ['object' => 'list',... etc. etc.


If I try to pass it to the front end from an ajax call:

 $this->set(compact('data', $data));
 $this->set('_serialize', 'data');

I get this: '{}'


I also cannot log it as a session variable:

$_SESSION['log'] = $data;

echoing $_SESSION['log'] returns {}

What's the deal here? The data is clearly set if I debug it in the back end, but the moment I try to set and return a variable to the front end through ajax or session logging the data empties out. Any ideas? I imagine it has something to do with the variables being protected.

It's frustrating because I point all of the way down and something like $invoices->data->id and get NULL, but if I debug $invoices->data->id I get the correct id. How in the world do I pass it back through AJAX?

Brian Krausz

unread,
Oct 21, 2014, 5:07:58 PM10/21/14
to api-d...@lists.stripe.com
Hi Chris,

FYI that response includes your test API secret, meaning anybody can now view and modify your test data. I'd recommend rolling that key, which you can do at https://dashboard.stripe.com/account/apikeys.

Generally passing the raw PHP object (Stripe_List) to your client or into $_SESSION is not recommended, as PHP is wonky when it comes to intelligently serializing objects. I'd recommend calling `$data->__toJSON();`, which gives you a JSON string you can then pass directly down to the client. You could also call `$data->__toArray(true);` if you want the raw array to JSONify as part of a larger object.

Hope that helps!

--Brian

--
You received this message because you are subscribed to the Google Groups "Stripe API Discussion" group.
To post to this group, send email to api-d...@lists.stripe.com.
Visit this group at http://groups.google.com/a/lists.stripe.com/group/api-discuss/.

To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Chris Greene

unread,
Oct 21, 2014, 5:20:44 PM10/21/14
to api-d...@lists.stripe.com
No worries on the API secret - the middle of it has a smashed 'wdsdfa3asfa3af' that replaces the characters that used to be there. I thought it through! :P

I'll try that method out and see what I can come up with.

Chris Greene

unread,
Oct 21, 2014, 5:28:33 PM10/21/14
to api-d...@lists.stripe.com
toArray and toJSON are both javascript commands, and I'm still in the php function in the backend. I need to format the data properly in PHP before I can touch it in the Javascript function; the response I get back so far is empty so there's nothing I can do with it.

I tried working with json_encode and serialize, but no dice. The data turns to {} whenever any json_encoding is performed on it.

I'm confused at why this is difficult; the API said it was just as simple as calling the API (and the data is definitely there). Is it not recommended to return a response through AJAX or something?

Basically what I've done is create a series of tabs, and one of them is an invoice tab. When I click on that tab, an ajax call is made to pull the invoices, and my idea was then to loop through the data returned (from the ajax response in javascript) to format the invoices nicely on the page. Is there a better way to do this?
--
Chris Greene
Web developer, designer, programmer
Web developer at Pharmedio & dbVantage

Brian Krausz

unread,
Oct 21, 2014, 5:43:34 PM10/21/14
to api-d...@lists.stripe.com
Ah, I missed the API key replacement...nevermind :).

Stripe's PHP bindings provide __toArray and __toJSON methods. In PHP, you can call either of those methods on any Stripe object and get back an array and JSON string, respectively. It's a single additional method call over your existing code.

My concern with serializing and json_encoding PHP objects is not due to a particular property of Stripe's bindings: they're known "weirdnesses" with PHP itself. While there's probably a way to serialize a PHP object directly to JSON, the nuances of it vary by PHP version and what method you're using (storing in $_SESSION vs. json_encode vs. serialize), which is why I recommend just dropping down to arrays via those two methods.

--Brian

Chris Greene

unread,
Oct 21, 2014, 5:57:06 PM10/21/14
to api-d...@lists.stripe.com
Oh I see! This worked perfectly. Sorry for the ignorance, and thank you so much! This is perfect!

Brian Krausz

unread,
Oct 21, 2014, 8:12:44 PM10/21/14
to api-d...@lists.stripe.com
No worries at all, happy to help :).

--Brian
Reply all
Reply to author
Forward
0 new messages