All articles

Payments

Streamline SEPA Bank Detail Collection: A Serverless Approach with Stripe and Cloudflare Workers

June 16, 2023 · 8 min read

Streamline SEPA Bank Detail Collection: A Serverless Approach with Stripe and Cloudflare Workers

Unlocking additional payment flows is transformative for businesses seeking growth. Low-code and no-code tools enable organizations to manage payment workflows directly on the Stripe platform, such as sending customers links to enter card details and auto-charging invoices - all without leaving the dashboard.

For larger transactions, businesses prefer payment methods beyond credit cards due to fees. In Europe, SEPA represents the bank-based scheme and requires different handling than card payments. Stripe currently supports saving SEPA details through Checkout sessions, which are short-lived, single-use sessions initiated by code. Users must agree to have their bank account debited - a mandatory compliance requirement for SEPA.

Generating Checkout sessions typically requires developers to add code to websites and create dedicated pages. However, small teams without significant development capacity face challenges implementing such changes.

One solution combines Stripe's Checkout sessions with Cloudflare Workers, enabling generation of shareable links that capture SEPA details for later charging. This serverless approach operates entirely within Cloudflare, eliminating the need for additional servers or website modifications.

What is Cloudflare, and what are Cloudflare Workers?

Cloudflare is a leading internet security and performance company powering significant internet infrastructure. They offer DNS management, SSL/TLS encryption, content delivery networks, and solutions enhancing website security, performance, and reliability.

Cloudflare Workers are JavaScript functions running directly on Cloudflare's infrastructure - between servers and end users. This positions code at the network edge, modifying requests before reaching backend servers. This enables complex caching strategies and content personalization.

Workers also provide serverless experiences by handling full user requests within the worker itself, eliminating server requirements and relying entirely on Cloudflare's network for hosting and serving content.

Creating a Cloudflare worker to accept SEPA details

Editing workers directly in the Cloudflare interface is possible, though this example uses local machine editing before deployment.

Cloudflare provides Wrangler, a Node.js-based tool handling project creation and deployment. Latest Node.js and npm versions are required.

Install Wrangler

After installing Wrangler, link your local installation to your Cloudflare account:

wrangler login

This command initiates an authorization process with Cloudflare. A browser dialog explains permissions being shared with the wrangler application. After authorizing, return to the console to complete login. If multiple Cloudflare accounts are linked to your login, you'll be prompted to select the desired account using keyboard navigation.

Create the project

Create a project and save the Stripe Node library:

npm init cloudflare sepa-details-example

This wizard guides basic configuration. Select "Hello World" for a thin shell to build upon. You'll be asked about TypeScript support (optional) and immediate deployment. Choosing deployment makes the "Hello World" example live at a URL based on project name and account name - for example, sepa-details-example.square1.workers.dev.

Once basic setup is complete, add the Stripe library:

npm i --save stripe

Saving the Stripe secret key

Creating Checkout sessions requires a Stripe secret key from the Stripe Dashboard. As sensitive information, avoid saving it in code. Instead, use Cloudflare's secrets storage:

wrangler secret put STRIPE_API_KEY

This prompts key entry and saves it as STRIPE_API_KEY. Verify stored secrets with wrangler secret list.

Creating a checkout session

Open src/worker.js and replace contents with code that loads the Stripe library, creates a checkout session for SEPA details, and redirects users:

const Stripe = require("stripe");

export default {
  fetch: handleRequest
};

async function handleRequest(request, env, ctx) {
  const stripe = Stripe(env.STRIPE_API_KEY, {
    httpClient: Stripe.createFetchHttpClient()
  });

  const session = await stripe.checkout.sessions.create({
    payment_method_types: ['sepa_debit'],
    mode: 'setup',
    customer_creation: 'always',
    success_url: 'https://www.example.com/?success=1',
    cancel_url: 'https://www.example.com/?retry=1'
  });

  return Response.redirect(session.url, 303);
}

Running wrangler locally and visiting the wrangler URL redirects to a checkout.stripe.com URL. Using test mode Stripe keys displays a "TEST MODE" badge, allowing testing with Stripe's test IBAN details. After entering details and confirming direct debit setup, the Stripe Dashboard shows the customer with attached SEPA details, ready for future use.

Handling post-save messaging

Previously, success_url and cancel_url pointed to example.com without informative messaging. Handle this within the worker by parsing URL parameters:

async function handleRequest(request, env, ctx) {
  const url = new URL(request.url);

  if (url.searchParams.get('success') === '1') {
    return showSuccess();
  } else if (url.searchParams.get('retry') === '1') {
    return showRetry();
  } else {
    return redirectToCheckout(env.STRIPE_API_KEY, url);
  }
}

Stripe redirects users with ?success=1 for successful completion or ?retry=1 for cancellation. Each handler returns its own Response with success or retry messaging.

Deploy worker to Cloudflare

With code ready, deploy to Cloudflare:

wrangler deploy

After deployment completes, the worker appears in Cloudflare. To serve it on your own domain, link a Workers Route under the relevant zone.

Stripe preparation

SEPA is available in test mode immediately upon account creation. Before going live, activate SEPA Direct Debit on your live account through Payment Methods settings. This may require additional verification, which can take time - don't delay this process.

Be aware of account limits. By default, SEPA transactions have €10,000 per-transaction limits, and new accounts face €10,000 weekly limits across the whole account (this increases over time). For processing more than €10,000 in initial weeks, contact Stripe support early to increase limits.

Beyond Checkout sessions

The included library is the full Stripe Node.js library, making the complete Stripe API available. Whether validating webhook signatures, listing products from your Stripe account, or redirecting users to the Billing Portal, you're not limited to Checkout sessions.

Alternatively, if selling specific items to users (books, courses, etc.), consider Stripe's Payment Links product. This lets you specify product details and receive a Stripe-hosted URL to share.

Keep reading

Related articles

Browse all articles →