Skip to content

About

Example payment gateway integration for Swell

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Swell Payment Gateway Example — Razorpay

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.

What it demonstrates

  • Declaring a payment extension in swell.json so 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 payment
    • payment.get_intent — verify and retrieve payment details after checkout
    • payment.charge — authorize/capture the payment when an order is submitted
    • payment.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 fetch and Web Crypto (HMAC signature verification) — compatible with the Cloudflare Workers runtime, no Node SDK required

How the pieces fit together

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

Project structure

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)

Setup

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 push

Then, in your test environment dashboard:

  1. Open Apps > Razorpay > Settings and enter your Razorpay Key Id and Secret key (use test mode keys while developing).
  2. Enable the payment method for checkout.

Notes

  • 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.

Documentation

License

MIT. See LICENSE. Contributions are welcome: see CONTRIBUTING.md.

About

Example payment gateway integration for Swell

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages