Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-9101] Create reusable \Adyen\Payment\Helper\PaymentMethods::getPaymentMethodsResponse helper #2569

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Helper/Api/PaymentMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2021 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Helper\Api;

use Adyen\AdyenException;
use Adyen\Client;
use Adyen\ConnectionException;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Logger\AdyenLogger;
use Magento\Store\Model\Store;

/**
* Class PaymentMethods
*
* @package Adyen\Payment\Helper\Api
*/
class PaymentMethods
{
/**
* @var \Adyen\Payment\Helper\Data
*/
protected Data $adyenHelper;

/**
* @var \Adyen\Payment\Logger\AdyenLogger
*/
protected AdyenLogger $adyenLogger;

/**
* PaymentMethods Constructor
*
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
*/
public function __construct(
Data $adyenHelper,
AdyenLogger $adyenLogger,
) {
$this->adyenHelper = $adyenHelper;
$this->adyenLogger = $adyenLogger;
}

/**
* @param array $requestParams
* @param \Magento\Store\Model\Store $store
* @return array
* @throws \Adyen\AdyenException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getPaymentMethods(array $requestParams, Store $store): array
{
// initialize the adyen client
$client = $this->adyenHelper->initializeAdyenClient($store->getId());

// initialize service
$service = $this->adyenHelper->createAdyenCheckoutService($client);

try {
$this->adyenHelper->logRequest($requestParams, Client::API_CHECKOUT_VERSION, '/paymentMethods');
$responseData = $service->paymentMethods($requestParams);
} catch (AdyenException $e) {
$this->adyenLogger->error(
"The Payment methods response is empty check your Adyen configuration in Magento."
);

// return empty result
return [];
} catch (ConnectionException $e) {
$this->adyenLogger->error(
"Connection to the endpoint failed. Check the Adyen Live endpoint prefix configuration."
);
return [];
}

$this->adyenHelper->logResponse($responseData);
return $responseData;
}
}
39 changes: 7 additions & 32 deletions Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Adyen\Util\ManualCapture;
use Exception;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
Expand All @@ -42,6 +41,8 @@
use Magento\Store\Model\Store;
use Magento\Vault\Api\PaymentTokenRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Adyen\Payment\Helper\Api\PaymentMethods as ApiPaymentMethods;

class PaymentMethods extends AbstractHelper
{
const ADYEN_HPP = 'adyen_hpp';
Expand Down Expand Up @@ -89,6 +90,7 @@ class PaymentMethods extends AbstractHelper
private SerializerInterface $serializer;
private PaymentTokenRepositoryInterface $paymentTokenRepository;
private SearchCriteriaBuilder $searchCriteriaBuilder;
protected ApiPaymentMethods $apiPaymentMethods;

public function __construct(
Context $context,
Expand All @@ -109,7 +111,8 @@ public function __construct(
SerializerInterface $serializer,
AdyenDataHelper $adyenDataHelper,
PaymentTokenRepositoryInterface $paymentTokenRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
SearchCriteriaBuilder $searchCriteriaBuilder,
ApiPaymentMethods $apiPaymentMethods
) {
parent::__construct($context);
$this->quoteRepository = $quoteRepository;
Expand All @@ -130,6 +133,7 @@ public function __construct(
$this->adyenDataHelper = $adyenDataHelper;
$this->paymentTokenRepository = $paymentTokenRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->apiPaymentMethods = $apiPaymentMethods;
}

public function getPaymentMethods(int $quoteId, ?string $country = null, ?string $shopperLocale = null): string
Expand Down Expand Up @@ -199,7 +203,7 @@ protected function fetchPaymentMethods(?string $country = null, ?string $shopper
}

$requestData = $this->getPaymentMethodsRequest($merchantAccount, $store, $quote, $shopperLocale, $country);
$responseData = $this->getPaymentMethodsResponse($requestData, $store);
$responseData = $this->apiPaymentMethods->getPaymentMethods($requestData, $store);
if (empty($responseData['paymentMethods'])) {
return json_encode([]);
}
Expand Down Expand Up @@ -299,35 +303,6 @@ protected function getCurrentCountryCode(Store $store): string
return "";
}

protected function getPaymentMethodsResponse(array $requestParams, Store $store): array
{
// initialize the adyen client
$client = $this->adyenHelper->initializeAdyenClient($store->getId());

// initialize service
$service = $this->adyenHelper->createAdyenCheckoutService($client);

try {
$this->adyenHelper->logRequest($requestParams, Client::API_CHECKOUT_VERSION, '/paymentMethods');
$responseData = $service->paymentMethods($requestParams);
} catch (AdyenException $e) {
$this->adyenLogger->error(
"The Payment methods response is empty check your Adyen configuration in Magento."
);
// return empty result
return [];
}
catch (ConnectionException $e) {
$this->adyenLogger->error(
"Connection to the endpoint failed. Check the Adyen Live endpoint prefix configuration."
);
return [];
}
$this->adyenHelper->logResponse($responseData);

return $responseData;
}

protected function getQuote(): \Magento\Quote\Model\Quote
{
return $this->quote;
Expand Down
Loading