Skip to main content

Overview

The Moment React SDK enables seamless payment integration into any React application. Whether you need a modal overlay or an inline embedded checkout, the SDK provides a simple, declarative API with React hooks to handle the entire payment flow. Built with React and modern hooks, the SDK integrates naturally with your React workflow and state management.

Embedded Modal :


Key Features

  • React Hooks API – Declarative interface with useCheckout hook
  • Flexible Display Options – Modal or inline embedded checkout
  • TypeScript Support – Full type definitions included
  • Flexible Callbacks – Handle success, failure, cancellation, and retry events
  • Cross-Origin Safe – Secure iframe communication via postMessage
  • Environment Detection – Automatic sandbox / production switching
  • PCI Compliant – Sensitive payment data handled entirely in Moment’s secure iframe

Display Variants


How It Works

  1. Page loadsMomentProvider mounts (SDK init), then MomentCheckoutProvider mounts, registering your fetchClientToken, fetchSessionStatus, and event callbacks (onSuccess, onFailure, etc.)
  2. Customer clicks Paycheckout.launch() is called from your component
  3. SDK calls fetchClientToken — your callback creates a session on your backend (with a success_url) and returns the client_token
  4. Checkout opens in your chosen variant (modal or inline)
  5. Customer submits payment — Moment takes over the full page and redirects to the authentication page
  6. Customer completes authentication on the external page
  7. Customer is redirected back to your success_url with ?session_id=abc123 appended
  8. Page loads againMomentCheckoutProvider detects the session_id in the URL automatically and calls your fetchSessionStatus('abc123') callback
  9. Your callback fetches the status from your backend, which queries Moment API
  10. SDK routes to the right handleronSuccess, onFailure, onExpired, or onCancel — and removes session_id from the URL silently
  11. Webhooks are delivered to your backend regardless of outcome

Installation

NPM

Yarn

pnpm


Quick Start

1. Initialize the SDK

Set up two providers at the root of your app: MomentProvider initializes the SDK instance (equivalent to Moment(clientKey) in vanilla JS), and MomentCheckoutProvider configures the checkout product (equivalent to moment.checkout(config)). You provide two fetch callbacks — the SDK calls them automatically at the right moments, so you never manage tokens or session IDs manually. Backend endpoints:
server.js
Frontend — wrap your app with MomentProvider + MomentCheckoutProvider:
App.jsx

2. Open Checkout

Add useCheckout to any component and call checkout.launch() to trigger checkout:
Checkout.jsx

API Reference

MomentProvider (SDK level)

Initializes the Moment SDK instance — equivalent to Moment(clientKey) in vanilla JS. Provides the SDK context to all product providers nested below it. Must be the outermost Moment wrapper in your app. Example:

MomentCheckoutProvider (checkout product)

Initializes the checkout product — equivalent to moment.checkout(config) in vanilla JS. Must be a child of MomentProvider and a parent of any component using useCheckout. Example:

useCheckout(config)

React hook that returns a checkout object for triggering checkout from any component. Must be used inside MomentCheckoutProvider. Callbacks defined here are component-level and fire alongside any global callbacks on MomentCheckoutProvider. Returns a checkout object — checkout.launch(), checkout.close(), and checkout.submit().

checkout.launch(options)

Opens the payment checkout. Internally calls fetchClientToken from MomentCheckoutProvider to retrieve a fresh token, then opens checkout. The display variant (modal or inline) is determined from the JWT payload variant claim, which is set when creating the session on your backend. Returns a Promise that resolves with { checkoutUrl, variant } once the checkout is open. Examples:

checkout.close()

Manually closes the modal or inline checkout.

checkout.submit() Coming soon

Triggers payment submission from outside the checkout UI — useful when you want an external “Pay” button rather than relying on the checkout’s built-in submit. Only available for the inline variant.

Callback Strategies

The Moment React SDK offers flexible callback handling at both the global (MomentCheckoutProvider) and component (useCheckout hook) levels. Understanding when to use each approach helps you write cleaner, more maintainable code.

Global Callbacks (MomentCheckoutProvider Level)

Define callbacks once at the MomentCheckoutProvider level to apply consistent behavior across your entire application. ✅ Best for:
  • App-wide analytics tracking
  • Global error logging
  • Consistent redirect patterns
  • Shared notification systems
App.jsx

Component-Level Callbacks (useCheckout Hook)

Override or supplement global callbacks with component-specific behavior. ✅ Best for:
  • Component-specific state updates
  • Different flows for different payment contexts
  • Local UI feedback
  • Context-aware error handling
ProductCheckout.jsx

Callback Precedence

Important: Component-level callbacks do not override global callbacks. Both will fire:
  1. Component-level callback fires first (if defined)
  2. Global callback fires second (if defined)
This allows you to handle both component-specific and app-wide logic.

Example: Multi-Level Callback Architecture

App.jsx
SubscriptionPage.jsx

Advanced Usage

Custom Hook for Payment Logic

hooks/usePayment.ts

Using the Custom Hook

ProductCheckout.tsx

Security Best Practices

  1. Never expose your secret key (sk_...) in frontend code
  2. Always create sessions server-side using your secret key
  3. Validate webhooks to confirm payment status server-side
  4. Use HTTPS for all pages that include the SDK
  5. Handle token expiry by requesting a new token if needed

Next Steps