|
| 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