festiv
festiv

Reputation: 419

How to get customer id from stripe event

I have set up a webhook where events sent from stripe are handled. I, however, have noticed that not all events share the same structure.

I am currently retrieving the customer this way:

$input = @file_get_contents("php://input");
$event_json = json_decode($input);

$customerId = $event_json->data->object->customer

After a day or two I found out that not all events contain the customer under $event_json->data->object->customer. Here is an example response:

{
  "object": {
    "id": "cus_Ac3Sx3Bn7cuvqB",
    "object": "customer",
    "account_balance": -3099,
    "created": 1494163341,
    "currency": "usd",
    "default_source": "card_1AGpL4ByNDe65wcFOfqQZGCc",
    "delinquent": false,
    "description": "John John",
    "discount": null,
    "email": "[email protected]",
    "livemode": false,
    "metadata": {
    },
    "shipping": null,
    "sources": {
      "object": "list",
      "data": [
        {
          "id": "card_1AGpL4ByNDe65wcFOfqQZGCc",
          "object": "card",
          "address_city": null,
          "address_country": null,
          "address_line1": null,
          "address_line1_check": null,
          "address_line2": null,
          "address_state": null,
          "address_zip": null,
          "address_zip_check": null,
          "brand": "Visa",
          "country": "US",
          "customer": "cus_Ac3Sx3Bn7cuvqB",
          "cvc_check": "pass",
          "dynamic_last4": null,
          "exp_month": 7,
          "exp_year": 2017,
          "fingerprint": "lI2tl3FOGKOG7PcZ",
          "funding": "credit",
          "last4": "4242",
          "metadata": {
          },
          "name": "John John",
          "tokenization_method": null
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/customers/cus_Ac3Sx3Bn7cuvqB/sources"
    },
    "subscriptions": {
      "object": "list",
      "data": [
      ],
      "has_more": false,
      "total_count": 0,
      "url": "/v1/customers/cus_Ac3Sx3Bn7cuvqB/subscriptions"
    }
  }
}

What I am asking is where I can see an example of all possible event structures so that I can make sure my webhook doesn't return Could not determine which URL to request: Stripe\Customer instance has invalid ID: (500 Internal Server Error)?

Note: I did see this question - How to get a customer ID from event object in stripe but there the only given way is $event_json->data->object->customer

Upvotes: 4

Views: 4157

Answers (1)

mlucy
mlucy

Reputation: 5289

Stripe webhooks share the same structure as the API endpoints for the relevant objects. In the example above, the object field is set to customer, so you can see the structure in the API docs at https://stripe.com/docs/api#customer_object . If you go to https://dashboard.stripe.com/account/webhooks and add an endpoint, and choose "Select types to send", you can see all the types of webhooks that will be sent to you.

There are a lot of events where it doesn't really make sense to look for a customer on the event, so it would probably be useful to only subscribe to the event types you want.

Upvotes: 2

Related Questions