简体   繁体   English

PHP - 检索现有条带客户及其卡?

[英]PHP - Retrieve existing stripe customer and their cards?

I'm successfully able to create a new customer in Stripe via my PHP backend. 我已经成功地通过我的PHP后端在Stripe中创建了一个新客户。 That being said, when my user wants to view their existing cards, the PHP below just creates another new user and ephemeral key. 话虽这么说,当我的用户想要查看他们现有的卡时,下面的PHP只会创建另一个新用户和临时密钥。 If I already have a customer ID, how can I change my below code to make it so that if I'm providing the customer ID, existing cards are returned? 如果我已经拥有客户ID,我该如何更改以下代码,以便在我提供客户ID时返回现有卡? Help is greatly appreciated! 非常感谢帮助!

if (!isset($_POST['api_version']))
{
 header('HTTP/1.1 400 Bad Request');
}

//USER DETAILS

$email = $_POST['email'];


$customer = \Stripe\Customer::create(array(
  'email' => $email, 
));


try {

    $key = \Stripe\EphemeralKey::create(
      ["customer" => $customer->id],
      ["stripe_version" => $_POST['api_version']]
    );


    header('Content-Type: application/json');
    exit(json_encode($key));
} catch (Exception $e) {
     header('HTTP/1.1 500 Internal Server Error'); 
}

You need to store the customer id you get back from stripe the first time you create a customer and then you can retrieve the user with: 您需要在第一次创建客户时存储从条带中获得的客户ID,然后您可以使用以下内容检索用户:

\Stripe\Customer::retrieve($customer_id);

Something like this: 像这样的东西:

$customer_id = $_POST['customer_id']; //get this id from somewhere a database table, post parameter, etc.

// if the customer id doesn't exist create the customer
if ($customer_id !== null) {
    // create the customer as you are now

} else {
    $cards = \Stripe\Customer::retrieve($customer_id)->sources->all()
    // return the cards
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM