我在测试模式下使用 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
事件。
关于php - Stripe Checkout webhook 不传递客户电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61579146/