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 Symfony
Bonaventure OgetoBy Bonaventure Ogeto|Updated July 20, 2026

Build Paystack Subscriptions in Symfony

To build Paystack subscriptions in Symfony, create a plan, initialize a transaction with the plan code, handle subscription webhooks to update a Doctrine entity, and use Security Voters to gate premium content behind active subscription checks.

In this article

  1. 1. Subscription Entity
  2. 2. Subscribe Endpoint
  3. 3. Webhook Handlers
  4. 4. Subscription Voter
  5. 5. Cancel Subscription
  6. 6. Ship Payments Faster

Subscription Entity

// src/Entity/Subscription.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Subscription
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\OneToOne(targetEntity: User::class)]
    private User $user;

    #[ORM\Column(length: 100, unique: true)]
    private string $subscriptionCode;

    #[ORM\Column(length: 100)]
    private string $planCode;

    #[ORM\Column(length: 100)]
    private string $emailToken;

    #[ORM\Column(length: 20)]
    private string $status = 'active';

    #[ORM\Column(nullable: true)]
    private ?\DateTimeInterface $nextPaymentDate = null;

    // Getters and setters...
}

Subscribe Endpoint

#[Route('/subscribe', methods: ['POST'])]
public function subscribe(Request $request, PaystackService $paystack): Response
{
    $user = $this->getUser();
    $planCode = $request->request->get('plan_code');

    $result = $paystack->initializeWithPlan($user->getEmail(), $planCode);

    if ($result['status'] ?? false) {
        return $this->redirect($result['data']['authorization_url']);
    }

    return new Response('Failed', 400);
}

Get weekly developer tips

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

No spam. Unsubscribe anytime.

Webhook Handlers

private function handleSubscriptionCreate(array $data, EntityManagerInterface $em): void
{
    $email = $data['customer']['email'];
    $user = $em->getRepository(User::class)->findOneBy(['email' => $email]);

    if (!$user) return;

    $sub = $em->getRepository(Subscription::class)->findOneBy(['user' => $user]);

    if (!$sub) {
        $sub = new Subscription();
        $sub->setUser($user);
    }

    $sub->setSubscriptionCode($data['subscription_code']);
    $sub->setPlanCode($data['plan']['plan_code']);
    $sub->setEmailToken($data['email_token'] ?? '');
    $sub->setStatus('active');

    $em->persist($sub);
    $em->flush();
}

Subscription Voter

// src/Security/Voter/SubscriptionVoter.php
namespace App\Security\Voter;

use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class SubscriptionVoter extends Voter
{
    protected function supports(string $attribute, mixed $subject): bool
    {
        return $attribute === 'ACTIVE_SUBSCRIPTION';
    }

    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
    {
        $user = $token->getUser();
        if (!$user) return false;

        $sub = $user->getSubscription();
        return $sub && $sub->getStatus() === 'active';
    }
}

// In controller:
#[IsGranted('ACTIVE_SUBSCRIPTION')]
#[Route('/premium')]
public function premium(): Response
{
    return $this->render('premium.html.twig');
}

Cancel Subscription

#[Route('/subscription/cancel', methods: ['POST'])]
public function cancel(PaystackService $paystack, EntityManagerInterface $em): Response
{
    $sub = $this->getUser()->getSubscription();

    if (!$sub) {
        return $this->redirectToRoute('subscribe');
    }

    $result = $paystack->disableSubscription($sub->getSubscriptionCode(), $sub->getEmailToken());

    if ($result['status'] ?? false) {
        $sub->setStatus('cancelled');
        $em->flush();
    }

    return $this->redirectToRoute('dashboard');
}

Ship Payments Faster

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

Sign up for the McTaba newsletter

Key Takeaways

  • ✓Paystack handles recurring billing. Create plans once and subscribe customers to them.
  • ✓Use a Doctrine entity for subscription tracking with status, subscription_code, and email_token.
  • ✓Webhook events drive subscription state. Handle subscription.create, charge.success, and invoice.payment_failed.
  • ✓Symfony Security Voters provide fine-grained access control for subscription-gated features.
  • ✓Store email_token for cancellation. It comes in the subscription.create webhook.
  • ✓Use Symfony Messenger for async webhook processing.

Frequently Asked Questions

Can I use Symfony Security for subscription checks?
Yes. Security Voters are the Symfony way to implement fine-grained authorization. Create a voter that checks the subscription status and use #[IsGranted] on your controllers.
How do I handle plan upgrades?
Cancel the current subscription and create a new one. Paystack does not support direct plan changes.
What if a renewal fails?
Paystack retries automatically. You receive invoice.payment_failed webhooks. Update the status to past_due and notify the customer.

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 Svelte and SvelteKit

Accept Paystack payments in SvelteKit using server endpoints for backend logic and Svelte components for the frontend. This guide covers initializing transactions, inline popup checkout, redirect checkout, and verification in a single full-stack SvelteKit project.

Read more

Verify Paystack Payments in Flask

Verify Paystack payments server-side in your Flask application. This guide covers calling the Paystack verify endpoint, validating amounts and currencies, building reusable verification helpers, and preventing double-granting.

Read more

Build Paystack Subscriptions in ASP.NET Core

Implement Paystack recurring billing in ASP.NET Core. This guide covers creating plans, subscribing customers through transaction initialization, handling renewal webhooks with C#, and cancelling subscriptions.

Read more

Paystack Checkout Conversion: Engineering Decisions That Move the Number

Technical decisions that directly affect whether customers complete Paystack checkout. Covers channel ordering, page load speed, error message recovery, mobile optimization, and trust signal placement.

Read more

Paystack SSL and Certificate Errors from Your Server

SSL and TLS errors when calling the Paystack API mean your server cannot verify the Paystack API certificate. This guide covers every cause, the proper fixes, and why disabling certificate verification is never acceptable for a payment integration.

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.