使用客户对象获取卡的最后 4 位数字 - 使用 PHP 的 Stripe API

2022-08-30 19:54:41

我想使用 Stripe 获取客户卡的最后 4 位数字。我已经使用以下内容存储了客户:

      // Get the credit card details submitted by the form
      $token = $_POST['stripeToken'];

      // Create a Customer
      $StripeCustomer = \Stripe\Customer::create(array(
              "description" => "$username",
              "card" => $token
      ));

现在我想访问并存储卡的最后4位数字。(对于上下文,我想向用户展示他们使用Stripe存储的卡以供将来付款 - 这不是订阅服务)。

我已经搜索了一个解决方案,但很多帖子都在充电后保存最后4位数字,并从充电中提取信息,例如:

$last4 = null;
try {
    $charge = Stripe_Charge::create(array(
    "amount" => $grandTotal, // amount in cents, again
    "currency" => "usd",
    "card" => $token,
    "description" => "Candy Kingdom Order")
);
$last4 = $charge->card->last4;

我想在收费之前做同样的事情,所以我想从客户对象中提取最后4个。Stripe API 文档显示了来自客户的 last4 的属性路径,
customer->sources->data->last4

但是,这似乎并没有给我正确的最后4位数字。
$last4 = $StripeCustomer->sources->data->last4;

我认为我误解了如何在 Stripe API 中使用属性。有人能给我指出正确的方向吗?


答案 1

$last 4 = $StripeCustomer->源->数据[0]->last4;

sources->data是一个数组,因此您必须选择第一张卡。

旁注:您正在使用令牌两次,一次用于创建客户,另一次用于创建费用,这将导致错误,因为令牌只能使用一次。您必须向客户而不是令牌收费。


答案 2

对于从搜索引擎登陆这里的任何其他人,这里有一个Stripe文档的链接,该文档介绍如何获取保存到客户的卡的最后4位数字 https://stripe.com/docs/api/customers/object#customer_object-sources-data-last4


推荐