Stripe:即使customer_id不为空,也会创建新客户吗?

时间:2019-01-21 17:37:29

标签: php stripe-payments

我正在尝试使用以下代码从Stripe检索用户的现有卡。就是说,当我使用下面的后端时,即使我告诉Stripe如果$ customer_id不存在也只能创建一个新客户,即使customer_id不为null还是要创建一个新的客户ID?我觉得我这里缺少明显的东西...

.php

$email = $_POST['email'];
$customer_id = $_POST['customer_id']; //get this id from somewhere a database table, post parameter, etc.
$customer = \Stripe\Customer::create(array(
  'email' => $email, 

));

$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) {

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

      header('Content-Type: application/json');
    exit(json_encode($key));

} else {

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

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

      header('Content-Type: application/json');
    exit(json_encode($key));
}

1 个答案:

答案 0 :(得分:1)

您的IF条件应该切换。当前,如果存在customer_id,则创建一个客户。根据描述,您希望看到相反的内容,对吧?

在这种情况下,您要做的就是切换if / else正文:

if ($customer_id !== null) {
  $cards = \Stripe\Customer::retrieve($customer_id)->sources->all(); 
  // return the cards
  header('Content-Type: application/json');
  exit(json_encode($cards)); // you might want to return the cards here?
} else {
  $key = \Stripe\EphemeralKey::create(
    ["customer" => $customer->id],
    ["stripe_version" => $_POST['api_version']]
  );

  header('Content-Type: application/json');
  exit(json_encode($key));
}

并删除顶部的创建块。这也将创建一个不需要的客户对象。