think.studio/laravel-cashier-subscription-pause

Support of Pausing and Resuming a Subscription.

2.1.1 2023-12-06 12:47 UTC

This package is auto-updated.

Last update: 2024-11-06 14:48:58 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

Installation

Install the package via composer:

composer require think.studio/laravel-cashier-subscription-pause

You can publish the migrations files with:

php artisan vendor:publish --provider="CashierSubscriptionPause\ServiceProvider" --tag="migrations"

Usage

use CashierSubscriptionPause\Eloquent\UsesPauseCollection;
use CashierSubscriptionPause\Eloquent\WithPauseCollection;

class Subscription extends \Laravel\Cashier\Subscription implements WithPauseCollection {
    
    use UsesPauseCollection;

    protected $table = 'stripe_subscriptions';

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'quantity'         => 'integer',
        'pause_collection' => 'array',
    ];
}
$subscription = $user->subscription( 'main' );

$resumesAt = Carbon::now()->addWeek();
$subscription->pause(WithPauseCollection::BEHAVIOR_MARK_UNCOLLECTIBLE, $resumesAt);
$subscription->unpause();

$subscription->pauseBehaviorMarkUncollectible($resumesAt);
$subscription->pauseBehaviorKeepAsDraft($resumesAt);
$subscription->pauseBehaviorVoid($resumesAt);

$subscription->syncStripePauseCollection();

$subscription->paused());
$subscription->paused(WithPauseCollection::BEHAVIOR_MARK_UNCOLLECTIBLE);
$subscription->paused(WithPauseCollection::BEHAVIOR_KEEP_AS_DRAFT);
$subscription->paused(WithPauseCollection::BEHAVIOR_VOID);
$subscription->pauseResumesAtTimestamp();
$subscription->pauseResumesAt();
Subscription::query()->paused()->count();
Subscription::query()->notPaused()->count();

Subscription::query()->paused(WithPauseCollection::BEHAVIOR_VOID)->count();
Subscription::query()->notPaused(WithPauseCollection::BEHAVIOR_VOID)->count();

Subscription::query()->paused(WithPauseCollection::BEHAVIOR_MARK_UNCOLLECTIBLE)->count();
Subscription::query()->notPaused(WithPauseCollection::BEHAVIOR_MARK_UNCOLLECTIBLE)->count();

Subscription::query()->paused(WithPauseCollection::BEHAVIOR_KEEP_AS_DRAFT)->count();
Subscription::query()->notPaused(WithPauseCollection::BEHAVIOR_KEEP_AS_DRAFT)->count();

Do not use default listener:

<?php

namespace App\Providers;

use CashierSubscriptionPause\Listeners\CashierWebhookHandledEventListener;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    public function boot()
    {
       // ...
        CashierWebhookHandledEventListener::deactivateListener();
    }
}

Credits

  • Think Studio