如何在Stripe API中同时创建客户和费用?

时间:2019-03-31 16:09:50

标签: php stripe-payments

我想创建一个客户并同时向他收取条纹费用。

我目前已经这样做了,但是我不得不发送$ token 2次。 如果仅执行充电,则在我的条纹测试后端中看不到它;如果仅执行custoer创建,它将创建自己,但不向他收费... 我有点迷路了

这是我的代码:

$token = $_POST['stripeToken'];
$email  = $_POST['stripeEmail'];

\Stripe\Customer::create([
  "source" => $token,
  "email" => $email
    ]);

$charge = \Stripe\Charge::create([
'amount' => 999,
'currency' => 'usd',
'description' => 'Example charge',
'source' => $token,
]);

1 个答案:

答案 0 :(得分:2)

您要这样做,创建客户,然后向客户收费(而不是令牌):

$token = $_POST['stripeToken'];
$email  = $_POST['stripeEmail'];


// Create a Customer:
$customer = \Stripe\Customer::create([
    'source' => $token,
    'email' => $email,
]);

// Charge the Customer instead of the source
$charge = \Stripe\Charge::create([
    'amount' => 1000,
    'currency' => 'usd',
    'customer' => $customer->id,
]);

请参见https://stripe.com/docs/saving-cards