1

I used stripe to return a Customer Object. I want to extract the bank account ID and save it in a database for later use. What is the proper syntax to access the array properly?

Here is what I have tried:

$bank_account=$customer->lastResponse['json']['sources']['data'][0]['id'];

I was told to try:

$bank_account=$customer->bank_accounts->data[0]['id'];

But that came up null as well...

Im not sure how to work with the object or what the right way to do this is. I need the bank_account_id This is what the var_dump($customer) looks like after some formatting and removing information that is not pertinent. I think I can either use a JSON decode or access the array called 'json' i dont know the proper syntax for either of those solutions. Help please (:

object(Stripe\Customer)#3572 (6) 
{ 
    ["_opts":protected]=> object(Stripe\Util\RequestOptions)#3576 (2) { ["headers"]=> array(1) { ["Stripe-Account"]=> string(21) "acct_1BNn74AprSj6yALS" } ["apiKey"]=> string(32) "secretkey" } 
    ["_values":protected]=> array(15) {//Removed For brevity} 
    ["_unsavedValues":protected]=> object(Stripe\Util\Set)#3590 (1) { ["_elts":"Stripe\Util\Set":private]=> array(0) { } } ["_transientValues":protected]=> object(Stripe\Util\Set)#3602 (1) { ["_elts":"Stripe\Util\Set":private]=> array(0) { } } ["_retrieveOptions":protected]=> array(0) { } 
    ["_lastResponse":protected]=> 
        object(Stripe\ApiResponse)#3714 (4) 
        { 
            ["headers"]=> array(15) { //Removed for brevity} 
            // Json
            ["body"]=> string(1148) "{
                "id": "cus_BoJOjFghfV7mmq", "object": "customer", "account_balance": 0, "created": 1511290036, "currency": null, "default_source": "bank_account_id", "delinquent": false, "description": "Name", "discount": null, "email": null, "livemode": true, "metadata": {}, "shipping": null, "sources": { "object": "list", "data": [ { "id": "bank_account_id", "object": "bank_account", "account_holder_name": "Daniel Taylor", "account_holder_type": "individual", "bank_name": "Bank Name", "country": "US", "currency": "usd", "customer": "cus_id", "fingerprint": "info", "last4": "last four of account", "metadata": {}, "routing_number": "routing number", "status": "new" } ], "has_more": false, "total_count": 1, "url": "/v1/customers/cus_BoJOjFghfV7mmq/sources" }, "subscriptions": { "object": "list", "data": [], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_BoJOjFghfV7mmq/subscriptions" } 
            } " 
            ["json"]=> array(15) 
            { 
                ["id"]=> string(18) "cus_id" 
                ["object"]=> string(8) "customer" 
                ["account_balance"]=> int(0) 
                ["created"]=> int(1511290036) 
                ["currency"]=> NULL 
                ["default_source"]=> string(27) "bank_account_id" 
                ["delinquent"]=> bool(false) 
                ["description"]=> string(13) "Name" 
                ["discount"]=> NULL 
                ["email"]=> NULL ["livemode"]=> bool(true) 
                ["metadata"]=> array(0) { } 
                ["shipping"]=> NULL 
                ["sources"]=> array(5) 
                { 
                    ["object"]=> string(4) "list" 
                    ["data"]=> array(1) 
                    { 
                        [0]=> array(13) 
                        {
                            ["id"]=> string(27) "bank_account_id" ["object"]=> string(12) "bank_account" 
                            ["account_holder_name"]=> string(13) "name" 
                            ["account_holder_type"]=> string(10) "individual" 
                            ["bank_name"]=> string(26) "Bank Name" 
                            ["country"]=> string(2) "US" ["currency"]=> string(3) "usd" 
                            ["customer"]=> string(18) "cus_ID" 
                            ["fingerprint"]=> string(16) "fingerprint" 
                            ["last4"]=> string(4) "lastfour" ["metadata"]=> array(0) { } 
                            ["routing_number"]=> string(9) "routenumber" 
                            ["status"]=> string(3) "new" 
                        } 
                    } 
                    ["has_more"]=> bool(false) 
                    ["total_count"]=> int(1) 
                    ["url"]=> string(40) "/v1/customers/cus_BoJOjFghfV7mmq/sources" 
                } 
                ["subscriptions"]=> array(5) { ["object"]=> string(4) "list" ["data"]=> array(0) { } ["has_more"]=> bool(false) ["total_count"]=> int(0) ["url"]=> string(46) "/v1/customers/cus_id/subscriptions" } 
            } 
            ["code"]=> int(200) 
        } 
}
8
  • @kchason The Stripe API returns the object already decoded. Commented Nov 21, 2017 at 19:17
  • The Stripe API documentation is at stripe.com/docs/api/php Commented Nov 21, 2017 at 19:18
  • _lastResponse has a leading undersore (_) and is an object. Because it is an object, json (which is a terrible name for this, BTW) is a property of it, not an index in an array. You want: $customer->_lastResponse->json['sources']['data'][0]['id'] Commented Nov 21, 2017 at 19:20
  • 1
    The _lastResponse value is protected, and probably cannot be directly accessed. If this is not a value provided by the api, you may be able to hack it using ReflectionClass, but if you find yourself having to do this, you are usually using the object wrong, and should use a different identifier. Commented Nov 21, 2017 at 19:22
  • Good catch on the protected property. Time for the OP to read some docs... Commented Nov 21, 2017 at 19:23

2 Answers 2

1

I think it should be:

$bank_account = $customer->sources->data[0]->id

If the customer has multiple fund sources, you may need to loop through the data array to find the one you want. $customer->sources->data[$i]->object will be "bank_account" for the source you want.

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

Comments

0

Pretty sure there is a getter in there. You can just use the properties you would expect on the object.

Example:

$customer_id = $customer->id;
$bank_account_id = $customer->sources->data[0]->id;

Comments

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.