PHP code example of kendenigerian / payzephyr

1. Go to this page and download the library: Download kendenigerian/payzephyr library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kendenigerian / payzephyr example snippets


use KenDeNigerian\PayZephyr\Facades\Payment;

// Basic payment (amount in major currency unit: 100.00 = ₦100.00 for NGN)
return Payment::amount(100.00)
    ->email('[email protected]')
    ->callback(route('payment.callback')) // Where to return after payment
    ->redirect(); // Sends customer to payment page

// With more options (including idempotency for retry safety)
return Payment::amount(500.00) // ₦500.00
    ->currency('NGN')
    ->email('[email protected]')
    ->callback(route('payment.callback'))
    ->reference('ORDER_123') // Your order ID
    ->description('Premium subscription')
    ->metadata(['order_id' => 12345]) // Custom data
    ->idempotency('unique-key-123') // Prevents duplicate charges on retries
    ->with('paystack') // Optional: force specific provider
    ->redirect();

public function callback(Request $request)
{
    // Get payment reference from URL
    $verification = Payment::verify($request->input('reference'));
    
    if ($verification->isSuccessful()) {
        // Payment succeeded - update your database
        Order::where('payment_reference', $verification->reference)
            ->update(['status' => 'paid']);
        return view('payment.success');
    }
    
    // Payment failed
    return view('payment.failed');
}

// app/Providers/EventServiceProvider.php
protected $listen = [
    'payments.webhook.paystack' => [
        \App\Listeners\HandlePaystackWebhook::class,
    ],
];

// app/Listeners/HandlePaystackWebhook.php
public function handle(array $payload): void
{
    if ($payload['event'] === 'charge.success') {
        Order::where('payment_reference', $payload['data']['reference'])
            ->update(['status' => 'paid']);
    }
}

use KenDeNigerian\PayZephyr\Facades\Payment;
use KenDeNigerian\PayZephyr\DataObjects\SubscriptionPlanDTO;

// Create a subscription plan (using facade)
$planDTO = new SubscriptionPlanDTO(
    name: 'Monthly Premium',
    amount: 5000.00,  // ₦5,000.00
    interval: 'monthly',
    currency: 'NGN',
);

$plan = Payment::subscription()
    ->planData($planDTO)
    ->with('paystack')  // Currently only PaystackDriver supports subscriptions
    ->createPlan();

// Create a subscription with idempotency (prevents duplicates on retries)
$subscription = payment()->subscription()
    ->customer('[email protected]')
    ->plan($plan['plan_code'])
    ->idempotency() // Auto-generates UUID, or pass custom key
    ->with('paystack')
    ->subscribe();  // Final action method (create() is also available as an alias)

// Query subscriptions using query builder
$activeSubscriptions = Payment::subscriptions()
    ->forCustomer('[email protected]')
    ->active()
    ->from('paystack')
    ->get();

// Subscription automatically logged to subscription_transactions table
// Access logged subscription:
use KenDeNigerian\PayZephyr\Models\SubscriptionTransaction;
$logged = SubscriptionTransaction::where('subscription_code', $subscription->subscriptionCode)->first();

// Save subscription details to your own table
DB::table('subscriptions')->insert([
    'subscription_code' => $subscription->subscriptionCode,
    'email_token' => $subscription->emailToken,  // Important for cancel/enable
    'status' => $subscription->status,
]);

use KenDeNigerian\PayZephyr\Models\PaymentTransaction;
use KenDeNigerian\PayZephyr\Models\SubscriptionTransaction;

// Payment transactions
PaymentTransaction::where('reference', 'ORDER_123')->first();
PaymentTransaction::successful()->get();
PaymentTransaction::failed()->get();

// Subscription transactions (automatically logged on create/update/cancel)
SubscriptionTransaction::where('subscription_code', 'SUB_xyz')->first();
SubscriptionTransaction::active()->get();
SubscriptionTransaction::forCustomer('[email protected]')->get();
SubscriptionTransaction::forPlan('PLN_abc123')->get();

// Fallback providers
Payment::amount(100.00)
    ->with(['paystack', 'stripe'])
    ->redirect();

// API-only mode (no redirect)
$response = Payment::amount(100.00)->charge();
return response()->json($response);

// Direct driver access
$driver = app(PaymentManager::class)->driver('paystack');
$driver->healthCheck();
bash
> php artisan vendor:publish --tag=payments-config
> php artisan vendor:publish --tag=payments-migrations
> php artisan migrate
> 
bash
php artisan queue:work