/**
  * {@inheritdoc}
  */
 public function createPayment(FieldDefinitionInterface $field_definition)
 {
     /** @var \Drupal\payment\Entity\PaymentInterface $payment */
     $payment = $this->entityManager->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;
 }
 /**
  * Implements form #submit callback.
  */
 public function addMoreSubmit(array &$form, FormStateInterface $form_state)
 {
     $triggering_element = $form_state->getTriggeringElement();
     $parents = array_slice($triggering_element['#array_parents'], 0, -2);
     $root_element = NestedArray::getValue($form, $parents);
     $values = $form_state->getValues();
     $values = NestedArray::getValue($values, array_slice($triggering_element['#parents'], 0, -2));
     $line_item = $this->paymentLineItemManager->createInstance($values['add_more']['type']);
     $line_item->setName(static::createLineItemName($root_element, $form_state, $values['add_more']['type']));
     $line_items = static::getLineItems($root_element, $form_state);
     $line_items[] = $line_item;
     static::setLineItems($root_element, $form_state, $line_items);
     $form_state->setRebuild();
 }
 /**
  * Tests getAmount().
  */
 protected function testGetAmount()
 {
     $amount = 3;
     $quantity = 2;
     for ($i = 0; $i < 2; $i++) {
         /** @var \Drupal\payment\Plugin\Payment\LineItem\Basic $line_item */
         $line_item = $this->lineItemManager->createInstance('payment_basic');
         $name = $this->randomMachineName();
         $line_item->setName($name);
         $line_item->setAmount($amount);
         $line_item->setQuantity($quantity);
         $this->payment->setLineItem($line_item);
     }
     $this->assertEqual($this->payment->getAmount(), 12);
 }
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $entity_type_id = $items->getEntity()->getEntityTypeId();
     $bundle = $items->getEntity()->bundle();
     $field_name = $this->fieldDefinition->getName();
     /** @var \Drupal\payment\Entity\PaymentInterface $payment */
     $payment = $this->paymentStorage->create(['bundle' => 'payment_form']);
     $payment->setCurrencyCode($this->fieldDefinition->getSetting('currency_code'));
     /** @var \Drupal\payment_form\Plugin\Payment\Type\PaymentForm $payment_type */
     $payment_type = $payment->getPaymentType();
     $payment_type->setDestinationUrl($this->requestStack->getCurrentRequest()->getUri());
     $payment_type->setEntityTypeId($entity_type_id);
     $payment_type->setBundle($bundle);
     $payment_type->setFieldName($field_name);
     foreach ($items as $item) {
         /** @var \Drupal\payment_form\Plugin\Field\FieldType\PaymentForm $item */
         $plugin_id = $item->get('plugin_id')->getValue();
         if ($plugin_id) {
             $payment->setLineItem($this->paymentLineItemManager->createInstance($plugin_id, $item->get('plugin_configuration')->getValue()));
         }
     }
     return $this->entityFormBuilder->getForm($payment, 'payment_form');
 }