首页 > 解决方案 > Stripe Checkout webhook 不传递客户电子邮件?

问题描述

我在测试模式下使用 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
}

标签: phpstripe-payments

解决方案


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

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

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


推荐阅读