PHP | Stripe API - 检查客户是否附有卡片

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,然后你可以简单地检查你是否得到了什么。如果客户有卡,那么您将获得卡号,如果客户没有卡,您将一无所获。

$cards = Customer::retrieve(STRIPE_CUSTOMER_ID)->sources->all([
    'object' => 'card'
])->data;

它将return一个卡片对象数组,如果不存在卡片,则为一个空数组。

if($cards == []) {
    // no cards exist for user
}