Jon Holm Jon Holm - 2 months ago 15
PHP Question

PHP | Stripe API - check if customer have card attached

I am trying to set up Stripe API to my website, but I ran into a problem. I need to check if the customer have a credit card attached to him, in order to process to subscribe to the plan.

I cannot seem to find anything about this in their docs: https://stripe.com/docs/api#retrieve_customer

The output from a customer looks like this:

Stripe\Customer JSON: {
"id": "CUSTOMER_ID",
"object": "customer",
"account_balance": 0,
"created": 1474620297,
"currency": "dkk",
"default_source": null,
"delinquent": false,
"description": null,
"discount": null,
"email": "",
"livemode": false,
"metadata": {
},
"shipping": null,
"sources": {
"object": "list",
"data": [

],
"has_more": false,
"total_count": 0,
"url": "/v1/customers/CUSTOMER_ID/sources"
},
"subscriptions": {
"object": "list",
"data": [

],
"has_more": false,
"total_count": 0,
"url": "/v1/customers/CUSTOMER_ID/subscriptions"
}
}


So, how to check if the customer have a credit card attached to him?

Answer

Try something like this:

$customer = \Stripe\Customer::retrieve($customerID);
$cardID = $customer->default_source;

if(isset($cardID)){
    echo $cardID;
} else {
    echo "No card";
    //Code for entering card info..
}

There you will get the card id, and then you can simply check if you get something. If customer have a card then you get card id, if costumer does not have a card you won't get anything.