You are here

public function RecurringOrderManager::refreshOrder in Commerce Recurring Framework 8

Refreshes the given recurring order.

Each subscription's order items will be rebuilt based on the most recent charges.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The recurring order.

Overrides RecurringOrderManagerInterface::refreshOrder

File

src/RecurringOrderManager.php, line 101

Class

RecurringOrderManager
Provides the default recurring order manager.

Namespace

Drupal\commerce_recurring

Code

public function refreshOrder(OrderInterface $order) {

  /** @var \Drupal\commerce_recurring\Plugin\Field\FieldType\BillingPeriodItem $billing_period_item */
  $billing_period_item = $order
    ->get('billing_period')
    ->first();
  $billing_period = $billing_period_item
    ->toBillingPeriod();
  $subscriptions = $this
    ->collectSubscriptions($order);
  $payment_method = $this
    ->selectPaymentMethod($subscriptions);
  $billing_profile = $payment_method ? $payment_method
    ->getBillingProfile() : NULL;
  $payment_gateway_id = $payment_method ? $payment_method
    ->getPaymentGatewayId() : NULL;
  $order
    ->set('billing_profile', $billing_profile);
  $order
    ->set('payment_method', $payment_method);
  $order
    ->set('payment_gateway', $payment_gateway_id);
  foreach ($subscriptions as $subscription) {
    $this
      ->applyCharges($order, $subscription, $billing_period);
  }
  $order_items = $order
    ->getItems();

  // OrderRefresh skips empty orders, an order without items can't stay open.
  if (!$order_items) {
    $order
      ->set('state', 'canceled');
  }

  // The same workaround that \Drupal\commerce_order\OrderRefresh does.
  foreach ($order_items as $order_item) {
    if ($order_item
      ->isNew()) {
      $order_item->order_id->entity = $order;
    }
  }
}