| 1 |
if (!window.SequraFE) { |
| 2 |
window.SequraFE = {}; |
| 3 |
} |
| 4 |
|
| 5 |
(function () { |
| 6 |
/** |
| 7 |
* @typedef PaymentMethod |
| 8 |
* @property {string} product |
| 9 |
* @property {string} title |
| 10 |
* @property {string | null} description |
| 11 |
* @property {number | null} minAmount |
| 12 |
* @property {number | null} maxAmount |
| 13 |
* @property {string | null} startsAt |
| 14 |
* @property {string | null} endsAt |
| 15 |
*/ |
| 16 |
|
| 17 |
/** |
| 18 |
* Handles payment methods page logic. |
| 19 |
* |
| 20 |
* @param {{ |
| 21 |
* getPaymentMethodsUrl: string, |
| 22 |
* getSellingCountriesUrl: string, |
| 23 |
* getCountrySettingsUrl: string, |
| 24 |
* getConnectionDataUrl: string |
| 25 |
* validateConnectionDataUrl: string |
| 26 |
* }} configuration |
| 27 |
* @constructor |
| 28 |
*/ |
| 29 |
function PaymentController(configuration) { |
| 30 |
const { templateService, elementGenerator: generator, components, utilities } = SequraFE; |
| 31 |
/** @type AjaxServiceType */ |
| 32 |
const api = SequraFE.ajaxService; |
| 33 |
/** @type string */ |
| 34 |
let currentStoreId = ''; |
| 35 |
/** @type Version */ |
| 36 |
let version; |
| 37 |
/** @type Store[] */ |
| 38 |
let stores; |
| 39 |
/** @type SellingCountry[] */ |
| 40 |
let sellingCountries; |
| 41 |
/** @type PaymentMethod[] */ |
| 42 |
let paymentMethods; |
| 43 |
/** @type CountrySettings[] */ |
| 44 |
let countryConfiguration; |
| 45 |
/** @type ConnectionSettings */ |
| 46 |
let connectionSettings; |
| 47 |
|
| 48 |
/** |
| 49 |
* Displays page content. |
| 50 |
* |
| 51 |
* @param {{ state?: string, storeId: string }} config |
| 52 |
*/ |
| 53 |
this.display = ({ storeId }) => { |
| 54 |
currentStoreId = storeId; |
| 55 |
templateService.clearMainPage(); |
| 56 |
|
| 57 |
stores = SequraFE.state.getData('stores'); |
| 58 |
version = SequraFE.state.getData('version'); |
| 59 |
connectionSettings = SequraFE.state.getData('connectionSettings'); |
| 60 |
countryConfiguration = SequraFE.state.getData('countrySettings'); |
| 61 |
sellingCountries = SequraFE.state.getData('sellingCountries'); |
| 62 |
paymentMethods = SequraFE.state.getData('paymentMethods'); |
| 63 |
|
| 64 |
if (paymentMethods && sellingCountries) { |
| 65 |
initializePage(); |
| 66 |
utilities.hideLoader(); |
| 67 |
|
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
Promise.all([ |
| 72 |
sellingCountries ? [] : api.get(configuration.getSellingCountriesUrl, null, SequraFE.customHeader), |
| 73 |
paymentMethods ? [] : api.get(configuration.getPaymentMethodsUrl.replace('{merchantId}', countryConfiguration[0].merchantId), null, SequraFE.customHeader), |
| 74 |
]).then(([sellingCountriesRes, paymentMethodsRes]) => { |
| 75 |
if (sellingCountriesRes.length !== 0) { |
| 76 |
sellingCountries = sellingCountriesRes; |
| 77 |
SequraFE.state.setData('sellingCountries', sellingCountriesRes) |
| 78 |
} |
| 79 |
|
| 80 |
if (paymentMethodsRes.length !== 0) { |
| 81 |
paymentMethods = paymentMethodsRes; |
| 82 |
SequraFE.state.setData('paymentMethods', paymentMethodsRes) |
| 83 |
} |
| 84 |
|
| 85 |
initializePage(); |
| 86 |
}).finally(() => utilities.hideLoader()); |
| 87 |
}; |
| 88 |
|
| 89 |
/** |
| 90 |
* Renders the page contents. |
| 91 |
*/ |
| 92 |
const initializePage = () => { |
| 93 |
const pageWrapper = document.getElementById('sq-page-wrapper'); |
| 94 |
|
| 95 |
pageWrapper.append( |
| 96 |
generator.createElement('div', 'sq-page-content-wrapper sqv--payments', '', null, [ |
| 97 |
SequraFE.components.PageHeader.create( |
| 98 |
{ |
| 99 |
currentVersion: version?.current, |
| 100 |
newVersion: { |
| 101 |
versionLabel: version?.new, |
| 102 |
versionUrl: version?.downloadNewVersionUrl |
| 103 |
}, |
| 104 |
mode: connectionSettings.environment === 'live' ? connectionSettings.environment : 'test', |
| 105 |
activeStore: currentStoreId, |
| 106 |
stores: stores.map((store) => ({ label: store.storeName, value: store.storeId })), |
| 107 |
onChange: (storeId) => { |
| 108 |
if (storeId !== SequraFE.state.getStoreId()) { |
| 109 |
SequraFE.state.setStoreId(storeId); |
| 110 |
window.location.hash = ''; |
| 111 |
SequraFE.state.display(); |
| 112 |
} |
| 113 |
}, |
| 114 |
menuItems: SequraFE.utilities.getMenuItems(SequraFE.appStates.PAYMENT) |
| 115 |
} |
| 116 |
), |
| 117 |
generator.createElement('div', 'sq-page-content', '', null, [ |
| 118 |
generator.createElement('div', 'sq-content-row', '', null, [ |
| 119 |
generator.createElement('main', 'sq-content', '', null, [ |
| 120 |
generator.createElement('div', 'sq-content-inner', '', null, [ |
| 121 |
generator.createElement('div', 'sq-table-heading', '', null, [ |
| 122 |
generator.createPageHeading({ |
| 123 |
title: 'payments.title', |
| 124 |
text: 'payments.description' |
| 125 |
}), |
| 126 |
sellingCountries.length > 0 ? generator.createDropdownField({ |
| 127 |
className: 'sqm--table-dropdown', |
| 128 |
label: 'payments.countries.label', |
| 129 |
description: 'payments.countries.description', |
| 130 |
value: countryConfiguration[0].merchantId, |
| 131 |
options: countryConfiguration.map((countrySetting) => { |
| 132 |
return { |
| 133 |
label: sellingCountries.find((country) => |
| 134 |
countrySetting.countryCode === country.code |
| 135 |
).name, value: countrySetting.merchantId |
| 136 |
} |
| 137 |
}), |
| 138 |
onChange: handleCountryChange |
| 139 |
}) : [], |
| 140 |
]), |
| 141 |
sellingCountries.length > 0 ? components.DataTable.create(getTableHeaders(), getTableRows()) : [] |
| 142 |
]), |
| 143 |
]) |
| 144 |
]) |
| 145 |
]), |
| 146 |
generator.createSupportLink() |
| 147 |
])) |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns table headers. |
| 152 |
* |
| 153 |
* @returns {TableCell[]} |
| 154 |
*/ |
| 155 |
const getTableHeaders = () => { |
| 156 |
return [ |
| 157 |
{ label: 'payments.paymentMethods', className: 'sqp-payment-method-header-cell sqm--text-left' }, |
| 158 |
{ label: 'payments.minimumAmount' }, |
| 159 |
{ label: 'payments.maximumAmount' }, |
| 160 |
{ label: 'payments.availableFrom' }, |
| 161 |
{ label: 'payments.availableTo' }, |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Returns table rows. |
| 167 |
* |
| 168 |
* @returns {TableCell[][]} |
| 169 |
*/ |
| 170 |
const getTableRows = () => { |
| 171 |
if (!paymentMethods) { |
| 172 |
return []; |
| 173 |
} |
| 174 |
return paymentMethods.map((method) => { |
| 175 |
return [ |
| 176 |
{ |
| 177 |
className: 'sqp-payment-method-cell sqm--text-left', |
| 178 |
renderer: (cell) => { |
| 179 |
const icon = method.icon ? method.icon : (SequraFE.imagesProvider.icons.payment || ''); |
| 180 |
cell.prepend( |
| 181 |
generator.createElementFromHTML(icon), |
| 182 |
generator.createElement('div', '', '', null, [ |
| 183 |
generator.createElement('h3', 'sqp-payment-method-title', method.title), |
| 184 |
generator.createElement( |
| 185 |
'p', |
| 186 |
'sqp-payment-method-description', |
| 187 |
method?.description ?? '' |
| 188 |
) |
| 189 |
]) |
| 190 |
) |
| 191 |
} |
| 192 |
}, |
| 193 |
{ label: method?.minAmount ? formatAmount(method.minAmount) : '/' }, |
| 194 |
{ label: method?.maxAmount ? formatAmount(method.maxAmount) : '/' }, |
| 195 |
{ label: method?.startsAt ? formatDate(method.startsAt) : '/' }, |
| 196 |
{ label: method?.endsAt ? formatDate(method.endsAt) : '/' } |
| 197 |
]; |
| 198 |
}); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Handles the customer country change. |
| 203 |
* |
| 204 |
* @param value |
| 205 |
*/ |
| 206 |
const handleCountryChange = (value) => { |
| 207 |
utilities.showLoader(); |
| 208 |
api.get(configuration.getPaymentMethodsUrl.replace('{merchantId}', value), null, SequraFE.customHeader) |
| 209 |
.then((methods) => { |
| 210 |
paymentMethods = [...methods]; |
| 211 |
document.querySelector('.sq-table-container').remove(); |
| 212 |
document.querySelector('.sq-content-inner')?.append( |
| 213 |
components.DataTable.create(getTableHeaders(), getTableRows()) |
| 214 |
) |
| 215 |
}) |
| 216 |
.finally(utilities.hideLoader); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Formats the given date to the required table view. |
| 221 |
* |
| 222 |
* @param date |
| 223 |
* |
| 224 |
* @returns {`${string}/${string}/${number}`} |
| 225 |
*/ |
| 226 |
const formatDate = (date) => { |
| 227 |
const dateTime = new Date(date.replace(/ /g, "T")); |
| 228 |
const day = String(dateTime.getDate()).padStart(2, '0'); |
| 229 |
const month = String(dateTime.getMonth() + 1).padStart(2, '0'); |
| 230 |
const year = dateTime.getFullYear(); |
| 231 |
|
| 232 |
return `${day}/${month}/${year}`; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Formats the given amount to the required table view. |
| 237 |
* |
| 238 |
* @param amount |
| 239 |
* |
| 240 |
* @returns {string} |
| 241 |
*/ |
| 242 |
const formatAmount = (amount) => { |
| 243 |
return (amount / 100).toLocaleString('es', { minimumFractionDigits: 2 }) + ' EUR'; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
SequraFE.PaymentController = PaymentController; |
| 248 |
})(); |
| 249 |
|