Commit 643fe507 authored by attilak's avatar attilak

[WIP] retrieve the payment methods is outsourced to the adyen-method.js

Send back the whole paymentMethods response and don't parse it or add
new fields into it so it can be used for stored payment methods
Json encode from the backend so we have the first level keys and decode
on the frontend
See TODOs to improve code quality
parent 86ce671a
......@@ -90,6 +90,11 @@ class PaymentMethods extends AbstractHelper
*/
protected $quote;
/**
* @var \Magento\Checkout\Model\PaymentDetailsFactory
*/
protected $paymentDetailsFactory;
/**
* PaymentMethods constructor.
*
......@@ -97,13 +102,14 @@ class PaymentMethods extends AbstractHelper
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param Data $adyenHelper
* @param \Magento\Checkout\Model\Session $session
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
* @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
* @param \Magento\Framework\View\DesignInterface $design
* @param \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
* @param \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
......@@ -116,7 +122,8 @@ class PaymentMethods extends AbstractHelper
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\View\Asset\Source $assetSource,
\Magento\Framework\View\DesignInterface $design,
\Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
\Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider,
\Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory
) {
$this->quoteRepository = $quoteRepository;
$this->config = $config;
......@@ -129,6 +136,7 @@ class PaymentMethods extends AbstractHelper
$this->assetSource = $assetSource;
$this->design = $design;
$this->themeProvider = $themeProvider;
$this->paymentDetailsFactory = $paymentDetailsFactory;
}
/**
......@@ -192,6 +200,16 @@ class PaymentMethods extends AbstractHelper
$responseData = $this->getPaymentMethodsResponse($adyFields, $store);
// TODO this should be the implemented with the PaymentDetailsInterface but for now we json encode it and
// on the fronend we decode it so we can move forward
// /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
// $paymentDetails = $this->paymentDetailsFactory->create();
// $paymentDetails->setPaymentMethods($responseData);
//$paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
//return $paymentDetails;
return json_encode($responseData);
$paymentMethods = [];
if (isset($responseData['paymentMethods'])) {
foreach ($responseData['paymentMethods'] as $paymentMethod) {
......@@ -200,9 +218,10 @@ class PaymentMethods extends AbstractHelper
$paymentMethod = $this->fieldMapPaymentMethod($paymentMethod);
// check if payment method is an openinvoice method
$paymentMethod['isPaymentMethodOpenInvoiceMethod'] =
$this->adyenHelper->isPaymentMethodOpenInvoiceMethod($paymentMethodCode);
//$paymentMethod['isPaymentMethodOpenInvoiceMethod'] =
//$this->adyenHelper->isPaymentMethodOpenInvoiceMethod($paymentMethodCode);
//TODO create the icons on the frontend or in a separate function but not here!
// add icon location in result
if ($this->adyenHelper->showLogos()) {
// Fix for MAGETWO-70402 https://github.com/magento/magento2/pull/7686
......
......@@ -59,6 +59,8 @@ class GuestAdyenPaymentMethodManagement implements \Adyen\Payment\Api\GuestAdyen
// if shippingAddress is provided use this country
$country = null;
// TODO check why the frontend doesn't have the shipping address
// or get it from the cart
if ($shippingAddress) {
$country = $shippingAddress->getCountryId();
}
......
......@@ -55,7 +55,10 @@ define(
serviceUrl, JSON.stringify(payload)
).done(
function (response) {
self.setPaymentMethods(response);
var jsonResponse = JSON.parse(response);
console.log(jsonResponse);
self.setPaymentMethods(jsonResponse);
if (callback) {
callback();
}
......
......@@ -24,11 +24,13 @@
define(
[
'uiComponent',
'Magento_Checkout/js/model/payment/renderer-list'
'Magento_Checkout/js/model/payment/renderer-list',
'Adyen_Payment/js/model/adyen-payment-service'
],
function (
Component,
rendererList
rendererList,
adyenPaymentService
) {
'use strict';
rendererList.push(
......@@ -67,6 +69,32 @@ define(
var self = this;
this._super();
// reset variable:
adyenPaymentService.setPaymentMethods();
adyenPaymentService.retrieveAvailablePaymentMethods(function () {
var paymentMethods = adyenPaymentService.getAvailablePaymentMethods();
console.log(paymentMethods);
if (!!window.checkoutConfig.payment.adyenHpp) {
if (JSON.stringify(paymentMethods).indexOf("ratepay") > -1) {
var ratePayId = window.checkoutConfig.payment.adyenHpp.ratePayId;
var dfValueRatePay = self.getRatePayDeviceIdentToken();
window.di = {
t: dfValueRatePay.replace(':', ''),
v: ratePayId,
l: 'Checkout'
};
// Load Ratepay script
var ratepayScriptTag = document.createElement('script');
ratepayScriptTag.src = "//d.ratepay.com/" + ratePayId + "/di.js";
ratepayScriptTag.type = "text/javascript";
document.body.appendChild(ratepayScriptTag);
}
}
});
// include checkout card component javascript
var checkoutCardComponentScriptTag = document.createElement('script');
checkoutCardComponentScriptTag.id = "AdyenCheckoutCardComponentScript";
......@@ -86,7 +114,10 @@ define(
},
isGooglePayEnabled: function() {
return window.checkoutConfig.payment.adyenGooglePay.active;
}
},
getRatePayDeviceIdentToken: function () {
return window.checkoutConfig.payment.adyenHpp.deviceIdentToken;
},
});
}
);
\ No newline at end of file
);
......@@ -29,8 +29,8 @@ define(
'Magento_Checkout/js/checkout-data',
'Magento_Checkout/js/model/payment/additional-validators',
'mage/storage',
'Magento_Checkout/js/model/url-builder',
'Adyen_Payment/js/model/adyen-payment-service',
'Magento_Checkout/js/model/url-builder',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/action/place-order',
......@@ -38,7 +38,7 @@ define(
'Magento_Ui/js/model/messages',
'Adyen_Payment/js/bundle'
],
function (ko, $, Component, selectPaymentMethodAction, quote, checkoutData, additionalValidators, storage, urlBuilder, adyenPaymentService, customer, fullScreenLoader, placeOrderAction, layout, Messages, AdyenComponent) {
function (ko, $, Component, selectPaymentMethodAction, quote, checkoutData, additionalValidators, storage, adyenPaymentService, urlBuilder, customer, fullScreenLoader, placeOrderAction, layout, Messages, AdyenComponent) {
'use strict';
var brandCode = ko.observable(null);
var paymentMethod = ko.observable(null);
......@@ -85,51 +85,31 @@ define(
*/
self.checkoutComponent = new AdyenCheckout({ locale: self.getLocale()});
// reset variable:
adyenPaymentService.setPaymentMethods();
adyenPaymentService.retrieveAvailablePaymentMethods(function () {
var paymentMethods = adyenPaymentService.getAvailablePaymentMethods();
if (JSON.stringify(paymentMethods).indexOf("ratepay") > -1) {
var ratePayId = window.checkoutConfig.payment.adyenHpp.ratePayId;
var dfValueRatePay = self.getRatePayDeviceIdentToken();
window.di = {
t: dfValueRatePay.replace(':', ''),
v: ratePayId,
l: 'Checkout'
};
var paymentMethods = adyenPaymentService.getAvailablePaymentMethods();
// Load Ratepay script
var ratepayScriptTag = document.createElement('script');
ratepayScriptTag.src = "//d.ratepay.com/" + ratePayId + "/di.js";
ratepayScriptTag.type = "text/javascript";
document.body.appendChild(ratepayScriptTag);
}
// create component needs to be in initialize method
var messageComponents = {};
_.map(paymentMethods, function (value) {
var messageContainer = new Messages();
var name = 'messages-' + self.getBrandCodeFromPaymentMethod(value);
var messagesComponent = {
parent: self.name,
name: 'messages-' + self.getBrandCodeFromPaymentMethod(value),
displayArea: 'messages-' + self.getBrandCodeFromPaymentMethod(value),
component: 'Magento_Ui/js/view/messages',
config: {
messageContainer: messageContainer
}
};
layout([messagesComponent]);
// create component needs to be in initialize method
var messageComponents = {};
_.map(paymentMethods, function (value) {
var messageContainer = new Messages();
var name = 'messages-' + self.getBrandCodeFromPaymentMethod(value);
var messagesComponent = {
parent: self.name,
name: 'messages-' + self.getBrandCodeFromPaymentMethod(value),
displayArea: 'messages-' + self.getBrandCodeFromPaymentMethod(value),
component: 'Magento_Ui/js/view/messages',
config: {
messageContainer: messageContainer
}
};
layout([messagesComponent]);
messageComponents[name] = messageContainer;
});
messageComponents[name] = messageContainer;
});
self.messageComponents = messageComponents;
self.messageComponents = messageComponents;
fullScreenLoader.stopLoader();
});
fullScreenLoader.stopLoader();
},
getAdyenHppPaymentMethods: function () {
var self = this;
......@@ -144,8 +124,11 @@ define(
}
var paymentMethods = adyenPaymentService.getAvailablePaymentMethods();
console.log(paymentMethods);
console.log(paymentMethods.paymentMethods);
var paymentList = _.reduce(paymentMethods, function (accumulator, value) {
console.log(value);
if (!self.isPaymentMethodSupported(value.type)) {
return accumulator;
......
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