Skip to content

E-commerce & payments

Validate customer emails and phone numbers in Stripe

Verify the email and phone of each new Stripe customer from a signed customer.created webhook, write the result to metadata, and check the IP before a payment.

Updated 2026-11-26

This recipe checks the email and phone number of every new Stripe customer. A signed customer.created webhook reaches a small receiver, the receiver calls the API, and the verdict goes back to the customer as metadata. A second, optional part checks the IP address of the checkout request in your backend before it creates a PaymentIntent. Stripe has no native Qelka app, so the receiver sits in between.

What you need

  • A Stripe account and a secret key (STRIPE_SECRET_KEY). A restricted Stripe key with write access to Customers and PaymentIntents is enough.
  • A Qelka API key in QELKA_API_KEY. A sk_test_ key returns simulated results and never spends credits: see Test mode. Use one with Stripe's sandbox, and a live key with live mode.
  • A Node.js 18 or later server reachable over HTTPS, with npm install express stripe @qelka/sdk.

Steps

1. Create the event destination

In the Stripe Dashboard, open Workbench, then the Webhooks tab, and create an event destination for your account. Choose the Snapshot payload, so each event carries the customer object, and select the customer.created event. Enter your receiver's HTTPS URL, for example https://example.com/stripe/webhook. Stripe then shows a signing secret that starts with whsec_: put it in STRIPE_WEBHOOK_SECRET.

To test on your machine first, the Stripe CLI forwards events to a local port and prints its own whsec_ secret:

bash
stripe listen --events customer.created --forward-to localhost:3000/stripe/webhook

2. The receiver

javascript
import express from 'express'
import Stripe from 'stripe'
import { ApiConnectionError, ApiError, Client } from '@qelka/sdk'

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { timeout: 10000 })
const validation = new Client({ apiKey: process.env.QELKA_API_KEY, timeoutMs: 8000 })
const app = express()

async function settle(task) {
  try {
    return await task()
  } catch (error) {
    if (!(error instanceof ApiError) && !(error instanceof ApiConnectionError)) throw error
    if (error instanceof ApiError && error.type === 'unprocessable_input') return null
    console.error('validation skipped', error instanceof ApiError ? `${error.type} ${error.requestId}` : 'unreachable')
    return null
  }
}

async function checkCustomer(customerId) {
  const customer = await stripe.customers.retrieve(customerId)
  if (customer.deleted || customer.metadata.email_status) return

  const country = customer.address?.country || undefined
  const [email, phone] = await Promise.all([
    customer.email ? settle(() => validation.email.verify({ email: customer.email })) : null,
    customer.phone ? settle(() => validation.phone.validate({ phone: customer.phone, country })) : null,
  ])

  const metadata = {}
  if (customer.email) {
    metadata.email_status = email ? email.status : 'unchecked'
    if (email?.reason) metadata.email_reason = email.reason
    if (email?.did_you_mean) metadata.email_suggestion = email.did_you_mean
  }
  if (customer.phone) {
    metadata.phone_status = phone ? phone.status : 'unchecked'
    if (phone?.e164) metadata.phone_e164 = phone.e164
    if (phone?.line_type) metadata.phone_line_type = phone.line_type
  }
  if (Object.keys(metadata).length > 0) await stripe.customers.update(customer.id, { metadata })
}

app.post('/stripe/webhook', express.raw({ type: 'application/json' }), (req, res) => {
  let event
  try {
    event = stripe.webhooks.constructEvent(req.body, req.get('Stripe-Signature'), process.env.STRIPE_WEBHOOK_SECRET)
  } catch (error) {
    return res.status(400).send(`Signature verification failed: ${error.message}`)
  }

  res.sendStatus(200)

  if (event.type === 'customer.created') {
    checkCustomer(event.data.object.id).catch((error) => console.error('customer check failed', event.id, error.message))
  }
})

app.listen(3000)

Four details matter here:

  • express.raw keeps the body as bytes. constructEvent verifies the Stripe-Signature header against the raw body and the signing secret, and rejects old timestamps to stop replays. A body that a JSON parser touched first fails verification.
  • The route answers 200 before it calls any API, because Stripe expects a quick 2xx and retries otherwise.
  • checkCustomer reads the customer again instead of trusting the snapshot, and skips it when email_status is already set. Stripe can deliver the same event twice, and a repeat check would cost credits again.
  • country comes from the customer's address and lets the API read a phone number written without +. It is left out, not sent as null, when there is no address.

3. Read the metadata

Key Values Suggested action
email_status undeliverable Receipts and invoices will bounce: ask the customer for another address, show email_suggestion to support
risky Read email_reason: disposable deserves review on trials and free tiers
deliverable or unknown Nothing to do
unchecked The API was unreachable or refused the key: re-check later
phone_status valid Use phone_e164 for calls and SMS
invalid Do not text it
unknown No + and no country: collect the country

Find the customers to fix with the Search API, for example stripe.customers.search({ query: 'metadata["email_status"]:"undeliverable"' }). Search results can lag behind recent changes, so do not use it right after a write.

Updating metadata sends a customer.updated event, not a new customer.created, so the receiver does not loop.

4. Check the IP before a payment (optional)

The webhook never sees the shopper's IP address, but your backend does when it creates the PaymentIntent. Check it there and ask for 3D Secure when the request looks risky:

javascript
app.set('trust proxy', 1)

app.post('/create-payment-intent', express.json(), async (req, res) => {
  const lookup = await settle(() => validation.ip.lookup({ ip: req.ip }))
  const risky = lookup !== null && (lookup.risk.level === 'high' || lookup.flags.tor)

  const paymentIntent = await stripe.paymentIntents.create({
    amount: await orderTotalInCents(req.body.cartId),
    currency: 'usd',
    automatic_payment_methods: { enabled: true },
    metadata: {
      ip_risk: lookup ? lookup.risk.level : 'unchecked',
      ip_flags: lookup ? Object.keys(lookup.flags).filter((flag) => lookup.flags[flag]).join(',') : '',
    },
    ...(risky ? { payment_method_options: { card: { request_three_d_secure: 'challenge' } } } : {}),
  })

  res.json({ clientSecret: paymentIntent.client_secret })
})

orderTotalInCents is your own function: always compute the amount on the server. trust proxy makes req.ip the client address behind one proxy or load balancer; set it to match your hosting. A private or local address, as in development, comes back as 422 unprocessable_input, which settle treats as "no signal". request_three_d_secure: 'challenge' asks Stripe to run 3D Secure with a challenge; the card issuer has the last word, and cards without 3D Secure proceed normally. The flags are written to the PaymentIntent's metadata, which Stripe copies to the charge, so a Radar rule can read them if your Radar plan has custom rules. See Flag VPN and data center traffic for what each flag means.

Do not refuse payments on a VPN flag alone: many honest people use one. Ask for more proof instead.

Handle errors and rate limits

Nothing is billed on errors. The SDK retries 429, 502 and 503, waiting Retry-After. 401 invalid_api_key, 403 forbidden and 402 insufficient_credits end as unchecked here and in the logs with the request id, so alert on them. A key allows 200 requests per 10 seconds; importing many customers at once fires many customer.created events, so for existing customers export them and run a bulk file check instead. Each email check costs 1 credit, each basic phone check 1 and each IP lookup 1. See Errors and Rate limits.

Frequently asked questions

Can Stripe block a customer with a bad email by itself?
Not with this check. A customer.created event arrives after the customer exists, so the webhook can only label it. To stop a bad address before anything is created, call the API in your own backend before you create the customer or the Checkout Session, and show the error in your form.
Why write the result to metadata?
Metadata travels with the customer: your support team sees it in the Dashboard, your code reads it on later events, and the Search API finds customers by it, for example metadata["email_status"]:"undeliverable". Stripe does not use metadata to accept or decline payments unless you write Radar rules that read it.
Does the IP check replace Radar?
No. Radar scores the payment with Stripe's own network data. The IP check adds a signal Radar does not have from your side: whether the request that creates the PaymentIntent comes through Tor, a known VPN or proxy service, or a data center. Use it to ask for 3D Secure or to review, not as the only reason to refuse.

More in E-commerce & payments

All integrations

Put this check in your product

Get an API key in under a minute. 100 free credits every month, no card required.

  • 100 free credits a month
  • No card
  • Test keys spend nothing
  • Cancel any time