条纹使多个客户使用相同的电子邮件地址

Thr*_*try 14 php stripe-payments

我有条纹检查与PHP.它创造了客户并为他们收费.我想创建一个捐赠表单,如果同一个客户回来并给出相同的电子邮件地址,Stripe不会创建另一个客户,但会向现有客户收取额外付款.这可能吗?或者结账总是创建具有新客户ID的新客户?

这是我的charge.php

<?php
    require_once('config.php');

    $token  = $_POST['stripeToken'];

    if($_POST) {
      $error = NULL;

      try{
        if(!isset($_POST['stripeToken']))
          throw new Exception("The Stripe Token was not generated correctly");
            $customer = Stripe_Customer::create(array(
              'card'  => $token,
              'email' =>  $_POST['stripeEmail'],
              'description' => 'Thrive General Donor'
            ));

            $charge = Stripe_Charge::create(array(
              'customer' => $customer->id,
              'amount'   => $_POST['donationAmount'] * 100,
              'currency' => 'usd'
            ));
      }
      catch(Exception $e) {
        $eror = $e->getMessage();
      }


    }

?>
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 10

您需要在数据库中存储电子邮件地址和Stripe客户ID之间的关系.我通过查看Stripe的客户API来确定这一点.

首先,在创建新客户时,每个字段都是可选的.这使我相信,每一次你POST/v1/customers,将"[创建]一个新的客户对象."

此外,在检索客户时,唯一可用的字段是id.这使我相信您无法根据电子邮件地址或其他字段检索客户.


如果您无法将此信息存储在数据库中,则始终可以列出所有客户GET /v1/customers.这将要求您分页并检查所有客户对象,直到找到具有匹配电子邮件地址的客户对象.如果每次尝试创建客户时都可以看到这样做效率很低.

  • 您可能会使用 webhook;但是,我会在您创建客户对象后立即保存信息(在您的代码中,`$customer = Stripe_Customer::create(array(...));`)。这将返回客户对象,您可以从中检索 Stripe ID 以在您的数据库中与提交的电子邮件地址相关联。 (2认同)
  • 让我用 [来自 Stripe 的数据](https://stripe.com/docs/api#token_object) 来增强最后一条评论:*“请注意,令牌不应多次存储或使用 - 存储这些详细信息稍后使用,您应该创建 Customer 或 Recipient 对象”*(不一定“不安全”,但不是“打算”存储)。 (2认同)

Jor*_*ney 7

您可以列出给定电子邮件地址的所有用户。 https://stripe.com/docs/api#list_customers

JavaScript中你可以这样做:

const customerAlreadyExists = (email)=>{
    return  doGet(email)
                .then(response => response.data.length > 0);
}

const doGet = (url: string)=>{
    return fetch('https://api.stripe.com/v1/customers' + '?email=' + email, {
        method: 'GET',
        headers: {
            Accept: 'application/json',
            Authorization: 'Bearer ' + STRIPE_API_KEY
        }
    }).then(function (response) {
        return response.json();
    }).catch(function (error) {
        console.error('Error:', error);
    });
}
Run Code Online (Sandbox Code Playgroud)