Subscriptions
Manage recurring payments with Plans and Subscriptions.
from djpaystack import PaystackClient
client = PaystackClient()
Plans
Create a Plan
response = client.plans.create(
name='Monthly Pro',
amount=500000, # 5,000 NGN
interval='monthly', # daily, weekly, monthly, quarterly, biannually, annually
description='Pro plan with all features',
)
plan_code = response['data']['plan_code']
List / Fetch Plans
response = client.plans.list(page=1, per_page=50)
response = client.plans.fetch(id_or_code=plan_code)
Update a Plan
response = client.plans.update(
id_or_code=plan_code,
name='Monthly Pro v2',
amount=600000,
)
Subscriptions
Create a Subscription
response = client.subscriptions.create(
customer='CUS_xxxxx',
plan=plan_code,
authorization='AUTH_xxxxx',
)
sub_code = response['data']['subscription_code']
List / Fetch Subscriptions
response = client.subscriptions.list()
response = client.subscriptions.fetch(id_or_code=sub_code)
Enable / Disable
response = client.subscriptions.enable(code=sub_code, token=email_token)
response = client.subscriptions.disable(code=sub_code, token=email_token)
Generate Update Link
Send the customer a link to update their card details:
response = client.subscriptions.generate_update_link(code=sub_code)
link = response['data']['link']
Webhook Events
Subscription-related webhook events:
subscription.create— New subscription createdsubscription.disable— Subscription disabledsubscription.not_renew— Subscription will not renewsubscription.expiring_cards— Cards about to expireinvoice.create— Invoice created for subscriptioninvoice.payment_failed— Subscription payment failed
Listen via Django signals:
from django.dispatch import receiver
from djpaystack.signals import paystack_subscription_created, paystack_subscription_cancelled
@receiver(paystack_subscription_created)
def on_sub_created(sender, transaction_data, **kwargs):
print(f"New subscription: {transaction_data}")
@receiver(paystack_subscription_cancelled)
def on_sub_cancelled(sender, transaction_data, **kwargs):
print(f"Subscription cancelled: {transaction_data}")