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.

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

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决