-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecurring_billing.php
More file actions
637 lines (540 loc) · 23.2 KB
/
recurring_billing.php
File metadata and controls
637 lines (540 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
<?php
/**
* Recurring Billing and Subscription Management Example
*
* This comprehensive example demonstrates how to implement recurring billing
* using Paystack Payment Requests. It covers subscription management,
* automated billing cycles, payment tracking, and customer notifications.
*
* Features Demonstrated:
* - Multi-tier subscription management
* - Automated monthly billing cycles
* - Payment status monitoring
* - Automated reminder notifications
* - Revenue tracking and reporting
* - Subscription upgrades/downgrades
* - Failed payment handling
*
* Use Cases:
* - SaaS subscription billing
* - Membership site billing
* - Service-based recurring payments
* - Multi-tier product subscriptions
*
* Before running:
* 1. Replace the secret key with your test key
* 2. Run: php examples/recurring_billing.php
*/
// Include Composer autoloader
require_once __DIR__ . '/../vendor/autoload.php';
// Import the Paystack Client
use StarfolkSoftware\Paystack\Client as PaystackClient;
// ============================================================================
// Configuration and Setup
// ============================================================================
$secretKey = 'sk_test_your_secret_key_here';
// Initialize the Paystack client
$paystack = new PaystackClient([
'secretKey' => $secretKey,
]);
// ============================================================================
// Subscription Configuration
// ============================================================================
/**
* Define subscription tiers with features and pricing
*/
$subscriptionTiers = [
'starter' => [
'name' => 'Starter Plan',
'monthly_fee' => 5000, // ₦50.00
'annual_fee' => 50000, // ₦500.00 (2 months free)
'description' => 'Perfect for individuals and small projects',
'features' => [
'5 Projects',
'10GB Storage',
'Email Support',
'Basic Analytics'
],
'limits' => [
'projects' => 5,
'storage_gb' => 10,
'api_calls' => 1000
]
],
'professional' => [
'name' => 'Professional Plan',
'monthly_fee' => 15000, // ₦150.00
'annual_fee' => 150000, // ₦1,500.00 (2 months free)
'description' => 'Ideal for growing businesses and teams',
'features' => [
'Unlimited Projects',
'100GB Storage',
'Priority Support',
'Advanced Analytics',
'API Access',
'Team Collaboration'
],
'limits' => [
'projects' => -1, // Unlimited
'storage_gb' => 100,
'api_calls' => 10000
]
],
'enterprise' => [
'name' => 'Enterprise Plan',
'monthly_fee' => 50000, // ₦500.00
'annual_fee' => 500000, // ₦5,000.00 (2 months free)
'description' => 'Full-featured plan for large organizations',
'features' => [
'Unlimited Everything',
'1TB Storage',
'Dedicated Support',
'Custom Analytics',
'Full API Access',
'Advanced Team Management',
'Custom Integrations',
'SLA Guarantee'
],
'limits' => [
'projects' => -1, // Unlimited
'storage_gb' => 1000,
'api_calls' => 100000
]
]
];
/**
* Sample customer database with subscription details
*/
$customers = [
[
'id' => 'CUST_001',
'email' => 'sarah.johnson@techstartup.com',
'first_name' => 'Sarah',
'last_name' => 'Johnson',
'company' => 'Tech Startup Inc.',
'phone' => '+2348123456789',
'subscription' => [
'tier' => 'professional',
'billing_cycle' => 'monthly',
'start_date' => '2024-01-15',
'last_billed' => '2024-10-15',
'next_billing' => '2024-11-15',
'status' => 'active'
],
'payment_history' => [
'successful_payments' => 9,
'failed_payments' => 1,
'total_paid' => 135000 // ₦1,350.00
]
],
[
'id' => 'CUST_002',
'email' => 'mike.chen@designagency.com',
'first_name' => 'Mike',
'last_name' => 'Chen',
'company' => 'Creative Design Agency',
'phone' => '+2348987654321',
'subscription' => [
'tier' => 'starter',
'billing_cycle' => 'annual',
'start_date' => '2024-03-01',
'last_billed' => '2024-03-01',
'next_billing' => '2025-03-01',
'status' => 'active'
],
'payment_history' => [
'successful_payments' => 1,
'failed_payments' => 0,
'total_paid' => 50000 // ₦500.00
]
],
[
'id' => 'CUST_003',
'email' => 'alex.rivera@bigcorp.com',
'first_name' => 'Alex',
'last_name' => 'Rivera',
'company' => 'Big Corporation Ltd.',
'phone' => '+2347012345678',
'subscription' => [
'tier' => 'enterprise',
'billing_cycle' => 'monthly',
'start_date' => '2024-06-01',
'last_billed' => '2024-10-01',
'next_billing' => '2024-11-01',
'status' => 'active'
],
'payment_history' => [
'successful_payments' => 5,
'failed_payments' => 0,
'total_paid' => 250000 // ₦2,500.00
]
],
[
'id' => 'CUST_004',
'email' => 'emma.wilson@freelancer.com',
'first_name' => 'Emma',
'last_name' => 'Wilson',
'company' => 'Freelance Designer',
'phone' => '+2348765432109',
'subscription' => [
'tier' => 'starter',
'billing_cycle' => 'monthly',
'start_date' => '2024-08-01',
'last_billed' => '2024-10-01',
'next_billing' => '2024-11-01',
'status' => 'payment_failed' // Needs attention
],
'payment_history' => [
'successful_payments' => 2,
'failed_payments' => 1,
'total_paid' => 10000 // ₦100.00
]
]
];
// ============================================================================
// Helper Functions
// ============================================================================
function formatCurrency(int $amountInKobo): string {
return '₦' . number_format($amountInKobo / 100, 2);
}
function printHeader(string $title): void {
echo "\n" . str_repeat('=', 80) . "\n";
echo " " . strtoupper($title) . "\n";
echo str_repeat('=', 80) . "\n";
}
function printSubHeader(string $title): void {
echo "\n" . str_repeat('-', 60) . "\n";
echo "🚀 " . $title . "\n";
echo str_repeat('-', 60) . "\n";
}
function calculateTax(int $amount, float $taxRate = 0.075): int {
return (int) round($amount * $taxRate);
}
function isCustomerDueForBilling(array $customer): bool {
$nextBilling = strtotime($customer['subscription']['next_billing']);
$today = strtotime(date('Y-m-d'));
// Customer is due if next billing date is today or in the past
return $nextBilling <= $today;
}
function getSubscriptionAmount(array $customer, array $subscriptionTiers): int {
$tier = $customer['subscription']['tier'];
$cycle = $customer['subscription']['billing_cycle'];
if ($cycle === 'annual') {
return $subscriptionTiers[$tier]['annual_fee'];
}
return $subscriptionTiers[$tier]['monthly_fee'];
}
// ============================================================================
// Start Recurring Billing Process
// ============================================================================
printHeader("Recurring Billing System - " . date('F Y'));
echo "🔄 Starting automated billing process...\n";
echo "📅 Processing Date: " . date('Y-m-d H:i:s') . "\n";
echo "👥 Total Customers: " . count($customers) . "\n";
// ============================================================================
// Step 1: Identify Customers Due for Billing
// ============================================================================
printSubHeader("Step 1: Identifying Customers Due for Billing");
$customersDue = array_filter($customers, 'isCustomerDueForBilling');
$customersWithFailedPayments = array_filter($customers, function($customer) {
return $customer['subscription']['status'] === 'payment_failed';
});
echo "📊 Billing Analysis:\n";
echo " • Customers due for billing: " . count($customersDue) . "\n";
echo " • Customers with failed payments: " . count($customersWithFailedPayments) . "\n";
echo " • Active subscriptions: " . count(array_filter($customers, function($c) {
return $c['subscription']['status'] === 'active';
})) . "\n\n";
if (empty($customersDue) && empty($customersWithFailedPayments)) {
echo "ℹ️ No customers due for billing today.\n";
echo " For demonstration, we'll process all customers anyway.\n\n";
$customersDue = $customers; // Process all for demo
}
// ============================================================================
// Step 2: Process Billing for Due Customers
// ============================================================================
printSubHeader("Step 2: Processing Billing for Due Customers");
$billingResults = [];
$totalBillingAmount = 0;
foreach (array_merge($customersDue, $customersWithFailedPayments) as $customer) {
$subscription = $customer['subscription'];
$tier = $subscriptionTiers[$subscription['tier']];
echo "Processing: {$customer['first_name']} {$customer['last_name']} ({$customer['email']})\n";
echo " Company: {$customer['company']}\n";
echo " Plan: {$tier['name']} ({$subscription['billing_cycle']})\n";
echo " Status: " . ucfirst($subscription['status']) . "\n";
try {
// Calculate billing amount
$amount = getSubscriptionAmount($customer, $subscriptionTiers);
$taxAmount = calculateTax($amount);
$totalAmount = $amount + $taxAmount;
// Prepare line items
$lineItems = [
[
'name' => "{$tier['name']} - " . ucfirst($subscription['billing_cycle']) . " Subscription",
'amount' => $amount,
'quantity' => 1,
'description' => $tier['description']
]
];
// Add usage-based charges if applicable
if ($subscription['tier'] === 'professional' || $subscription['tier'] === 'enterprise') {
// Simulate additional usage charges
$extraStorage = random_int(0, 20); // GB
if ($extraStorage > 0) {
$storageCharge = $extraStorage * 100; // ₦1.00 per GB
$lineItems[] = [
'name' => 'Additional Storage',
'amount' => $storageCharge,
'quantity' => $extraStorage,
'description' => 'Extra storage beyond plan limit'
];
$totalAmount += $storageCharge;
}
}
// Prepare payment request
$billingPeriod = $subscription['billing_cycle'] === 'annual' ?
date('Y') : date('F Y');
$description = "{$tier['name']} - {$billingPeriod}";
// Add retry indicator for failed payments
if ($subscription['status'] === 'payment_failed') {
$description .= " (Payment Retry)";
}
$paymentRequest = $paystack->paymentRequests->create([
'description' => $description,
'line_items' => $lineItems,
'tax' => [
[
'name' => 'VAT (7.5%)',
'amount' => calculateTax($totalAmount - $taxAmount)
]
],
'customer' => $customer['email'],
'due_date' => date('Y-m-d', strtotime('+7 days')),
'send_notification' => true,
'currency' => 'NGN',
'metadata' => [
'customer_id' => $customer['id'],
'subscription_tier' => $subscription['tier'],
'billing_cycle' => $subscription['billing_cycle'],
'billing_period' => $billingPeriod,
'retry_count' => $subscription['status'] === 'payment_failed' ? 1 : 0
]
]);
if ($paymentRequest['status']) {
$requestCode = $paymentRequest['data']['request_code'];
$finalAmount = $paymentRequest['data']['amount'];
echo " ✅ Payment request created successfully\n";
echo " 📋 Request Code: {$requestCode}\n";
echo " 💰 Amount: " . formatCurrency($finalAmount) . "\n";
echo " 📧 Notification sent to: {$customer['email']}\n";
$billingResults[] = [
'customer' => $customer,
'request_code' => $requestCode,
'amount' => $finalAmount,
'status' => 'created',
'tier' => $tier
];
$totalBillingAmount += $finalAmount;
} else {
throw new Exception($paymentRequest['message']);
}
} catch (Exception $e) {
echo " ❌ Failed to create payment request: {$e->getMessage()}\n";
$billingResults[] = [
'customer' => $customer,
'status' => 'failed',
'error' => $e->getMessage(),
'tier' => $tier
];
}
echo "\n";
}
// ============================================================================
// Step 3: Billing Summary and Analytics
// ============================================================================
printSubHeader("Step 3: Billing Summary and Analytics");
$successfulBilling = array_filter($billingResults, function($result) {
return $result['status'] === 'created';
});
$failedBilling = array_filter($billingResults, function($result) {
return $result['status'] === 'failed';
});
echo "📊 Billing Results Summary:\n";
echo " ✅ Successful: " . count($successfulBilling) . " payment requests\n";
echo " ❌ Failed: " . count($failedBilling) . " attempts\n";
echo " 💰 Total Billed: " . formatCurrency($totalBillingAmount) . "\n";
echo " 📈 Success Rate: " . (count($billingResults) > 0 ?
round((count($successfulBilling) / count($billingResults)) * 100, 1) : 0) . "%\n\n";
// Breakdown by subscription tier
$tierBreakdown = [];
foreach ($successfulBilling as $result) {
$tierName = $result['tier']['name'];
if (!isset($tierBreakdown[$tierName])) {
$tierBreakdown[$tierName] = ['count' => 0, 'amount' => 0];
}
$tierBreakdown[$tierName]['count']++;
$tierBreakdown[$tierName]['amount'] += $result['amount'];
}
echo "📋 Revenue Breakdown by Tier:\n";
foreach ($tierBreakdown as $tierName => $data) {
echo " • {$tierName}: {$data['count']} customers → " .
formatCurrency($data['amount']) . "\n";
}
// ============================================================================
// Step 4: Payment Status Monitoring
// ============================================================================
printSubHeader("Step 4: Payment Status Monitoring");
echo "🔍 Checking payment status for recent payment requests...\n\n";
foreach ($successfulBilling as $result) {
$customer = $result['customer'];
$requestCode = $result['request_code'];
echo "Monitoring: {$customer['first_name']} {$customer['last_name']} ({$requestCode})\n";
try {
$verification = $paystack->paymentRequests->verify($requestCode);
if ($verification['status']) {
$data = $verification['data'];
$isPaid = $data['paid'];
$status = $data['status'];
$amountPaid = $data['amount_paid'];
$amountDue = $data['amount'] - $amountPaid;
echo " 📊 Status: " . ucfirst($status) . "\n";
echo " 💰 Amount: " . formatCurrency($data['amount']) . "\n";
echo " ✅ Paid: " . formatCurrency($amountPaid) . "\n";
echo " ⏳ Outstanding: " . formatCurrency($amountDue) . "\n";
echo " 📅 Due Date: {$data['due_date']}\n";
if (isset($data['pdf_url'])) {
echo " 🔗 Invoice URL: {$data['pdf_url']}\n";
}
// Automated actions based on payment status
if (!$isPaid && $status === 'pending') {
echo " 📧 Automated Action: Payment reminder will be sent in 3 days\n";
} elseif ($isPaid) {
echo " 🎉 Automated Action: Subscription renewal confirmed\n";
}
}
} catch (Exception $e) {
echo " ❌ Error checking status: {$e->getMessage()}\n";
}
echo "\n";
}
// ============================================================================
// Step 5: Account-Wide Analytics
// ============================================================================
printSubHeader("Step 5: Account-Wide Payment Analytics");
try {
echo "📈 Fetching comprehensive payment analytics...\n\n";
$totals = $paystack->paymentRequests->totals();
if ($totals['status']) {
$totalsData = $totals['data'];
echo "🏢 Account Overview:\n";
echo " 📋 Total Payment Requests: {$totalsData['total_requests']}\n";
echo " ⏳ Pending Amount: " . formatCurrency($totalsData['pending_amount']) . "\n";
echo " ✅ Successful Amount: " . formatCurrency($totalsData['successful_amount']) . "\n";
echo " 📊 Success Rate: " . ($totalsData['total_requests'] > 0 ?
round(($totalsData['successful_requests'] / $totalsData['total_requests']) * 100, 1) : 0) . "%\n\n";
}
} catch (Exception $e) {
echo "❌ Error fetching analytics: {$e->getMessage()}\n\n";
}
// ============================================================================
// Step 6: Subscription Management Actions
// ============================================================================
printSubHeader("Step 6: Subscription Management Actions");
echo "🔧 Demonstrating subscription management actions...\n\n";
// Simulate subscription upgrade
$upgradeCustomer = $customers[0]; // Sarah Johnson
echo "📈 Simulating subscription upgrade for {$upgradeCustomer['first_name']} {$upgradeCustomer['last_name']}:\n";
echo " Current Plan: Professional (₦150/month)\n";
echo " Upgrade To: Enterprise (₦500/month)\n";
echo " Prorated Amount: " . formatCurrency(35000) . " (for remaining billing period)\n";
try {
$upgradeRequest = $paystack->paymentRequests->create([
'description' => 'Subscription Upgrade - Professional to Enterprise',
'line_items' => [
[
'name' => 'Plan Upgrade (Prorated)',
'amount' => 35000, // Prorated difference
'quantity' => 1,
'description' => 'Upgrade from Professional to Enterprise plan'
]
],
'customer' => $upgradeCustomer['email'],
'due_date' => date('Y-m-d', strtotime('+3 days')),
'send_notification' => true,
'metadata' => [
'type' => 'subscription_upgrade',
'from_tier' => 'professional',
'to_tier' => 'enterprise',
'customer_id' => $upgradeCustomer['id']
]
]);
if ($upgradeRequest['status']) {
echo " ✅ Upgrade payment request created: {$upgradeRequest['data']['request_code']}\n";
}
} catch (Exception $e) {
echo " ❌ Upgrade request failed: {$e->getMessage()}\n";
}
// ============================================================================
// Step 7: Failed Payment Recovery
// ============================================================================
printSubHeader("Step 7: Failed Payment Recovery Process");
$failedPaymentCustomers = array_filter($customers, function($customer) {
return $customer['subscription']['status'] === 'payment_failed';
});
echo "🔄 Processing failed payment recovery...\n\n";
foreach ($failedPaymentCustomers as $customer) {
echo "Recovering payment for: {$customer['first_name']} {$customer['last_name']}\n";
echo " Failed payments: {$customer['payment_history']['failed_payments']}\n";
echo " Last successful payment: " . formatCurrency($customer['payment_history']['total_paid']) . "\n";
// Implement recovery strategy
$failedCount = $customer['payment_history']['failed_payments'];
if ($failedCount === 1) {
echo " 🔄 Strategy: Immediate retry with email notification\n";
} elseif ($failedCount === 2) {
echo " 📞 Strategy: Personal outreach + payment plan option\n";
} else {
echo " ⚠️ Strategy: Account suspension warning\n";
}
echo "\n";
}
// ============================================================================
// Completion Summary
// ============================================================================
printHeader("Recurring Billing Process Completed");
echo "🎉 Billing cycle completed successfully!\n\n";
echo "📊 Final Summary:\n";
echo " • Total customers processed: " . count($billingResults) . "\n";
echo " • Payment requests created: " . count($successfulBilling) . "\n";
echo " • Total revenue billed: " . formatCurrency($totalBillingAmount) . "\n";
echo " • Processing time: " . number_format(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2) . " seconds\n\n";
echo "🔧 Next Steps in Production:\n";
echo " • Schedule this script to run monthly/daily via cron\n";
echo " • Set up webhook handlers for payment notifications\n";
echo " • Implement automatic subscription renewals\n";
echo " • Create customer portal for subscription management\n";
echo " • Set up automated dunning management for failed payments\n";
echo " • Generate detailed analytics and reporting dashboards\n\n";
echo "📚 Integration Examples:\n";
echo " • webhook_handler.php - Handle real-time payment events\n";
echo " • subscription_portal.php - Customer self-service portal\n";
echo " • analytics_dashboard.php - Revenue and subscription analytics\n";
echo " • dunning_management.php - Automated failed payment recovery\n\n";
// ============================================================================
// Development Notes
// ============================================================================
if ($secretKey === 'sk_test_your_secret_key_here') {
echo "⚠️ DEVELOPMENT REMINDER:\n";
echo " Replace the API key with your actual test key for full functionality\n";
echo " Get your key from: https://dashboard.paystack.com/#/settings/developer\n\n";
}
echo "🧪 Testing Information:\n";
echo " • This example uses test mode (safe for development)\n";
echo " • Use test card 4084084084084081 for successful payments\n";
echo " • Monitor results in your Paystack dashboard\n";
echo " • Test different billing cycles and scenarios\n\n";
echo "📖 Documentation Links:\n";
echo " • Payment Requests API: https://paystack.com/docs/api/payment-request/\n";
echo " • Subscription Best Practices: docs/advanced-usage.md\n";
echo " • Error Handling Guide: docs/troubleshooting.md\n";
echo " • SDK GitHub Repository: https://github.com/starfolksoftware/paystack-php\n";