PaymentReferenceElement.php in Payment 8.2
File
modules/payment_reference_test/src/PaymentReferenceElement.php
View source
<?php
namespace Drupal\payment_reference_test;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\payment\Entity\Payment;
use Drupal\payment\Tests\Generate;
use Drupal\payment_reference\PaymentReference;
class PaymentReferenceElement implements FormInterface {
public function getFormId() {
return 'payment_reference_test_payment_reference_element';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$key = 'payment_reference_element_prototype_payment';
if ($form_state
->has($key)) {
$prototype_payment = $form_state
->get($key);
$payment_type = $prototype_payment
->getPaymentType();
}
else {
$entity_type_id = 'user';
$bundle = 'user';
$field_name = 'foobarbaz';
$prototype_payment = Payment::create(array(
'bundle' => 'payment_reference',
));
$prototype_payment
->setCurrencyCode('EUR')
->setOwnerId(2)
->setLineItems(Generate::createPaymentLineItems());
$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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
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);
}
}
}