You are here

class PaymentFactory in Payment 8.2

Provides a payment factory service.

Hierarchy

Expanded class hierarchy of PaymentFactory

1 file declares its use of PaymentFactory
PaymentFactoryTest.php in modules/payment_reference/tests/src/Unit/PaymentFactoryTest.php
1 string reference to 'PaymentFactory'
payment_reference.services.yml in modules/payment_reference/payment_reference.services.yml
modules/payment_reference/payment_reference.services.yml
1 service uses PaymentFactory
payment_reference.payment_factory in modules/payment_reference/payment_reference.services.yml
Drupal\payment_reference\PaymentFactory

File

modules/payment_reference/src/PaymentFactory.php, line 12

Namespace

Drupal\payment_reference
View source
class PaymentFactory implements PaymentFactoryInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The payment line item manager.
   *
   * @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemManagerInterface
   */
  protected $paymentLineItemManager;

  /**
   * Constructs a new instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemManagerInterface $payment_line_item_manager
   *   The payment line item manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, PaymentLineItemManagerInterface $payment_line_item_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->paymentLineItemManager = $payment_line_item_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function createPayment(FieldDefinitionInterface $field_definition) {

    /** @var \Drupal\payment\Entity\PaymentInterface $payment */
    $payment = $this->entityTypeManager
      ->getStorage('payment')
      ->create([
      'bundle' => 'payment_reference',
    ]);

    /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */
    $payment_type = $payment
      ->getPaymentType();
    $payment_type
      ->setEntityTypeId($field_definition
      ->getFieldStorageDefinition()
      ->getTargetEntityTypeId());
    $payment_type
      ->setBundle($field_definition
      ->getTargetBundle());
    $payment_type
      ->setFieldName($field_definition
      ->getName());
    $payment
      ->setCurrencyCode($field_definition
      ->getSetting('currency_code'));
    foreach ($field_definition
      ->getSetting('line_items_data') as $line_item_data) {
      $line_item = $this->paymentLineItemManager
        ->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']);
      $payment
        ->setLineItem($line_item);
    }
    return $payment;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentFactory::$entityTypeManager protected property The entity type manager.
PaymentFactory::$paymentLineItemManager protected property The payment line item manager.
PaymentFactory::createPayment public function Creates a payment for a field. Overrides PaymentFactoryInterface::createPayment
PaymentFactory::__construct public function Constructs a new instance.