woocommerce-paypal-payments/modules/ppcp-axo/resources/js/Views/CardView.js

115 lines
4.3 KiB
JavaScript
Raw Normal View History

2024-03-12 09:23:42 +00:00
import FormFieldGroup from "../Components/FormFieldGroup";
2024-03-08 17:41:21 +00:00
class CardView {
2024-03-12 09:23:42 +00:00
constructor(selector, elements, manager) {
this.el = elements;
2024-03-08 17:41:21 +00:00
this.manager = manager;
2024-04-10 18:27:21 +01:00
this.group = new FormFieldGroup({
2024-03-08 17:41:21 +00:00
baseSelector: '.ppcp-axo-payment-container',
contentSelector: selector,
template: (data) => {
const selectOtherPaymentMethod = () => {
if (!this.manager.hideGatewaySelection) {
return '';
}
2024-03-12 09:23:42 +00:00
return `<p style="margin-top: 40px; text-align: center;"><a href="javascript:void(0)" ${this.el.showGatewaySelectionLink.attributes}>Select other payment method</a></p>`;
2024-03-08 17:41:21 +00:00
};
if (data.isEmpty()) {
return `
<div style="margin-bottom: 20px; text-align: center;">
2024-03-14 10:54:15 +00:00
<div style="border:2px solid #cccccc; border-radius: 10px; padding: 26px 20px; margin-bottom: 20px; background-color:#f6f6f6">
<div>Please fill in your card details.</div>
</div>
<h4><a href="javascript:void(0)" ${this.el.changeCardLink.attributes}>Add card details</a></h4>
2024-03-08 17:41:21 +00:00
${selectOtherPaymentMethod()}
</div>
`;
}
2024-03-14 10:54:15 +00:00
const expiry = data.value('expiry').split('-');
const cardIcons = {
2024-03-14 16:00:37 +00:00
'VISA': 'visa-dark.svg',
'MASTER_CARD': 'mastercard-dark.svg',
'AMEX': 'amex.svg',
'DISCOVER': 'discover.svg',
2024-03-14 10:54:15 +00:00
};
2024-03-08 17:41:21 +00:00
return `
<div style="margin-bottom: 20px;">
2024-03-14 10:54:15 +00:00
<h3>Card Details <a href="javascript:void(0)" ${this.el.changeCardLink.attributes} style="margin-left: 20px;">Edit</a></h3>
<div style="border:2px solid #cccccc; border-radius: 10px; padding: 16px 20px; background-color:#f6f6f6">
<div style="float: right;">
<img
class="ppcp-card-icon"
title="${data.value('brand')}"
src="/wp-content/plugins/woocommerce-paypal-payments/modules/ppcp-wc-gateway/assets/images/${cardIcons[data.value('brand')]}"
alt="${data.value('brand')}"
>
</div>
<div style="font-family: monospace; font-size: 1rem; margin-top: 10px;">${data.value('lastDigits') ? '**** **** **** ' + data.value('lastDigits'): ''}</div>
<div>${expiry[1]}/${expiry[0]}</div>
<div style="text-transform: uppercase">${data.value('name')}</div>
</div>
2024-03-08 17:41:21 +00:00
${selectOtherPaymentMethod()}
</div>
`;
},
fields: {
brand: {
'valuePath': 'card.paymentSource.card.brand',
},
expiry: {
'valuePath': 'card.paymentSource.card.expiry',
},
lastDigits: {
'valuePath': 'card.paymentSource.card.lastDigits',
},
name: {
'valuePath': 'card.paymentSource.card.name',
},
}
});
}
activate() {
2024-04-10 18:27:21 +01:00
this.group.activate();
2024-03-08 17:41:21 +00:00
}
deactivate() {
2024-04-10 18:27:21 +01:00
this.group.deactivate();
2024-03-08 17:41:21 +00:00
}
refresh() {
2024-04-10 18:27:21 +01:00
this.group.refresh();
2024-03-08 17:41:21 +00:00
}
setData(data) {
2024-04-10 18:27:21 +01:00
this.group.setData(data);
}
2024-04-15 17:20:02 +01:00
toSubmitData(data) {
2024-04-10 18:27:21 +01:00
const name = this.group.dataValue('name');
const { firstName, lastName } = this.splitName(name);
2024-04-17 10:14:27 +01:00
data['billing_first_name'] = firstName;
data['billing_last_name'] = lastName ? lastName : firstName;
2024-04-10 18:27:21 +01:00
2024-04-15 17:20:02 +01:00
return this.group.toSubmitData(data);
2024-04-10 18:27:21 +01:00
}
splitName(fullName) {
let nameParts = fullName.trim().split(' ');
let firstName = nameParts[0];
let lastName = nameParts.length > 1 ? nameParts[nameParts.length - 1] : '';
return { firstName, lastName };
2024-03-08 17:41:21 +00:00
}
}
export default CardView;