Skip to content
swellstoresPublic

About

JS library for building storefronts and checkouts with Swell ecommerce.

Topics

Resources

Stars

144 stars

Watchers

7 watching

Forks

Repository files navigation

Swell.js - Headless ecommerce storefront SDK

Universal JavaScript client for Swell's Frontend API, providing client-safe access to store and customer data. You can use it in JAMstack or SSR apps to:

  • Fetch products, categories, store settings, nav menus, and custom content
  • Create, recover, and update shopping carts
  • Build custom checkout and subscription flows
  • Authenticate customers and allow them to edit account details, orders, and subscriptions
  • Resolve linked content to dynamically generate page URLs
  • Format prices in the store's currency

This SDK implements a subset of operations available in Swell's Backend API and is authorized with a public key + session token, making it safe to use in any context. You should only use the Backend API server-side, and keep your secret keys stored as environment variables.

About Swell

Swell is an ecommerce platform with subscriptions and B2B pricing built in. Run your store on a Swell theme, or go headless with its APIs.

Checkout payments

Besides createElements, tokenize and handleRedirect (see the documentation), swell.payment offers helpers a checkout page needs:

  • tokenize({ card: { createIntent: false } }) stores a Stripe card without authorizing it now. tokenize({ card: { paymentMethod: 'pm_…' } }) authorizes a Stripe card the shopper already has (a saved card, a wallet's payment method) with an intent and 3-D Secure, like a new card, before the order is submitted. Both reuse the cart's intent while it still matches the card and amount.
  • Saferpay card and TWINT (twint) redirect to the Saferpay payment page from tokenize and come back through handleRedirect; iDEAL returns through handleRedirect({ ideal }) too.
  • Stripe iDEAL needs no element: tokenize({ ideal: {} }) without createElements redirects to the iDEAL page, where the shopper picks the bank, and returns through handleRedirect({ ideal }).
  • handleAsyncRedirect(order) sends the shopper to the gateway when a submitted order waits for an asynchronous payment.
  • setupPaymentMethod({ card }) saves a card without a cart, e.g. for an account page. On Stripe it mounts the Payment Element in setup mode with the store currency, and confirm() calls the host's required getSetupIntent() for { client_secret } and confirms that SetupIntent with 3-D Secure. Any other card gateway mounts nothing: confirm(fields) tokenizes the host's own card fields through card.createToken. Both resolve { gateway, token, brand, last4, exp_month, exp_year } for the host to save (Stripe adds test: true outside live mode); a Stripe error keeps its code, type and decline_code. destroy() removes the mounted fields, e.g. when the host's effect re-runs; it does nothing for the host's own fields. Quickpay, Saferpay and ConvesioPay reject with code: 'unsupported_gateway'.
  • Stripe card and iDEAL can render through the Stripe Payment Element instead of the split elements: pass paymentElement: true to both createElements and tokenize. Stripe draws the whole form (fields, labels, errors) and takes its look from appearance (Appearance API). The element mounts into #payment-element unless elementId is set; options go to elements.create('payment') over the defaults layout: 'tabs', fields.billingDetails: 'never' (billing details come from the cart) and no Apple Pay / Google Pay. tokenize re-reads the cart and updates the amount before elements.submit(), so a changed cart total needs no extra call.
const card = {
  paymentElement: true,
  appearance: {
    theme: 'stripe',
    labels: 'floating',
    variables: { colorPrimary: '#111' },
  },
  elementId: 'card-payment-element',
  onChange: (event) => setComplete(event.complete),
  onSuccess: () => submitOrder(),
  onError: (error) => showError(error.message),
};

await swell.payment.createElements({ card });
// on "Pay": validates, creates the PaymentMethod and the intent, stores them on the cart
await swell.payment.tokenize({ card });

// iDEAL: the same flag; tokenize redirects to the bank and returns through handleRedirect({ ideal })
await swell.payment.createElements({
  ideal: {
    paymentElement: true,
    appearance,
    elementId: 'ideal-payment-element',
  },
});

Deprecated

  • swell.app (app components rendered by swell-js) is deprecated and will be removed in the next major version. Apps render their components through @swell/apps-sdk/components instead; hosted checkout mounts payment components at the place checkout.payment.methods.

Documentation

📖 View Swell.js Documentation

About

JS library for building storefronts and checkouts with Swell ecommerce.

Topics

Resources

Stars

144 stars

Watchers

7 watching

Forks

Releases

Used by

Contributors

Languages