SermepaForm.php in Commerce sermepa 8.2
File
src/PluginForm/OffsiteRedirect/SermepaForm.php
View source
<?php
namespace Drupal\commerce_sermepa\PluginForm\OffsiteRedirect;
use CommerceRedsys\Payment\Sermepa as SermepaApi;
use Drupal\commerce\Response\NeedsRedirectException;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\PluginForm\PaymentOffsiteForm as BasePaymentOffsiteForm;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SermepaForm extends BasePaymentOffsiteForm implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $messenger;
protected $time;
public function __construct(MessengerInterface $messenger, TimeInterface $time, EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->messenger = $messenger;
$this->time = $time;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'), $container
->get('datetime.time'), $container
->get('entity_type.manager'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$payment = $this->entity;
$order = $payment
->getOrder();
$payment_gateway_plugin = $payment
->getPaymentGateway()
->getPlugin();
$gateway_settings = $payment_gateway_plugin
->getConfiguration();
$merchant_consumer_language = $payment_gateway_plugin
->getUnknowFallbackLanguage();
try {
$gateway = new SermepaApi($gateway_settings['merchant_name'], $gateway_settings['merchant_code'], $gateway_settings['merchant_terminal'], $gateway_settings['merchant_password'], $payment_gateway_plugin
->getMode());
$date = DrupalDateTime::createFromTimestamp($this->time
->getRequestTime());
$parameters = FALSE;
$currency_code = $payment
->getAmount()
->getCurrencyCode();
$currency = $this->entityTypeManager
->getStorage('commerce_currency')
->load($currency_code);
if ($currency
->getNumericCode() == $gateway_settings['currency']) {
$amount = $payment
->getAmount()
->multiply(100)
->getNumber();
$gateway
->setAmount($amount)
->setCurrency($gateway_settings['currency'])
->setOrder(substr($date
->format('ymdHis') . 'Id' . $order
->id(), -12, 12))
->setMerchantMerchantGroup($gateway_settings['merchant_group'])
->setPaymentMethod($gateway_settings['merchant_paymethods'])
->setConsumerLanguage($merchant_consumer_language)
->setMerchantData($order
->id())
->setTransactionType($gateway_settings['transaction_type'])
->setMerchantURL($payment_gateway_plugin
->getNotifyUrl()
->toString())
->setUrlKO($form['#cancel_url'])
->setUrlOK($form['#return_url']);
$parameters = $gateway
->composeMerchantParameters();
}
} catch (\Exception $exception) {
watchdog_exception('commerce_sermepa', $exception);
}
if (empty($parameters)) {
$this->messenger
->addError($this
->t('An error has been occurred trying of process the payment data, please contact with us.'));
return $this
->redirectToPaymentInformationPane($order);
}
$data = [
'Ds_SignatureVersion' => $gateway
->getSignatureVersion(),
'Ds_MerchantParameters' => $parameters,
'Ds_Signature' => $gateway
->composeMerchantSignature(),
];
return $this
->buildRedirectForm($form, $form_state, $gateway
->getEnvironment(), $data, BasePaymentOffsiteForm::REDIRECT_POST);
}
protected function redirectToPaymentInformationPane(OrderInterface $order) {
try {
$checkout_flow = $order
->get('checkout_flow')
->first()
->get('entity')
->getTarget()
->getValue()
->getPlugin();
$step_id = $checkout_flow
->getPane('payment_information')
->getStepId();
if ($step_id == '_disabled') {
throw new \RuntimeException('Cannot get the step ID for the payment_information pane. The pane is disabled.');
}
$checkout_flow
->redirectToStep($step_id);
} catch (\Exception $exception) {
$redirect_url = Url::fromRoute('<front>', [
'absolute' => TRUE,
])
->toString();
throw new NeedsRedirectException($redirect_url);
}
}
}
Classes
Name |
Description |
SermepaForm |
Provides the Sermepa/Redsýs class for the payment form. |