Elements Checkout

Elements Checkout

Build your own checkout form from Stripe's official elements: Apple Pay, Link and a card form on the paywall, while Superwall takes the payment.

Elements checkout lets a web paywall build its own checkout form from Stripe's official elements: wallet buttons, a card form, a billing address and your own purchase button, laid out and styled however you like. Superwall still does the purchase. It prices the form from the product, takes the payment, runs 3D Secure and the free trial, and completes the purchase with entitlements and redemption, the same way the sheet and Apple Pay do.

It's for web paywalls and teleport checkout routes that want payment on the page itself, in the paywall's own design, instead of in Superwall's sheet. If you already run a custom Stripe checkout, your elements carry over almost unchanged.

import { PurchaseButton } from "superwall/checkout";
import {
  ExpressCheckoutElement,
  LinkAuthenticationElement,
  PaymentElement,
  StripeCheckout,
} from "superwall/checkout/stripe";

<StripeCheckout product="annual" onComplete={...} onFailure={...} onAbandon={...}>
  <ExpressCheckoutElement />
  <LinkAuthenticationElement />
  <PaymentElement />
  <PurchaseButton>Subscribe</PurchaseButton>
</StripeCheckout>

Not sure this is the mode you need? See Apple Pay, Sheet or Elements?.

Requirements

  • Stripe products. The form sells Stripe products only, declared as {environment}:{priceId}:{offer}, see Products. Stripe keys and products are set up in the dashboard, see Web Checkout.
  • A host that takes payment in the paywall. A hosted web paywall, the Web SDK, or a teleport checkout route, with a public API key. A native SDK must be 4.10.8 or later. See Where the form can't take payment.
  • Subscriptions. The form pays for a recurring price, with or without a free trial. A one-time price reports session_unsupported.
  • Nothing to install from Stripe. Every element comes from superwall/checkout/stripe. A paywall that never imports it ships none of Stripe's React code.

Turn it on

Set the paywall's checkout mode to elements in config.ts:

config.ts
export default definePaywall({
  checkout: { mode: "elements", prefetch: "trial" },
  products: {
    trial: "live:price_1ABC…:7days-free",
    monthly: "live:price_1DEF…:no-trial",
  },
});

prefetch loads that plan's price as the paywall opens, so the form renders without a wait. <StripeCheckout> loads any other plan's price when the shopper picks it. Nothing is created in Stripe until the shopper pays.

A plain purchase() on an elements paywall still works: it opens Superwall's sheet.

This is the layout we recommend for Apple Pay, Link and a card, and the default layout of the elements-checkout example:

  • Superwall's own Apple Pay sheet on a button of yours. It's the best-instrumented Apple Pay path, the same one checkout: "applePay" opens.
  • Link on its own in Stripe's express row.
  • Stripe's card form with every wallet turned off, so no wallet shows twice, and <PurchaseButton> to pay.

Use the full ExpressCheckoutElement with every wallet in it only when you want all express methods to appear together.

  1. 1

    Start from the example

    superwall create --example elements-checkout
    superwall dev

    Point the identifiers in config.ts at Stripe products in your dashboard. Until they match, Studio lists the missing ones and the form reports unknown_product.

  2. 2

    Wrap the form in StripeCheckout

    <StripeCheckout> sells one product. Pass the selected plan; switching plans re-prices the form, and Stripe's fields keep what the shopper typed.

    import { StripeCheckout } from "superwall/checkout/stripe";
    
    const [plan, setPlan] = React.useState<"trial" | "monthly">("trial");
    
    <StripeCheckout product={plan}>
      {/* the form */}
    </StripeCheckout>;
  3. 3

    Add the Apple Pay button

    In elements mode the paywall prepares Superwall's Apple Pay wallet from the same price as the form. Show your button when useApplePay(product) says it's ready, and open the wallet with purchase(product, { checkout: "applePay" }):

    import { useApplePay, usePurchase } from "superwall/hooks";
    
    const ApplePayButton = ({ product }: { product: "trial" | "monthly" }) => {
      const { product: wallet } = useApplePay(product);
      const { purchase } = usePurchase();
      if (wallet.status !== "ready") return null;
      return (
        <button
          type="button"
          className="apple-pay"
          aria-label="Pay with Apple Pay"
          onClick={() => void purchase(product, { checkout: "applePay" })}
        />
      );
    };

    Where Apple Pay isn't available, such as in desktop Chrome or on a device with no card in Wallet, the button doesn't render and the card form stands alone. See Show an Apple Pay button for every reason it can be missing.

  4. 4

    Add Link in the express row

    ExpressCheckoutElement takes Stripe's options. Turn off Apple Pay and Google Pay so only Link shows:

    import { ExpressCheckoutElement } from "superwall/checkout/stripe";
    
    <ExpressCheckoutElement
      options={{
        buttonHeight: 52,
        paymentMethods: { applePay: "never", googlePay: "never", link: "auto" },
      }}
    />

    A shopper who pays with Link here completes the purchase without your purchase button. Closing Link reports onAbandon.

  5. 5

    Add the email and card fields

    Every purchase needs the shopper's email: it's the Stripe customer's email, for the receipt, the redemption link and the billing portal. LinkAuthenticationElement collects it, signs Link shoppers in, and fills itself in when the paywall already knows the user's email attribute.

    import { LinkAuthenticationElement, PaymentElement } from "superwall/checkout/stripe";
    
    <LinkAuthenticationElement />
    <PaymentElement
      options={{ layout: "tabs", wallets: { applePay: "never", googlePay: "never" } }}
    />

    The wallets are off in PaymentElement because the Apple Pay button and the express row already offer them.

  6. 6

    Add the purchase button

    <PurchaseButton> pays with what the shopper entered. It's disabled until the form can take payment and while it pays, and sets data-status so you can style each state:

    import { PurchaseButton, useCheckout } from "superwall/checkout";
    
    const DueToday = () => {
      const { amounts } = useCheckout();
      if (!amounts) return null;
      return <p>Due today: {formatAmount(amounts.dueNow, amounts.currency)}</p>;
    };
    
    <DueToday />
    <PurchaseButton className="cta">Start free trial</PurchaseButton>

    useCheckout() works anywhere inside <StripeCheckout>. amounts.dueNow is 0 during a free trial, and amounts.trialDays says how long it is. A 100% discount also makes dueNow 0, so check trialDays before you say "free trial". Amounts are in the currency's minor units.

    Unlike a store purchase, a card payment opens no sheet. Show that it's paying, with a "Paying…" label or a spinner, so the shopper doesn't tap again.

  7. 7

    React to the purchase

    <StripeCheckout> takes the same callbacks as usePurchase(): onStart, onSubmit, onComplete, onAbandon and onFailure. A declined card keeps the form open and reports onFailure with a failure reason; useCheckout().failure holds it until the next payment starts.

    <StripeCheckout
      product={plan}
      onComplete={() => track("subscribed")}
      onFailure={(failure) => setError(failure.message ?? "Your payment was declined.")}
    >

    usePurchase()'s own callbacks hear this form's purchases too, and the native SDK gets the same stripe_checkout_* events as for the sheet.

  8. 8

    Try it with a test card

    Push the paywall and open its live URL in a test-mode project: superwall dev never takes payment. Then pay with:

    • 4242 4242 4242 4242, any future date and CVC: pays.
    • 4000 0000 0000 0002: declined, the form stays open.
    • 4000 0025 0000 3155: asks for 3D Secure.

    See Push, promote & publish.

Where the form can't take payment

useCheckout().status is "unavailable" where the form can't pay, with a reason and an English message for logs. Render a fallback for it, so nothing on the page looks broken:

WherereasonWhat a purchase does instead
Inside the iOS appunsupported_hostpurchase() opens Superwall's hosted checkout, or your teleport route
Studio (superwall dev)unsupported_hostStudio's purchase prompt
A native SDK below 4.10.8unsupported_hostThe hosted checkout page
A host with no public API keymissing_public_api_keyNothing to pay with
checkout is external or not setexternal_checkoutSuperwall's hosted checkout

The form is also unavailable for a product it can't sell, such as an App Store product (unsupported_product), or when Stripe.js doesn't load (store_load_failed). It also reports session_failed when fetching the product's price fails, and session_unsupported for a price the form can't pay, such as a one-time price or an account on Stripe managed payments.

const { status, message } = useCheckout();
if (status === "unavailable") return <p>Payment isn't available here.</p>;

Limitations

  • Cards, wallets and Link only. Redirect and bank-debit payment methods, such as iDEAL, Klarna, Afterpay, Cash App Pay, SEPA and ACH, aren't supported yet. The form drops them from paymentMethodTypes with a console warning, and a payment of any other type fails with card_not_supported before anything is charged.
  • No shipping, tax IDs or saved cards. The form pays as a new customer each time.
  • Not inside the iOS app. A Stripe purchase in the app always leaves for the browser.
  • Prepaid cards can't be blocked in the form. prepaidCards: "block" applies only to the sheet fallback.
  • Import elements only from superwall/checkout/stripe. Your own copy of @stripe/react-stripe-js has its own React context, so its elements throw "Could not find Elements context".

How is this guide?

On this page