McTaba Labs logo
Software EngineeringThe WhiteboardProjectsEventsMasterclassesWebinarsLearn
Book Call

500+ developers already in the community

Get answers, find accountability partners, and stay ahead

Join Discord →
McTaba Labs logo

Educating 1 million students in tech by 2030.

Programs

  • HighSchool Graduates Launchpad
  • AI & Business Automation
  • Corporate Training
  • McTaba Story & Mission
  • Full-Stack Curriculum
  • Course Pricing & Plans
  • Frequently Asked Questions

Resources

  • Join Discord Community
  • Tech Events in Nairobi
  • Masterclasses
  • Free Resources
  • Learn
  • Campus
  • Africa Tech Talent Report
  • Partner With Us
  • Start a Project
  • Our Work

Contact Us

  • 124 Manyani East Road, Lavington, Nairobi
  • +254727795603
  • Book a Discovery Call

Office Hours:

Mon-Fri: 9am - 2pm EAT

Sat: 10am - 12pm EAT

© 2027 McTaba Labs - A product of Edtech Industries. All rights reserved.

Privacy PolicyTerms of ServiceRefund Policy
  1. Learn
  2. Paystack for African Developers
  3. Build Paystack Subscriptions in Firebase Cloud Functions
Bonaventure OgetoBy Bonaventure Ogeto|Updated July 22, 2026

Build Paystack Subscriptions in Firebase Cloud Functions

Pass a plan code in your Cloud Function when calling /transaction/initialize to start a subscription. After checkout, Paystack fires subscription.create — handle it in your webhook Cloud Function to store the subscription_code and email_token in Firestore. Read subscription status from Firestore in your app rather than calling Paystack directly.

In this article

  1. 1. Subscribe Cloud Function
  2. 2. Subscription Webhook Events
  3. 3. Cancel Subscription
  4. 4. Learn More

Subscribe Cloud Function

// functions/src/index.ts
import { onRequest } from "firebase-functions/v2/https";
import { defineSecret } from "firebase-functions/params";
import { getFirestore } from "firebase-admin/firestore";
import { initializeApp } from "firebase-admin/app";
import * as cors from "cors";

initializeApp();

const paystackSecretKey = defineSecret("PAYSTACK_SECRET_KEY");
const corsHandler = cors({ origin: true });

export const subscribe = onRequest(
  { secrets: [paystackSecretKey] },
  async (req, res) => {
    corsHandler(req, res, async () => {
      if (req.method !== "POST") {
        res.status(405).send("Method not allowed");
        return;
      }

      const { email, planCode } = req.body;

      if (!email || !planCode) {
        res.status(400).json({ error: "email and planCode are required" });
        return;
      }

      const reference = "sub_" + Date.now() + "_" + Math.random().toString(36).slice(2, 8);
      const secretKey = paystackSecretKey.value();

      try {
        const paystackRes = await fetch(
          "https://api.paystack.co/transaction/initialize",
          {
            method: "POST",
            headers: {
              Authorization: "Bearer " + secretKey,
              "Content-Type": "application/json",
            },
            body: JSON.stringify({ email, plan: planCode, reference }),
          }
        );

        const data = await paystackRes.json();

        if (!data.status) {
          res.status(400).json({ error: data.message });
          return;
        }

        // Store pending subscription in Firestore
        const db = getFirestore();
        await db.collection("subscriptions").doc(email).set({
          email,
          plan_code: planCode,
          reference,
          status: "pending",
          created_at: new Date().toISOString(),
        }, { merge: true });

        res.json({
          authorization_url: data.data.authorization_url,
          reference: data.data.reference,
        });
      } catch {
        res.status(500).json({ error: "Failed to initialize subscription" });
      }
    });
  }
);

Subscription Webhook Events

// In your paystackWebhook Cloud Function, add subscription event handling:

case "subscription.create": {
  const sub = event.data;
  const db = getFirestore();
  await db.collection("subscriptions").doc(sub.customer.email).set({
    status: "active",
    subscription_code: sub.subscription_code,
    email_token: sub.email_token,
    plan_name: sub.plan?.name,
    next_billing_date: sub.next_payment_date,
    activated_at: new Date().toISOString(),
  }, { merge: true });
  break;
}

case "charge.success": {
  // Recurring billing charge on an active subscription
  if (event.data.plan) {
    const db = getFirestore();
    await db.collection("subscriptions").doc(event.data.customer.email).update({
      status: "active",
      last_paid_at: new Date().toISOString(),
    });
  }
  break;
}

case "invoice.payment_failed": {
  const db = getFirestore();
  await db.collection("subscriptions").doc(event.data.customer.email).update({
    status: "past_due",
    last_failed_at: new Date().toISOString(),
  });
  break;
}

case "subscription.disable": {
  const db = getFirestore();
  const snapshot = await db.collection("subscriptions")
    .where("subscription_code", "==", event.data.subscription_code)
    .get();
  snapshot.forEach(doc => doc.ref.update({ status: "cancelled" }));
  break;
}

Get weekly developer tips

Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.

No spam. Unsubscribe anytime.

Cancel Subscription

export const cancelSubscription = onRequest(
  { secrets: [paystackSecretKey] },
  async (req, res) => {
    corsHandler(req, res, async () => {
      const { email } = req.body;
      const db = getFirestore();

      const subDoc = await db.collection("subscriptions").doc(email).get();
      const sub = subDoc.data();

      if (!sub || sub.status !== "active") {
        res.status(404).json({ error: "No active subscription found" });
        return;
      }

      const secretKey = paystackSecretKey.value();

      const paystackRes = await fetch(
        "https://api.paystack.co/subscription/disable",
        {
          method: "POST",
          headers: {
            Authorization: "Bearer " + secretKey,
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            code: sub.subscription_code,
            token: sub.email_token,
          }),
        }
      );

      const data = await paystackRes.json();

      if (!data.status) {
        res.status(400).json({ error: data.message });
        return;
      }

      await db.collection("subscriptions").doc(email).update({
        status: "cancelled",
        cancelled_at: new Date().toISOString(),
      });

      res.json({ cancelled: true });
    });
  }
);

Learn More

This guide is part of the Paystack integration guides by language and framework series.

Sign up for the McTaba newsletter

Key Takeaways

  • ✓Start a subscription by passing a plan code to /transaction/initialize in your Cloud Function.
  • ✓After the first successful payment, Paystack fires subscription.create with subscription_code and email_token.
  • ✓Store subscription_code and email_token in Firestore — you need both to cancel via /subscription/disable.
  • ✓Handle invoice.payment_failed to mark subscriptions as past_due and notify users.
  • ✓Read subscription status from Firestore in your app. Avoid calling the Paystack API on every page load.
  • ✓To cancel, call your Cloud Function which calls POST /subscription/disable with code and token.

Frequently Asked Questions

How do I create a Paystack plan for use with Firebase Cloud Functions?
Create the plan once in the Paystack dashboard or via POST /plan with your secret key. You get a plan code (PLN_xxxx). Pass this as the plan parameter in your /transaction/initialize call. Paystack handles all recurring billing automatically after the first payment.
Can I store subscription data in Firestore instead of a SQL database?
Yes. Firestore works well for subscription state. Use the customer email as the document ID for easy lookups. Store subscription_code, email_token, status, and next_billing_date. Update these fields from your webhook Cloud Function on each subscription event.
What happens if the webhook fires but the Firestore write fails?
Paystack retries webhooks for up to 72 hours if your endpoint returns a non-200 response. Since you return 200 before processing, Paystack will not retry. Protect against missed writes by also verifying subscription status through the Paystack API endpoint if discrepancies are found.
How do I check subscription status in my Flutter or React Native app?
Create a getSubscriptionStatus Cloud Function that reads from Firestore by email. Call this on app launch and after checkout. Do not call the Paystack API directly from mobile — it requires the secret key. Your Cloud Function is the right place to fetch and relay subscription state.

Learn Paystack Integration

KES 2,999

The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.

Start Paystack Course

Not ready? Create Free Account

Keep reading

Paystack Integration Guides by Language and Framework

A complete directory of Paystack integration guides organized by programming language and framework. Each guide covers accepting payments, verifying transactions, handling webhooks, and building subscriptions in your specific stack.

Read more

Accept Payments with Paystack in Firebase Cloud Functions

Firebase Cloud Functions provide a Node.js backend for Paystack without a dedicated server. This guide covers setting up a Cloud Function to initialize Paystack transactions, storing secrets securely, configuring CORS, and calling from a frontend.

Read more

Verify Paystack Payments in Firebase Cloud Functions

Verify Paystack payments server-side using Firebase Cloud Functions. This guide covers reading the transaction reference, calling the Paystack verify endpoint, validating the amount against Firestore, and updating the order status.

Read more

Handle Paystack Webhooks in Firebase Cloud Functions

Handle Paystack webhook events in Firebase Cloud Functions. This guide covers HMAC SHA512 signature verification using Node.js crypto, reading the raw request body, returning 200 before processing, and updating Firestore with payment results.

Read more

Handling subscription.create, subscription.disable and invoice Events

A complete walkthrough of every Paystack subscription and invoice webhook event. Covers subscription.create, subscription.not_renew, subscription.disable, invoice.create, invoice.payment_failed, and invoice.update with production-ready Node.js handlers.

Read more

Paystack Subscription Not Charging

Your Paystack subscription exists but recurring charges are not happening. The subscription might be disabled, the authorization code might not be reusable, the card might have expired, the plan amount might have changed, or the subscription might be in arrears. This guide walks through every cause with a systematic debug checklist.

Read more

Explore related topics

M-Pesa & African Stack

M-Pesa Daraja API tutorials, USSD development, and WhatsApp Business API guides. The African Stack that global bootcamps do not teach.

Learn to Code in Kenya

Everything a Kenyan needs to know before writing their first line of code. Career paths, real costs, hardware requirements, and honest answers about what it takes.

Learning to Code

Roadmaps, language guides, and practical advice for learning to code and launching a programming career.

Breaking Into Tech

Practical, no-nonsense guides for anyone looking to start or change their career in tech. From choosing your first language to acing interviews.

The African Stack: Skills for This Market

The skills that make Kenyan developers hard to replace: M-Pesa, USSD, WhatsApp, fintech, and building for the African market.

Bonaventure Ogeto

Bonaventure Ogeto

Founder, McTaba Labs

Bonaventure is the founder of McTaba Labs, a coding bootcamp in Nairobi, Kenya. He has spent years building products for the African market and mentoring the next generation of developers across the continent.