php - 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
}
解决方案
我也花了很长时间才弄清楚这一点。
如果您在结帐前已经知道用户的电子邮件,则对象中的customer_email
字段checkout.session.completed
仅用于将电子邮件传递到 Stripe 生成的结帐表单。
要在交易后通过您的 webhook 提取用户的电子邮件,请将您的 webhook 设置为也发送customer.created
事件(您可以从 webhook 事件选项中选择)。用户在 Checkout 付款表单中输入的电子邮件将可在该对象上访问。但是,请注意,它在事件中被称为email
,而不是。customer_email
customer.created
推荐阅读
- r - 错误 foreach 函数:维数不正确
- javascript - React/React-Hooks:需要在 React Hooks 组件中运行 onLoad 函数
- angular - 故事书中的RouterLink?
- c# - 无法在 DbSet 测试替身上调用 AddRange
- ios - 在 Firebase 测试实验室测试 APNS
- vb.net - 如何使用 VB.net 下载 YouTube 视频
- google-apps-script - OnChange 在某些工作表中更改时触发,而不是在工作簿的任何工作表中
- facebook - 如何将标题和描述等信息添加到帖子中的链接
- ios - 如何在数据库中保存 Apple 应用内购买的收据?
- c# - 尝试序列化异常时忽略自定义解析器