Stripe Checkout 网络钩子不通过客户电子邮件?

rdo*_*720 5 php stripe-payments

我在测试模式下使用 Stripe 的 Checkout。我试图在 Stripe 中获取客户的 ID 以及他们在结账时提供的电子邮件以更新我的数据库。

我为 checkout.session.completed 设置了一个 webhook。如果我发送测试 webhook,则会填写 id,但不会填写 customer_email。

我想也许测试 webhook 不会传递该信息,所以我填写了结帐表格。我得到的 id 很好,但 customer_email 为空。

我猜我只是不明白与 Stripe 互动的正确方式。


// straight from Stripe's documentation
try {
  $event = \Stripe\Webhook::constructEvent(
    $payload, $sig_header, $endpoint_secret
  );
} catch(\UnexpectedValueException $e) {
  // Invalid payload
  http_response_code(400);
  exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
  // Invalid signature
  http_response_code(400);
  exit();
}

// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
  $session = $event->data->object;

  // Fulfill the purchase...
  handle_checkout_session($session);
}

http_response_code(200);

// my simple function
function handle_checkout_session($session){
  $stripeID=$session['id'];
  $userEmail=$session['customer_email'];
  print 'Email: ' . $userEmail . '\n'; // works
  print 'Stripe ID: ' . $stripeID . '\n'; // empty
}
Run Code Online (Sandbox Code Playgroud)

And*_*ndy 5

我也花了很长时间才弄清楚这一点。

如果您在结帐前已经知道用户的电子邮件,则customer_email对象中的字段仅checkout.session.completed用于将电子邮件传递到 Stripe 生成的结帐表单。

要在交易后通过 Webhook 提取用户的电子邮件,请将 Webhook 设置为发送customer.created事件(您可以从 Webhook 事件选项中进行选择)。用户在 Checkout 付款表单中输入的电子邮件将可以在该对象上访问。但是,请注意,它在事件中被称为email,而不是。customer_emailcustomer.created