PHP code example of roundly-consulting / purchases-for-laravel

1. Go to this page and download the library: Download roundly-consulting/purchases-for-laravel 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/ */

    

roundly-consulting / purchases-for-laravel example snippets


use RoundlyConsulting\Purchases\Facades\Purchases;

$model = Purchases::handle('stripe', $request);   // verify the signature, record, fire events

use App\Models\User;
use Illuminate\Support\Facades\Event;
use RoundlyConsulting\Purchases\Events\SubscriptionStarted;

Event::listen(function (SubscriptionStarted $event): void {
    $customer = $event->result->raw()['customer'];          // the Stripe customer id

    $event->subscription->owner()->associate(User::firstWhere('stripe_id', $customer));
    $event->subscription->name = 'pro';                      // Stripe names no plan
    $event->subscription->save();
});

Purchases::for($user)->subscribedTo('pro');          // true
Purchases::for($user)->activeSubscription('pro');    // ?Subscription
bash
composer purchases:install --providers   # publishes config + migrations, appends the providers' .env keys
php artisan migrate