PaymentOffsiteForm.php in Commerce Core 8.2
File
modules/payment_example/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php
View source
<?php
namespace Drupal\commerce_payment_example\PluginForm\OffsiteRedirect;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\PluginForm\PaymentOffsiteForm as BasePaymentOffsiteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class PaymentOffsiteForm extends BasePaymentOffsiteForm {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$payment = $this->entity;
$payment_gateway_plugin = $payment
->getPaymentGateway()
->getPlugin();
$redirect_method = $payment_gateway_plugin
->getConfiguration()['redirect_method'];
$remove_js = $redirect_method == 'post_manual';
if (in_array($redirect_method, [
'post',
'post_manual',
])) {
$redirect_url = Url::fromRoute('commerce_payment_example.dummy_redirect_post')
->toString();
$redirect_method = 'post';
}
else {
$order = $payment
->getOrder();
if ($order
->getBillingProfile()
->get('address')->family_name == 'FAIL') {
throw new PaymentGatewayException('Could not get the redirect URL.');
}
$redirect_url = Url::fromRoute('commerce_payment_example.dummy_redirect_302', [], [
'absolute' => TRUE,
])
->toString();
}
$data = [
'return' => $form['#return_url'],
'cancel' => $form['#cancel_url'],
'total' => $payment
->getAmount()
->getNumber(),
];
$form = $this
->buildRedirectForm($form, $form_state, $redirect_url, $data, $redirect_method);
if ($remove_js) {
unset($form['#attached']['library']);
}
return $form;
}
}