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

时间:2019-01-10 20:40:54

标签: php stripe-payments

我可以通过我的PHP后端在Stripe中成功创建一个新客户。话虽如此,当我的用户想要查看其现有卡时,下面的PHP只会创建另一个新用户和临时密钥。如果我已经有一个客户ID,我该如何更改下面的代码以使其变通,以便在我提供客户ID的情况下退回现有卡?非常感谢您的帮助!

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'); 
}

1 个答案:

答案 0 :(得分:3)

您需要存储在第一次创建客户时从stripe获取的客户id,然后可以使用以下方法检索用户:

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

类似这样的东西:

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