You are here

public function SubscriptionTypeBase::collectTrialCharges in Commerce Recurring Framework 8

Collects charges for a subscription's trial period.

Parameters

\Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription: The subscription.

\Drupal\commerce_recurring\BillingPeriod $trial_period: The trial period.

Return value

\Drupal\commerce_recurring\Charge[] The charges.

Overrides SubscriptionTypeInterface::collectTrialCharges

File

src/Plugin/Commerce/SubscriptionType/SubscriptionTypeBase.php, line 84

Class

SubscriptionTypeBase
Defines the subscription base class.

Namespace

Drupal\commerce_recurring\Plugin\Commerce\SubscriptionType

Code

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,
  ];
}