PLAYGROUND real router · preview checkout
Fork on GitHub Run the example
PREVIEW route · ch-inline flow · inline
S stitch example app
← Back to plans
UPGRADING TO

Pro plan

For small teams

Pro · monthly
One-time payment
CHF 27.00
SubtotalCHF 27.00
VAT / Taxcalculated at confirmation
Total today CHF 27.00
    route · ch-inline

    How would you like to pay?

    you@stitch.example
    4242 4242 4242 4242 V M
    MM / YY
    CVC

    Powered by stripe via tenderlane · By paying, you agree to the Terms.

    RULES · routing.ts
    first-match-wins · 5 rules · 1 fallback
    fallback matched
    CONTEXT · the inputs your rules read
    country
    currency
    plan
    user.tier
    flags
    inline-checkout
    3ds-always
    localized-receipts
    RESOLVED CONTEXT
     
    EVENT STREAM
    onRouteEvaluated · onStateChange
    context routing state adapter
    SOURCE · the code that wires this playground
    import { TenderlaneCheckoutForm, TenderlaneProvider } from '@tenderlane/react';
    import { stripeProvider } from '@tenderlane/stripe';
    import { StripePaymentElement } from '@tenderlane/stripe/react';
    import { routing } from './routing';
    
    const stripe = stripeProvider({
      publishableKey: 'pk_test_...',
      serverEndpoint: '/api/payments',
    });
    
    export function Checkout({ country }: { country: string }) {
      return (
        <TenderlaneProvider config={{ context: { country }, providers: [stripe], routing }}>
          <TenderlaneCheckoutForm
            input={{
              items: [{ sku: 'pro-plan', quantity: 1 }],
              successUrl: 'https://shop.example/success',
              cancelUrl: 'https://shop.example/cancel',
            }}
            elements={{ stripe: StripePaymentElement }}
          >
            {({ submit, canSubmit, error }) => (
              <>
                <button disabled={!canSubmit} onClick={submit}>
                  Pay
                </button>
                {error && <p role="alert">{error.message}</p>}
              </>
            )}
          </TenderlaneCheckoutForm>
        </TenderlaneProvider>
      );
    }
    import { createRulesRouter } from '@tenderlane/core';
    import type { RouteTarget, RoutingRule } from '@tenderlane/core';
    
    // First match wins. Rules are plain data: keep them in code, a database or a feature flag.
    export const rules = [
      {
        id: 'ch-inline',
        when: { country: 'CH', currency: 'chf' },
        use: { provider: 'stripe', flow: 'inline', paymentMethods: ['card', 'twint'] },
      },
      {
        id: 'nl-ideal',
        when: { country: 'NL', currency: 'eur' },
        use: { provider: 'stripe', flow: 'inline', paymentMethods: ['card', 'ideal', 'sepa_debit'] },
      },
      {
        id: 'de-klarna',
        when: { country: 'DE', currency: 'eur' },
        use: { provider: 'stripe', flow: 'inline', paymentMethods: ['card', 'sepa_debit', 'klarna'] },
      },
      {
        id: 'gb-card',
        when: { country: 'GB', currency: 'gbp' },
        use: { provider: 'stripe', flow: 'inline', paymentMethods: ['card'] },
      },
      {
        id: 'us-polar',
        when: { country: 'US' },
        use: { provider: 'polar', flow: 'hosted', paymentMethods: ['card'] },
      },
    ] satisfies RoutingRule[];
    
    // Everywhere else: the provider's own hosted page.
    export const fallback = {
      provider: 'stripe',
      flow: 'hosted',
      paymentMethods: ['card'],
    } satisfies RouteTarget;
    
    export const routing = createRulesRouter({ rules, fallback });
    // app/api/payments/route.ts
    import { createInlineCatalog } from '@tenderlane/core';
    import { createTenderlaneHandler } from '@tenderlane/core/server';
    import { stripeServerAdapter } from '@tenderlane/stripe/server';
    import { routing } from './routing';
    
    const catalog = createInlineCatalog({
      'pro-plan': { name: 'Pro plan', pricing: { amount: 2900, currency: 'chf' } },
    });
    
    const secretKey = process.env.STRIPE_SECRET_KEY;
    if (!secretKey) throw new Error('STRIPE_SECRET_KEY is not set');
    
    const handler = createTenderlaneHandler({
      providers: [stripeServerAdapter({ secretKey })],
      catalog, // the only source of prices
      routing, // rejects a request for a provider the rules would not select
      resolveContext: (request) => ({
        country: request.headers.get('x-vercel-ip-country') ?? undefined,
      }),
    });
    
    export const POST = handler.POST;