abstract class SubscriptionTypeBase in Commerce Recurring Framework 8
Defines the subscription base class.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_recurring\Plugin\Commerce\SubscriptionType\SubscriptionTypeBase implements SubscriptionTypeInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of SubscriptionTypeBase
File
- src/
Plugin/ Commerce/ SubscriptionType/ SubscriptionTypeBase.php, line 19
Namespace
Drupal\commerce_recurring\Plugin\Commerce\SubscriptionTypeView source
abstract class SubscriptionTypeBase extends PluginBase implements SubscriptionTypeInterface, ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new SubscriptionTypeBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
return [];
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->pluginDefinition['label'];
}
/**
* {@inheritdoc}
*/
public function getPurchasableEntityTypeId() {
if (!empty($this->pluginDefinition['purchasable_entity_type'])) {
return $this->pluginDefinition['purchasable_entity_type'];
}
}
/**
* {@inheritdoc}
*/
public function collectTrialCharges(SubscriptionInterface $subscription, BillingPeriod $trial_period) {
$start_date = $subscription
->getStartDate();
$billing_type = $subscription
->getBillingSchedule()
->getBillingType();
if ($billing_type == BillingScheduleInterface::BILLING_TYPE_PREPAID) {
$billing_schedule = $subscription
->getBillingSchedule()
->getPlugin();
// The base charge for prepaid subscriptions always covers the next
// period, which in the case of trials is the first billing period.
$base_unit_price = $subscription
->getUnitPrice();
$first_billing_period = $billing_schedule
->generateFirstBillingPeriod($start_date);
$base_billing_period = $this
->adjustBillingPeriod($first_billing_period, $subscription);
$full_billing_period = $first_billing_period;
}
else {
// The base charge for postpaid subscriptions covers the current (trial)
// period, which means that the plan needs to be free.
$base_unit_price = $subscription
->getUnitPrice()
->multiply('0');
$base_billing_period = $this
->adjustTrialPeriod($trial_period, $subscription);
$full_billing_period = $trial_period;
}
$base_charge = new Charge([
'purchased_entity' => $subscription
->getPurchasedEntity(),
'title' => $subscription
->getTitle(),
'quantity' => $subscription
->getQuantity(),
'unit_price' => $base_unit_price,
'billing_period' => $base_billing_period,
'full_billing_period' => $full_billing_period,
]);
return [
$base_charge,
];
}
/**
* {@inheritdoc}
*/
public function collectCharges(SubscriptionInterface $subscription, BillingPeriod $billing_period) {
$start_date = $subscription
->getStartDate();
$billing_type = $subscription
->getBillingSchedule()
->getBillingType();
if ($billing_type == BillingScheduleInterface::BILLING_TYPE_PREPAID) {
// The subscription has either ended, or is scheduled for cancellation,
// meaning there's nothing left to prepay.
if ($subscription
->getState()
->getId() != 'active' || $subscription
->hasScheduledChange('state', 'canceled')) {
return [];
}
$billing_schedule = $subscription
->getBillingSchedule()
->getPlugin();
// The initial order (which starts the subscription) pays the first
// billing period, so the base charge is always for the next one.
// The October recurring order (ending on Nov 1st) charges for November.
$next_billing_period = $billing_schedule
->generateNextBillingPeriod($start_date, $billing_period);
$base_billing_period = $this
->adjustBillingPeriod($next_billing_period, $subscription);
$full_billing_period = $next_billing_period;
}
else {
// Postpaid means we're always charging for the current billing period.
// The October recurring order (ending on Nov 1st) charges for October.
$base_billing_period = $this
->adjustBillingPeriod($billing_period, $subscription);
$full_billing_period = $billing_period;
}
$base_charge = new Charge([
'purchased_entity' => $subscription
->getPurchasedEntity(),
'title' => $subscription
->getTitle(),
'quantity' => $subscription
->getQuantity(),
'unit_price' => $subscription
->getUnitPrice(),
'billing_period' => $base_billing_period,
'full_billing_period' => $full_billing_period,
]);
return [
$base_charge,
];
}
/**
* Adjusts the trial period to reflect the trial end date.
*
* There is no need to adjust the start date because trial periods are
* always rolling.
*
* @param \Drupal\commerce_recurring\BillingPeriod $trial_period
* The trial period.
* @param \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription
* The subscription.
*
* @return \Drupal\commerce_recurring\BillingPeriod
* The adjusted trial period.
*/
protected function adjustTrialPeriod(BillingPeriod $trial_period, SubscriptionInterface $subscription) {
$trial_end_date = $subscription
->getTrialEndDate();
$end_date = $trial_period
->getEndDate();
if ($trial_end_date && $trial_period
->contains($trial_end_date)) {
// The trial ended before the end of the trial period.
$end_date = $trial_end_date;
}
return new BillingPeriod($trial_period
->getStartDate(), $end_date);
}
/**
* Adjusts the billing period to reflect the subscription start/end dates.
*
* @param \Drupal\commerce_recurring\BillingPeriod $billing_period
* The billing period.
* @param \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription
* The subscription.
*
* @return \Drupal\commerce_recurring\BillingPeriod
* The adjusted billing period.
*/
protected function adjustBillingPeriod(BillingPeriod $billing_period, SubscriptionInterface $subscription) {
$subscription_start_date = $subscription
->getStartDate();
$subscription_end_date = $subscription
->getEndDate();
$start_date = $billing_period
->getStartDate();
$end_date = $billing_period
->getEndDate();
if ($billing_period
->contains($subscription_start_date)) {
// The subscription started after the billing period (E.g: customer
// subscribed on Mar 10th for a Mar 1st - Apr 1st period).
$start_date = $subscription_start_date;
}
if ($subscription_end_date && $billing_period
->contains($subscription_end_date)) {
// The subscription ended before the end of the billing period.
$end_date = $subscription_end_date;
}
return new BillingPeriod($start_date, $end_date);
}
/**
* {@inheritdoc}
*/
public function onSubscriptionCreate(SubscriptionInterface $subscription, OrderItemInterface $order_item) {
}
/**
* {@inheritdoc}
*/
public function onSubscriptionTrialStart(SubscriptionInterface $subscription, OrderInterface $order) {
}
/**
* {@inheritdoc}
*/
public function onSubscriptionTrialCancel(SubscriptionInterface $subscription) {
}
/**
* {@inheritdoc}
*/
public function onSubscriptionActivate(SubscriptionInterface $subscription, OrderInterface $order) {
}
/**
* {@inheritdoc}
*/
public function onSubscriptionRenew(SubscriptionInterface $subscription, OrderInterface $order, OrderInterface $next_order) {
}
/**
* {@inheritdoc}
*/
public function onSubscriptionExpire(SubscriptionInterface $subscription) {
}
/**
* {@inheritdoc}
*/
public function onSubscriptionCancel(SubscriptionInterface $subscription) {
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
SubscriptionTypeBase:: |
protected | property | The entity type manager. | |
SubscriptionTypeBase:: |
protected | function | Adjusts the billing period to reflect the subscription start/end dates. | |
SubscriptionTypeBase:: |
protected | function | Adjusts the trial period to reflect the trial end date. | |
SubscriptionTypeBase:: |
public | function |
Builds the field definitions for entities of this bundle. Overrides BundlePluginInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Collects charges for a subscription's billing period. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Collects charges for a subscription's trial period. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Gets the subscription type label. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Gets the subscription type's purchasable entity type ID. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Acts on a subscription after it has been activated. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Acts on a subscription after it has been canceled. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Acts on a subscription after it has been created from an order item. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Acts on a subscription after it has expired. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Acts on a subscription after it has been renewed. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Acts on a subscription after a trial has been canceled. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Acts on a subscription after a trial has been started. Overrides SubscriptionTypeInterface:: |
|
SubscriptionTypeBase:: |
public | function |
Constructs a new SubscriptionTypeBase object. Overrides PluginBase:: |