An example Swell app that implements a complete credit card payment gateway using a payment extension, with Razorpay as the gateway. Use it as a reference for integrating any payment provider with Swell checkout.
- Declaring a payment extension in
swell.jsonso the gateway appears as a payment method in Swell - Handling the four payment extension events with app functions:
payment.create_intent— create the gateway-side customer and order before paymentpayment.get_intent— verify and retrieve payment details after checkoutpayment.charge— authorize/capture the payment when an order is submittedpayment.refund— refund a captured payment
- A checkout component (
components/Razorpay.tsx, Preact and@swell/apps-sdk) that Swell's checkout renders for the payment method. It opens Razorpay Checkout, tokenizes the card, and returns the token for the cart's billing - App settings for gateway credentials, with the public key exposed to the storefront and the secret key kept server-side
- A minimal gateway REST client built on
fetchand Web Crypto (HMAC signature verification) — compatible with the Cloudflare Workers runtime, no Node SDK required
Checkout (storefront) Swell platform Razorpay
───────────────────── ────────────── ────────
components/Razorpay.tsx
└─ on('submit')
└─ call('createIntent') ─────────▶ payment.create_intent ────────▶ create customer + order
└─ open Razorpay Checkout ◀────────────────────────────────────── card entry + tokenization
└─ call('getIntent') ────────────▶ payment.get_intent ───────────▶ verify signature, fetch token
└─ return { billing } (card token)
Order submitted ────────────────────────▶ payment.charge ───────────────▶ create/capture payment
Refund issued ──────────────────────────▶ payment.refund ───────────────▶ refund payment
swell.json App config with the payment extension declaration
settings/card.json Gateway credential settings (key_id public, key_secret private)
functions/
card-intent-create.ts payment.create_intent handler
card-intent-get.ts payment.get_intent handler (signature verification)
card-charge.ts payment.charge handler (authorize + capture)
card-refund.ts payment.refund handler
lib/razorpay.ts Razorpay REST client (fetch + Web Crypto HMAC)
lib/swell.ts App settings helper
components/
Razorpay.tsx Checkout component (Preact, @swell/apps-sdk)
lib/razorpay.ts Checkout helpers (intent data, currency subunits)
Requires Swell CLI 2.9.28 or later and a Swell account.
npm install -g @swell/cli
swell login
git clone [email protected]:swellstores/payment-example-app.git
cd payment-example-app
npm install
swell app pushThen, in your test environment dashboard:
- Open Apps > Razorpay > Settings and enter your Razorpay Key Id and Secret key (use test mode keys while developing).
- Enable the payment method for checkout.
- Amounts are converted to gateway subunits (
components/lib/razorpay.ts) — Razorpay expects paise/cents. - The charge flow supports both immediate capture and authorize-only (
captured: false), and reuses a saved card token for recurring payments when no checkout intent is present. - Gateway event handlers return
{ success, transaction_id }(or{ success: false, error }), which Swell records on the payment. - This app is an example for learning and building from — review it carefully and test against your gateway's sandbox before using any of it in production.
MIT. See LICENSE. Contributions are welcome: see CONTRIBUTING.md.