You are here

final class Charge in Commerce Recurring Framework 8

Represents a charge.

Charges are returned from the subscription type, and then mapped to new or existing recurring order items. This allows order items to be reused when possible.

Hierarchy

  • class \Drupal\commerce_recurring\Charge

Expanded class hierarchy of Charge

4 files declare their use of Charge
ChargeTest.php in tests/src/Kernel/ChargeTest.php
ProductVariationTest.php in tests/src/Kernel/Plugin/Commerce/SubscriptionType/ProductVariationTest.php
StandaloneTest.php in tests/src/Kernel/Plugin/Commerce/SubscriptionType/StandaloneTest.php
SubscriptionTypeBase.php in src/Plugin/Commerce/SubscriptionType/SubscriptionTypeBase.php

File

src/Charge.php, line 15

Namespace

Drupal\commerce_recurring
View source
final class Charge {

  /**
   * The purchased entity, when available.
   *
   * @var \Drupal\commerce\PurchasableEntityInterface|null
   */
  protected $purchasedEntity;

  /**
   * The title.
   *
   * @var string
   */
  protected $title;

  /**
   * The quantity.
   *
   * @var string
   */
  protected $quantity;

  /**
   * The unit price.
   *
   * @var \Drupal\commerce_price\Price
   */
  protected $unitPrice;

  /**
   * The billing period.
   *
   * @var \Drupal\commerce_recurring\BillingPeriod
   */
  protected $billingPeriod;

  /**
   * The full billing period.
   *
   * @var \Drupal\commerce_recurring\BillingPeriod
   */
  protected $fullBillingPeriod;

  /**
   * Constructs a new Charge object.
   *
   * @param array $definition
   *   The definition.
   */
  public function __construct(array $definition) {
    foreach ([
      'title',
      'unit_price',
      'billing_period',
      'full_billing_period',
    ] as $required_property) {
      if (empty($definition[$required_property])) {
        throw new \InvalidArgumentException(sprintf('Missing required property "%s".', $required_property));
      }
    }
    if (isset($definition['purchased_entity']) && !$definition['purchased_entity'] instanceof PurchasableEntityInterface) {
      throw new \InvalidArgumentException(sprintf('The "purchased_entity" property must be an instance of %s.', PurchasableEntityInterface::class));
    }
    if (!$definition['unit_price'] instanceof Price) {
      throw new \InvalidArgumentException(sprintf('The "unit_price" property must be an instance of %s.', Price::class));
    }
    if (!$definition['billing_period'] instanceof BillingPeriod) {
      throw new \InvalidArgumentException(sprintf('The "billing_period" property must be an instance of %s.', BillingPeriod::class));
    }
    if (!$definition['full_billing_period'] instanceof BillingPeriod) {
      throw new \InvalidArgumentException(sprintf('The "full_billing_period" property must be an instance of %s.', BillingPeriod::class));
    }
    $this->purchasedEntity = isset($definition['purchased_entity']) ? $definition['purchased_entity'] : NULL;
    $this->title = $definition['title'];
    $this->quantity = isset($definition['quantity']) ? $definition['quantity'] : '1';
    $this->unitPrice = $definition['unit_price'];
    $this->billingPeriod = $definition['billing_period'];
    $this->fullBillingPeriod = $definition['full_billing_period'];
  }

  /**
   * Gets the purchased entity.
   *
   * @return \Drupal\commerce\PurchasableEntityInterface|null
   *   The purchased entity, or NULL if the charge is not backed by one.
   */
  public function getPurchasedEntity() {
    return $this->purchasedEntity;
  }

  /**
   * Gets the title.
   *
   * @return string
   *   The title.
   */
  public function getTitle() {
    return $this->title;
  }

  /**
   * Gets the quantity.
   *
   * @return string
   *   The quantity.
   */
  public function getQuantity() {
    return $this->quantity;
  }

  /**
   * Gets the unit price.
   *
   * This is the price for a full billing period, and will be prorated on
   * the order item based on the actual billing period ($this->billingPeriod).
   *
   * @return \Drupal\commerce_price\Price
   *   The unit price.
   */
  public function getUnitPrice() {
    return $this->unitPrice;
  }

  /**
   * Gets the billing period.
   *
   * @return \Drupal\commerce_recurring\BillingPeriod
   *   The billing period.
   */
  public function getBillingPeriod() {
    return $this->billingPeriod;
  }

  /**
   * Gets the full billing period.
   *
   * @return \Drupal\commerce_recurring\BillingPeriod
   *   The full billing period.
   */
  public function getFullBillingPeriod() {
    return $this->fullBillingPeriod;
  }

  /**
   * Gets whether the unit price needs to be prorated.
   *
   * @return bool
   *   TRUE if the unit price needs to be prorated, FALSE otherwise.
   */
  public function needsProration() {
    return $this->fullBillingPeriod
      ->getDuration() != $this->billingPeriod
      ->getDuration();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Charge::$billingPeriod protected property The billing period.
Charge::$fullBillingPeriod protected property The full billing period.
Charge::$purchasedEntity protected property The purchased entity, when available.
Charge::$quantity protected property The quantity.
Charge::$title protected property The title.
Charge::$unitPrice protected property The unit price.
Charge::getBillingPeriod public function Gets the billing period.
Charge::getFullBillingPeriod public function Gets the full billing period.
Charge::getPurchasedEntity public function Gets the purchased entity.
Charge::getQuantity public function Gets the quantity.
Charge::getTitle public function Gets the title.
Charge::getUnitPrice public function Gets the unit price.
Charge::needsProration public function Gets whether the unit price needs to be prorated.
Charge::__construct public function Constructs a new Charge object.