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.
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. |
doesUserHaveSubscriptions() | 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. |
getSubscriptionCountByPlanId() | Returns subscription count for a plan. |
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() | |
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.
Arguments ​
$event
(craft\events\ModelEvent) – The event.
cancelSubscription()
​
Cancel a subscription.
Arguments ​
$subscription
$parameters
Throws ​
- yii\base\InvalidConfigException
if the gateway does not support subscriptions - craft\commerce\errors\SubscriptionException
if something went wrong when canceling subscription
createSubscription()
​
Subscribe a user to a subscription plan.
Arguments ​
$user
(craft\elements\User) – The user subscribing to a plan$plan
(craft\commerce\base\Plan) – The plan the user is being subscribed to$parameters
(craft\commerce\models\subscriptions\SubscriptionForm) – Array of additional parameters to use$fieldValues
(array) – Array of content field values to set
Returns ​
craft\commerce\elements\Subscription – The subscription
Throws ​
- craft\errors\ElementNotFoundException
- yii\base\Exception
- yii\base\InvalidConfigException
if the gateway does not support subscriptions - craft\commerce\errors\SubscriptionException
if something went wrong during subscription - Throwable
doesUserHaveAnySubscriptions()
​
DEPRECATED
Deprecated in 4.0. Use doesUserHaveSubscriptions() instead.
Return true if the user has any subscriptions at all, even expired ones.
Arguments ​
$userId
doesUserHaveSubscriptions()
​
Return true if the user has any subscriptions at all, even expired ones.
Arguments ​
$userId
expireSubscription()
​
Expire a subscription.
Arguments ​
$subscription
(craft\commerce\elements\Subscription) – Subscription to expire$dateTime
(DateTime, null) – Expiry date time
Returns ​
boolean – Whether successfully expired subscription
Throws ​
- craft\errors\ElementNotFoundException
- yii\base\Exception
- Throwable
if cannot expire subscription
getSubscriptionCountByPlanId()
​
Returns subscription count for a plan.
Arguments ​
$planId
getSubscriptionCountForPlanById()
​
DEPRECATED
Deprecated in 4.0. Use getSubscriptionCountByPlanId() instead.
Returns subscription count for a plan.
Arguments ​
$planId
handleChangedFieldLayout()
​
Handle field layout change
Arguments ​
$event
Throws ​
handleDeletedFieldLayout()
​
Handle field layout being deleted
pruneDeletedField()
​
DEPRECATED
Deprecated in 3.4.17. Unused fields will be pruned automatically as field layouts are resaved.
reactivateSubscription()
​
Reactivate a subscription.
Arguments ​
$subscription
Throws ​
- yii\base\InvalidConfigException
if the gateway does not support subscriptions - Throwable
- craft\errors\ElementNotFoundException
- yii\base\Exception
receivePayment()
​
Receive a payment for a subscription
Arguments ​
$subscription
$payment
$paidUntil
Throws ​
switchSubscriptionPlan()
​
Switch a subscription to a different subscription plan.
Arguments ​
$subscription
(craft\commerce\elements\Subscription) – The subscription to modify$plan
(craft\commerce\base\Plan) – The plan to change the subscription to$parameters
(craft\commerce\models\subscriptions\SwitchPlansForm) – Additional parameters to use
Throws ​
updateSubscription()
​
Update a subscription.
Arguments ​
$subscription
Throws ​
Constants ​
Constant | Description |
---|---|
CONFIG_FIELDLAYOUT_KEY |
Events ​
EVENT_AFTER_CANCEL_SUBSCRIPTION ​
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 ​
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 ​
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 ​
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 ​
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 ​
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 ​
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 ​
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 ​
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 ​
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 ​
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
// ...
}
);