For the complete documentation index, see llms.txt. This page is also available as Markdown.

First Payment

In the next few steps you will authorize the payment, handle errors, capture funds, and process refunds. And then you will be ready to send payment to any payment processor, without writing specialized code for each.

If you are not PCI compliant, first create a Stripe client authentication token on your backend, use the returned client_secret to initialize Stripe.js / Stripe Elements on the frontend, tokenize the payment method in the browser, and then use the resulting payment_method_id to authorize the payment.

If your payment processor API keys are enabled to accept PCI compliant raw card data directly, jump to Authorize with Raw Card Details.

Non-PCI Stripe flow: get the payment_method_id first

1. Create a client authentication token on your backend

Use Prism's client authentication token flow to fetch the Stripe client_secret required by Stripe.js.

const {
  MerchantAuthenticationClient,
  IntegrationError,
  ConnectorError,
  NetworkError,
  types,
} = require("hyperswitch-prism");

// Reuse stripeConfig from installation.md
const authClient = new MerchantAuthenticationClient(stripeConfig);

async function createClientAuthenticationToken() {
  try {
    const response = await authClient.createClientAuthenticationToken({
      merchantClientSessionId: "client_session_001",
      payment: {
        amount: {
          minorAmount: 1000,
          currency: types.Currency.USD,
        },
      },
      testMode: true,
    });

    const clientSecret =
      response.sessionData?.connectorSpecific?.stripe?.clientSecret?.value;

    console.log("Client secret:", clientSecret);
    return clientSecret;
  } catch (error) {
    if (error instanceof IntegrationError) {
      console.error(
        "Integration error:",
        error.errorCode,
        error.message,
      );
    } else if (error instanceof ConnectorError) {
      console.error(
        "Connector error:",
        error.errorCode,
        error.message,
      );
    } else if (error instanceof NetworkError) {
      console.error("Network error:", error.errorCode, error.message);
    } else {
      console.error(
        "Client auth token creation failed:",
        error.message || error,
      );
    }
    throw error;
  }
}

2. Use the client_secret in Stripe.js / Stripe Elements

Initialize Stripe Elements on the frontend, collect the card details there, and let Stripe return a tokenized payment_method_id.

Authorize with Payment Method ID

Use the payment_method_id returned by Stripe.js / Stripe Elements to authorize the payment:

Authorize with Raw Card Details (PCI Compliant)

If you're PCI compliant and collect card details directly:

Complete Payment Flow

After authorization, capture funds and handle refunds:

Error Scenarios

Declined Card

Network Timeout

Business Use Cases

E-commerce: Two-Step Flow

Authorize at checkout. Capture when you ship.

SaaS: Immediate Capture

For digital goods, capture immediately.

Marketplace: Partial Refund

Customer returns one item from a multi-item order.

Key Takeaways

  • One error handler works for all connectors

  • Unified error codes tell you exactly what happened

  • connectorTransactionId is the key identifier for all operations

  • Same code works for Stripe, Adyen, PayPal, and 50+ more

See extending payment flows for subscriptions, 3D Secure, and more.

Last updated

Was this helpful?