From 55b4289ba1972c6a637998b6872189ec5f08be57 Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 19 Feb 2024 17:04:13 +0100 Subject: [PATCH] Feature: Add attribute to retrieve available POS terminals --- Block/Form/Pointofsale.php | 44 +++---------- .../Cart/AvailableIssuersForMethod.php | 16 ----- .../Cart/AvailableTerminalsForMethod.php | 51 +++++++++++++++ Service/Mollie/AvailableTerminals.php | 63 +++++++++++++++++++ Service/Mollie/PointOfSaleAvailability.php | 10 +++ Webapi/PaymentInformationMeta.php | 9 ++- etc/schema.graphqls | 9 +++ 7 files changed, 150 insertions(+), 52 deletions(-) create mode 100644 GraphQL/Resolver/Cart/AvailableTerminalsForMethod.php create mode 100644 Service/Mollie/AvailableTerminals.php diff --git a/Block/Form/Pointofsale.php b/Block/Form/Pointofsale.php index 7f197ad22e8..c2b751f13aa 100644 --- a/Block/Form/Pointofsale.php +++ b/Block/Form/Pointofsale.php @@ -8,9 +8,7 @@ use Magento\Framework\View\Element\Template\Context; use Magento\Payment\Block\Form; -use Mollie\Api\Exceptions\ApiException; -use Mollie\Api\Resources\Terminal; -use Mollie\Payment\Service\Mollie\MollieApiClient; +use Mollie\Payment\Service\Mollie\AvailableTerminals; /** * Class Pointofsale @@ -20,22 +18,23 @@ class Pointofsale extends Form { /** - * @var string + * @var AvailableTerminals */ - protected $_template = 'Mollie_Payment::form/pointofsale.phtml'; + private $availableTerminals; + /** - * @var MollieApiClient + * @var string */ - private $mollieApiClient; + protected $_template = 'Mollie_Payment::form/pointofsale.phtml'; public function __construct( Context $context, - MollieApiClient $mollieApiClient, + AvailableTerminals $availableTerminals, array $data = [] ) { parent::__construct($context, $data); - $this->mollieApiClient = $mollieApiClient; + $this->availableTerminals = $availableTerminals; } /** @@ -46,36 +45,11 @@ public function __construct( * serialNumber: string|null, * description: string * } - * @throws \Magento\Framework\Exception\LocalizedException - * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getTerminals(): array { $storeId = $this->_storeManager->getStore()->getId(); - try { - $mollieApiClient = $this->mollieApiClient->loadByStore((int)$storeId); - $terminals = $mollieApiClient->terminals->page(); - } catch (ApiException $exception) { - return []; - } - - $output = []; - /** @var Terminal $terminal */ - foreach ($terminals as $terminal) { - if (!$terminal->isActive()) { - continue; - } - - $output[] = [ - 'id' => $terminal->id, - 'brand' => $terminal->brand, - 'model' => $terminal->model, - 'serialNumber' => $terminal->serialNumber, - 'description' => $terminal->description, - ]; - } - - return $output; + return $this->availableTerminals->execute((int)$storeId); } } diff --git a/GraphQL/Resolver/Cart/AvailableIssuersForMethod.php b/GraphQL/Resolver/Cart/AvailableIssuersForMethod.php index 1058334eb25..261137b3db2 100644 --- a/GraphQL/Resolver/Cart/AvailableIssuersForMethod.php +++ b/GraphQL/Resolver/Cart/AvailableIssuersForMethod.php @@ -6,8 +6,6 @@ namespace Mollie\Payment\GraphQL\Resolver\Cart; -use Mollie\Payment\Helper\General; -use Mollie\Payment\Model\Mollie; use Mollie\Payment\Service\Mollie\GetIssuers; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Config\Element\Field; @@ -15,28 +13,14 @@ class AvailableIssuersForMethod implements ResolverInterface { - /** - * @var Mollie - */ - private $mollieModel; - - /** - * @var General - */ - private $mollieHelper; - /** * @var GetIssuers */ private $getIssuers; public function __construct( - Mollie $mollieModel, - General $mollieHelper, GetIssuers $getIssuers ) { - $this->mollieModel = $mollieModel; - $this->mollieHelper = $mollieHelper; $this->getIssuers = $getIssuers; } diff --git a/GraphQL/Resolver/Cart/AvailableTerminalsForMethod.php b/GraphQL/Resolver/Cart/AvailableTerminalsForMethod.php new file mode 100644 index 00000000000..23f950a6ec2 --- /dev/null +++ b/GraphQL/Resolver/Cart/AvailableTerminalsForMethod.php @@ -0,0 +1,51 @@ +pointOfSaleAvailability = $pointOfSaleAvailability; + $this->availableTerminals = $availableTerminals; + } + + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + $method = $value['code']; + if ($method != 'mollie_methods_pointofsale' || !$context->getExtensionAttributes()->getIsCustomer()) { + return []; + } + + $storeId = (int)$context->getExtensionAttributes()->getStore()->getId(); + $customerGroupId = $context->getExtensionAttributes()->getCustomerGroupId(); + if (!$this->pointOfSaleAvailability->isAvailableForCustomerGroupId($customerGroupId, $storeId)) { + return []; + } + + return $this->availableTerminals->execute((int)$storeId); + } +} diff --git a/Service/Mollie/AvailableTerminals.php b/Service/Mollie/AvailableTerminals.php new file mode 100644 index 00000000000..5dd8b9dd67f --- /dev/null +++ b/Service/Mollie/AvailableTerminals.php @@ -0,0 +1,63 @@ +mollieApiClient = $mollieApiClient; + } + + /** + * @return array{ + * id: string, + * brand: string, + * model: string, + * serialNumber: string|null, + * description: string + * } + */ + public function execute(int $storeId = null): array + { + try { + $mollieApiClient = $this->mollieApiClient->loadByStore($storeId); + $terminals = $mollieApiClient->terminals->page(); + } catch (ApiException $exception) { + return []; + } + + $output = []; + /** @var Terminal $terminal */ + foreach ($terminals as $terminal) { + if (!$terminal->isActive()) { + continue; + } + + $output[] = [ + 'id' => $terminal->id, + 'brand' => $terminal->brand, + 'model' => $terminal->model, + 'serialNumber' => $terminal->serialNumber, + 'description' => $terminal->description, + ]; + } + + return $output; + } +} diff --git a/Service/Mollie/PointOfSaleAvailability.php b/Service/Mollie/PointOfSaleAvailability.php index 09a9b68f245..07b6b4c6bc4 100644 --- a/Service/Mollie/PointOfSaleAvailability.php +++ b/Service/Mollie/PointOfSaleAvailability.php @@ -42,4 +42,14 @@ public function isAvailable(CartInterface $cart): bool $allowedGroups ); } + + public function isAvailableForCustomerGroupId(int $customerGroupId, int $storeId): bool + { + $allowedGroups = explode(',', $this->config->pointofsaleAllowedCustomerGroups($storeId)); + + return in_array( + (string)$customerGroupId, + $allowedGroups + ); + } } diff --git a/Webapi/PaymentInformationMeta.php b/Webapi/PaymentInformationMeta.php index 5f1e09faf27..ee3c70ee6f9 100644 --- a/Webapi/PaymentInformationMeta.php +++ b/Webapi/PaymentInformationMeta.php @@ -17,6 +17,7 @@ use Mollie\Payment\Api\Webapi\PaymentInformationMetaInterface; use Mollie\Payment\Block\Form\Pointofsale; use Mollie\Payment\Config; +use Mollie\Payment\Service\Mollie\AvailableTerminals; use Mollie\Payment\Service\Mollie\GetIssuers; use Mollie\Payment\Service\Mollie\MollieApiClient; use Mollie\Payment\Service\Mollie\PaymentMethods; @@ -43,6 +44,10 @@ class PaymentInformationMeta implements PaymentInformationMetaInterface * @var Pointofsale */ private $pointofsale; + /** + * @var AvailableTerminals + */ + private $availableTerminals; /** * @var Config */ @@ -63,6 +68,7 @@ public function __construct( PaymentMethods $paymentMethods, GetIssuers $getIssuers, Pointofsale $pointofsale, + AvailableTerminals $availableTerminals, IssuerInterfaceFactory $issuerFactory, TerminalInterfaceFactory $terminalFactory ) { @@ -71,6 +77,7 @@ public function __construct( $this->paymentMethods = $paymentMethods; $this->getIssuers = $getIssuers; $this->pointofsale = $pointofsale; + $this->availableTerminals = $availableTerminals; $this->config = $config; $this->issuerFactory = $issuerFactory; $this->terminalFactory = $terminalFactory; @@ -119,6 +126,6 @@ private function getTerminals(string $code): array return array_map(function (array $terminal) { return $this->terminalFactory->create($terminal); - }, $this->pointofsale->getTerminals()); + }, $this->availableTerminals->execute()); } } diff --git a/etc/schema.graphqls b/etc/schema.graphqls index 411f19e7f83..32fdf3903a8 100644 --- a/etc/schema.graphqls +++ b/etc/schema.graphqls @@ -24,6 +24,7 @@ type Cart { type AvailablePaymentMethod { mollie_available_issuers: [MollieIssuer!] @resolver(class: "Mollie\\Payment\\GraphQL\\Resolver\\Cart\\AvailableIssuersForMethod") @doc(description: "Available issuers for this payment method") + mollie_available_terminals: [MollieTerminalOutput!] @resolver(class: "Mollie\\Payment\\GraphQL\\Resolver\\Cart\\AvailableTerminalsForMethod") @doc(description: "Available terminals for this payment method") mollie_meta: MolliePaymentMethodMeta! @resolver(class: "Mollie\\Payment\\GraphQL\\Resolver\\Cart\\PaymentMethodMeta") @doc(description: "Retrieve meta information for this payment method (image)") } @@ -73,6 +74,14 @@ type Query { molliePaymentMethods(input: MolliePaymentMethodsInput): MolliePaymentMethodsOutput @resolver(class: "Mollie\\Payment\\GraphQL\\Resolver\\General\\MolliePaymentMethods") @cache(cacheIdentity: "Mollie\\Payment\\GraphQL\\Resolver\\Cache\\PaymentMethodsCache") } +type MollieTerminalOutput { + id: String! + brand: String! + model: String! + serialNumber: String + description: String! +} + type MollieResetCartOutput { cart: Cart! }