From becaa3f5fc7fdb1ebe85e1c5589381f9bc26224e Mon Sep 17 00:00:00 2001
From: andrii-onufriichuk
<117644505+andrii-onufriichuk@users.noreply.github.com>
Date: Wed, 6 Dec 2023 14:52:38 +0200
Subject: [PATCH 1/3] Feature/capture later flow (#62)
* Add support for separate auth and capture card flow
---
Controller/Redirect/In.php | 5 ++-
Gateway/Command/CreatePayment.php | 4 +++
Gateway/Method.php | 16 +++++++++
Model/ProcessOrder/Processing.php | 35 +++++++++++++++++--
.../AddOrderHistoryCommentObserver.php | 2 +-
Plugin/LoadMethodInstances.php | 30 +++++++---------
Traits/LoadMethods.php | 1 +
composer.json | 2 +-
etc/di.xml | 2 ++
.../web/js/action/place-order-mixin.js | 17 +++++++++
view/frontend/web/js/view/payment/messages.js | 3 +-
11 files changed, 93 insertions(+), 24 deletions(-)
create mode 100644 view/frontend/web/js/action/place-order-mixin.js
diff --git a/Controller/Redirect/In.php b/Controller/Redirect/In.php
index 19cd751a..30ab3899 100644
--- a/Controller/Redirect/In.php
+++ b/Controller/Redirect/In.php
@@ -14,6 +14,7 @@
use Psr\Log\LoggerInterface;
use Rvvup\Payments\Api\Data\ProcessOrderResultInterface;
use Rvvup\Payments\Api\Data\SessionMessageInterface;
+use Rvvup\Payments\Gateway\Method;
use Rvvup\Payments\Model\Payment\PaymentDataGetInterface;
use Rvvup\Payments\Model\ProcessOrder\ProcessorPool;
use Rvvup\Payments\Service\Order;
@@ -166,7 +167,9 @@ public function execute()
}
if ($rvvupData['status'] != $rvvupData['payments'][0]['status']) {
- $this->processorPool->getProcessor($rvvupData['status'])->execute($order, $rvvupData);
+ if ($rvvupData['payments'][0]['status'] !== Method::STATUS_AUTHORIZED) {
+ $this->processorPool->getProcessor($rvvupData['status'])->execute($order, $rvvupData);
+ }
}
$result = $this->processorPool->getProcessor($rvvupData['payments'][0]['status'])
diff --git a/Gateway/Command/CreatePayment.php b/Gateway/Command/CreatePayment.php
index 62a18380..048acc12 100644
--- a/Gateway/Command/CreatePayment.php
+++ b/Gateway/Command/CreatePayment.php
@@ -53,6 +53,10 @@ public function execute(array $commandSubject)
]
];
+ if ($captureType = $payment->getMethodInstance()->getCaptureType()) {
+ $data['input']['captureType'] = $captureType;
+ }
+
return $this->sdkProxy->createPayment(
$data
);
diff --git a/Gateway/Method.php b/Gateway/Method.php
index a5b9c8bd..eab45c67 100644
--- a/Gateway/Method.php
+++ b/Gateway/Method.php
@@ -45,6 +45,8 @@ class Method extends Adapter
public const STATUS_EXPIRED = 'EXPIRED';
public const STATUS_PENDING = 'PENDING';
public const STATUS_REQUIRES_ACTION = 'REQUIRES_ACTION';
+ public const STATUS_AUTHORIZED = "AUTHORIZED";
+ public const STATUS_AUTHORIZATION_EXPIRED = "AUTHORIZATION_EXPIRED";
public const STATUS_SUCCEEDED = 'SUCCEEDED';
/**
@@ -57,6 +59,9 @@ class Method extends Adapter
/** @var string */
private $title;
+
+ /** @var string */
+ private $captureType;
/** @var array */
private $limits;
/** @var StoreManagerInterface */
@@ -77,6 +82,7 @@ class Method extends Adapter
* @param string $infoBlockType
* @param StoreManagerInterface $storeManager
* @param LoggerInterface|RvvupLog $logger // Set via di.xml
+ * @param string $captureType
* @param CommandPoolInterface|null $commandPool
* @param ValidatorPoolInterface|null $validatorPool
* @param CommandManagerInterface|null $commandExecutor
@@ -92,6 +98,7 @@ public function __construct(
string $infoBlockType,
StoreManagerInterface $storeManager,
LoggerInterface $logger,
+ string $captureType = '',
CommandPoolInterface $commandPool = null,
ValidatorPoolInterface $validatorPool = null,
CommandManagerInterface $commandExecutor = null,
@@ -112,6 +119,7 @@ public function __construct(
$this->title = $title;
$this->limits = $limits;
+ $this->captureType = $captureType;
$this->storeManager = $storeManager;
$this->logger = $logger;
}
@@ -171,4 +179,12 @@ public function getMaxOrderTotal(string $currencyCode): ?string
{
return $this->limits[$currencyCode]['max'] ?? null;
}
+
+ /**
+ * @return string
+ */
+ public function getCaptureType(): string
+ {
+ return $this->captureType;
+ }
}
diff --git a/Model/ProcessOrder/Processing.php b/Model/ProcessOrder/Processing.php
index 564cfdd0..5391ddfc 100644
--- a/Model/ProcessOrder/Processing.php
+++ b/Model/ProcessOrder/Processing.php
@@ -13,6 +13,7 @@
use Rvvup\Payments\Controller\Redirect\In;
use Rvvup\Payments\Exception\PaymentValidationException;
use Rvvup\Payments\Gateway\Method;
+use Magento\Framework\Stdlib\DateTime\DateTime;
class Processing implements ProcessorInterface
{
@@ -27,23 +28,28 @@ class Processing implements ProcessorInterface
/** @var LoggerInterface|RvvupLog */
private $logger;
+ /** @var DateTime */
+ private $dateTime;
+
/**
* @param EventManager $eventManager
* @param OrderRepositoryInterface $orderRepository
* @param ProcessOrderResultInterfaceFactory $processOrderResultFactory
* @param LoggerInterface $logger
- * @return void
+ * @param DateTime $dateTime
*/
public function __construct(
EventManager $eventManager,
OrderRepositoryInterface $orderRepository,
ProcessOrderResultInterfaceFactory $processOrderResultFactory,
- LoggerInterface $logger
+ LoggerInterface $logger,
+ DateTime $dateTime
) {
$this->eventManager = $eventManager;
$this->orderRepository = $orderRepository;
$this->processOrderResultFactory = $processOrderResultFactory;
$this->logger = $logger;
+ $this->dateTime = $dateTime;
}
/**
@@ -71,10 +77,26 @@ public function execute(OrderInterface $order, array $rvvupData): ProcessOrderRe
$order = $this->changeNewOrderStatus($order);
+ $eventMessage = 'Rvvup Payment is being processed.';
+
+ $payment = $rvvupData['payments'][0];
+ if ($this->inAuthorizedManualPayment($payment)) {
+ $formattedExpirationDate = $this->dateTime->date(
+ "jS M Y H:i:s T",
+ strtotime($payment["authorizationExpiresAt"])
+ );
+
+ $eventMessage = "Payment authorization expires at " .
+ $formattedExpirationDate .
+ ". Please navigate to the Rvvup dashboard to manually capture the payment. " .
+ "When the authorization expires, the order will be cancelled " .
+ "and the funds will be returned to the customer.";
+ }
+
$this->eventManager->dispatch('rvvup_payments_process_order_processing_after', [
'payment_process_type' => self::TYPE,
'payment_process_result' => true,
- 'event_message' => 'Rvvup Payment is being processed.',
+ 'event_message' => $eventMessage,
'order_id' => $order->getEntityId(),
'rvvup_id' => $rvvupData['id'] ?? null,
'original_order_state' => $originalOrderState,
@@ -124,4 +146,11 @@ private function changeNewOrderStatus(OrderInterface $order): OrderInterface
return $this->orderRepository->save($order);
}
+
+ private function inAuthorizedManualPayment(array $payment): bool
+ {
+ return isset($payment["authorizationExpiresAt"]) &&
+ $payment["captureType"] === "MANUAL" &&
+ $payment['status'] == Method::STATUS_AUTHORIZED;
+ }
}
diff --git a/Observer/Model/ProcessOrder/AddOrderHistoryCommentObserver.php b/Observer/Model/ProcessOrder/AddOrderHistoryCommentObserver.php
index 322b6b2b..d92fb778 100644
--- a/Observer/Model/ProcessOrder/AddOrderHistoryCommentObserver.php
+++ b/Observer/Model/ProcessOrder/AddOrderHistoryCommentObserver.php
@@ -98,7 +98,7 @@ private function addEventMessageComment(Observer $observer, OrderInterface $orde
// Prepare comment.
$comment = is_string($eventMessage) ? trim($eventMessage) : 'Rvvup Order Process was performed.';
- $comment .= $rvvupId !== null ? ' Rvvup Payment ID: ' . $rvvupId : ' Rvvup Payment ID: N/A';
+ $comment .= $rvvupId !== null ? ' Rvvup Order ID: ' . $rvvupId : ' Rvvup Payment ID: N/A';
$orderStatusHistory = $this->createNewOrderStatusHistoryObject($order);
$orderStatusHistory->setComment($comment);
diff --git a/Plugin/LoadMethodInstances.php b/Plugin/LoadMethodInstances.php
index 2ba66d39..e3848177 100644
--- a/Plugin/LoadMethodInstances.php
+++ b/Plugin/LoadMethodInstances.php
@@ -67,24 +67,20 @@ public function aroundGetMethodInstance(\Magento\Payment\Helper\Data $subject, c
if (isset($this->processed[$code])) {
$method = $this->processed[$code];
- } else {
- $method = [
- 'code' => $code
- ];
+ /** @var Method $instance */
+ $instance = $this->methodFactory->create(
+ 'RvvupFacade',
+ [
+ 'code' => $code,
+ 'title' => $method['title'] ?? 'Rvvup',
+ 'summary_url' => $method['summaryUrl'] ?? '',
+ 'logo_url' => $method['logoUrl'] ?? '',
+ 'limits' => $method['limits'] ?? null,
+ 'captureType' => $method['captureType'] ?? '',
+ ]
+ );
+ return $instance;
}
-
- /** @var Method $instance */
- $instance = $this->methodFactory->create(
- 'RvvupFacade',
- [
- 'code' => $code,
- 'title' => $method['title'] ?? 'Rvvup',
- 'summary_url' => $method['summaryUrl'] ?? '',
- 'logo_url' => $method['logoUrl'] ?? '',
- 'limits' => $method['limits'] ?? null,
- ]
- );
- return $instance;
}
return $proceed($code);
}
diff --git a/Traits/LoadMethods.php b/Traits/LoadMethods.php
index 202e8f1b..73ee9227 100644
--- a/Traits/LoadMethods.php
+++ b/Traits/LoadMethods.php
@@ -23,6 +23,7 @@ protected function processMethods(array $methods): array
$processed[$code]['summaryUrl'] = $method['summaryUrl'];
$processed[$code]['logoUrl'] = $method['logoUrl'] ?? '';
$processed[$code]['limits'] = $this->processLimits($method['limits']['total'] ?? []);
+ $processed[$code]['captureType'] = $method['captureType'] ?? '';
}
$this->processed = $processed;
}
diff --git a/composer.json b/composer.json
index 6904bc44..ff9ba2ee 100644
--- a/composer.json
+++ b/composer.json
@@ -11,7 +11,7 @@
"guzzlehttp/guzzle": ">=6",
"magento/module-catalog": "^103.0 || ^104.0",
"magento/module-grouped-product": ">=100.1",
- "rvvup/sdk": "0.13.6",
+ "rvvup/sdk": "0.13.7",
"ext-json": "*",
"php": "^7.3 || ^8.0"
},
diff --git a/etc/di.xml b/etc/di.xml
index 96bfef3f..e4760288 100644
--- a/etc/di.xml
+++ b/etc/di.xml
@@ -283,8 +283,10 @@
- Rvvup\Payments\Model\ProcessOrder\Cancel
- Rvvup\Payments\Model\ProcessOrder\Cancel
- Rvvup\Payments\Model\ProcessOrder\Cancel
+ - Rvvup\Payments\Model\ProcessOrder\Cancel
- Rvvup\Payments\Model\ProcessOrder\Processing
- Rvvup\Payments\Model\ProcessOrder\Processing
+ - Rvvup\Payments\Model\ProcessOrder\Processing
- Rvvup\Payments\Model\ProcessOrder\Complete
- Rvvup\Payments\Model\ProcessOrder\UpdateOrder
diff --git a/view/frontend/web/js/action/place-order-mixin.js b/view/frontend/web/js/action/place-order-mixin.js
new file mode 100644
index 00000000..b9d653c0
--- /dev/null
+++ b/view/frontend/web/js/action/place-order-mixin.js
@@ -0,0 +1,17 @@
+define([
+ 'mage/utils/wrapper',
+ 'jquery',
+], function (wrapper, $) {
+ 'use strict';
+
+ return function (placeOrderAction) {
+ return wrapper.wrap(placeOrderAction, function (originalAction, paymentData, redirectOnSuccess) {
+ if (paymentData && paymentData.method.startsWith('rvvup_')) {
+ // do not create order for rvvup payments as it will be created later on.
+ //return;
+ }
+
+ return originalAction(paymentData, redirectOnSuccess);
+ });
+ };
+});
diff --git a/view/frontend/web/js/view/payment/messages.js b/view/frontend/web/js/view/payment/messages.js
index df76c1a3..14e608dd 100644
--- a/view/frontend/web/js/view/payment/messages.js
+++ b/view/frontend/web/js/view/payment/messages.js
@@ -5,7 +5,8 @@ define([
'mage/storage',
'mage/translate',
'Magento_Checkout/js/model/url-builder',
- 'Magento_Ui/js/model/messageList'
+ 'Magento_Ui/js/model/messageList',
+ 'domReady!',
], function (Component, $, _, storage, $t, urlBuilder, globalMessages) {
return Component.extend({
/**
From 99b1334b7ef56d362f11a181c3007265137315d9 Mon Sep 17 00:00:00 2001
From: andrii-onufriichuk
<117644505+andrii-onufriichuk@users.noreply.github.com>
Date: Thu, 14 Dec 2023 13:01:18 +0200
Subject: [PATCH 2/3] Feature/multi store setup (#64)
Fixed multi-store view incompatibility
---
Model/Config.php | 90 +++++++++++++++------------------
Model/ConfigInterface.php | 25 +++------
Model/SessionMessagesGet.php | 61 +++++-----------------
Observer/ConfigSaveObserver.php | 59 ++++++---------------
Plugin/JsLayout.php | 5 +-
Test/Unit/Model/Config.php | 10 +++-
6 files changed, 88 insertions(+), 162 deletions(-)
diff --git a/Model/Config.php b/Model/Config.php
index 62aa8105..bc8b14cb 100644
--- a/Model/Config.php
+++ b/Model/Config.php
@@ -2,15 +2,16 @@
namespace Rvvup\Payments\Model;
-use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
+use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
+use Magento\Store\Model\StoreManagerInterface;
use stdClass;
class Config implements ConfigInterface
{
/**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
+ * @var ScopeConfigInterface
*/
private $scopeConfig;
@@ -20,54 +21,47 @@ class Config implements ConfigInterface
private $jwt;
/**
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- * @return void
+ * @var StoreManagerInterface
*/
- public function __construct(ScopeConfigInterface $scopeConfig)
- {
+ private $storeManager;
+
+ /**
+ * @param ScopeConfigInterface $scopeConfig
+ * @param StoreManagerInterface $storeManager
+ */
+ public function __construct(
+ ScopeConfigInterface $scopeConfig,
+ StoreManagerInterface $storeManager
+ ) {
$this->scopeConfig = $scopeConfig;
+ $this->storeManager = $storeManager;
}
/**
* Validate whether Rvvup module & payment methods are active.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return bool
*/
- public function isActive(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): bool
+ public function isActive(string $scopeType = ScopeInterface::SCOPE_STORE): bool
{
- if (!$this->getActiveConfig($scopeType, $scopeCode)) {
+ if (!$this->getActiveConfig($scopeType)) {
return false;
}
- $jwt = $this->scopeConfig->getValue(self::RVVUP_CONFIG . self::XML_PATH_JWT);
- if (!is_string($jwt)) {
- return false;
- }
- $parts = explode('.', $jwt);
- if (count($parts) !== 3) {
- return false;
- }
- list($head, $body, $crypto) = $parts;
- try {
- // phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged
- $this->jwt = json_decode(base64_decode($body), false, 2, JSON_THROW_ON_ERROR);
- return true;
- } catch (Exception $e) {
- return false;
- }
+ $jwt = $this->getJwt($scopeType);
+ return (bool)$jwt;
}
/**
* Get the active value from the config.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or ID as a string
* @return bool
*/
- public function getActiveConfig(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): bool
+ public function getActiveConfig(string $scopeType = ScopeInterface::SCOPE_STORE): bool
{
+ $scopeCode = $this->storeManager->getStore() ? $this->storeManager->getStore()->getCode() : null;
return (bool) $this->scopeConfig->getValue(self::RVVUP_CONFIG . self::XML_PATH_ACTIVE, $scopeType, $scopeCode);
}
@@ -75,11 +69,12 @@ public function getActiveConfig(string $scopeType = ScopeInterface::SCOPE_STORE,
* Get the JWT value from the config.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return string|null
*/
- public function getJwtConfig(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): ?string
+ public function getJwtConfig(string $scopeType = ScopeInterface::SCOPE_STORE): ?string
{
+ $scopeCode = $this->storeManager->getStore() ? $this->storeManager->getStore()->getCode() : null;
+
$value = $this->scopeConfig->getValue(self::RVVUP_CONFIG . self::XML_PATH_JWT, $scopeType, $scopeCode);
if ($value === null) {
@@ -104,12 +99,11 @@ public function getJwtConfig(string $scopeType = ScopeInterface::SCOPE_STORE, st
* Get the endpoint URL.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or ID as a string
* @return string
*/
- public function getEndpoint(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): string
+ public function getEndpoint(string $scopeType = ScopeInterface::SCOPE_STORE): string
{
- $jwt = $this->getJwt($scopeType, $scopeCode);
+ $jwt = $this->getJwt($scopeType);
return $jwt === null ? '' : (string) $jwt->aud;
}
@@ -118,12 +112,11 @@ public function getEndpoint(string $scopeType = ScopeInterface::SCOPE_STORE, str
* Get the Merchant ID.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or ID as a string
* @return string
*/
- public function getMerchantId(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): string
+ public function getMerchantId(string $scopeType = ScopeInterface::SCOPE_STORE): string
{
- $jwt = $this->getJwt($scopeType, $scopeCode);
+ $jwt = $this->getJwt($scopeType);
return $jwt === null ? '' : (string) $jwt->merchantId;
}
@@ -132,12 +125,11 @@ public function getMerchantId(string $scopeType = ScopeInterface::SCOPE_STORE, s
* Get the Authorization Token.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or ID as a string
* @return string
*/
- public function getAuthToken(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): string
+ public function getAuthToken(string $scopeType = ScopeInterface::SCOPE_STORE): string
{
- $jwt = $this->getJwt($scopeType, $scopeCode);
+ $jwt = $this->getJwt($scopeType);
if ($jwt === null) {
return '';
@@ -150,11 +142,11 @@ public function getAuthToken(string $scopeType = ScopeInterface::SCOPE_STORE, st
* Check whether debug mode is enabled.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or ID as a string
* @return bool
*/
- public function isDebugEnabled(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): bool
+ public function isDebugEnabled(string $scopeType = ScopeInterface::SCOPE_STORE): bool
{
+ $scopeCode = $this->storeManager->getStore() ? $this->storeManager->getStore()->getCode() : null;
return (bool) $this->scopeConfig->getValue(self::RVVUP_CONFIG . self::XML_PATH_DEBUG, $scopeType, $scopeCode);
}
@@ -162,14 +154,13 @@ public function isDebugEnabled(string $scopeType = ScopeInterface::SCOPE_STORE,
* Get a standard class by decoding the config JWT.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or ID as a string
*
* @return \stdClass|null
*/
- private function getJwt(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): ?stdClass
+ private function getJwt(string $scopeType = ScopeInterface::SCOPE_STORE): ?stdClass
{
- if (!$this->jwt || $scopeType !== ScopeInterface::SCOPE_STORE || $scopeCode !== null) {
- $jwt = $this->getJwtConfig($scopeType, $scopeCode);
+ if (!$this->jwt) {
+ $jwt = $this->getJwtConfig($scopeType);
if ($jwt === null) {
$this->jwt = null;
@@ -204,24 +195,23 @@ public function getPaypalBlockStyling(string $config): string
public function getPayPalBlockConfig(
string $config,
- string $scopeType = ScopeInterface::SCOPE_STORE,
- string $scopeCode = null
+ string $scopeType = ScopeInterface::SCOPE_STORE
): string {
-
$config = self::RVVUP_CONFIG . self::XML_PATH_PAYPAL_BLOCK . $config;
+ $scopeCode = $this->storeManager->getStore() ? $this->storeManager->getStore()->getCode() : null;
return $this->scopeConfig->getValue($config, $scopeType, $scopeCode);
}
/**
* @param string $scopeType
- * @param string|null $scopeCode
* @return bool
+ * @throws NoSuchEntityException
*/
public function getValidProductTypes(
- string $scopeType = ScopeInterface::SCOPE_STORE,
- string $scopeCode = null
+ string $scopeType = ScopeInterface::SCOPE_STORE
): array {
+ $scopeCode = $this->storeManager->getStore() ? $this->storeManager->getStore()->getCode() : null;
$path = self::RVVUP_CONFIG . self::PRODUCT_RESTRICTIONS . self::XML_PATH_PRODUCT_TYPES_ENABLED;
$types = $this->scopeConfig->getValue($path, $scopeType, $scopeCode);
return explode(',', $types);
diff --git a/Model/ConfigInterface.php b/Model/ConfigInterface.php
index cb63c851..1a0c6fe4 100644
--- a/Model/ConfigInterface.php
+++ b/Model/ConfigInterface.php
@@ -25,64 +25,57 @@ interface ConfigInterface
* Validate whether Rvvup module & payment methods are active.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return bool
*/
- public function isActive(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): bool;
+ public function isActive(string $scopeType = ScopeInterface::SCOPE_STORE): bool;
/**
* Get the active value from the config.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return bool
*/
- public function getActiveConfig(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): bool;
+ public function getActiveConfig(string $scopeType = ScopeInterface::SCOPE_STORE): bool;
/**
* Get the JWT value from the config.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return string|null
*/
- public function getJwtConfig(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): ?string;
+ public function getJwtConfig(string $scopeType = ScopeInterface::SCOPE_STORE): ?string;
/**
* Get the endpoint URL.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return string
*/
- public function getEndpoint(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): string;
+ public function getEndpoint(string $scopeType = ScopeInterface::SCOPE_STORE): string;
/**
* Get the Merchant ID.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return string
*/
- public function getMerchantId(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): string;
+ public function getMerchantId(string $scopeType = ScopeInterface::SCOPE_STORE): string;
/**
* Get the Authorization Token.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return string
*/
- public function getAuthToken(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): string;
+ public function getAuthToken(string $scopeType = ScopeInterface::SCOPE_STORE): string;
/**
* Check whether debug mode is enabled.
*
* @param string $scopeType
- * @param string|null $scopeCode The store's code or storeId as a string
* @return bool
*/
- public function isDebugEnabled(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): bool;
+ public function isDebugEnabled(string $scopeType = ScopeInterface::SCOPE_STORE): bool;
/**
* Get style for paypal button
@@ -94,11 +87,9 @@ public function getPaypalBlockStyling(string $config): string;
/**
* Get valid product types
* @param string $scopeType
- * @param string|null $scopeCode
* @return array
*/
public function getValidProductTypes(
- string $scopeType = ScopeInterface::SCOPE_STORE,
- string $scopeCode = null
+ string $scopeType = ScopeInterface::SCOPE_STORE
): array;
}
diff --git a/Model/SessionMessagesGet.php b/Model/SessionMessagesGet.php
index f6f51152..6387374d 100644
--- a/Model/SessionMessagesGet.php
+++ b/Model/SessionMessagesGet.php
@@ -5,12 +5,8 @@
namespace Rvvup\Payments\Model;
use LogicException;
-use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
-use Magento\Store\Api\Data\StoreInterface;
-use Magento\Store\Model\ScopeInterface;
-use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Rvvup\Payments\Api\Data\SessionMessageInterface;
use Rvvup\Payments\Api\Data\SessionMessageInterfaceFactory;
@@ -19,57 +15,49 @@
class SessionMessagesGet implements SessionMessagesGetInterface
{
/**
- * @var \Magento\Framework\Message\ManagerInterface
+ * @var ManagerInterface
*/
private $messageManager;
/**
- * @var \Magento\Framework\View\Element\Message\InterpretationStrategyInterface
+ * @var InterpretationStrategyInterface
*/
private $messageInterpretationStrategy;
/**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- private $storeManager;
-
- /**
- * @var \Rvvup\Payments\Api\Data\SessionMessageInterfaceFactory
+ * @var SessionMessageInterfaceFactory
*/
private $sessionMessageFactory;
/**
- * @var \Rvvup\Payments\Model\ConfigInterface
+ * @var ConfigInterface
*/
private $config;
/**
* Set via di.xml
*
- * @var \Psr\Log\LoggerInterface|RvvupLog
+ * @var LoggerInterface|RvvupLog
*/
private $logger;
/**
- * @param \Magento\Framework\Message\ManagerInterface $messageManager
- * @param \Magento\Framework\View\Element\Message\InterpretationStrategyInterface $messageInterpretationStrategy
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Rvvup\Payments\Api\Data\SessionMessageInterfaceFactory $sessionMessageFactory
- * @param \Rvvup\Payments\Model\ConfigInterface $config
- * @param \Psr\Log\LoggerInterface $logger
+ * @param ManagerInterface $messageManager
+ * @param InterpretationStrategyInterface $messageInterpretationStrategy
+ * @param SessionMessageInterfaceFactory $sessionMessageFactory
+ * @param ConfigInterface $config
+ * @param LoggerInterface $logger
* @return void
*/
public function __construct(
ManagerInterface $messageManager,
InterpretationStrategyInterface $messageInterpretationStrategy,
- StoreManagerInterface $storeManager,
SessionMessageInterfaceFactory $sessionMessageFactory,
ConfigInterface $config,
LoggerInterface $logger
) {
$this->messageManager = $messageManager;
$this->messageInterpretationStrategy = $messageInterpretationStrategy;
- $this->storeManager = $storeManager;
$this->sessionMessageFactory = $sessionMessageFactory;
$this->config = $config;
$this->logger = $logger;
@@ -78,18 +66,14 @@ public function __construct(
/**
* Get the Rvvup Payments session messages.
*
- * @return \Rvvup\Payments\Api\Data\SessionMessageInterface[]
+ * @return SessionMessageInterface[]
*/
public function execute(): array
{
$messages = [];
- $store = $this->getStore();
// If module not active for store, return empty array.
- if (!$this->config->getActiveConfig(
- ScopeInterface::SCOPE_STORE,
- $store === null ? null : $store->getCode()
- )) {
+ if (!$this->config->getActiveConfig()) {
return $messages;
}
@@ -103,17 +87,14 @@ public function execute(): array
foreach ($messageCollection->getItems() as $message) {
try {
- /** @var \Rvvup\Payments\Api\Data\SessionMessageInterface $sessionMessage */
+ /** @var SessionMessageInterface $sessionMessage */
$sessionMessage = $this->sessionMessageFactory->create();
$sessionMessage->setType($message->getType());
$sessionMessage->setText($this->messageInterpretationStrategy->interpret($message));
$messages[] = $sessionMessage;
} catch (LogicException $ex) {
- if (!$this->config->isDebugEnabled(
- ScopeInterface::SCOPE_STORE,
- $store === null ? null : $store->getCode()
- )) {
+ if (!$this->config->isDebugEnabled()) {
continue;
}
@@ -123,18 +104,4 @@ public function execute(): array
return $messages;
}
-
- /**
- * Get current store.
- *
- * @return \Magento\Store\Api\Data\StoreInterface|null
- */
- private function getStore(): ?StoreInterface
- {
- try {
- return $this->storeManager->getStore();
- } catch (LocalizedException $ex) {
- return null;
- }
- }
}
diff --git a/Observer/ConfigSaveObserver.php b/Observer/ConfigSaveObserver.php
index c25f5b32..aa16a9c0 100644
--- a/Observer/ConfigSaveObserver.php
+++ b/Observer/ConfigSaveObserver.php
@@ -3,14 +3,13 @@
namespace Rvvup\Payments\Observer;
use Exception;
-use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\Event;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\UrlInterface;
-use Magento\Store\Model\ScopeInterface;
+use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Rvvup\Payments\Model\ConfigInterface;
use Rvvup\Payments\Model\SdkProxy;
@@ -20,6 +19,7 @@ class ConfigSaveObserver implements ObserverInterface
/** @var State */
private $appState;
/** @var UrlInterface */
+
private $urlBuilder;
/**
@@ -29,6 +29,7 @@ class ConfigSaveObserver implements ObserverInterface
/** @var SdkProxy */
private $sdkProxy;
+
/** @var ManagerInterface */
private $messageManager;
@@ -39,14 +40,19 @@ class ConfigSaveObserver implements ObserverInterface
*/
private $logger;
+ /**
+ * @var StoreManagerInterface
+ */
+ private $storeManager;
+
/**
* @param State $appState
* @param UrlInterface $urlBuilder
- * @param \Rvvup\Payments\Model\ConfigInterface $config
+ * @param ConfigInterface $config
* @param SdkProxy $sdkProxy
* @param ManagerInterface $messageManager
- * @param \Psr\Log\LoggerInterface $logger
- * @return void
+ * @param StoreManagerInterface $storeManager
+ * @param LoggerInterface $logger
*/
public function __construct(
State $appState,
@@ -54,6 +60,7 @@ public function __construct(
ConfigInterface $config,
SdkProxy $sdkProxy,
ManagerInterface $messageManager,
+ StoreManagerInterface $storeManager,
LoggerInterface $logger
) {
$this->appState = $appState;
@@ -61,6 +68,7 @@ public function __construct(
$this->config = $config;
$this->sdkProxy = $sdkProxy;
$this->messageManager = $messageManager;
+ $this->storeManager = $storeManager;
$this->logger = $logger;
}
@@ -100,11 +108,10 @@ private function sendApiDataOnMethodDisabled(Event $event): void
)) {
return;
}
-
- $scope = $this->mapScope($event);
+ $scope = $this->storeManager->getStore() ? $this->storeManager->getStore()->getCode() : null;
// No action if it was activated.
- if ($this->config->getActiveConfig($scope['scopeType'], $scope['scopeCode']) === true) {
+ if ($this->config->getActiveConfig() === true) {
return;
}
@@ -113,44 +120,10 @@ private function sendApiDataOnMethodDisabled(Event $event): void
$this->sdkProxy->createEvent(
'MERCHANT_PLUGIN_DEACTIVATED',
'Magento Payment method deactivated',
- $scope
+ [$scope]
);
} catch (Exception $ex) {
$this->logger->error('Failed to send create event API request to Rvvup with message: ' . $ex->getMessage());
}
}
-
- /**
- * Get the relevant scope from the event data
- *
- * @param \Magento\Framework\Event $event
- * @return array
- */
- private function mapScope(Event $event): array
- {
- $website = is_string($event->getData('website')) ? $event->getData('website') : '';
- $store = is_string($event->getData('store')) ? $event->getData('store') : '';
-
- // If no value for either website or store, return default scope config.
- if (empty($website) && empty($store)) {
- return [
- 'scopeType' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
- 'scopeCode' => null
- ];
- }
-
- // Otherwise, it's either website (if store is empty)
- if (empty($store)) {
- return [
- 'scopeType' => ScopeInterface::SCOPE_WEBSITE,
- 'scopeCode' => $website
- ];
- }
-
- // Or store if not empty.
- return [
- 'scopeType' => ScopeInterface::SCOPE_STORE,
- 'scopeCode' => $store
- ];
- }
}
diff --git a/Plugin/JsLayout.php b/Plugin/JsLayout.php
index 5ceb2f02..1cca2007 100644
--- a/Plugin/JsLayout.php
+++ b/Plugin/JsLayout.php
@@ -57,10 +57,7 @@ public function __construct(
*/
public function beforeProcess(LayoutProcessor $subject, $jsLayout): array
{
- if ($this->config->isActive(
- ScopeInterface::SCOPE_STORE,
- $this->getStore() === null ? null : $this->getStore()->getCode()
- )) {
+ if ($this->config->isActive()) {
// Add payment methods.
$renders = &$jsLayout["components"]["checkout"]["children"]["steps"]["children"]["billing-step"]["children"]
["payment"]["children"]["renders"]["children"];
diff --git a/Test/Unit/Model/Config.php b/Test/Unit/Model/Config.php
index 7ab89921..d793a1c9 100644
--- a/Test/Unit/Model/Config.php
+++ b/Test/Unit/Model/Config.php
@@ -6,6 +6,7 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
+use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\TestCase;
use Rvvup\Payments\Model\ConfigInterface;
@@ -14,6 +15,9 @@ class Config extends TestCase
/** @var ScopeConfigInterface */
private $scopeConfigMock;
+ /** @var StoreManagerInterface */
+ private $storeManagerMock;
+
/** @var \Rvvup\Payments\Model\Config */
private $config;
@@ -22,7 +26,11 @@ protected function setUp(): void
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->disableOriginalConstructor()->getMock();
- $this->config = new \Rvvup\Payments\Model\Config($this->scopeConfigMock);
+ $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
+ ->disableOriginalConstructor()->getMock();
+ $this->storeManagerMock->method('getStore')->willReturn(null);
+
+ $this->config = new \Rvvup\Payments\Model\Config($this->scopeConfigMock, $this->storeManagerMock);
}
public function testPaypalBlockDefaultStyling()
From bd3ab2c27a40077d3647007b653842114ddcca98 Mon Sep 17 00:00:00 2001
From: andrii-onufriichuk
<117644505+andrii-onufriichuk@users.noreply.github.com>
Date: Thu, 14 Dec 2023 17:09:38 +0200
Subject: [PATCH 3/3] Fix multi-store setup (#66)
---
Model/Queue/Handler/Handler.php | 11 -----------
1 file changed, 11 deletions(-)
diff --git a/Model/Queue/Handler/Handler.php b/Model/Queue/Handler/Handler.php
index 52ceb2cb..70f3d313 100644
--- a/Model/Queue/Handler/Handler.php
+++ b/Model/Queue/Handler/Handler.php
@@ -69,17 +69,6 @@ public function execute(int $id)
try {
$webhook = $this->webhookRepository->getById($id);
$payload = $this->serializer->unserialize($webhook->getPayload());
- // Ensure required params are present
-
- // Ensure configured merchant_id matches request
- if ($payload['merchant_id'] !== $this->config->getMerchantId()) {
- /**
- * The configuration in Magento is different from the webhook. We don't want Rvvup's backend to
- * continually make repeated calls so return a 200 and log the issue.
- */
- $this->logger->warning("`merchant_id` from webhook does not match configuration");
- return;
- }
$rvvupOrderId = $payload['order_id'];