keymelon

How to Automatically Deliver Digital Products with Stripe

Stripe collects the money. Fulfilment is the part you design: verified webhooks, private files, expiring links, licence keys, refunds and purchase recovery.

A payment card connected to a file and a licence key

Selling a download with Stripe takes minutes to set up. Making sure the right person gets the right thing, once, and can get it again next year, takes more thought. That second part is fulfilment, and it is yours to design.

Payments and fulfilment are different jobs

Stripe takes the payment and tells you what happened. Fulfilment decides what the buyer receives and for how long.

PaymentFulfilment
Charge the cardCreate the licence
Report success or failureGrant access to a file
Process a refundDecide whether access ends
Keep the payment recordHelp the buyer find the purchase later

Payment Links are hosted pages you create without code. Checkout Sessions are created by your server for each purchase, so you can attach your own customer reference. Stripe's fulfilment guide notes that Payment Links use Checkout, so the same fulfilment approach applies to both.

In both cases, what was bought is read from the session's line items. Map each Stripe price ID to something concrete, such as "Pro licence, 2 devices, plus the installer".

The workflow

Workflow: a buyer pays, Stripe sends a signed event, your server verifies it, checks payment status, records the order once, then creates the licence and file access and emails the buyer. Refund and dispute events lead back to access rules.

Start from verified payment events

Do not grant access because someone reached your success page. Anyone can type that address, and real buyers sometimes close the tab before it loads. Stripe says you cannot rely on the landing page alone and that webhooks are required for fulfilment.

A webhook is a message Stripe sends to your server. Verify its signature before acting, because anyone who finds the address can post to it. Then retrieve the session and check its payment status.

import express from 'express'
import Stripe from 'stripe'

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY)
const app = express()

// The signature is checked against the raw body, so do not parse JSON first.
app.post('/stripe/webhook', express.raw({ type: 'application/json' }), async (req, res) => {
  let event
  try {
    event = stripe.webhooks.constructEvent(
      req.body, req.headers['stripe-signature'], process.env.STRIPE_WEBHOOK_SECRET)
  } catch {
    return res.sendStatus(400)
  }
  if (event.type === 'checkout.session.completed' ||
      event.type === 'checkout.session.async_payment_succeeded') {
    await queue.add(event.data.object.id) // durable: survives a restart
  }
  res.sendStatus(200)
})

async function fulfil(sessionId) {
  const session = await stripe.checkout.sessions.retrieve(sessionId, { expand: ['line_items'] })
  if (session.payment_status === 'unpaid') return
  await orders.createOnce(sessionId, session.line_items.data) // unique on sessionId
}

queue and orders stand for your own job queue and database. If a session can have many line items, page through all of them.

Delayed payments

Some payment methods, such as bank debits, finish after checkout does. The session completes while the money is still processing. Stripe sends checkout.session.async_payment_succeeded if it later succeeds, and checkout.session.async_payment_failed if it does not.

That is why the example checks payment_status and listens for both success events. Tell a waiting buyer what is happening so they do not pay twice.

Prevent duplicate fulfilment

Assume your fulfilment function will run more than once for the same purchase, possibly at the same moment. Stripe's documentation says endpoints can receive an event more than once, delivery order is not guaranteed, and live deliveries are retried for up to three days.

  • Put a unique constraint on the Checkout Session ID in your orders table. A check in code alone loses races.
  • Create the order, licence and file grant in one transaction.
  • Record processed event IDs.
  • Give each email its own unique key so a retry cannot send it again.
  • Respond quickly and do slow work from a queue.

Then test it: send the same event twice, at once, and confirm one licence and one email.

Test it before real buyers do

The Stripe CLI can forward sandbox events to your machine. Stripe's fulfilment guide shows this command, which needs CLI version 1.51.0 or later:

stripe listen --all-snapshot --forward-to localhost:4242/stripe/webhook

It prints a signing secret for local use. Pay with a Stripe test card, then check four things: the event arrived, the signature passed, one order exists and one email went out.

Then try the awkward cases. Close the browser before the success page loads. Resend the same event. Use a test payment method that confirms later, and one that fails. Stop your worker, pay, and start it again. A buyer should still receive exactly one delivery.

A worked example

Say you sell a desktop tool with a two-device lifetime licence and an installer. One price ID maps to two grants: a licence with a limit of two, and access to the installer. On a confirmed payment you create both under the same order. The email contains a portal link instead of the file itself, so you can update the installer later without sending it again. If the order is refunded in full, the licence stops validating and the download button disappears, while the order stays in your records.

Secure file storage

Keep paid files in private storage. A hidden address on a public server is still public once someone shares it.

  • Store files in a private bucket or a folder your web server cannot serve.
  • Scan uploads and record a checksum and version.
  • Name stored objects with random identifiers, not customer or product names.

When a signed-in buyer asks for a file, check that they are still entitled to it, then create a link that expires soon. Object storage services support signed addresses for this.

Check entitlement every time a link is created. Keep expiry short, because a link can be forwarded while it is valid. If you limit downloads, count them on your server and leave room for failed attempts.

Software licence generation

  • Generate keys from a secure random source. Never derive them from an email address or order number.
  • Store the activation limit and expiry with the licence, not in the key.
  • Keep keys encrypted, with a separate keyed hash for lookups.
  • Validate on your server, identify each installation, and let buyers free a seat when they change computers.
  • Decide how your app behaves offline.

Refunds and access revocation

Decide the policy before the first refund. Stripe recommends listening at least for refund.created. A charge.refunded event also covers partial refunds, and refund.failed means money did not go back after all.

SituationA sensible starting rule
Full refundRevoke the licence and new downloads, or keep access if your terms promise it
Partial refundKeep access and flag for review
Dispute openedPause access and keep the record
Dispute closed in your favourRestore access

Disputes arrive as charge.dispute.created and charge.dispute.closed. Revoking means changing access, not deleting history.

Customer purchase recovery

People lose emails and change computers. Give them a way back:

  • A portal where the buyer enters their email and receives a one-time link.
  • The same response whether or not the address exists, so nobody can discover your customers.
  • Limits on requests, so the form cannot be used to flood an inbox.
  • Their licences, remaining activations and current downloads in one place.

If you use Stripe Managed Payments

Managed Payments makes Stripe the merchant of record for eligible digital sales and handles areas such as indirect tax. It still does not deliver your product: Lemon Squeezy's migration documentation states it does not deliver files, links or licence keys. Sellers leaving Lemon Squeezy should read the migration guide.

Build it or use a provider

Build your own ifUse a provider if
Your access rules are unusualYour needs are licences and downloads
Someone can watch webhooks, queues and emailNobody wants to be on call for them
You want full control of the data modelYou want a buyer portal without building one

Before choosing either, ask: can I export every key and order, what happens if the service is down, and what does leaving look like?

KeyMelon is one provider option, built for Stripe sellers: verified events, queued fulfilment, licence keys, private downloads and a buyer portal. It is in early access and currently runs against Stripe test payments. Its compatibility with Managed Payments has not been verified.

Launch checklist

  • Webhook signatures verified on the raw body
  • Payment status checked before access
  • Delayed success and failure handled
  • One order per session, enforced by the database
  • Private storage and short-lived links
  • Refund and dispute rules written and tested
  • A recovery path for buyers
  • Failed jobs visible to a person

Sources

Checked December 2, 2026. Providers change their documentation, so confirm anything time sensitive at the source. KeyMelon is independent and is not affiliated with or endorsed by Lemon Squeezy or Stripe.