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
- Your server mints a short-lived session for one subscription.
- Your page passes that session to the
@fungies/fungies-jspackage, which opens the form as an overlay or inside a container you choose. - The customer enters a new card. Fungies saves it as the subscription's payment method.
- 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"
}
}| Field | What it is |
|---|---|
sessionToken | Bearer token for this one subscription. Hand it to the SDK; never log it or put it in a URL. |
expiresAt | When the token stops working at the latest (Unix time in milliseconds). Tokens live 15 minutes. |
url | The 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-jsPaymentMethod.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
| Event | When it fires |
|---|---|
fungies:payment-method:updated | The new payment method was saved. An overlay closes itself. |
fungies:payment-method:failed | The 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:close | The 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'stype(plusbrandandlastDigitsfor 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 istrialing. Deduplicate onsetupIntentId.
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