Skip to content

Commit

Permalink
Feature: Add attribute to retrieve available POS terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Feb 19, 2024
1 parent ac30d84 commit 55b4289
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 52 deletions.
44 changes: 9 additions & 35 deletions Block/Form/Pointofsale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}
16 changes: 0 additions & 16 deletions GraphQL/Resolver/Cart/AvailableIssuersForMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,21 @@

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;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

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;
}

Expand Down
51 changes: 51 additions & 0 deletions GraphQL/Resolver/Cart/AvailableTerminalsForMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\GraphQL\Resolver\Cart;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Mollie\Payment\Service\Mollie\AvailableTerminals;
use Mollie\Payment\Service\Mollie\PointOfSaleAvailability;

class AvailableTerminalsForMethod implements ResolverInterface
{
/**
* @var PointOfSaleAvailability
*/
private $pointOfSaleAvailability;
/**
* @var AvailableTerminals
*/
private $availableTerminals;

public function __construct(
PointOfSaleAvailability $pointOfSaleAvailability,
AvailableTerminals $availableTerminals
) {
$this->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);
}
}
63 changes: 63 additions & 0 deletions Service/Mollie/AvailableTerminals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Service\Mollie;

use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\Terminal;

class AvailableTerminals
{
/**
* @var MollieApiClient
*/
private $mollieApiClient;

public function __construct(
MollieApiClient $mollieApiClient
) {
$this->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;
}
}
10 changes: 10 additions & 0 deletions Service/Mollie/PointOfSaleAvailability.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
9 changes: 8 additions & 1 deletion Webapi/PaymentInformationMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +44,10 @@ class PaymentInformationMeta implements PaymentInformationMetaInterface
* @var Pointofsale
*/
private $pointofsale;
/**
* @var AvailableTerminals
*/
private $availableTerminals;
/**
* @var Config
*/
Expand All @@ -63,6 +68,7 @@ public function __construct(
PaymentMethods $paymentMethods,
GetIssuers $getIssuers,
Pointofsale $pointofsale,
AvailableTerminals $availableTerminals,
IssuerInterfaceFactory $issuerFactory,
TerminalInterfaceFactory $terminalFactory
) {
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}
9 changes: 9 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}

Expand Down Expand Up @@ -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!
}
Expand Down

0 comments on commit 55b4289

Please sign in to comment.