Elements Checkout Recipes
Ready-made Elements Checkout layouts: wallets and card, Apple Pay with card fields, card first, a one-line card, wallets only, billing address and promo codes.
Each recipe is one way to build the same purchase, and each is a layout in the elements-checkout example, which you can switch between on the page. Every recipe goes inside <StripeCheckout> and pays through Superwall, with the same callbacks.
import { StripeCheckout } from "superwall/checkout/stripe";
<StripeCheckout product={plan} options={options}>
{/* a recipe */}
</StripeCheckout>;options here is the design from Style Elements Checkout. Every element takes all of Stripe's own options; each recipe shows only the ones it needs.
| Recipe | Stripe pieces | Purchase button |
|---|---|---|
| Apple Pay, Link and card | Superwall's Apple Pay, ExpressCheckoutElement (Link), LinkAuthenticationElement, PaymentElement | <PurchaseButton> |
| Apple Pay only | Superwall's Apple Pay | None |
| Wallets and card | ExpressCheckoutElement, LinkAuthenticationElement, PaymentElement | <PurchaseButton> |
| Apple Pay and card fields | ExpressCheckoutElement (Apple Pay), the three card fields | Your own |
| Card first | LinkAuthenticationElement, PaymentElement, ExpressCheckoutElement | <PurchaseButton> |
| Card fields | CardNumberElement, CardExpiryElement, CardCvcElement | Your own |
| Single card line | CardElement | Your own |
| Wallets only | ExpressCheckoutElement | None |
| With a billing address | LinkAuthenticationElement, AddressElement, PaymentElement | <PurchaseButton> |
| Promo code | LinkAuthenticationElement, PaymentElement, useDiscount() | <PurchaseButton> |
The recipes with your own email box use your own button, because <PurchaseButton> doesn't show the email_required error. See Your own purchase button.
Apple Pay, Link and card
The layout we recommend for these three. Superwall's own Apple Pay sheet on your button, Link alone in Stripe's express row, and the card form with every wallet off, so no wallet shows twice:
import { PurchaseButton } from "superwall/checkout";
import {
ExpressCheckoutElement,
LinkAuthenticationElement,
PaymentElement,
} from "superwall/checkout/stripe";
import { useApplePay, usePurchase } from "superwall/hooks";
const ApplePayButton = ({ product }: { product: string }) => {
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" })}
/>
);
};
<ApplePayButton product={plan} />
<ExpressCheckoutElement
options={{ paymentMethods: { applePay: "never", googlePay: "never", link: "auto" } }}
/>
<LinkAuthenticationElement />
<PaymentElement options={{ layout: "tabs", wallets: { applePay: "never", googlePay: "never" } }} />
<PurchaseButton>Subscribe</PurchaseButton>The Apple Pay sheet's callbacks reach usePurchase() only, not <StripeCheckout>. See Elements Checkout Events. The step-by-step guide builds this one.
Apple Pay only
Superwall's Apple Pay sheet alone, the same sheet a checkout: "applePay" paywall opens. In elements mode the paywall prepares it from the form's price. Say why when it isn't ready, since nothing else is on the page:
const { product: wallet } = useApplePay(plan);
const { purchase } = usePurchase();
if (wallet.status === "ready") {
return <button className="apple-pay" onClick={() => void purchase(plan, { checkout: "applePay" })} />;
}
return <p>{wallet.status === "preparing" ? "Getting Apple Pay ready…" : wallet.message}</p>;Wallets and card
Stripe's express row with every wallet it can show, then the email and the card form:
<ExpressCheckoutElement options={{ buttonHeight: 52 }} />
<LinkAuthenticationElement />
<PaymentElement options={{ layout: "tabs" }} />
<PurchaseButton>Subscribe</PurchaseButton>The express row renders nothing on a device with no wallet. To show an "or pay with card" divider only when there's a wallet above it, wait for onReady:
const [hasWallet, setHasWallet] = React.useState(false);
<ExpressCheckoutElement
onReady={({ availablePaymentMethods }) =>
setHasWallet(Object.values(availablePaymentMethods ?? {}).some(Boolean))
}
/>
{hasWallet ? <p className="divider">or pay with card</p> : null}Apple Pay and card fields
Only Apple Pay in the express row, above separate card fields. paymentMethods turns off every other wallet; where the device has no Apple Pay, the row renders nothing and the card fields stand alone:
import {
CardCvcElement,
CardExpiryElement,
CardNumberElement,
ExpressCheckoutElement,
} from "superwall/checkout/stripe";
<StripeCheckout product={plan} email={email}>
<ExpressCheckoutElement
options={{ paymentMethods: { applePay: "auto", googlePay: "never", link: "never" } }}
/>
<input type="email" value={email} onChange={(event) => setEmail(event.target.value)} />
<CardNumberElement options={{ style, showIcon: true }} />
<CardExpiryElement options={{ style }} />
<CardCvcElement options={{ style }} />
<PayButton />
</StripeCheckout>;PayButton is your own purchase button. This uses Stripe's Apple Pay in the express row; for Superwall's own sheet, use the button from Apple Pay, Link and card instead.
Card first
The elements are plain components, so put them in any order. Here the card form comes first and the wallets below it:
<LinkAuthenticationElement />
<PaymentElement options={{ layout: "accordion", wallets: { applePay: "never", googlePay: "never" } }} />
<p className="divider">or pay with a wallet</p>
<ExpressCheckoutElement options={{ layout: { maxColumns: 2, maxRows: 1 } }} />
<PurchaseButton>Subscribe</PurchaseButton>Card fields
Stripe's separate card number, expiry and CVC fields, the way many existing checkouts are built, with your own email box passed to <StripeCheckout email>:
const [email, setEmail] = React.useState("");
<StripeCheckout product={plan} email={email}>
<input type="email" autoComplete="email" value={email} onChange={(event) => setEmail(event.target.value)} />
<CardNumberElement className="input" options={{ style, showIcon: true }} />
<CardExpiryElement className="input" options={{ style }} />
<CardCvcElement className="input" options={{ style }} />
<PayButton />
</StripeCheckout>;The card fields pay without elements.submit(). If a PaymentElement is also mounted, it wins over them. Style them with options.style, see Style the card fields.
Single card line
Stripe's one-line CardElement, with your own email box:
import { CardElement } from "superwall/checkout/stripe";
<StripeCheckout product={plan} email={email}>
<input type="email" value={email} onChange={(event) => setEmail(event.target.value)} />
<CardElement className="input" options={{ style, hidePostalCode: true }} />
<PayButton />
</StripeCheckout>;Wallets only
Only the one-tap wallets, with no purchase button. The wallet pays on its own and asks for the email itself:
<StripeCheckout product={plan}>
<ExpressCheckoutElement options={{ buttonHeight: 52 }} />
</StripeCheckout>Closing the wallet reports onAbandon.
With a billing address
Stripe's billing address goes onto the payment method, and stripeMetadata onto the subscription:
import { AddressElement } from "superwall/checkout/stripe";
<StripeCheckout product={plan} stripeMetadata={{ source: "web-paywall" }}>
<LinkAuthenticationElement />
<AddressElement options={{ mode: "billing" }} />
<PaymentElement options={{ layout: "accordion" }} />
<PurchaseButton>Subscribe</PurchaseButton>
</StripeCheckout>;AddressElement with mode: "shipping" isn't supported: a subscription ships nothing.
Promo code
A code box on useDiscount(). A redeemed code re-prices the form: useCheckout().amounts and the charge both follow the discounted price.
import { useDiscount } from "superwall/hooks";
const PromoCode = () => {
const { redeem, clear, applied, isRedeeming } = useDiscount();
const [code, setCode] = React.useState("");
const [refused, setRefused] = React.useState(false);
if (applied) {
return (
<p>
{applied.code} applied <button onClick={clear}>Remove</button>
</p>
);
}
return (
<form
onSubmit={async (event) => {
event.preventDefault();
setRefused(false);
const result = await redeem(code);
if (!result.valid) setRefused(true);
}}
>
<input value={code} onChange={(event) => setCode(event.target.value)} />
<button disabled={!code.trim() || isRedeeming}>Apply</button>
{refused ? <p role="alert">That code isn't valid.</p> : null}
</form>
);
};Put <PromoCode /> inside <StripeCheckout> next to the card form. A 100% discount makes amounts.dueNow 0 with no trial, so check amounts.trialDays before you say "free trial". See Promotion codes.
Your own purchase button
<PurchaseButton> shows nothing when the form is incomplete. Build your own on useCheckout().submit() to show why, for example when your own email box is empty:
import { useCheckout } from "superwall/checkout";
const PayButton = () => {
const { canSubmit, status, submit } = useCheckout();
const [problem, setProblem] = React.useState<string>();
return (
<>
{problem ? <p role="alert">{problem}</p> : null}
<button
disabled={!canSubmit}
onClick={async () => {
setProblem(undefined);
const result = await submit();
if (result.status === "incomplete") setProblem(result.error.message || "Check your details.");
}}
>
{status === "paying" ? "Paying…" : "Subscribe"}
</button>
</>
);
};A declined card isn't incomplete: it reports onFailure, and useCheckout().failure holds it. See submit() results.
Related
- Elements Checkout: requirements and the full build
- Elements Checkout Reference: every prop, and which elements work
- Style Elements Checkout: the
optionsandstylethese recipes use
How is this guide?
Style Elements Checkout
Make Stripe's elements match your paywall: appearance variables and rules from your design tokens, fonts that load, and styles for the card fields.
Migrate a Stripe Checkout to Elements Checkout
Move an existing custom Stripe Elements checkout onto Superwall in 8 steps: keep your elements and design, and delete your payment backend.