I'm using Stripe's Checkout in test mode. I'm trying to grab the customer's id in Stripe as well as the email they provided during checkout to update my database.
I set up a webhook for checkout.session.completed. If I send a test webhook, the id is filled in, but the customer_email is not.
I thought maybe the test webhook wouldn't pass that info, so I filled out the checkout form. I get the id just fine, but customer_email is null.
I'm guessing I just don't understand the right way to interact with 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
}
Stripe uses webhooks to notify your application when an event happens in your account. Webhooks are particularly useful for asynchronous events like when a customer's bank confirms a payment, a customer disputes a charge, a recurring payment succeeds, or when collecting subscription payments.
In test mode, Stripe retries three times over a few hours. Webhooks can be manually retried after this time in the Dashboard, and you can also query for missed events to reconcile the data over any time period.
It took me quite awhile to figure this out as well.
The customer_email
field in the checkout.session.completed
object is only there for passing through an email to the Stripe-generated checkout form if you already know the user's email before checkout.
To pull the user's email out via your webhook after the transaction, set your webhook to send the customer.created
event as well (which you can select from the webhook event options). The user's email, as they entered it in the Checkout payment form, will be accessible on that object. However, note that it's called email
, not customer_email
, on the customer.created
event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With