public function SubscriptionTypeBase::collectCharges in Commerce Recurring Framework 8
Collects charges for a subscription's billing period.
Parameters
\Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription: The subscription.
\Drupal\commerce_recurring\BillingPeriod $billing_period: The billing period.
Return value
\Drupal\commerce_recurring\Charge[] The charges.
Overrides SubscriptionTypeInterface::collectCharges
File
- src/
Plugin/ Commerce/ SubscriptionType/ SubscriptionTypeBase.php, line 118
Class
- SubscriptionTypeBase
- Defines the subscription base class.
Namespace
Drupal\commerce_recurring\Plugin\Commerce\SubscriptionTypeCode
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,
];
}