You are here

class PaymentReferenceElement in Payment 8.2

Provides a form for testing the payment_reference element.

Hierarchy

Expanded class hierarchy of PaymentReferenceElement

1 string reference to 'PaymentReferenceElement'
payment_reference_test.routing.yml in modules/payment_reference_test/payment_reference_test.routing.yml
modules/payment_reference_test/payment_reference_test.routing.yml

File

modules/payment_reference_test/src/PaymentReferenceElement.php, line 14

Namespace

Drupal\payment_reference_test
View source
class PaymentReferenceElement implements FormInterface {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'payment_reference_test_payment_reference_element';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $key = 'payment_reference_element_prototype_payment';
    if ($form_state
      ->has($key)) {

      /** @var \Drupal\payment\Entity\PaymentInterface $prototype_payment */
      $prototype_payment = $form_state
        ->get($key);
      $payment_type = $prototype_payment
        ->getPaymentType();
    }
    else {
      $entity_type_id = 'user';
      $bundle = 'user';
      $field_name = 'foobarbaz';

      /** @var \Drupal\payment\Entity\PaymentInterface $prototype_payment */
      $prototype_payment = Payment::create(array(
        'bundle' => 'payment_reference',
      ));
      $prototype_payment
        ->setCurrencyCode('EUR')
        ->setOwnerId(2)
        ->setLineItems(Generate::createPaymentLineItems());

      /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */
      $payment_type = $prototype_payment
        ->getPaymentType();
      $payment_type
        ->setEntityTypeId($entity_type_id);
      $payment_type
        ->setBundle($bundle);
      $payment_type
        ->setFieldName($field_name);
      $form_state
        ->set($key, $prototype_payment);
    }
    $form['payment_reference'] = array(
      '#plugin_selector_id' => 'payment_select_list',
      '#prototype_payment' => $prototype_payment,
      '#queue_category_id' => $payment_type
        ->getEntityTypeId() . '.' . $payment_type
        ->getBundle() . '.' . $payment_type
        ->getFieldName(),
      '#queue_owner_id' => 2,
      '#required' => TRUE,
      '#title' => 'FooBarBaz',
      '#type' => 'payment_reference',
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $payment_id = $form['payment_reference']['#value'];
    $acquisition_code = PaymentReference::queue()
      ->claimPayment($payment_id);
    if ($acquisition_code && PaymentReference::queue()
      ->acquirePayment($payment_id, $acquisition_code)) {
      \Drupal::state()
        ->set('payment_reference_test_payment_reference_element', $payment_id);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentReferenceElement::buildForm public function Form constructor. Overrides FormInterface::buildForm
PaymentReferenceElement::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PaymentReferenceElement::submitForm public function Form submission handler. Overrides FormInterface::submitForm
PaymentReferenceElement::validateForm public function Form validation handler. Overrides FormInterface::validateForm