PHP code example of a2zwebltd / laravel-affiliate

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

    

a2zwebltd / laravel-affiliate example snippets


namespace App\Affiliate;

use A2ZWeb\Affiliate\Contracts\RevenueResolver;
use App\Models\Invoice;
use Illuminate\Support\Carbon;

class InvoiceRevenueResolver implements RevenueResolver
{
    // Realised revenue, net of refunds, for a fully closed past month. Called by
    // affiliate:close-month, including again for earlier months to pick up refunds.
    public function revenueForUserInMonth(int $userId, int $year, int $month): int
    {
        $start = Carbon::create($year, $month, 1)->startOfMonth();

        return $this->paidCents($userId, $start, $start->copy()->endOfMonth());
    }

    // Month-to-date revenue, shown as the in-progress estimate on the partner dashboard.
    // Return 0 if you have no live data source.
    public function currentMonthRunningRevenueCents(int $userId): int
    {
        return $this->paidCents($userId, Carbon::now()->startOfMonth(), Carbon::now());
    }

    private function paidCents(int $userId, Carbon $from, Carbon $to): int
    {
        return (int) Invoice::query()
            ->where('user_id', $userId)
            ->whereBetween('paid_at', [$from, $to])
            ->sum('amount_cents');
    }
}

'resolvers' => [
    'revenue' => \App\Affiliate\InvoiceRevenueResolver::class,
    'referred_user_info' => null, // optional, see ReferredUserInfoResolver
],

$this->app->bind(\A2ZWeb\Affiliate\Contracts\RevenueResolver::class, \App\Affiliate\InvoiceRevenueResolver::class);

use A2ZWeb\Affiliate\Concerns\HasAffiliateProgram;

class User extends Authenticatable
{
    use HasAffiliateProgram;
}

event(new \Illuminate\Auth\Events\Registered($user));

Schedule::command('affiliate:close-month')
    ->monthlyOn(1, '02:00')
    ->onOneServer();

use A2ZWeb\Affiliate\Services\ReferralAttributor;
use Illuminate\Support\Carbon;

$referral = app(ReferralAttributor::class)->manuallyAttach(
    $partner,
    $referredUser,
    Carbon::parse('2026-02-15'),
);

use A2ZWeb\Affiliate\Services\CommissionStatementGenerator;

$statement = app(CommissionStatementGenerator::class)->generateForPartner(
    $partner,
    Carbon::parse('2026-02-01')->startOfMonth(),
    Carbon::parse('2026-02-01')->endOfMonth(),
);

use A2ZWeb\Affiliate\Nova\NovaIntegration;

protected function resources(): void
{
    Nova::resources(NovaIntegration::resources());
}

protected function gates(): void
{
    // ...your existing setup...
    Nova::mainMenu(fn (Request $request) => [
        // ...
        NovaIntegration::menuSection(),
    ]);
}
bash
php artisan vendor:publish --tag=affiliate-migrations
php artisan migrate
bash
php artisan affiliate:recalc-partner 1234