Skip to content
Snippets Groups Projects
Commit 7fa84e81 authored by dscutaru's avatar dscutaru Committed by Andrei Vesterli
Browse files

Issue #3352115 by dscutaru, Ranjit1032002: Add support for D10

parent 4f05dc98
No related branches found
No related tags found
2 merge requests!7Draft: Aux/3459193,!51.x
name: Commerce MAIB
type: module
description: Provides Commerce integration for the MAIB Gateway.
core_version_requirement: ^8 || ^9 || ^10
core_version_requirement: ^8.8 || ^9 || ^10
package: Commerce (contrib)
dependencies:
- commerce:commerce_payment
......@@ -78,6 +78,7 @@ function commerce_maib_cron() {
$payment_ids = $payment_storage_query
->condition($state_group)
->condition('payment_gateway', $maib_payment_gateway_ids, 'IN')
->accessCheck(FALSE)
->execute();
// Check for stalled payment transactions and try to update theirs status.
$queue = \Drupal::queue('commerce_maib_queue');
......
......@@ -101,7 +101,7 @@ class PaymentCheckoutController implements ContainerInjectionInterface {
* @param \Drupal\commerce_cart\CartSessionInterface $cart_session
* Commerce session object.
*/
public function __construct(
final public function __construct(
CheckoutOrderManagerInterface $checkout_order_manager,
MessengerInterface $messenger,
LoggerChannelFactoryInterface $logger_factory,
......
......@@ -21,6 +21,15 @@ use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Symfony\Component\HttpFoundation\Request;
use Maib\MaibApi\MaibClient;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\commerce_payment\PaymentTypeManager;
use Drupal\commerce_payment\PaymentMethodTypeManager;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\commerce_price\MinorUnitsConverterInterface;
/**
* Provides the Off-site Redirect payment gateway.
......@@ -41,6 +50,79 @@ use Maib\MaibApi\MaibClient;
*/
class OffsiteRedirect extends OffsitePaymentGatewayBase implements SupportsAuthorizationsInterface, SupportsRefundsInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected LanguageManagerInterface $languageManager;
/**
* The commerce maib logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected LoggerChannelInterface $logger;
/**
* Constructs a new OffsiteRedirect object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\commerce_payment\PaymentTypeManager $payment_type_manager
* The payment type manager.
* @param \Drupal\commerce_payment\PaymentMethodTypeManager $payment_method_type_manager
* The payment method type manager.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
* @param \Drupal\commerce_price\MinorUnitsConverterInterface $minor_units_converter
* The minor units converter.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory object.
*/
final public function __construct(
array $configuration,
string $plugin_id,
mixed $plugin_definition,
EntityTypeManagerInterface $entity_type_manager,
PaymentTypeManager $payment_type_manager,
PaymentMethodTypeManager $payment_method_type_manager,
TimeInterface $time,
MinorUnitsConverterInterface $minor_units_converter = NULL,
LanguageManagerInterface $language_manager,
LoggerChannelFactoryInterface $logger_factory
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time, $minor_units_converter);
$this->languageManager = $language_manager;
$this->logger = $logger_factory->get('commerce_maib');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, mixed $plugin_id, mixed $plugin_definition): static {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('plugin.manager.commerce_payment_type'),
$container->get('plugin.manager.commerce_payment_method_type'),
$container->get('datetime.time'),
$container->get('commerce_price.minor_units_converter'),
$container->get('language_manager'),
$container->get('logger.factory')
);
}
/**
* {@inheritdoc}
*/
......@@ -237,7 +319,7 @@ class OffsiteRedirect extends OffsitePaymentGatewayBase implements SupportsAutho
if ($intent == 'authorize') {
$payment->setState('authorization')->setRemoteState($payment_info[MAIBGateway::MAIB_RESULT])->save();
$this->messenger()->addMessage($this->t('Your transaction was successful.'));
\Drupal::logger('commerce_maib')
$this->logger
->notice('Completed authorization payment @payment with transaction id @trans_id for order @order. @data',
[
'@trans_id' => $transactionId,
......@@ -249,7 +331,7 @@ class OffsiteRedirect extends OffsitePaymentGatewayBase implements SupportsAutho
else {
$payment->setState('completed')->setRemoteState($payment_info[MAIBGateway::MAIB_RESULT])->save();
$this->messenger()->addMessage($this->t('Your transaction was successful.'));
\Drupal::logger('commerce_maib')
$this->logger
->notice('Completed payment @payment with transaction id @trans_id for order @order. @data',
[
'@trans_id' => $transactionId,
......@@ -263,7 +345,8 @@ class OffsiteRedirect extends OffsitePaymentGatewayBase implements SupportsAutho
$this->messenger()->addError($this->t('Your transaction was cancelled. Remote status: @status', [
'@status' => $payment_info[MAIBGateway::MAIB_RESULT],
]));
\Drupal::logger('commerce_maib')->error(
$this->logger
->error(
'Voided payment @payment with transaction id @trans_id for order @order. Remote status was @remote. @data',
[
'@trans_id' => $transactionId,
......@@ -399,12 +482,16 @@ class OffsiteRedirect extends OffsitePaymentGatewayBase implements SupportsAutho
$currency = $payment->getAmount()->getCurrencyCode();
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currencyObj*/
$currencyObj = \Drupal::entityTypeManager()->getStorage('commerce_currency')->load($currency);
$currencyObj = $this->entityTypeManager->getStorage('commerce_currency')
->load($currency);
$clientIpAddr = $payment->getOrder()->getIpAddress();
$description = (string) $this->t('Order #@id', ['@id' => $payment->getOrderId()]);
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$language = $this->languageManager
->getCurrentLanguage()
->getId();
$result = $this->getClient()->makeDMSTrans($transaction_id, $decimal_amount, $currencyObj->getNumericCode(), $clientIpAddr, $description, $language);
$result = $this->getClient()
->makeDMSTrans($transaction_id, $decimal_amount, $currencyObj->getNumericCode(), $clientIpAddr, $description, $language);
}
catch (\Exception $e) {
throw new MAIBException(sprintf('MAIB error: %s', $e->getMessage()));
......@@ -415,7 +502,7 @@ class OffsiteRedirect extends OffsitePaymentGatewayBase implements SupportsAutho
$payment->setRemoteState($result[MAIBGateway::MAIB_RESULT]);
$payment->setAmount($amount);
$payment->save();
\Drupal::logger('commerce_maib')
$this->logger
->notice('Completed authorized payment @payment with transaction id @trans_id for order @order and amount @amount @curr',
[
'@trans_id' => $payment->getRemoteId(),
......@@ -448,7 +535,7 @@ class OffsiteRedirect extends OffsitePaymentGatewayBase implements SupportsAutho
throw new MAIBException(sprintf('MAIB error: %s', $result['error']));
}
if ($result[MAIBGateway::MAIB_RESULT] == MAIBGateway::MAIB_RESULT_OK) {
\Drupal::logger('commerce_maib')
$this->logger
->notice('Voided payment @payment with transaction id @trans_id for order @order',
[
'@trans_id' => $transactionId,
......@@ -493,7 +580,7 @@ class OffsiteRedirect extends OffsitePaymentGatewayBase implements SupportsAutho
$payment->setRefundedAmount($amount);
$payment->save();
\Drupal::logger('commerce_maib')
$this->logger
->notice('Refunded payment @payment with transaction id @trans_id for order @order. Data: @data.',
[
'@trans_id' => $payment->getRemoteId(),
......
......@@ -51,7 +51,7 @@ class PaymentWorker extends QueueWorkerBase implements ContainerFactoryPluginInt
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerChannelFactory
* The logger channel.
*/
public function __construct(
final public function __construct(
array $configuration,
string $plugin_id,
mixed $plugin_definition,
......
......@@ -6,6 +6,12 @@ use Drupal\commerce_maib\Exception\MAIBException;
use Drupal\commerce_payment\PluginForm\PaymentOffsiteForm as BasePaymentOffsiteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_maib\MAIBGateway;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
/**
* Class PaymentOffsiteForm for Maib.
......@@ -15,7 +21,59 @@ use Drupal\commerce_maib\MAIBGateway;
* @return array $form
* The base payment offsite form for maib.
*/
class PaymentOffsiteForm extends BasePaymentOffsiteForm {
class PaymentOffsiteForm extends BasePaymentOffsiteForm implements ContainerInjectionInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected LanguageManagerInterface $languageManager;
/**
* The commerce maib logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected LoggerChannelInterface $logger;
/**
* Constructs a new PaymentOffsiteForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager object.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory object.
*/
final public function __construct(
EntityTypeManagerInterface $entity_type_manager,
LanguageManagerInterface $language_manager,
LoggerChannelFactoryInterface $logger_factory
) {
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
$this->logger = $logger_factory->get('commerce_maib');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): static {
return new static(
$container->get('entity_type.manager'),
$container->get('language_manager'),
$container->get('logger.factory')
);
}
/**
* {@inheritdoc}
......@@ -39,12 +97,15 @@ class PaymentOffsiteForm extends BasePaymentOffsiteForm {
$capture = !empty($form['#capture']);
$currency = $payment->getAmount()->getCurrencyCode();
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currencyObj */
$currencyObj = \Drupal::entityTypeManager()->getStorage('commerce_currency')->load($currency);
$currencyObj = $this->entityTypeManager->getStorage('commerce_currency')
->load($currency);
$amount = $payment->getAmount()->getNumber();
$client_ip_addr = $payment->getOrder()->getIpAddress();
$description = (string) $this->t('Order #@id', ['@id' => $payment->getOrderId()]);
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$language = $this->languageManager
->getCurrentLanguage()
->getId();
$transaction_id = NULL;
try {
......@@ -70,11 +131,11 @@ class PaymentOffsiteForm extends BasePaymentOffsiteForm {
$pending_payment = $payment_gateway_plugin->storePendingPayment($payment->getOrder(), $transaction_id);
}
catch (\Exception $e) {
\Drupal::logger('commerce_maib')->error($e->getMessage());
$this->logger->error($e->getMessage());
throw new MAIBException(sprintf('MAIB error: %s', $e->getMessage()));
}
\Drupal::logger('commerce_maib')->notice($this->t(
$this->logger->notice($this->t(
'Got transaction id @trans_id for order @order and payment @payment',
[
'@trans_id' => $transaction_id,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment