[英]PHP | Stripe API - check if customer have card attached
我正在尝试为我的网站设置Stripe API,但是遇到了问题。 我需要检查客户是否附有信用卡,以便处理订阅计划。
我似乎无法在他们的文档中找到关于此的任何信息: https : //stripe.com/docs/api#retrieve_customer
客户的输出如下所示:
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"
}
}
那么,如何检查客户是否附有信用卡呢?
尝试这样的事情:
$customer = \Stripe\Customer::retrieve($customerID);
$cardID = $customer->default_source;
if(isset($cardID)){
echo $cardID;
} else {
echo "No card";
//Code for entering card info..
}
在那里,您将获得卡ID,然后只需检查是否有东西即可。 如果客户有卡,那么您将获得卡ID,如果客户没有卡,您将不会获得任何东西。
$cards = Customer::retrieve(STRIPE_CUSTOMER_ID)->sources->all([
'object' => 'card'
])->data;
如果没有卡片,它将返回一个卡片对象数组或一个空数组。
if($cards == []) {
// no cards exist for user
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.