commerce-8.x-2.8/modules/payment_example/src/Plugin/Commerce/PaymentGateway/OffsiteRedirect.php
modules/payment_example/src/Plugin/Commerce/PaymentGateway/OffsiteRedirect.php
<?php
namespace Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides the Off-site Redirect payment gateway.
*
* @CommercePaymentGateway(
* id = "example_offsite_redirect",
* label = "Example (Off-site redirect)",
* display_label = "Example",
* forms = {
* "offsite-payment" = "Drupal\commerce_payment_example\PluginForm\OffsiteRedirect\PaymentOffsiteForm",
* },
* payment_method_types = {"credit_card"},
* credit_card_types = {
* "amex", "dinersclub", "discover", "jcb", "maestro", "mastercard", "visa",
* },
* )
*/
class OffsiteRedirect extends OffsitePaymentGatewayBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'redirect_method' => 'post',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
// A real gateway would always know which redirect method should be used,
// it's made configurable here for test purposes.
$form['redirect_method'] = [
'#type' => 'radios',
'#title' => $this->t('Redirect method'),
'#options' => [
'get' => $this->t('Redirect via GET (302 header)'),
'post' => $this->t('Redirect via POST (automatic)'),
'post_manual' => $this->t('Redirect via POST (manual)'),
],
'#default_value' => $this->configuration['redirect_method'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state->getErrors()) {
$values = $form_state->getValue($form['#parents']);
$this->configuration['redirect_method'] = $values['redirect_method'];
}
}
/**
* {@inheritdoc}
*/
public function onReturn(OrderInterface $order, Request $request) {
// @todo Add examples of request validation.
$payment_storage = $this->entityTypeManager->getStorage('commerce_payment');
$payment = $payment_storage->create([
'state' => 'authorization',
'amount' => $order->getTotalPrice(),
'payment_gateway' => $this->entityId,
'order_id' => $order->id(),
'remote_id' => $request->query->get('txn_id'),
'remote_state' => $request->query->get('payment_status'),
]);
$payment->save();
}
}
