1. Go to this page and download the library: Download sunucode/afripay 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/ */
Event::listen(PaymentCompleted::class, function ($event) {
$order = $event->transaction->payable;
$order->markAsPaid();
});
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Event;
use SunuCode\AfriPay\Events\PaymentCompleted;
use SunuCode\AfriPay\Events\PaymentFailed;
use SunuCode\AfriPay\Events\PaymentRefunded;
public function boot(): void
{
Event::listen(PaymentCompleted::class, function ($event) {
$transaction = $event->transaction;
$payable = $transaction->payable; // Le modèle lié (Order, Subscription...)
match ($transaction->payable_type) {
\App\Models\Subscription::class => $payable->activate(),
\App\Models\Order::class => $payable->markAsPaid(),
default => $this->handleGenericPayment($transaction),
};
});
Event::listen(PaymentFailed::class, function ($event) {
$transaction = $event->transaction;
match ($transaction->payable_type) {
\App\Models\Order::class => $transaction->payable->cancel(),
default => null,
};
// Notifier l'utilisateur dans tous les cas
// Notification::send($transaction->payable?->user, new PaymentFailedNotification($transaction));
});
Event::listen(PaymentRefunded::class, function ($event) {
// $event->reason contient le motif du remboursement
});
}
use SunuCode\AfriPay\Facades\AfriPay;
use SunuCode\AfriPay\Models\Transaction as AfriPayTransaction;
public function success(string $reference)
{
$transaction = AfriPayTransaction::where('reference', $reference)->firstOrFail();
// Vérifie auprès de la passerelle ET dispatche PaymentCompleted si confirmé
$transaction = AfriPay::verifyAndProcess($transaction);
if ($transaction->status->isCompleted()) {
return view('payment.success', compact('transaction'));
}
// Le paiement n'est pas encore confirmé (webhook en attente)
return view('payment.pending', compact('transaction'));
}
// Toutes les passerelles activées via .env
$gateways = AfriPay::enabledGateways();
// ['wave', 'stripe', 'paydunya', 'paytech']
// Vérifier si une passerelle est active
if (AfriPay::isEnabled('orange_money')) {
// ...
}
// Dans un ServiceProvider
use SunuCode\AfriPay\PaymentManager;
PaymentManager::extend('cinetpay', function (array $config) {
return new CinetPayGateway($config);
});
// Utilisation
AfriPay::via('cinetpay')->charge([...]);
use Illuminate\Support\Facades\Event;
use SunuCode\AfriPay\Events\PaymentCompleted;
use SunuCode\AfriPay\Events\PaymentFailed;
public function boot(): void
{
Event::listen(PaymentCompleted::class, function ($event) {
$transaction = $event->transaction;
$payable = $transaction->payable;
match ($transaction->payable_type) {
\App\Models\Subscription::class => $payable->activate(),
\App\Models\Order::class => $payable->markAsPaid(),
default => null,
};
});
Event::listen(PaymentFailed::class, function ($event) {
// Notify user, log failure, etc.
});
}
use SunuCode\AfriPay\Facades\AfriPay;
use SunuCode\AfriPay\Models\Transaction as AfriPayTransaction;
public function success(string $reference)
{
$transaction = AfriPayTransaction::where('reference', $reference)->firstOrFail();
$transaction = AfriPay::verifyAndProcess($transaction);
if ($transaction->status->isCompleted()) {
return view('payment.success', compact('transaction'));
}
return view('payment.pending', compact('transaction'));
}
use SunuCode\AfriPay\Contracts\GatewayInterface;
use SunuCode\AfriPay\PaymentManager;
class CinetPayGateway implements GatewayInterface
{
// Implement the 4 methods: charge(), handleWebhook(), verify(), verifySignature()
}
PaymentManager::extend('cinetpay', fn($config) => new CinetPayGateway($config));