Skip to content

Commit b8ee920

Browse files
committed
Add example scripts for Paystack PHP client library usage
1 parent d8d28c0 commit b8ee920

File tree

5 files changed

+480
-0
lines changed

5 files changed

+480
-0
lines changed

examples/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Paystack PHP Examples
2+
3+
This directory contains example scripts demonstrating how to use the Paystack PHP client library.
4+
5+
## Prerequisites
6+
7+
Before running these examples, make sure you have:
8+
9+
1. Installed this package via Composer (`composer require starfolksoftware/paystack-php`)
10+
2. A valid Paystack secret key (test or live)
11+
12+
## Available Examples
13+
14+
### Payment Request Examples
15+
16+
- `simple_payment_request.php`: Basic example showing how to create and list payment requests
17+
- `payment_request_demo.php`: Comprehensive example demonstrating all payment request API methods
18+
- `invoice_workflow.php`: Demonstrates a complete invoice workflow using payment requests
19+
- `recurring_billing.php`: Advanced example showing how to implement recurring billing using payment requests
20+
21+
## How to Run
22+
23+
1. Update the `$secretKey` variable in the example files with your Paystack secret key.
24+
2. Execute the script using PHP:
25+
26+
```bash
27+
php examples/simple_payment_request.php
28+
```
29+
30+
## Notes
31+
32+
- These examples use the test mode by default. To use them in production, replace the test key with your live key.
33+
- The payment request API allows you to create, manage, and track payment requests and invoices for your customers.
34+
- Some examples create draft payment requests that don't send notifications automatically, giving you control over when to finalize and notify customers.
35+
36+
## Error Handling
37+
38+
The examples include basic error handling. In a production environment, you should implement more comprehensive error handling strategies based on your application's requirements.
39+
40+
## Documentation
41+
42+
For more information on the available methods and parameters, refer to:
43+
- The official Paystack API documentation: https://paystack.com/docs/api/payment-request/
44+
- The library's main README file

examples/invoice_workflow.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
// Include Composer autoloader
4+
require_once __DIR__ . '/../vendor/autoload.php';
5+
6+
// Import the Paystack Client
7+
use StarfolkSoftware\Paystack\Client as PaystackClient;
8+
9+
// Replace with your actual test secret key
10+
$secretKey = 'sk_test_your_secret_key_here';
11+
12+
// Initialize the Paystack client
13+
$paystack = new PaystackClient([
14+
'secretKey' => $secretKey,
15+
]);
16+
17+
/**
18+
* This example demonstrates a complete invoice workflow using Payment Requests:
19+
* 1. Create a customer (if they don't exist)
20+
* 2. Create a draft payment request
21+
* 3. Update the payment request with additional line items
22+
* 4. Finalize the payment request
23+
* 5. Send notification to the customer
24+
* 6. Check payment status later
25+
*/
26+
27+
// Step 1: First, make sure we have a customer
28+
// Note: In a real application, you might want to check if the customer exists first
29+
try {
30+
$customer = $paystack->customers->create([
31+
'email' => 'john.doe@example.com',
32+
'first_name' => 'John',
33+
'last_name' => 'Doe',
34+
'phone' => '+2348123456789'
35+
]);
36+
37+
$customerCode = $customer['data']['customer_code'];
38+
echo "Customer created with code: $customerCode\n";
39+
} catch (Exception $e) {
40+
// Customer might already exist
41+
echo "Note: " . $e->getMessage() . "\n";
42+
43+
// In a real application, you would search for the customer here
44+
$customerCode = 'CUS_existing_customer_code';
45+
}
46+
47+
// Step 2: Create a draft payment request
48+
$draftPaymentRequest = $paystack->paymentRequests->create([
49+
'description' => 'Monthly Service Invoice - June 2025',
50+
'line_items' => [
51+
['name' => 'Basic Subscription', 'amount' => 30000, 'quantity' => 1]
52+
],
53+
'customer' => $customerCode,
54+
'due_date' => '2025-07-15',
55+
'draft' => true, // Create as draft initially
56+
'has_invoice' => true // Generate an invoice number
57+
]);
58+
59+
$requestCode = $draftPaymentRequest['data']['request_code'];
60+
$invoiceNumber = $draftPaymentRequest['data']['invoice_number'];
61+
echo "Draft payment request created with code: $requestCode and invoice #$invoiceNumber\n";
62+
63+
// Step 3: Update the payment request with additional items
64+
$updatedRequest = $paystack->paymentRequests->update($requestCode, [
65+
'line_items' => [
66+
['name' => 'Basic Subscription', 'amount' => 30000, 'quantity' => 1],
67+
['name' => 'Premium Support', 'amount' => 15000, 'quantity' => 1],
68+
['name' => 'Additional Storage', 'amount' => 5000, 'quantity' => 2]
69+
],
70+
'tax' => [
71+
['name' => 'VAT (7.5%)', 'amount' => 4125]
72+
],
73+
'description' => 'Monthly Service Invoice - June 2025 (Updated)'
74+
]);
75+
76+
echo "Payment request updated with additional items\n";
77+
78+
// Step 4: Finalize the payment request
79+
$finalizedRequest = $paystack->paymentRequests->finalize($requestCode, [
80+
'send_notification' => false // We'll send it manually in the next step
81+
]);
82+
83+
echo "Payment request finalized\n";
84+
85+
// Step 5: Send notification to the customer
86+
$notification = $paystack->paymentRequests->sendNotification($requestCode);
87+
echo "Payment notification sent to customer\n";
88+
89+
// Step 6: Check payment status (this would typically happen later)
90+
echo "\nSimulating checking payment status after some time...\n";
91+
92+
// In a real application, this would happen in a separate process or callback
93+
$verifiedRequest = $paystack->paymentRequests->verify($requestCode);
94+
$status = $verifiedRequest['data']['status'];
95+
$isPaid = $verifiedRequest['data']['paid'];
96+
97+
echo "Current status: $status\n";
98+
echo "Paid: " . ($isPaid ? "Yes" : "No") . "\n";
99+
100+
// Output the URL where the customer can view and pay the invoice (if available)
101+
if (isset($verifiedRequest['data']['pdf_url'])) {
102+
echo "Invoice URL: " . $verifiedRequest['data']['pdf_url'] . "\n";
103+
}
104+
105+
echo "\nPayment request workflow completed!\n";

examples/payment_request_demo.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
// Include Composer autoloader
4+
require_once __DIR__ . '/../vendor/autoload.php';
5+
6+
// Import the Paystack Client
7+
use StarfolkSoftware\Paystack\Client as PaystackClient;
8+
9+
// Replace with your actual test secret key
10+
$secretKey = 'sk_test_your_secret_key_here';
11+
12+
// Initialize the Paystack client
13+
$paystack = new PaystackClient([
14+
'secretKey' => $secretKey,
15+
]);
16+
17+
// Helper function to display results in a readable format
18+
function displayResponse($title, $response) {
19+
echo "=== {$title} ===\n";
20+
echo json_encode($response, JSON_PRETTY_PRINT) . "\n\n";
21+
}
22+
23+
try {
24+
// Step 1: Create a payment request
25+
echo "Creating a payment request...\n";
26+
$createResponse = $paystack->paymentRequests->create([
27+
'description' => 'Test Payment Request',
28+
'line_items' => [
29+
['name' => 'Product A', 'amount' => 10000, 'quantity' => 2],
30+
['name' => 'Product B', 'amount' => 5000, 'quantity' => 1]
31+
],
32+
'tax' => [
33+
['name' => 'VAT', 'amount' => 1000]
34+
],
35+
'customer' => 'customer_email@example.com', // Can be customer code or email
36+
'due_date' => date('Y-m-d', strtotime('+7 days')),
37+
'draft' => true // Create as draft so we can test the finalize method later
38+
]);
39+
displayResponse('Create Payment Request', $createResponse);
40+
41+
// Save the request code for later use
42+
$requestCode = $createResponse['data']['request_code'] ?? null;
43+
if (!$requestCode) {
44+
throw new Exception("Failed to get request code from the created payment request");
45+
}
46+
47+
// Step 2: List payment requests
48+
echo "Listing payment requests...\n";
49+
$listResponse = $paystack->paymentRequests->all([
50+
'perPage' => 5,
51+
'page' => 1
52+
]);
53+
displayResponse('List Payment Requests', $listResponse);
54+
55+
// Step 3: Fetch a specific payment request
56+
echo "Fetching payment request with code: {$requestCode}...\n";
57+
$fetchResponse = $paystack->paymentRequests->fetch($requestCode);
58+
displayResponse('Fetch Payment Request', $fetchResponse);
59+
60+
// Step 4: Verify a payment request
61+
echo "Verifying payment request with code: {$requestCode}...\n";
62+
$verifyResponse = $paystack->paymentRequests->verify($requestCode);
63+
displayResponse('Verify Payment Request', $verifyResponse);
64+
65+
// Step 5: Update the payment request
66+
echo "Updating payment request with code: {$requestCode}...\n";
67+
$updateResponse = $paystack->paymentRequests->update($requestCode, [
68+
'description' => 'Updated Test Payment Request',
69+
'due_date' => date('Y-m-d', strtotime('+10 days'))
70+
]);
71+
displayResponse('Update Payment Request', $updateResponse);
72+
73+
// Step 6: Finalize the draft payment request
74+
echo "Finalizing draft payment request with code: {$requestCode}...\n";
75+
$finalizeResponse = $paystack->paymentRequests->finalize($requestCode, [
76+
'send_notification' => true
77+
]);
78+
displayResponse('Finalize Payment Request', $finalizeResponse);
79+
80+
// Step 7: Send a notification for the payment request
81+
echo "Sending notification for payment request with code: {$requestCode}...\n";
82+
$notificationResponse = $paystack->paymentRequests->sendNotification($requestCode);
83+
displayResponse('Send Notification', $notificationResponse);
84+
85+
// Step 8: Get payment requests totals
86+
echo "Getting payment request totals...\n";
87+
$totalsResponse = $paystack->paymentRequests->totals();
88+
displayResponse('Payment Request Totals', $totalsResponse);
89+
90+
// Step 9: Archive the payment request (uncommenting will archive the request)
91+
/*
92+
echo "Archiving payment request with code: {$requestCode}...\n";
93+
$archiveResponse = $paystack->paymentRequests->archive($requestCode);
94+
displayResponse('Archive Payment Request', $archiveResponse);
95+
*/
96+
97+
echo "All payment request operations completed successfully!\n";
98+
99+
} catch (Exception $e) {
100+
echo "Error: " . $e->getMessage() . "\n";
101+
echo "File: " . $e->getFile() . " on line " . $e->getLine() . "\n";
102+
echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
103+
}

0 commit comments

Comments
 (0)