commerce_gc_client-8.x-1.9/src/EventSubscriber/GoCardlessEventSubscriber.php

src/EventSubscriber/GoCardlessEventSubscriber.php
<?php

namespace Drupal\commerce_gc_client\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\commerce_gc_client\Event\GoCardlessEvents;
use Drupal\commerce_gc_client\Event\PaymentCreatedEvent;
use Drupal\commerce_gc_client\Event\PaymentDetailsEvent;
use Drupal\commerce_order\Entity\OrderItem;

/**
 * Class EntityTypeSubscriber.
 *
 * @package Drupal\commerce_gc_client\EventSubscriber
 */
class GoCardlessEventSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   *
   * @return array
   *   The event names to listen for, and the methods that should be executed.
   */
  public static function getSubscribedEvents() {
    return [
      GoCardlessEvents::PAYMENT_CREATED => 'paymentCreated',
      GoCardlessEvents::PAYMENT_DETAILS => 'paymentDetails',
    ];
  }

  /**
   * Responds to Payment Details events.
   *
   * @param \Drupal\commerce_gc_client\Event\PaymentDetailsEvent $event
   *   The Payment Details event.
   */
  public function paymentDetails(PaymentDetailsEvent $event) {

    // If a final payment creation time is set and it is less than the
    // next_payment creation time, then empty the payment details array
    // to stop a payment from being created, and set the next_payment
    // date to NULL.
    $item_id = $event->getItemId();
    $item = OrderItem::load($item_id);
    $data = $item->getData('gc');
    if (array_key_exists('gc_end_type', $data)) {
      if ($data['gc_end_type'] == 'fixed' || $data['gc_end_type'] == 'relative') {
        $db = \Drupal::service('database');
        $next_payment = $db->select('commerce_gc_client_item', 'i')
          ->fields('i', ['next_payment'])
          ->condition('item_id', $item_id)
          ->execute()->fetchField();

        if ($next_payment > $data['gc_end_time']) {
          $event->setPaymentDetails([]);

          $db->update('commerce_gc_client_item')
          ->fields([
            'next_payment' => NULL,
          ])
          ->condition('item_id', $item_id)
          ->execute();
        }
      }
    }
  }

  /**
   * Responds to Payment Created events.
   *
   * @param \Drupal\commerce_gc_client\Event\PaymentCreatedEvent $event
   *   The Payment Created event.
   */
  public function paymentCreated(PaymentCreatedEvent $event) {
    
    // If a payment count is set then update the count, and if there are non
    // remaining set the next_payment creation date to NULL.
    if ($event->getContext() == 'manual') {
      return;
    }
    $item = OrderItem::load($event->getItemId());
    $data = $item->getData('gc');
    if (array_key_exists('gc_end_type', $data)) {
      if ($data['gc_end_type'] == 'count') {
        $remaining = $data['gc_count'];
        if (array_key_exists('gc_count_remaining', $data)) {
          $remaining = $data['gc_count_remaining'];
        }
        if ($remaining) {
          $data['gc_count_remaining'] = $remaining -1;
          $item->setData('gc', $data)->save();  
          if (!$data['gc_count_remaining']) {
            $db = \Drupal::service('database');
            $db->update('commerce_gc_client_item')
              ->fields([
                'next_payment' => NULL,
              ])
              ->condition('item_id', $item->id())
              ->execute();
          }
        }
      }
    }
  }
  
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc