We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Commit c8144029 authored by rikterbeek's avatar rikterbeek

Fixes #43 generate payment methods based on shipping country selected in first step of checkout

parent 2108f77e
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Api;
/**
* Interface GuestAdyenPaymentMethodManagementInterface
* @api
* @package Adyen\Payment\Api
*/
interface AdyenPaymentMethodManagementInterface
{
/**
* Get payment information
*
* @param string $cartId
* @return \Magento\Checkout\Api\Data\PaymentDetailsInterface
*/
public function getPaymentMethods($cartId, \Magento\Quote\Api\Data\AddressInterface $shippingAddress);
}
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Api;
/**
* Interface GuestAdyenPaymentMethodManagementInterface
* @api
* @package Adyen\Payment\Api
*/
interface GuestAdyenPaymentMethodManagementInterface
{
/**
* Get payment information
*
* @param string $cartId
* @return \Magento\Checkout\Api\Data\PaymentDetailsInterface
*/
public function getPaymentMethods($cartId, \Magento\Quote\Api\Data\AddressInterface $shippingAddress);
}
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
/**
* @SuppressWarnings(PHPMD.LongVariable)
*/
class PaymentMethods extends AbstractHelper
{
/**
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $_quoteRepository;
/**
* @var \Magento\Quote\Model\QuoteIdMaskFactory
*/
protected $_quoteIdMaskFactory;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface $config
*/
protected $_config;
/**
* @var \Adyen\Payment\Helper\Data
*/
protected $_adyenHelper;
/**
* @var AdyenGenericConfig
*/
protected $_genericConfig;
/**
* @var \Magento\Checkout\Model\Session
*/
protected $_session;
/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
protected $_localeResolver;
/**
* @var \Adyen\Payment\Logger\AdyenLogger
*/
protected $_adyenLogger;
/**
* @var Repository
*/
protected $_assetRepo;
/**
* @var RequestInterface
*/
protected $_request;
/**
* @var \Magento\Framework\View\Asset\Source
*/
protected $_assetSource;
/**
* PaymentMethods constructor.
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
* @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param Data $adyenHelper
* @param \Adyen\Payment\Model\AdyenGenericConfig $genericConfig
* @param \Magento\Checkout\Model\Session $session
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
* @param \Magento\Framework\View\Asset\Repository $assetRepo
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\View\Asset\Source $assetSource
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Model\AdyenGenericConfig $genericConfig,
\Magento\Checkout\Model\Session $session,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Magento\Framework\View\Asset\Repository $assetRepo,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\View\Asset\Source $assetSource
) {
$this->_quoteRepository = $quoteRepository;
$this->_quoteIdMaskFactory = $quoteIdMaskFactory;
$this->_config = $config;
$this->_adyenHelper = $adyenHelper;
$this->_genericConfig = $genericConfig;
$this->_session = $session;
$this->_localeResolver = $localeResolver;
$this->_adyenLogger = $adyenLogger;
$this->_assetRepo = $assetRepo;
$this->_request = $request;
$this->_assetSource = $assetSource;
}
/**
* {@inheritDoc}
*/
public function getPaymentMethods($quoteId, $country = null)
{
// get quote from quoteId
$quote = $this->_quoteRepository->getActive($quoteId);
$store = $quote->getStore();
$paymentMethods = $this->_addHppMethodsToConfig($store, $country);
return $paymentMethods;
}
/**
* @param $store
* @return array
*/
protected function _addHppMethodsToConfig($store, $country)
{
$paymentMethods = [];
$ccEnabled = $this->_config->getValue('payment/'.\Adyen\Payment\Model\Method\Cc::METHOD_CODE.'/active');
$ccTypes = array_keys($this->_adyenHelper->getCcTypesAltData());
$sepaEnabled = $this->_config->getValue('payment/'.\Adyen\Payment\Model\Method\Sepa::METHOD_CODE.'/active');
foreach ($this->_fetchHppMethods($store, $country) as $methodCode => $methodData) {
/*
* skip payment methods if it is a creditcard that is enabled in adyen_cc
* or if payment is sepadirectdebit and SEPA api is enabled
*/
if ($ccEnabled && in_array($methodCode, $ccTypes)) {
continue;
} elseif ($methodCode == 'sepadirectdebit' && $sepaEnabled) {
continue;
}
$paymentMethods[$methodCode] = $methodData;
}
return $paymentMethods;
}
/**
* @param $store
* @return array
*/
protected function _fetchHppMethods($store, $country)
{
$skinCode = $this->_adyenHelper->getAdyenHppConfigData('skin_code');
$merchantAccount = $this->_adyenHelper->getAdyenAbstractConfigData('merchant_account');
if (!$skinCode || !$merchantAccount) {
return [];
}
$adyFields = [
"paymentAmount" => (int) $this->_adyenHelper->formatAmount(
$this->_getCurrentPaymentAmount(),
$this->_getCurrentCurrencyCode($store)
),
"currencyCode" => $this->_getCurrentCurrencyCode($store),
"merchantReference" => "Get Payment methods",
"skinCode" => $skinCode,
"merchantAccount" => $merchantAccount,
"sessionValidity" => date(
DATE_ATOM,
mktime(date("H") + 1, date("i"), date("s"), date("m"), date("j"), date("Y"))
),
"countryCode" => $this->_getCurrentCountryCode($store, $country),
"shopperLocale" => $this->_getCurrentLocaleCode($store)
];
$responseData = $this->_getDirectoryLookupResponse($adyFields, $store);
$paymentMethods = [];
if (isset($responseData['paymentMethods'])) {
foreach ($responseData['paymentMethods'] as $paymentMethod) {
$paymentMethodCode = $paymentMethod['brandCode'];
$paymentMethod = $this->_fieldMapPaymentMethod($paymentMethod);
// add icon location in result
if ($this->_genericConfig->showLogos()) {
$params = [];
// use frontend area
$params = array_merge(['area' => 'frontend', '_secure' => $this->_request->isSecure()], $params);
$asset = $this->_assetRepo->createAsset('Adyen_Payment::images/logos/' .
$paymentMethodCode . '.png', $params);
$placeholder = $this->_genericConfig->findRelativeSourceFilePath($asset);
$icon = null;
if ($placeholder) {
list($width, $height) = getimagesize($asset->getSourceFile());
$icon = [
'url' => $asset->getUrl(),
'width' => $width,
'height' => $height
];
}
$paymentMethod['icon'] = $icon;
}
$paymentMethods[$paymentMethodCode] = $paymentMethod;
}
}
return $paymentMethods;
}
/**
* @return bool|int
*/
protected function _getCurrentPaymentAmount()
{
if (($grandTotal = $this->_getQuote()->getGrandTotal()) > 0) {
return $grandTotal;
}
return 10;
}
/**
* @param $store
* @return mixed
*/
protected function _getCurrentCurrencyCode($store)
{
return $this->_getQuote()->getQuoteCurrencyCode() ?: $store->getBaseCurrencyCode();
}
/**
* @param $store
* @return int|mixed|string
*/
protected function _getCurrentCountryCode($store, $country)
{
// if fixed countryCode is setup in config use this
$countryCode = $this->_adyenHelper->getAdyenHppConfigData('country_code', $store->getId());
if ($countryCode != "") {
return $countryCode;
}
if ($country != null) {
return $country;
}
if ($country = $this->_getQuote()->getBillingAddress()->getCountry()) {
return $country;
}
$defaultCountry = $this->_config->getValue(
\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);
if ($defaultCountry) {
return $defaultCountry;
}
return "";
}
/**
* @param $store
* @return mixed|string
*/
protected function _getCurrentLocaleCode($store)
{
$localeCode = $this->_adyenHelper->getAdyenAbstractConfigData('shopper_locale', $store->getId());
if ($localeCode != "") {
return $localeCode;
}
$locale = $this->_localeResolver->getLocale();
if ($locale) {
return $locale;
}
// should have the vulue if not fall back to default
$localeCode = $this->_config->getValue(
Data::XML_PATH_DEFAULT_LOCALE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);
return $localeCode;
}
/**
* @var array
*/
protected $_fieldMapPaymentMethod = [
'name' => 'title'
];
/**
* @param $paymentMethod
* @return mixed
*/
protected function _fieldMapPaymentMethod($paymentMethod)
{
foreach ($this->_fieldMapPaymentMethod as $field => $newField) {
if (isset($paymentMethod[$field])) {
$paymentMethod[$newField] = $paymentMethod[$field];
unset($paymentMethod[$field]);
}
}
return $paymentMethod;
}
/**
* @param $requestParams
* @param $store
* @return array
* @throws \Adyen\AdyenException
*/
protected function _getDirectoryLookupResponse($requestParams, $store)
{
$cacheKey = $this->_getCacheKeyForRequest($requestParams, $store);
// initialize the adyen client
$client = new \Adyen\Client();
if ($this->_adyenHelper->isDemoMode()) {
$client->setEnvironment(\Adyen\Environment::TEST);
} else {
$client->setEnvironment(\Adyen\Environment::LIVE);
}
// connect to magento log
$client->setLogger($this->_adyenLogger);
$hmacKey = $this->_adyenHelper->getHmac();
// create and add signature
try {
$requestParams["merchantSig"] = \Adyen\Util\Util::calculateSha256Signature($hmacKey, $requestParams);
} catch (\Adyen\AdyenException $e) {
$this->_adyenLogger->error($e->getMessage());
// return empty result
return [];
}
// initialize service
$service = new \Adyen\Service\DirectoryLookup($client);
try {
$responseData = $service->directoryLookup($requestParams);
} catch (\Adyen\AdyenException $e) {
$this->_adyenLogger->error(
"The Directory Lookup response is empty check your Adyen configuration in Magento."
);
// return empty result
return [];
}
return $responseData;
}
/**
* @var array
*/
protected $_cacheParams = array(
'currencyCode',
'merchantReference',
'skinCode',
'merchantAccount',
'countryCode',
'shopperLocale',
);
/**
* @param $requestParams
* @param $store
* @return string
*/
protected function _getCacheKeyForRequest($requestParams, $store)
{
$cacheParams = [];
$cacheParams['store'] = $store->getId();
foreach ($this->_cacheParams as $paramKey) {
if (isset($requestParams[$paramKey])) {
$cacheParams[$paramKey] = $requestParams[$paramKey];
}
}
return md5(implode('|', $cacheParams));
}
/**
* @return \Magento\Quote\Model\Quote
*/
protected function _getQuote()
{
return $this->_session->getQuote();
}
}
\ No newline at end of file
...@@ -30,51 +30,16 @@ use Magento\Directory\Helper\Data; ...@@ -30,51 +30,16 @@ use Magento\Directory\Helper\Data;
class AdyenHppConfigProvider implements ConfigProviderInterface class AdyenHppConfigProvider implements ConfigProviderInterface
{ {
/**
* @var \Magento\Framework\App\State
*/
protected $_appState;
/**
* @var \Magento\Checkout\Model\Session
*/
protected $_session;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/** /**
* @var PaymentHelper * @var PaymentHelper
*/ */
protected $_paymentHelper; protected $_paymentHelper;
/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
protected $_localeResolver;
/** /**
* @var \Adyen\Payment\Helper\Data * @var \Adyen\Payment\Helper\Data
*/ */
protected $_adyenHelper; protected $_adyenHelper;
/**
* @var \Adyen\Payment\Logger\AdyenLogger
*/
protected $_adyenLogger;
/**
* @var AdyenGenericConfig
*/
protected $_genericConfig;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface $config
*/
protected $_config;
/** /**
* @var string[] * @var string[]
*/ */
...@@ -90,36 +55,15 @@ class AdyenHppConfigProvider implements ConfigProviderInterface ...@@ -90,36 +55,15 @@ class AdyenHppConfigProvider implements ConfigProviderInterface
/** /**
* AdyenHppConfigProvider constructor. * AdyenHppConfigProvider constructor.
* *
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Checkout\Model\Session $session
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param PaymentHelper $paymentHelper * @param PaymentHelper $paymentHelper
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
* @param AdyenGenericConfig $genericConfig
*/ */
public function __construct( public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Checkout\Model\Session $session,
\Magento\Store\Model\StoreManagerInterface $storeManager,
PaymentHelper $paymentHelper, PaymentHelper $paymentHelper,
\Magento\Framework\Locale\ResolverInterface $localeResolver, \Adyen\Payment\Helper\Data $adyenHelper
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Model\AdyenGenericConfig $genericConfig
) { ) {
$this->_appState = $context->getAppState();
$this->_session = $session;
$this->_storeManager = $storeManager;
$this->_paymentHelper = $paymentHelper; $this->_paymentHelper = $paymentHelper;
$this->_localeResolver = $localeResolver;
$this->_config = $config;
$this->_adyenHelper = $adyenHelper; $this->_adyenHelper = $adyenHelper;
$this->_adyenLogger = $adyenLogger;
$this->_genericConfig = $genericConfig;
foreach ($this->_methodCodes as $code) { foreach ($this->_methodCodes as $code) {
$this->methods[$code] = $this->_paymentHelper->getMethodInstance($code); $this->methods[$code] = $this->_paymentHelper->getMethodInstance($code);
...@@ -142,328 +86,11 @@ class AdyenHppConfigProvider implements ConfigProviderInterface ...@@ -142,328 +86,11 @@ class AdyenHppConfigProvider implements ConfigProviderInterface
foreach ($this->_methodCodes as $code) { foreach ($this->_methodCodes as $code) {
if ($this->methods[$code]->isAvailable()) { if ($this->methods[$code]->isAvailable()) {
// get payment methods $paymentMethodSelectionOnAdyen =
$config['payment'] ['adyenHpp']['paymentMethods'] = $this->getAdyenHppPaymentMethods(); $this->_adyenHelper->getAdyenHppConfigDataFlag('payment_selection_on_adyen');
$config['payment'] ['adyenHpp']['isPaymentMethodSelectionOnAdyen'] = $paymentMethodSelectionOnAdyen;
} }
} }
$paymentMethodSelectionOnAdyen = $this->_adyenHelper->getAdyenHppConfigDataFlag('payment_selection_on_adyen');
$config['payment'] ['adyenHpp']['isPaymentMethodSelectionOnAdyen'] = $paymentMethodSelectionOnAdyen;
return $config; return $config;
} }
/**
* @return array|null
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAdyenHppPaymentMethods()
{
$paymentMethods = null;
// is admin?
if ($this->_appState->getAreaCode() === \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) {
//retrieve storeId from quote
$store = $this->_session->getQuote()->getStore();
} else {
$store = $this->_storeManager->getStore();
}
// is adyen HPP enabled ?
$hppActive = $this->methods['adyen_hpp']->isAvailable();
if ($hppActive) {
$paymentMethods = $this->_addHppMethodsToConfig($store);
}
return $paymentMethods;
}
/**
* @param $store
* @return array
*/
protected function _addHppMethodsToConfig($store)
{
$paymentMethods = [];
$ccEnabled = $this->_config->getValue('payment/'.\Adyen\Payment\Model\Method\Cc::METHOD_CODE.'/active');
$ccTypes = array_keys($this->_adyenHelper->getCcTypesAltData());
$sepaEnabled = $this->_config->getValue('payment/'.\Adyen\Payment\Model\Method\Sepa::METHOD_CODE.'/active');
foreach ($this->_fetchHppMethods($store) as $methodCode => $methodData) {
/*
* skip payment methods if it is a creditcard that is enabled in adyen_cc
* or if payment is sepadirectdebit and SEPA api is enabled
*/
if ($ccEnabled && in_array($methodCode, $ccTypes)) {
continue;
} elseif ($methodCode == 'sepadirectdebit' && $sepaEnabled) {
continue;
}
$paymentMethods[$methodCode] = $methodData;
}
return $paymentMethods;
}
/**
* @param $store
* @return array
*/
protected function _fetchHppMethods($store)
{
$skinCode = $this->_adyenHelper->getAdyenHppConfigData('skin_code');
$merchantAccount = $this->_adyenHelper->getAdyenAbstractConfigData('merchant_account');
if (!$skinCode || !$merchantAccount) {
return [];
}
$adyFields = [
"paymentAmount" => (int) $this->_adyenHelper->formatAmount($this->_getCurrentPaymentAmount(), $this->_getCurrentCurrencyCode($store)),
"currencyCode" => $this->_getCurrentCurrencyCode($store),
"merchantReference" => "Get Payment methods",
"skinCode" => $skinCode,
"merchantAccount" => $merchantAccount,
"sessionValidity" => date(
DATE_ATOM,
mktime(date("H") + 1, date("i"), date("s"), date("m"), date("j"), date("Y"))
),
"countryCode" => $this->_getCurrentCountryCode($store),
"shopperLocale" => $this->_getCurrentLocaleCode($store)
];
$responseData = $this->_getDirectoryLookupResponse($adyFields, $store);
$paymentMethods = [];
if (isset($responseData['paymentMethods'])) {
foreach ($responseData['paymentMethods'] as $paymentMethod) {
$paymentMethodCode = $paymentMethod['brandCode'];
$paymentMethod = $this->_fieldMapPaymentMethod($paymentMethod);
// add icon location in result
if ($this->_genericConfig->showLogos()) {
$asset = $this->_genericConfig->createAsset('Adyen_Payment::images/logos/' .
$paymentMethodCode . '.png');
$placeholder = $this->_genericConfig->findRelativeSourceFilePath($asset);
$icon = null;
if ($placeholder) {
list($width, $height) = getimagesize($asset->getSourceFile());
$icon = [
'url' => $asset->getUrl(),
'width' => $width,
'height' => $height
];
}
$paymentMethod['icon'] = $icon;
}
$paymentMethods[$paymentMethodCode] = $paymentMethod;
}
}
return $paymentMethods;
}
/**
* @return bool|int
*/
protected function _getCurrentPaymentAmount()
{
if (($grandTotal = $this->_getQuote()->getGrandTotal()) > 0) {
return $grandTotal;
}
return 10;
}
/**
* @param $store
* @return mixed
*/
protected function _getCurrentCurrencyCode($store)
{
return $this->_getQuote()->getQuoteCurrencyCode() ?: $store->getBaseCurrencyCode();
}
/**
* @param $store
* @return int|mixed|string
*/
protected function _getCurrentCountryCode($store)
{
// if fixed countryCode is setup in config use this
$countryCode = $this->_adyenHelper->getAdyenHppConfigData('country_code', $store->getId());
if ($countryCode != "") {
return $countryCode;
}
if ($country = $this->_getQuote()->getBillingAddress()->getCountry()) {
return $country;
}
$defaultCountry = $this->_config->getValue(
\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);
if ($defaultCountry) {
return $defaultCountry;
}
return "";
}
/**
* @param $store
* @return mixed|string
*/
protected function _getCurrentLocaleCode($store)
{
$localeCode = $this->_adyenHelper->getAdyenAbstractConfigData('shopper_locale', $store->getId());
if ($localeCode != "") {
return $localeCode;
}
$locale = $this->_localeResolver->getLocale();
if ($locale) {
return $locale;
}
// should have the vulue if not fall back to default
$localeCode = $this->_config->getValue(
Data::XML_PATH_DEFAULT_LOCALE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);
return $localeCode;
}
/**
* @var array
*/
protected $_fieldMapPaymentMethod = [
'name' => 'title'
];
/**
* @param $paymentMethod
* @return mixed
*/
protected function _fieldMapPaymentMethod($paymentMethod)
{
foreach ($this->_fieldMapPaymentMethod as $field => $newField) {
if (isset($paymentMethod[$field])) {
$paymentMethod[$newField] = $paymentMethod[$field];
unset($paymentMethod[$field]);
}
}
return $paymentMethod;
}
/**
* @param $requestParams
* @param $store
* @return array
* @throws \Adyen\AdyenException
*/
protected function _getDirectoryLookupResponse($requestParams, $store)
{
$cacheKey = $this->_getCacheKeyForRequest($requestParams, $store);
// initialize the adyen client
$client = new \Adyen\Client();
if ($this->_adyenHelper->isDemoMode()) {
$client->setEnvironment(\Adyen\Environment::TEST);
} else {
$client->setEnvironment(\Adyen\Environment::LIVE);
}
// connect to magento log
$client->setLogger($this->_adyenLogger);
$hmacKey = $this->_adyenHelper->getHmac();
// create and add signature
try {
$requestParams["merchantSig"] = \Adyen\Util\Util::calculateSha256Signature($hmacKey, $requestParams);
} catch (\Adyen\AdyenException $e) {
$this->_adyenLogger->error($e->getMessage());
// return empty result
return [];
}
// initialize service
$service = new \Adyen\Service\DirectoryLookup($client);
try {
$responseData = $service->directoryLookup($requestParams);
} catch (\Adyen\AdyenException $e) {
$this->_adyenLogger->error(
"The Directory Lookup response is empty check your Adyen configuration in Magento."
);
// return empty result
return [];
}
return $responseData;
}
/**
* @var array
*/
protected $_cacheParams = array(
'currencyCode',
'merchantReference',
'skinCode',
'merchantAccount',
'countryCode',
'shopperLocale',
);
/**
* @param $requestParams
* @param $store
* @return string
*/
protected function _getCacheKeyForRequest($requestParams, $store)
{
$cacheParams = [];
$cacheParams['store'] = $store->getId();
foreach ($this->_cacheParams as $paramKey) {
if (isset($requestParams[$paramKey])) {
$cacheParams[$paramKey] = $requestParams[$paramKey];
}
}
return md5(implode('|', $cacheParams));
}
/**
* @return \Magento\Quote\Model\Quote
*/
protected function _getQuote()
{
return $this->_session->getQuote();
}
/**
* Create a file asset that's subject of fallback system
*
* @param string $fileId
* @param array $params
* @return \Magento\Framework\View\Asset\File
*/
protected function _createAsset($fileId, array $params = [])
{
$params = array_merge(['_secure' => $this->request->isSecure()], $params);
return $this->assetRepo->createAsset($fileId, $params);
}
} }
\ No newline at end of file
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Model;
class AdyenPaymentMethodManagement implements \Adyen\Payment\Api\AdyenPaymentMethodManagementInterface
{
/**
* @var \Adyen\Payment\Helper\PaymentMethods
*/
protected $_paymentMethodsHelper;
/**
* AdyenPaymentMethodManagement constructor.
*
* @param \Adyen\Payment\Helper\PaymentMethods $paymentMethodsHelper
*/
public function __construct(
\Adyen\Payment\Helper\PaymentMethods $paymentMethodsHelper
) {
$this->_paymentMethodsHelper = $paymentMethodsHelper;
}
/**
* {@inheritDoc}
*/
public function getPaymentMethods($cartId, \Magento\Quote\Api\Data\AddressInterface $shippingAddress = null)
{
// if shippingAddress is provided use this country
$country = null;
if ($shippingAddress) {
$country = $shippingAddress->getCountryId();
}
return $this->_paymentMethodsHelper->getPaymentMethods($cartId, $country);
}
}
\ No newline at end of file
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Model;
class GuestAdyenPaymentMethodManagement implements \Adyen\Payment\Api\GuestAdyenPaymentMethodManagementInterface
{
/**
* @var \Magento\Quote\Model\QuoteIdMaskFactory
*/
protected $_quoteIdMaskFactory;
/**
* @var \Adyen\Payment\Helper\PaymentMethods
*/
protected $_paymentMethodsHelper;
/**
* GuestAdyenPaymentMethodManagement constructor.
*
* @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
* @param \Adyen\Payment\Helper\PaymentMethods $paymentMethodsHelper
*/
public function __construct(
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
\Adyen\Payment\Helper\PaymentMethods $paymentMethodsHelper
) {
$this->_quoteIdMaskFactory = $quoteIdMaskFactory;
$this->_paymentMethodsHelper = $paymentMethodsHelper;
}
/**
* {@inheritDoc}
*/
public function getPaymentMethods($cartId, \Magento\Quote\Api\Data\AddressInterface $shippingAddress = null)
{
$quoteIdMask = $this->_quoteIdMaskFactory->create()->load($cartId, 'masked_id');
$quoteId = $quoteIdMask->getQuoteId();
// if shippingAddress is provided use this country
$country = null;
if ($shippingAddress) {
$country = $shippingAddress->getCountryId();
}
return $this->_paymentMethodsHelper->getPaymentMethods($quoteId, $country);
}
}
\ No newline at end of file
...@@ -87,4 +87,6 @@ ...@@ -87,4 +87,6 @@
<argument name="dataStorage" xsi:type="object">adyenPaymentConfigDataStorage</argument> <argument name="dataStorage" xsi:type="object">adyenPaymentConfigDataStorage</argument>
</arguments> </arguments>
</type> </type>
<preference for="Adyen\Payment\Api\GuestAdyenPaymentMethodManagementInterface" type="Adyen\Payment\Model\GuestAdyenPaymentMethodManagement" />
<preference for="Adyen\Payment\Api\AdyenPaymentMethodManagementInterface" type="Adyen\Payment\Model\AdyenPaymentMethodManagement" />
</config> </config>
\ No newline at end of file
<?xml version="1.0"?>
<!--
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/guest-carts/:cartId/retrieve-adyen-payment-methods" method="POST">
<service class="Adyen\Payment\Api\GuestAdyenPaymentMethodManagementInterface" method="getPaymentMethods"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>
<route url="/V1/carts/mine/retrieve-adyen-payment-methods" method="POST">
<service class="Adyen\Payment\Api\AdyenPaymentMethodManagementInterface" method="getPaymentMethods"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="cartId" force="true">%cart_id%</parameter>
</data>
</route>
</routes>
\ No newline at end of file
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
define(
[
'ko'
],
function(ko) {
'use strict';
return ko.observableArray([]);
}
);
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
define(
[
'underscore',
'Magento_Checkout/js/model/quote',
'Adyen_Payment/js/model/adyen-method-list',
],
function (_, quote, methodList) {
'use strict';
return {
/**
* Populate the list of payment methods
* @param {Array} methods
*/
setPaymentMethods: function (methods) {
methodList(methods);
},
/**
* Get the list of available payment methods.
* @returns {Array}
*/
getAvailablePaymentMethods: function () {
return methodList();
}
};
}
);
...@@ -30,9 +30,14 @@ define( ...@@ -30,9 +30,14 @@ define(
'Magento_Checkout/js/action/select-payment-method', 'Magento_Checkout/js/action/select-payment-method',
'Magento_Checkout/js/model/quote', 'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/checkout-data', 'Magento_Checkout/js/checkout-data',
'Magento_Checkout/js/model/payment/additional-validators' 'Magento_Checkout/js/model/payment/additional-validators',
'mage/storage',
'Magento_Checkout/js/model/url-builder',
'Adyen_Payment/js/model/adyen-payment-service',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/model/full-screen-loader'
], ],
function (ko, $, Component, setPaymentMethodAction, selectPaymentMethodAction, quote, checkoutData, additionalValidators) { function (ko, $, Component, setPaymentMethodAction, selectPaymentMethodAction, quote, checkoutData, additionalValidators, storage, urlBuilder, adyenPaymentService, customer, fullScreenLoader) {
'use strict'; 'use strict';
var brandCode = ko.observable(null); var brandCode = ko.observable(null);
var paymentMethod = ko.observable(null); var paymentMethod = ko.observable(null);
...@@ -51,14 +56,53 @@ define( ...@@ -51,14 +56,53 @@ define(
]); ]);
return this; return this;
}, },
initialize: function () {
this._super();
fullScreenLoader.startLoader();
// reset variable:
adyenPaymentService.setPaymentMethods();
// retrieve payment methods
var serviceUrl,
payload;
if(customer.isLoggedIn()) {
serviceUrl = urlBuilder.createUrl('/carts/mine/retrieve-adyen-payment-methods', {});
} else {
serviceUrl = urlBuilder.createUrl('/guest-carts/:cartId/retrieve-adyen-payment-methods', {
cartId: quote.getQuoteId()
});
}
payload = {
cartId: quote.getQuoteId(),
shippingAddress: quote.shippingAddress()
};
storage.post(
serviceUrl, JSON.stringify(payload)
).done(
function (response) {
adyenPaymentService.setPaymentMethods(response);
fullScreenLoader.stopLoader();
}
).fail(function(error) {
console.log(JSON.stringify(error));
fullScreenLoader.stopLoader();
});
},
getAdyenHppPaymentMethods: function() { getAdyenHppPaymentMethods: function() {
var self = this; var self = this;
// convert to list so you can iterate
var paymentList = _.map(window.checkoutConfig.payment.adyenHpp.paymentMethods, function(value, key) {
if(key == "ideal") { var paymentMethods = adyenPaymentService.getAvailablePaymentMethods();
var paymentList = _.map(paymentMethods, function(value) {
if(value.brandCode == "ideal") {
return { return {
'value': key, 'value': value.brandCode,
'name': value, 'name': value,
'method': self.item.method, 'method': self.item.method,
'issuerIds': value.issuers, 'issuerIds': value.issuers,
...@@ -72,7 +116,7 @@ define( ...@@ -72,7 +116,7 @@ define(
} }
} else { } else {
return { return {
'value': key, 'value': value.brandCode,
'name': value, 'name': value,
'method': self.item.method, 'method': self.item.method,
getCode: function() { getCode: function() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment