Payment Method Element

API v0: update a card from your page

Let a subscriber replace their card inside your own page, through API v0 and the JavaScript SDK.

This page is for workspaces that integrate through API v0. It lets a subscriber replace the card their Subscription is charged with, inside your own page. Card data goes straight to Fungies; your page never handles it. In the dashboard, the same job is done by the hosted Update payment method page.

The frame loads only on domains authorized for your workspace. The feature must also be enabled for your workspace: minting a session returns 403 until it is.

How it works

  1. Your server mints a short-lived session for one subscription.
  2. Your page passes that session to the @fungies/fungies-js package, which opens the form as an overlay or inside a container you choose.
  3. The customer enters a new card. Fungies saves it as the subscription's payment method.
  4. The SDK fires a browser event on your page, and Fungies sends a webhook to your server.

Mint a session

Call this from your backend. The secret key must never reach the browser.

curl -X POST "https://api.fungies.io/v0/subscriptions/L8VQK3N2M7KpQ9nR/paymentMethodSession" \
  -H "x-fngs-public-key: pub_your_public_key" \
  -H "x-fngs-secret-key: sec_your_secret_key"
{
  "status": "success",
  "data": {
    "sessionToken": "pmst_...",
    "expiresAt": 1790000000000,
    "url": "https://yourstore.fungies.io/payment-method-element"
  }
}
FieldWhat it is
sessionTokenBearer token for this one subscription. Hand it to the SDK; never log it or put it in a URL.
expiresAtWhen the token stops working at the latest (Unix time in milliseconds). Tokens live 15 minutes.
urlThe page the SDK loads. It carries no token, except as noted below.

For a subscription sold through the dashboard's checkout elements, url is a hosted page carrying a single-use code, and sessionToken is that code: the page opens once, within 5 minutes (expiresAt). Send the customer to it at once instead of loading it through the SDK; never log or store it, and mint again for another attempt.

Mint a fresh token for each attempt rather than storing one. Minting is rate-limited per subscription and returns 429 over the limit. The subscription must be active, trialing, past_due, unpaid or paused; any other state returns 400.

Install

npm install @fungies/fungies-js

PaymentMethod.update() works without calling Fungies.Initialize().

Open the element

Fetch a session from your own backend (see Mint a session), then pass the sessionToken and url to the SDK.

import { Fungies } from "@fungies/fungies-js";

const { sessionToken, url } = await fetch("/api/payment-method-session", {
  method: "POST",
}).then((r) => r.json());

Fungies.PaymentMethod.update({
  sessionToken,
  url,
  settings: {
    mode: "overlay", // 'overlay' | 'embed'
    frameTarget: "target-element-id", // required with 'embed', ignored by 'overlay'
  },
});

// Close it from your code (fires no event)
Fungies.PaymentMethod.close();

The token reaches the element over postMessage only. There is no data-* attribute version of this flow on purpose: a token in the DOM can be read by every script on your page and is captured by session-replay tools.

settings.mode must be 'overlay' or 'embed'. Any other value, a missing sessionToken or an invalid url logs a console warning and opens nothing.

Events

EventWhen it fires
fungies:payment-method:updatedThe new payment method was saved. An overlay closes itself.
fungies:payment-method:failedThe session can't be used: expired, already used or invalid, or the frame didn't answer within 15 seconds (for example, your domain isn't authorized). Mint a new token to retry.
fungies:payment-method:closeThe customer closed the overlay. Not fired by PaymentMethod.close().

At most one of updated or failed fires per session. A declined card fires nothing: the form stays open and the customer can try another card.

The event names are exported as DOM_PAYMENT_METHOD_EVENTS. To import them without loading the SDK, for example in server-side code, use @fungies/fungies-js/events.

React example

import { useEffect } from "react";
import { DOM_PAYMENT_METHOD_EVENTS, Fungies } from "@fungies/fungies-js";

export function UpdateCardButton() {
  useEffect(() => {
    const onUpdated = () => {
      // Refresh the card shown on your page. Act on the webhook for anything that must happen.
    };
    const onFailed = () => {
      // The session is dead. Let the customer try again with a fresh one.
    };

    document.addEventListener(DOM_PAYMENT_METHOD_EVENTS.UPDATED, onUpdated);
    document.addEventListener(DOM_PAYMENT_METHOD_EVENTS.FAILED, onFailed);
    return () => {
      document.removeEventListener(
        DOM_PAYMENT_METHOD_EVENTS.UPDATED,
        onUpdated,
      );
      document.removeEventListener(DOM_PAYMENT_METHOD_EVENTS.FAILED, onFailed);
    };
  }, []);

  const open = async () => {
    const { sessionToken, url } = await fetch("/api/payment-method-session", {
      method: "POST",
    }).then((r) => r.json());

    Fungies.PaymentMethod.update({
      sessionToken,
      url,
      settings: { mode: "overlay" },
    });
  };

  return <button onClick={open}>Update card</button>;
}

Know when the card changed

  • In the browser: the SDK fires fungies:payment-method:updated. See Events.
  • On your server: subscribe your webhook to subscription_payment_method_updated. It carries the new method's type (plus brand and lastDigits for cards), and fires for every card change on a subscription that is not trialing, including ones made in the customer portal. It is not sent for any card change while the subscription is trialing. Deduplicate on setupIntentId.

Use the webhook for anything that must happen, like clearing a dunning flag. The browser event only fires while your page is open.

Last updated