Skip to main content

Hosted SDK v0.1.0

Install the cancel flow

The SDK has no framework dependency, renders inside Shadow DOM, and never receives your Stripe restricted key. Identity tokens are short-lived and must be signed on your backend.

1

Mint an identity token on your backend

import { createHmac } from "node:crypto";

const payload = {
  sub: stripeSubscriptionId,
  cus: stripeCustomerId,
  mrr: 2900,
  currency: "usd",
  subscriptionCreatedAt: stripeSubscriptionCreatedAt * 1000,
  iat: Date.now(),
  exp: Date.now() + 5 * 60 * 1000,
};
const body = Buffer.from(JSON.stringify(payload)).toString("base64url");
const signature = createHmac("sha256", process.env.CHURNPLUG_SIGNING_SECRET!)
  .update(body)
  .digest("base64url");
const identityToken = `${body}.${signature}`;
2

Load the hosted SDK and intercept cancel

<script src="https://churnplug.com/churnplug.js"></script>
<script>
  churnplug.init({ publicKey: "pk_YOUR_KEY", apiBase: "https://churnplug.com" });
  cancelButton.onclick = () => churnplug.open({ token: identityToken });
</script>
3

Listen for outcomes

Pass onSaved, onProceedCancel, and onClose callbacks, or listen for window messages whose source is churnplug. Events include opened, reason_selected, offer_shown,saved, and proceed_cancel.