PHP code example of meilleursbiens / laravel-proabono-webhook

1. Go to this page and download the library: Download meilleursbiens/laravel-proabono-webhook 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/ */

    

meilleursbiens / laravel-proabono-webhook example snippets


// routes/web.php
Route::proAbonoWebhooks('billing/proabono');

use MeilleursBiens\ProAbonoWebhook\Events\ProAbonoVerificationCodeReceived;

Event::listen(function (ProAbonoVerificationCodeReceived $event) {
    Log::info("ProAbono verification code: {$event->code}");
});

'jobs' => [
    'SubscriptionStarted' => \App\Jobs\ProAbono\HandleStartedSubscription::class,
    'InvoiceDebitPaid' => \App\Jobs\ProAbono\HandlePaidInvoice::class,
    // '*' => \App\Jobs\ProAbono\HandleAnyEvent::class,
],

namespace App\Jobs\ProAbono;

use Illuminate\Contracts\Queue\ShouldQueue;
use MeilleursBiens\ProAbonoWebhook\Models\ProAbonoWebhookCall;

class HandleStartedSubscription implements ShouldQueue
{
    public function __construct(public ProAbonoWebhookCall $webhookCall) {}

    public function handle(): void
    {
        $notification = $this->webhookCall->notification();

        $agent = Agent::where('proabono_customer_id', $notification->idCustomer())->first();

        $agent?->activateSubscription($notification->idSubscription());
    }
}

use Illuminate\Support\Facades\Event;
use MeilleursBiens\ProAbonoWebhook\Models\ProAbonoWebhookCall;

Event::listen('proabono-webhooks::InvoiceDebitPaid', function (ProAbonoWebhookCall $call) {
    // ...
});

use MeilleursBiens\ProAbonoWebhook\Events\ProAbonoWebhookReceived;
use MeilleursBiens\ProAbonoWebhook\ProAbonoTrigger;

Event::listen(function (ProAbonoWebhookReceived $event) {
    match ($event->trigger()) {
        ProAbonoTrigger::SubscriptionStarted => /* ... */,
        ProAbonoTrigger::SubscriptionTerminated => /* ... */,
        default => null,
    };
});

use MeilleursBiens\ProAbonoWebhook\ProAbonoTrigger;

'triggers' => [
    ProAbonoTrigger::SubscriptionStarted,
    ProAbonoTrigger::SubscriptionTerminated,
    ProAbonoTrigger::InvoiceDebitPaid,
],

$notification = $webhookCall->notification();

$notification->id();               // 'trg_42' — ProAbono's id for this trigger
$notification->trigger();          // ProAbonoTrigger::SubscriptionStarted|null
$notification->triggerName();      // 'SubscriptionStarted'
$notification->is(ProAbonoTrigger::SubscriptionStarted); // bool
$notification->idBusiness();       // ?int
$notification->idSegment();        // ?int
$notification->referenceSegment(); // ?string
$notification->dateTrigger();      // ?CarbonImmutable — when the event happened

$notification->customer()?->reference(); // your own customer reference
$notification->customer()?->email();
$notification->customer()?->status();    // 'Enabled', 'Suspended', …

$notification->subscription()?->isActive();          // bool
$notification->offer()?->reference();                // 'offer_sample'
$notification->invoice()?->amountTotal();            // 4491 — debit or credit
$notification->gatewayPermission()?->nameDisplay();  // '****-****-****-4242'

$notification->idCustomer();        // Customer.Id
$notification->referenceCustomer(); // Customer.ReferenceCustomer
$notification->idSubscription();    // Subscription.Id
$notification->idOffer();           // Offer.Id
$notification->idInvoice();         // InvoiceDebit.Id, falling back to InvoiceCredit.Id

$notification->get('Subscription.StateSubscription'); // 'ActiveRunning'
$notification->all();                                 // the raw body

use MeilleursBiens\ProAbonoWebhook\Models\ProAbonoWebhookCall;
use MeilleursBiens\ProAbonoWebhook\ProAbonoTrigger;

ProAbonoWebhookCall::forTrigger(ProAbonoTrigger::InvoiceDebitPaid)->latest()->get();
ProAbonoWebhookCall::verifications()->latest()->first();
ProAbonoWebhookCall::whereNotNull('exception')->get();   // calls whose job blew up

use Illuminate\Support\Facades\Schedule;
use MeilleursBiens\ProAbonoWebhook\Models\ProAbonoWebhookCall;

Schedule::command('model:prune', [
    '--model' => [ProAbonoWebhookCall::class],
])->daily();

ProAbonoTrigger::SubscriptionStarted->category();          // ProAbonoTriggerCategory::Subscription
ProAbonoTriggerCategory::Invoice->triggers();              // all invoice triggers
ProAbonoTrigger::tryFromLoose('subscriptionstarted');      // ProAbonoTrigger::SubscriptionStarted

use MeilleursBiens\ProAbonoWebhook\SignatureValidator\ProAbonoSignatureValidator;

$key = 'test-key';

$this->postJson('/proabono/webhook', ['TypeTrigger' => 'SubscriptionStarted'], [
    'x-proabono-key' => $key,
    'x-proabono-signature' => ProAbonoSignatureValidator::sign($key, config('proabono-webhook.signing_secret')),
])->assertOk();
bash
php artisan vendor:publish --tag=proabono-webhook-config
php artisan vendor:publish --tag=proabono-webhook-migrations
php artisan migrate
bash
   php artisan proabono:verification-code