dqbr37828 2015-05-25 22:55
浏览 74
已采纳

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

I want to get the last 4 digits of a customers card using Stripe. I have already stored the Customer using:

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

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

Now I'd like to access and then store the card's last 4 digits. (For context, I want to show users which card they have stored using Stripe for future payments - this is not a subscription service).

I have searched for a solution but a lot of the posts are saving the last4 digits AFTER a charge, and pull the information from the charge like:

$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;

I would like to do the same BEFORE the charge , so I want to pull the last 4 from the Customer Object. The Stripe API documentation shows the attribute path for last4 from Customers,
customer->sources->data->last4

However, this does not seem to give me the correct last 4 digits.
$last4 = $StripeCustomer->sources->data->last4;

I think I am misunderstanding how to use attributes in the Stripe API. Could someone point me in the right direction?

  • 写回答

1条回答 默认 最新

  • dongou3158 2015-05-26 05:24
    关注

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

    sources->data is an array so you'd have to select the first card.

    Side note: You're using the token twice, once to create the customer, and the second to create the charge, this will result in an error as the token can only be used once. You'd have to charge the customer instead of the token.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开