We will be off from 27/1 (Monday) to 31/1 (Friday) (GMT +7) for our Tet Holiday (Lunar New Year) in our country

Commit a2a393c6 authored by cyattilakiss's avatar cyattilakiss Committed by GitHub

Merge pull request #472 from Adyen/PW-1385

PW-1385 Refresh payment methods on country change #461
parents 301965fc 6c204f96
...@@ -7,8 +7,11 @@ define( ...@@ -7,8 +7,11 @@ define(
'underscore', 'underscore',
'Magento_Checkout/js/model/quote', 'Magento_Checkout/js/model/quote',
'Adyen_Payment/js/model/adyen-method-list', 'Adyen_Payment/js/model/adyen-method-list',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/model/url-builder',
'mage/storage'
], ],
function (_, quote, methodList) { function (_, quote, methodList, customer, urlBuilder, storage) {
'use strict'; 'use strict';
return { return {
...@@ -25,6 +28,43 @@ define( ...@@ -25,6 +28,43 @@ define(
*/ */
getAvailablePaymentMethods: function () { getAvailablePaymentMethods: function () {
return methodList(); return methodList();
},
/**
* Retrieve the list of available payment methods from the server
*/
retrieveAvailablePaymentMethods: function (callback = null) {
var self = this;
// 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) {
self.setPaymentMethods(response);
if (callback !== null) {
callback();
}
}
).fail(
function (response) {
self.setPaymentMethods([]);
}
)
} }
}; };
} }
......
...@@ -42,6 +42,7 @@ define( ...@@ -42,6 +42,7 @@ 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;
/** /**
* Shareble adyen checkout component * Shareble adyen checkout component
* @type {AdyenCheckout} * @type {AdyenCheckout}
...@@ -89,74 +90,62 @@ define( ...@@ -89,74 +90,62 @@ define(
// reset variable: // reset variable:
adyenPaymentService.setPaymentMethods(); adyenPaymentService.setPaymentMethods();
adyenPaymentService.retrieveAvailablePaymentMethods(function() {
let 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'
};
// retrieve payment methods // Load Ratepay script
var serviceUrl, var ratepayScriptTag = document.createElement('script');
payload; ratepayScriptTag.src = "//d.ratepay.com/" + ratePayId + "/di.js";
if (customer.isLoggedIn()) { ratepayScriptTag.type = "text/javascript";
serviceUrl = urlBuilder.createUrl('/carts/mine/retrieve-adyen-payment-methods', {}); document.body.appendChild(ratepayScriptTag);
} 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);
if (JSON.stringify(response).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);
}
// create component needs to be in initialize method // create component needs to be in initialize method
var messageComponents = {}; var messageComponents = {};
_.map(response, function (value) { _.map(paymentMethods, function (value) {
var messageContainer = new Messages(); var messageContainer = new Messages();
var name = 'messages-' + self.getBrandCodeFromPaymentMethod(value); var name = 'messages-' + self.getBrandCodeFromPaymentMethod(value);
var messagesComponent = { var messagesComponent = {
parent: self.name, parent: self.name,
name: 'messages-' + self.getBrandCodeFromPaymentMethod(value), name: 'messages-' + self.getBrandCodeFromPaymentMethod(value),
displayArea: 'messages-' + self.getBrandCodeFromPaymentMethod(value), displayArea: 'messages-' + self.getBrandCodeFromPaymentMethod(value),
component: 'Magento_Ui/js/view/messages', component: 'Magento_Ui/js/view/messages',
config: { config: {
messageContainer: messageContainer messageContainer: messageContainer
} }
}; };
layout([messagesComponent]); layout([messagesComponent]);
messageComponents[name] = messageContainer; messageComponents[name] = messageContainer;
}); });
self.messageComponents = messageComponents; self.messageComponents = messageComponents;
fullScreenLoader.stopLoader();
}
).fail(function (error) {
fullScreenLoader.stopLoader(); fullScreenLoader.stopLoader();
}); });
}, },
getAdyenHppPaymentMethods: function () { getAdyenHppPaymentMethods: function () {
var self = this; var self = this;
let currentShippingAddressCountryCode = quote.shippingAddress().countryId;
// retrieve new payment methods if country code changed
if (shippingAddressCountryCode != currentShippingAddressCountryCode) {
fullScreenLoader.startLoader();
adyenPaymentService.retrieveAvailablePaymentMethods();
shippingAddressCountryCode = currentShippingAddressCountryCode;
fullScreenLoader.stopLoader();
}
var paymentMethods = adyenPaymentService.getAvailablePaymentMethods(); var paymentMethods = adyenPaymentService.getAvailablePaymentMethods();
var paymentList = _.map(paymentMethods, function (value) { var paymentList = _.map(paymentMethods, function (value) {
......
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