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

jae*_*o0k 16 php stripe-payments

我想使用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
      ));
Run Code Online (Sandbox Code Playgroud)

现在我想访问并存储卡的最后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;
Run Code Online (Sandbox Code Playgroud)

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

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

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

Mat*_*kin 32

$ last4 = $ StripeCustomer-> sources-> data [0] - > last4;

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

附注:您使用令牌两次,一次创建客户,第二次创建费用,这将导致错误,因为令牌只能使用一次.您必须向客户收取费用而不是代币.