Subscriptions

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\Subscriptions » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
2.0

Subscriptions service.

View source

# Public Properties

Property Description
behaviors yii\base\Behavior – List of behaviors attached to this component

# Public Methods

Method Description
__call() Calls the named method which is not a class method.
__clone() This method is called after the object is created by cloning an existing one.
__construct() Constructor.
__get() Returns the value of a component property.
__isset() Checks if a property is set, i.e. defined and not null.
__set() Sets the value of a component property.
__unset() Sets a component property to be null.
attachBehavior() Attaches a behavior to this component.
attachBehaviors() Attaches a list of behaviors to the component.
beforeDeleteUserHandler() Prevent deleting a user if they have any subscriptions - active or otherwise.
behaviors() Returns a list of behaviors that this component should behave as.
canGetProperty() Returns a value indicating whether a property can be read.
canSetProperty() Returns a value indicating whether a property can be set.
cancelSubscription() Cancel a subscription.
className() Returns the fully qualified name of this class.
createSubscription() Subscribe a user to a subscription plan.
detachBehavior() Detaches a behavior from the component.
detachBehaviors() Detaches all behaviors from the component.
doesUserHaveAnySubscriptions() Return true if the user has any subscriptions at all, even expired ones.
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component.
expireSubscription() Expire a subscription.
getBehavior() Returns the named behavior object.
getBehaviors() Returns all behaviors attached to this component.
getSubscriptionCountForPlanById() Returns subscription count for a plan.
handleChangedFieldLayout() Handle field layout change
handleDeletedFieldLayout() Handle field layout being deleted
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event.
hasMethod() Returns a value indicating whether a method is defined.
hasProperty() Returns a value indicating whether a property is defined for this component.
init() Initializes the object.
off() Detaches an existing event handler from this component.
on() Attaches an event handler to an event.
pruneDeletedField() Prune a deleted field from subscription field layouts.
reactivateSubscription() Reactivate a subscription.
receivePayment() Receive a payment for a subscription
switchSubscriptionPlan() Switch a subscription to a different subscription plan.
trigger() Triggers an event.
updateSubscription() Update a subscription.

# beforeDeleteUserHandler()

Prevent deleting a user if they have any subscriptions - active or otherwise.

View source

Arguments

# cancelSubscription()

Cancel a subscription.

View source

Arguments

Returns

boolean

Throws

# createSubscription()

Subscribe a user to a subscription plan.

View source

Arguments

Returns

craft\commerce\elements\Subscription – The subscription

Throws

# doesUserHaveAnySubscriptions()

Return true if the user has any subscriptions at all, even expired ones.

View source

Arguments

Returns

boolean

# expireSubscription()

Expire a subscription.

View source

Arguments

Returns

boolean – Whether successfully expired subscription

Throws

# getSubscriptionCountForPlanById()

Returns subscription count for a plan.

View source

Arguments

Returns

integer

# handleChangedFieldLayout()

Handle field layout change

View source

Arguments

# handleDeletedFieldLayout()

Handle field layout being deleted

View source

Arguments

# pruneDeletedField()

Prune a deleted field from subscription field layouts.

View source

Arguments

# reactivateSubscription()

Reactivate a subscription.

View source

Arguments

Returns

boolean

Throws

# receivePayment()

Receive a payment for a subscription

View source

Arguments

Returns

boolean

Throws

# switchSubscriptionPlan()

Switch a subscription to a different subscription plan.

View source

Arguments

Returns

boolean

Throws

# updateSubscription()

Update a subscription.

View source

Arguments

Returns

boolean

Throws

# Constants

Constant Description
CONFIG_FIELDLAYOUT_KEY

# Events

# EVENT_AFTER_CANCEL_SUBSCRIPTION

Type
craft\commerce\events\CancelSubscriptionEvent

The event that is triggered after a subscription gets canceled.

use craft\commerce\events\CancelSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\CancelSubscriptionForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_CANCEL_SUBSCRIPTION,
    function(CancelSubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var CancelSubscriptionForm $params
        $params = $event->parameters;

        // Refund the user for the remainder of the subscription
        // ...
    }
);

# EVENT_AFTER_CREATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered after a subscription is created.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_CREATE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // Call a third party service to authorize a user
        // ...
    }
);

# EVENT_AFTER_EXPIRE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered after a subscription has expired.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_EXPIRE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // Make a call to third party service to de-authorize a user
        // ...
    }
);

# EVENT_AFTER_REACTIVATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered after a subscription gets reactivated.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_REACTIVATE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // Re-authorize the user with a third-party service
        // ...
    }
);

# EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN

Type
craft\commerce\events\SubscriptionSwitchPlansEvent

The event that is triggered after a subscription gets switched to a different plan.

use craft\commerce\events\SubscriptionSwitchPlansEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\base\Plan;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\SwitchPlansForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN,
    function(SubscriptionSwitchPlansEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var Plan $oldPlan
        $oldPlan = $event->oldPlan;
        // @var Plan $newPlan
        $newPlan = $event->newPlan;
        // @var SwitchPlansForm $params
        $params = $event->parameters;

        // Adjust the user’s permissions on a third party service
        // ...
    }
);

# EVENT_BEFORE_CANCEL_SUBSCRIPTION

Type
craft\commerce\events\CancelSubscriptionEvent

The event that is triggered before a subscription is canceled.

You may set the isValid property to false on the event to prevent the subscription from being canceled.

use craft\commerce\events\CancelSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\CancelSubscriptionForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_CANCEL_SUBSCRIPTION,
    function(CancelSubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var CancelSubscriptionForm $params
        $params = $event->parameters;

        // Check whether the user is permitted to cancel the subscription
        // ...
    }
);

# EVENT_BEFORE_CREATE_SUBSCRIPTION

Type
craft\commerce\events\CreateSubscriptionEvent

The event that is triggered before a subscription is created.

You may set the isValid property to false on the event to prevent the user from being subscribed to the plan.

use craft\commerce\events\CreateSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\elements\User;
use craft\commerce\base\Plan;
use craft\commerce\models\subscriptions\SubscriptionForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_CREATE_SUBSCRIPTION,
    function(CreateSubscriptionEvent $event) {
        // @var User $user
        $user = $event->user;
        // @var Plan $plan
        $plan = $event->plan;
        // @var SubscriptionForm $params
        $params = $event->parameters;

        // Set the trial days based on some business logic
        // ...
    }
);

# EVENT_BEFORE_REACTIVATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

TThe event that is triggered before a subscription gets reactivated.

You may set the isValid property to false on the event to prevent the subscription from being reactivated.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_REACTIVATE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // Use business logic to determine whether the user can reactivate
        // ...
    }
);

# EVENT_BEFORE_SWITCH_SUBSCRIPTION_PLAN

Type
craft\commerce\events\SubscriptionSwitchPlansEvent

The event that is triggered before a subscription is switched to a different plan.

You may set the isValid property to false on the event to prevent the switch from happening.

use craft\commerce\events\SubscriptionSwitchPlansEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\base\Plan;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\SwitchPlansForm;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_SWITCH_SUBSCRIPTION_PLAN,
    function(SubscriptionSwitchPlansEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var Plan $oldPlan
        $oldPlan = $event->oldPlan;
        // @var Plan $newPlan
        $newPlan = $event->newPlan;
        // @var SwitchPlansForm $params
        $params = $event->parameters;

        // Modify the switch parameters based on some business logic
        // ...
    }
);

# EVENT_BEFORE_UPDATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered before a subscription gets updated. Typically this event is fired when subscription data is updated on the gateway.

use craft\commerce\events\SubscriptionEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_BEFORE_UPDATE_SUBSCRIPTION,
    function(SubscriptionEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;

        // ...
    }
);

# EVENT_RECEIVE_SUBSCRIPTION_PAYMENT

Type
craft\commerce\events\SubscriptionPaymentEvent

The event that is triggered when a subscription payment is received.

use craft\commerce\events\SubscriptionPaymentEvent;
use craft\commerce\services\Subscriptions;
use craft\commerce\elements\Subscription;
use craft\commerce\models\subscriptions\SubscriptionPayment;
use DateTime;
use yii\base\Event;

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_RECEIVE_SUBSCRIPTION_PAYMENT,
    function(SubscriptionPaymentEvent $event) {
        // @var Subscription $subscription
        $subscription = $event->subscription;
        // @var SubscriptionPayment $payment
        $payment = $event->payment;
        // @var DateTime $until
        $until = $event->paidUntil;

        // Update loyalty reward data
        // ...
    }
);