OrderSubscriber.php in Commerce Recurring Framework 8
File
src/EventSubscriber/OrderSubscriber.php
View source
<?php
namespace Drupal\commerce_recurring\EventSubscriber;
use Drupal\commerce_recurring\RecurringOrderManagerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderSubscriber implements EventSubscriberInterface {
protected $entityTypeManager;
protected $recurringOrderManager;
protected $time;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RecurringOrderManagerInterface $recurring_order_manager, TimeInterface $time) {
$this->entityTypeManager = $entity_type_manager;
$this->recurringOrderManager = $recurring_order_manager;
$this->time = $time;
}
public static function getSubscribedEvents() {
$events['commerce_order.place.pre_transition'] = 'onPlace';
$events['commerce_order.cancel.pre_transition'] = 'onCancel';
return $events;
}
public function onPlace(WorkflowTransitionEvent $event) {
$subscription_storage = $this->entityTypeManager
->getStorage('commerce_subscription');
$order = $event
->getEntity();
if ($order
->bundle() == 'recurring') {
return;
}
$payment_method = $order
->get('payment_method')->entity;
$start_time = $this->time
->getRequestTime();
foreach ($order
->getItems() as $order_item) {
$purchased_entity = $order_item
->getPurchasedEntity();
if (!$purchased_entity || !$purchased_entity
->hasField('subscription_type')) {
continue;
}
$subscription_type_item = $purchased_entity
->get('subscription_type');
$billing_schedule_item = $purchased_entity
->get('billing_schedule');
if ($subscription_type_item
->isEmpty() || $billing_schedule_item
->isEmpty()) {
continue;
}
$billing_schedule = $billing_schedule_item->entity;
if (!$billing_schedule
->getPlugin()
->allowTrials() && empty($payment_method)) {
continue;
}
$subscription = $subscription_storage
->createFromOrderItem($order_item, [
'type' => $subscription_type_item->target_plugin_id,
'billing_schedule' => $billing_schedule,
]);
if (!empty($payment_method)) {
$subscription
->setPaymentMethod($payment_method);
}
if ($billing_schedule
->getPlugin()
->allowTrials()) {
$subscription
->setState('trial');
$subscription
->setTrialStartTime($start_time);
$subscription
->save();
$this->recurringOrderManager
->startTrial($subscription);
}
else {
$subscription
->setState('active');
$subscription
->setStartTime($start_time);
$subscription
->save();
$this->recurringOrderManager
->startRecurring($subscription);
}
}
}
public function onCancel(WorkflowTransitionEvent $event) {
$subscription_storage = $this->entityTypeManager
->getStorage('commerce_subscription');
$order = $event
->getEntity();
if ($order
->bundle() == 'recurring') {
return;
}
$query = $subscription_storage
->getQuery();
$query
->condition('initial_order', $order
->id())
->condition('state', [
'trial',
'active',
], 'IN')
->accessCheck(FALSE);
$result = $query
->execute();
if (!$result) {
return;
}
$subscriptions = $subscription_storage
->loadMultiple($result);
foreach ($subscriptions as $subscription) {
$subscription
->getState()
->applyTransitionById('cancel');
$subscription
->save();
}
}
}