Commit 0cfc5323 authored by attilak's avatar attilak

Fix rerendering the payment methods when shipping address changes

parent 2b4ac52d
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
*/ */
define( define(
[ [
'ko',
'underscore', 'underscore',
'Magento_Checkout/js/model/quote', 'Magento_Checkout/js/model/quote',
'Magento_Customer/js/model/customer', 'Magento_Customer/js/model/customer',
...@@ -11,9 +12,10 @@ define( ...@@ -11,9 +12,10 @@ define(
'mage/storage', 'mage/storage',
'Adyen_Payment/js/bundle', 'Adyen_Payment/js/bundle',
], ],
function (_, quote, customer, urlBuilder, storage, adyenComponent) { function (ko, _, quote, customer, urlBuilder, storage, adyenComponent) {
'use strict'; 'use strict';
var checkoutComponent = {}; var checkoutComponent = {};
var paymentMethods = ko.observable({})
return { return {
/** /**
* Retrieve the list of available payment methods from the server * Retrieve the list of available payment methods from the server
...@@ -73,10 +75,16 @@ define( ...@@ -73,10 +75,16 @@ define(
originKey: originKey, originKey: originKey,
environment: environment, environment: environment,
paymentMethodsResponse: paymentMethodsResponse, paymentMethodsResponse: paymentMethodsResponse,
showPayButton: false
}); });
paymentMethods(paymentMethodsResponse.paymentMethods);
}, },
getCheckoutComponent: function() { getCheckoutComponent: function() {
return checkoutComponent; return checkoutComponent;
},
getPaymentMethodsObservable: function() {
return paymentMethods;
} }
}; };
} }
......
...@@ -26,13 +26,15 @@ define( ...@@ -26,13 +26,15 @@ define(
'uiComponent', 'uiComponent',
'Magento_Checkout/js/model/payment/renderer-list', 'Magento_Checkout/js/model/payment/renderer-list',
'Adyen_Payment/js/model/adyen-payment-service', 'Adyen_Payment/js/model/adyen-payment-service',
'Adyen_Payment/js/model/adyen-configuration' 'Adyen_Payment/js/model/adyen-configuration',
'Magento_Checkout/js/model/quote'
], ],
function ( function (
Component, Component,
rendererList, rendererList,
adyenPaymentService, adyenPaymentService,
adyenConfiguration adyenConfiguration,
quote
) { ) {
'use strict'; 'use strict';
rendererList.push( rendererList.push(
...@@ -70,47 +72,48 @@ define( ...@@ -70,47 +72,48 @@ define(
initialize: function () { initialize: function () {
this._super(); this._super();
adyenPaymentService.retrieveAvailablePaymentMethods().done(function (response) {
var responseJson = JSON.parse(response);
var paymentMethodsResponse = responseJson.paymentMethodsResponse;
// TODO check if this is still required or if can be outsourced for the generic component, or checkout can create a ratepay component
/*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);
}
}*/
// Initialises adyen checkout main component with default configuration
adyenPaymentService.initCheckoutComponent(
paymentMethodsResponse,
adyenConfiguration.getOriginKey(),
adyenConfiguration.getLocale(),
adyenConfiguration.getCheckoutEnvironment()
);
})
if (this.isGooglePayEnabled()) { if (this.isGooglePayEnabled()) {
var googlepayscript = document.createElement('script'); var googlepayscript = document.createElement('script');
googlepayscript.src = "https://pay.google.com/gp/p/js/pay.js"; googlepayscript.src = "https://pay.google.com/gp/p/js/pay.js";
googlepayscript.type = "text/javascript"; googlepayscript.type = "text/javascript";
document.head.appendChild(googlepayscript); document.head.appendChild(googlepayscript);
} }
quote.shippingAddress.subscribe(function() {
adyenPaymentService.retrieveAvailablePaymentMethods().done(function (response) {
var responseJson = JSON.parse(response);
var paymentMethodsResponse = responseJson.paymentMethodsResponse;
// TODO check if this is still required or if can be outsourced for the generic component, or checkout can create a ratepay component
/*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);
}
}*/
// Initialises adyen checkout main component with default configuration
adyenPaymentService.initCheckoutComponent(
paymentMethodsResponse,
adyenConfiguration.getOriginKey(),
adyenConfiguration.getLocale(),
adyenConfiguration.getCheckoutEnvironment()
);
})
})
}, },
isGooglePayEnabled: function() { isGooglePayEnabled: function() {
return window.checkoutConfig.payment.adyenGooglePay.active; return window.checkoutConfig.payment.adyenGooglePay.active;
......
...@@ -60,7 +60,6 @@ define( ...@@ -60,7 +60,6 @@ define(
var brandCode = ko.observable(null); var brandCode = ko.observable(null);
var paymentMethod = ko.observable(null); var paymentMethod = ko.observable(null);
var messageComponents; var messageComponents;
var shippingAddressCountryCode = quote.shippingAddress().countryId;
var unsupportedPaymentMethods = ['scheme', 'boleto', 'bcmc_mobile_QR', 'wechatpay', /^bcmc$/, "applepay", "paywithgoogle"]; var unsupportedPaymentMethods = ['scheme', 'boleto', 'bcmc_mobile_QR', 'wechatpay', /^bcmc$/, "applepay", "paywithgoogle"];
/** /**
* Shareble adyen checkout component * Shareble adyen checkout component
...@@ -78,7 +77,8 @@ define( ...@@ -78,7 +77,8 @@ define(
initObservable: function () { initObservable: function () {
this._super() this._super()
.observe([ .observe([
'brandCode' 'brandCode',
'paymentListObservable'
]); ]);
return this; return this;
}, initialize: function () { }, initialize: function () {
...@@ -86,7 +86,7 @@ define( ...@@ -86,7 +86,7 @@ define(
var self = this; var self = this;
this._super(); this._super();
fullScreenLoader.startLoader(); var paymentMethodsObservable = adyenPaymentService.getPaymentMethodsObservable();
/** /**
* Create sherable checkout component * Create sherable checkout component
...@@ -94,8 +94,21 @@ define( ...@@ -94,8 +94,21 @@ define(
*/ */
self.checkoutComponent = adyenPaymentService.getCheckoutComponent(); self.checkoutComponent = adyenPaymentService.getCheckoutComponent();
var paymentMethods = self.checkoutComponent.paymentMethodsResponse.paymentMethods; paymentMethodsObservable.subscribe(function() {
console.log('test');
self.checkoutComponent = adyenPaymentService.getCheckoutComponent();
self.setAdyenHppPaymentMethods();
});
},
getAdyenHppPaymentMethods: function () {
return this.paymentListObservable;
},
setAdyenHppPaymentMethods: function () {
var self = this;
fullScreenLoader.startLoader();
var paymentMethods = self.checkoutComponent.paymentMethodsResponse.paymentMethods;
// create component needs to be in initialize method // create component needs to be in initialize method
var messageComponents = {}; var messageComponents = {};
...@@ -119,20 +132,6 @@ define( ...@@ -119,20 +132,6 @@ define(
self.messageComponents = messageComponents; self.messageComponents = messageComponents;
fullScreenLoader.stopLoader();
},
getAdyenHppPaymentMethods: function () {
var self = this;
var currentShippingAddressCountryCode = quote.shippingAddress().countryId;
// retrieve new payment methods if country code changed
if (shippingAddressCountryCode != currentShippingAddressCountryCode) {
fullScreenLoader.startLoader();
adyenPaymentService.retrieveAvailablePaymentMethods();
shippingAddressCountryCode = currentShippingAddressCountryCode;
fullScreenLoader.stopLoader();
}
// filter unnecessary field details from checkout component // filter unnecessary field details from checkout component
// TODO refactor // TODO refactor
self.checkoutComponent.paymentMethodsResponse.paymentMethods.forEach(function (paymentMethod, index) { self.checkoutComponent.paymentMethodsResponse.paymentMethods.forEach(function (paymentMethod, index) {
...@@ -147,6 +146,8 @@ define( ...@@ -147,6 +146,8 @@ define(
// Get paymentMethod list with filtered details // Get paymentMethod list with filtered details
var paymentMethods = self.checkoutComponent.paymentMethodsResponse.paymentMethods; var paymentMethods = self.checkoutComponent.paymentMethodsResponse.paymentMethods;
var paymentListObservable = ko.observable([]);
// Iterate through the payment methods and render them // Iterate through the payment methods and render them
var paymentList = _.reduce(paymentMethods, function (accumulator, value) { var paymentList = _.reduce(paymentMethods, function (accumulator, value) {
if (!self.isPaymentMethodSupported(value.type)) { if (!self.isPaymentMethodSupported(value.type)) {
...@@ -227,9 +228,9 @@ define( ...@@ -227,9 +228,9 @@ define(
return accumulator; return accumulator;
}, []); }, []);
return paymentList; self.paymentListObservable(paymentList);
fullScreenLoader.stopLoader();
}, },
// TODO prefill gender in components where it is available // TODO prefill gender in components where it is available
getGenderTypes: function () { getGenderTypes: function () {
return _.map(window.checkoutConfig.payment.adyenHpp.genderTypes, function (value, key) { return _.map(window.checkoutConfig.payment.adyenHpp.genderTypes, function (value, key) {
......
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