Skip to content
Snippets Groups Projects
Commit ba975777 authored by Primoz Hmeljak's avatar Primoz Hmeljak
Browse files

by Primsi: basic module structure.

parent de0ea166
Branches
Tags
No related merge requests found
name: Commerce Datatrans
type: module
description: 'Provides payment gateway for Datatrans.'
package: Commerce
core: 8.x
dependencies:
- commerce:commerce_payment
commerce_datatrans.dummy_redirect_post:
path: 'commerce_datatrans/dummy_redirect_post'
defaults:
_controller: '\Drupal\commerce_datatrans\Controller\DummyRedirectController::post'
options:
no_cache: TRUE
requirements:
_access: 'TRUE'
commerce_datatrans.dummy_redirect_302:
path: 'commerce_datatrans/dummy_redirect_302'
defaults:
_controller: '\Drupal\commerce_datatrans\Controller\DummyRedirectController::on302'
options:
no_cache: TRUE
requirements:
_access: 'TRUE'
commerce_payment.commerce_payment_gateway.plugin.datatrans:
type: commerce_payment_gateway_configuration
mapping:
redirect_method:
type: string
label: 'Redirect method'
<?php
namespace Drupal\commerce_datatrans\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* This is a dummy controller for mocking an off-site gateway.
*/
class DummyRedirectController implements ContainerInjectionInterface {
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $currentRequest;
/**
* Constructs a new DummyRedirectController object.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(RequestStack $request_stack) {
$this->currentRequest = $request_stack->getCurrentRequest();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('request_stack')
);
}
/**
* Callback method which accepts POST.
*
* @throws \Drupal\commerce\Response\NeedsRedirectException
*/
public function post() {
$cancel = $this->currentRequest->request->get('cancel');
$return = $this->currentRequest->request->get('return');
$total = $this->currentRequest->request->get('total');
if ($total > 20) {
return new TrustedRedirectResponse($return);
}
return new TrustedRedirectResponse($cancel);
}
/**
* Callback method which reacts to GET from a 302 redirect.
*
* @throws \Drupal\commerce\Response\NeedsRedirectException
*/
public function on302() {
$cancel = $this->currentRequest->query->get('cancel');
$return = $this->currentRequest->query->get('return');
$total = $this->currentRequest->query->get('total');
if ($total > 20) {
return new TrustedRedirectResponse($return);
}
return new TrustedRedirectResponse($cancel);
}
}
<?php
namespace Drupal\commerce_datatrans\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 Datatrans payment gateway.
*
* @CommercePaymentGateway(
* id = "datatrans",
* label = "Datatrans",
* display_label = "Datatrans",
* forms = {
* "offsite-payment" = "Drupal\commerce_datatrans\PluginForm\Datatrans\DatatransForm",
* },
* payment_method_types = {"credit_card"},
* credit_card_types = {
* "amex", "dinersclub", "discover", "jcb", "maestro", "mastercard", "visa",
* },
* )
*/
class Datatrans 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'),
],
'#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(),
'test' => $this->getMode() == 'test',
'remote_id' => $request->query->get('txn_id'),
'remote_state' => $request->query->get('payment_status'),
'authorized' => REQUEST_TIME,
]);
$payment->save();
drupal_set_message('Payment was processed');
}
}
<?php
namespace Drupal\commerce_datatrans\PluginForm\OffsiteRedirect;
use Drupal\commerce_payment\PluginForm\PaymentOffsiteForm as BasePaymentOffsiteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class PaymentOffsiteForm extends BasePaymentOffsiteForm {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
/** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
$payment = $this->entity;
/** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface $payment_gateway_plugin */
$payment_gateway_plugin = $payment->getPaymentGateway()->getPlugin();
$redirect_method = $payment_gateway_plugin->getConfiguration()['redirect_method'];
if ($redirect_method == 'post') {
$redirect_url = Url::fromRoute('commerce_datatrans.dummy_redirect_post')->toString();
}
else {
$order = $payment->getOrder();
// Gateways that use the GET redirect method usually perform an API call
// that prepares the remote payment and provides the actual url to
// redirect to. Any params received from that API call that need to be
// persisted until later payment creation can be saved in $order->data.
// Example: $order->setData('my_gateway', ['test' => '123']), followed
// by an $order->save().
$redirect_url = Url::fromRoute('commerce_datatrans.dummy_redirect_302', [], ['absolute' => TRUE])->toString();
}
$data = [
'return' => $form['#return_url'],
'cancel' => $form['#cancel_url'],
'total' => $payment->getAmount()->getNumber(),
];
return $this->buildRedirectForm($form, $form_state, $redirect_url, $data, $redirect_method);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment