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

Susbcriptions service.

View source

Public Properties

PropertyDescription
$behaviorsyii\base\Behavior – List of behaviors attached to this component

Public Methods

MethodDescription
__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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to 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 susbcription count for a plan.
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.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
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

Signature

public void beforeDeleteUserHandler ( craft\events\ModelEvent $event )

cancelSubscription()

Cancel a subscription.

View source

Arguments

Returns

boolean

Throws

Signature

public boolean cancelSubscription ( \craft\commerce\elements\Subscription $subscription, craft\commerce\models\subscriptions\CancelSubscriptionForm $parameters )

createSubscription()

Subscribe a user to a subscription plan.

View source

Arguments

Returns

\craft\commerce\elements\Subscription – The subscription

Throws

Signature

public \craft\commerce\elements\Subscription createSubscription ( craft\elements\User $user, craft\commerce\base\Plan $plan, craft\commerce\models\subscriptions\SubscriptionForm $parameters, array $fieldValues = [] )

doesUserHaveAnySubscriptions()

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

View source

Arguments

Returns

boolean

Signature

public boolean doesUserHaveAnySubscriptions ( \craft\commerce\services\int $userId )

expireSubscription()

Expire a subscription.

View source

Arguments

  • $subscription (\craft\commerce\elements\Subscription) – Subscription to expire
  • $dateTime (DateTime) – Expiry date time

Returns

boolean – Whether succesfully expired subscription

Throws

Signature

public boolean expireSubscription ( \craft\commerce\elements\Subscription $subscription, DateTime $dateTime = null )

getSubscriptionCountForPlanById()

Returns susbcription count for a plan.

View source

Arguments

Returns

integer

Signature

public integer getSubscriptionCountForPlanById ( \craft\commerce\services\int $planId )

reactivateSubscription()

Reactivate a subscription.

View source

Arguments

  • $subscription (\craft\commerce\elements\Subscription)

Returns

boolean

Throws

Signature

public boolean reactivateSubscription ( \craft\commerce\elements\Subscription $subscription )

receivePayment()

Receive a payment for a subscription

View source

Arguments

Returns

boolean

Throws

Signature

public boolean receivePayment ( \craft\commerce\elements\Subscription $subscription, craft\commerce\models\subscriptions\SubscriptionPayment $payment, DateTime $paidUntil )

switchSubscriptionPlan()

Switch a subscription to a different subscription plan.

View source

Arguments

Returns

boolean

Throws

Signature

public boolean switchSubscriptionPlan ( \craft\commerce\elements\Subscription $subscription, craft\commerce\base\Plan $plan, craft\commerce\models\subscriptions\SwitchPlansForm $parameters )

updateSubscription()

Update a subscription.

View source

Arguments

  • $subscription (\craft\commerce\elements\Subscription)

Returns

boolean

Throws

Signature

public boolean updateSubscription ( \craft\commerce\elements\Subscription $subscription )

Events

EVENT_AFTER_CANCEL_SUBSCRIPTION

Type
craft\commerce\events\CancelSubscriptionEvent

The event that is triggered after a subscription is canceled

Plugins can get notified after a subscription gets canceled.

use craft\commerce\events\CancelSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;

Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_CANCEL_SUBSCRIPTION, function(CancelSubscriptionEvent $e) {
    // Do something - maybe 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

Plugins can get notified after a subscription is created.

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

Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_CREATE_SUBSCRIPTION, function(SubscriptionEvent $e) {
    // Do something about it - perhaps make a call to 3rd party service to authorize a user
});

EVENT_AFTER_EXPIRE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered when a subscription is expired.

Plugins can get notified when a subscription is being expired.

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

Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_EXPIRE_SUBSCRIPTION, function(SubscriptionEvent $e) {
    // Do something about it - perhaps make a call to 3rd party service to de-authorize a user.
});

EVENT_AFTER_REACTIVATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered after a subscription is reactivated

Plugins can get notified before a subscription gets reactivated.

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

Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_REACTIVATE_SUBSCRIPTION, function(SubscriptionEvent $e) {
    // Do something - maybe the user needs to be re-authorized with a 3rd party service.
});

EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN

Type
craft\commerce\events\SubscriptionSwitchPlansEvent

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

Plugins can get notified after a subscription gets switched to a different plan.

use craft\commerce\events\SubscriptionSwitchPlansEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;

Event::on(Subscriptions::class, Subscriptions::EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN, function(SubscriptionSwitchPlansEvent $e) {
    // Do something - maybe the user needs their permissions adjusted on a 3rd party service.
});

EVENT_BEFORE_CANCEL_SUBSCRIPTION

Type
craft\commerce\events\CancelSubscriptionEvent

The event that is triggered before a subscription is canceled You may set craft\commerce\events\CancelSubscriptionEvent::$isValid to false to prevent the subscription from being canceled

Plugins can get notified before a subscription is canceled.

use craft\commerce\events\CancelSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;

Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_CANCEL_SUBSCRIPTION, function(CancelSubscriptionEvent $e) {
    // Do something - maybe the user is not permitted to cancel the subscription for some reason.
});

EVENT_BEFORE_CREATE_SUBSCRIPTION

Type
craft\commerce\events\CreateSubscriptionEvent

The event that is triggered before a subscription is created. You may set craft\commerce\events\CreateSubscriptionEvent::$isValid to false to prevent the user from being subscribed to the plan.

Plugins can get notified before a subscription is created.

use craft\commerce\events\CreateSubscriptionEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;

Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_CREATE_SUBSCRIPTION, function(CreateSubscriptionEvent $e) {
    // Do something - perhaps check if the user is eligible for the parameters set and prevent the subscription if not.
});

EVENT_BEFORE_REACTIVATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered before a subscription is reactivated You may set craft\commerce\events\SubscriptionEvent::$isValid to false to prevent the subscription from being reactivated

Plugins can get notified before a subscription gets reactivated.

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

Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_REACTIVATE_SUBSCRIPTION, function(SubscriptionEvent $e) {
    // Do something - maybe the user does not qualify for reactivation due to some business logic.
});

EVENT_BEFORE_SWITCH_SUBSCRIPTION_PLAN

Type
craft\commerce\events\SubscriptionSwitchPlansEvent

The event that is triggered before a plan switch happens for a subscription You may set craft\commerce\events\SubscriptionSwitchPlansEvent::$isValid to false to prevent the switch from happening

Plugins can get notified before a subscription is switched to a different plan.

use craft\commerce\events\SubscriptionSwitchPlansEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;

Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_SWITCH_SUBSCRIPTION_PLAN, function(SubscriptionSwitchPlansEvent $e) {
    // Do something - maybe the user is not permitted to switch to that plan due to some business logic.
});

EVENT_BEFORE_UPDATE_SUBSCRIPTION

Type
craft\commerce\events\SubscriptionEvent

The event that is triggered after an existing

Plugins can get notified 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 yii\base\Event;

Event::on(Subscriptions::class, Subscriptions::EVENT_BEFORE_UPDATE_SUBSCRIPTION, function(SubscriptionEvent $e) {
    // Do something - maybe refund the user for the remainder of the subscription.
});

EVENT_RECEIVE_SUBSCRIPTION_PAYMENT

Type
craft\commerce\events\SubscriptionPaymentEvent

The event that is triggered after receiving a subscription payment

Plugins can get notified when a subscription payment is received.

use craft\commerce\events\SubscriptionPaymentEvent;
use craft\commerce\services\Subscriptions;
use yii\base\Event;

Event::on(Subscriptions::class, Subscriptions::EVENT_RECEIVE_SUBSCRIPTION_PAYMENT, function(SubscriptionPaymentEvent $e) {
    // Do something - perhaps update the loyalty reward data.
});