2023-08-31 12:48:01 +02:00
|
|
|
import ContextHandlerFactory from "./Context/ContextHandlerFactory";
|
2023-08-31 16:43:40 +02:00
|
|
|
import {createAppleErrors} from "./Helper/applePayError";
|
2023-09-08 12:29:23 +02:00
|
|
|
import {setVisible} from '../../../ppcp-button/resources/js/modules/Helper/Hiding';
|
|
|
|
import {setEnabled} from '../../../ppcp-button/resources/js/modules/Helper/ButtonDisabler';
|
2023-09-21 09:53:17 +02:00
|
|
|
import FormValidator from "../../../ppcp-button/resources/js/modules/Helper/FormValidator";
|
|
|
|
import ErrorHandler from '../../../ppcp-button/resources/js/modules/ErrorHandler';
|
2023-11-03 10:30:31 +00:00
|
|
|
import widgetBuilder from "../../../ppcp-button/resources/js/modules/Renderer/WidgetBuilder";
|
2023-12-11 17:14:43 +00:00
|
|
|
import {apmButtonsInit} from "../../../ppcp-button/resources/js/modules/Helper/ApmButtons";
|
2023-08-31 12:48:01 +02:00
|
|
|
|
|
|
|
class ApplepayButton {
|
|
|
|
|
|
|
|
constructor(context, externalHandler, buttonConfig, ppcpConfig) {
|
2023-12-15 17:11:16 +00:00
|
|
|
apmButtonsInit(ppcpConfig);
|
2023-12-07 18:20:58 +00:00
|
|
|
|
2023-08-31 12:48:01 +02:00
|
|
|
this.isInitialized = false;
|
|
|
|
|
|
|
|
this.context = context;
|
|
|
|
this.externalHandler = externalHandler;
|
|
|
|
this.buttonConfig = buttonConfig;
|
|
|
|
this.ppcpConfig = ppcpConfig;
|
|
|
|
this.paymentsClient = null;
|
2023-11-09 17:50:57 +00:00
|
|
|
this.formData = null;
|
2023-08-31 12:48:01 +02:00
|
|
|
|
|
|
|
this.contextHandler = ContextHandlerFactory.create(
|
|
|
|
this.context,
|
|
|
|
this.buttonConfig,
|
|
|
|
this.ppcpConfig
|
|
|
|
);
|
2023-08-31 16:43:40 +02:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
this.updatedContactInfo = []
|
2023-08-31 16:43:40 +02:00
|
|
|
this.selectedShippingMethod = []
|
2023-10-31 15:00:05 +00:00
|
|
|
this.nonce = document.getElementById('woocommerce-process-checkout-nonce')?.value || buttonConfig.nonce
|
2023-10-12 15:27:55 +01:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
// Stores initialization data sent to the button.
|
|
|
|
this.initialPaymentRequest = null;
|
|
|
|
|
2023-12-21 11:46:21 +00:00
|
|
|
// Default eligibility status.
|
|
|
|
this.isEligible = true;
|
|
|
|
|
2023-10-13 14:36:11 +01:00
|
|
|
this.log = function() {
|
2023-10-12 15:27:55 +01:00
|
|
|
if ( this.buttonConfig.is_debug ) {
|
2024-04-11 17:14:22 +01:00
|
|
|
//console.log('[ApplePayButton]', ...arguments);
|
2023-10-12 15:27:55 +01:00
|
|
|
}
|
|
|
|
}
|
2023-10-25 17:52:00 +01:00
|
|
|
|
|
|
|
this.refreshContextData();
|
2023-11-09 17:50:57 +00:00
|
|
|
|
|
|
|
// Debug helpers
|
|
|
|
jQuery(document).on('ppcp-applepay-debug', () => {
|
|
|
|
console.log('ApplePayButton', this.context, this);
|
|
|
|
});
|
|
|
|
document.ppcpApplepayButtons = document.ppcpApplepayButtons || {};
|
|
|
|
document.ppcpApplepayButtons[this.context] = this;
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init(config) {
|
|
|
|
if (this.isInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-20 18:13:09 +01:00
|
|
|
|
2023-11-13 17:36:18 +00:00
|
|
|
if (!this.contextHandler.validateContext()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('Init', this.context);
|
2023-09-08 12:29:23 +02:00
|
|
|
this.initEventHandlers();
|
2023-08-31 12:48:01 +02:00
|
|
|
this.isInitialized = true;
|
|
|
|
this.applePayConfig = config;
|
2023-12-21 11:46:21 +00:00
|
|
|
this.isEligible = (this.applePayConfig.isEligible && window.ApplePaySession) || this.buttonConfig.is_admin;
|
2023-11-09 17:50:57 +00:00
|
|
|
|
2023-12-21 11:46:21 +00:00
|
|
|
if (this.isEligible) {
|
2023-09-07 09:56:46 +02:00
|
|
|
this.fetchTransactionInfo().then(() => {
|
|
|
|
this.addButton();
|
2023-09-10 13:21:19 +02:00
|
|
|
const id_minicart = "#apple-" + this.buttonConfig.button.mini_cart_wrapper;
|
|
|
|
const id = "#apple-" + this.buttonConfig.button.wrapper;
|
|
|
|
|
2023-09-21 09:53:17 +02:00
|
|
|
if (this.context === 'mini-cart') {
|
2023-10-12 15:27:55 +01:00
|
|
|
document.querySelector(id_minicart)?.addEventListener('click', (evt) => {
|
2023-09-10 13:21:19 +02:00
|
|
|
evt.preventDefault();
|
|
|
|
this.onButtonClick();
|
|
|
|
});
|
|
|
|
} else {
|
2023-10-12 15:27:55 +01:00
|
|
|
document.querySelector(id)?.addEventListener('click', (evt) => {
|
2023-09-10 13:21:19 +02:00
|
|
|
evt.preventDefault();
|
|
|
|
this.onButtonClick();
|
|
|
|
});
|
|
|
|
}
|
2023-09-07 09:56:46 +02:00
|
|
|
});
|
2023-12-07 18:20:58 +00:00
|
|
|
} else {
|
|
|
|
jQuery('#' + this.buttonConfig.button.wrapper).hide();
|
|
|
|
jQuery('#' + this.buttonConfig.button.mini_cart_wrapper).hide();
|
|
|
|
jQuery('#express-payment-method-ppcp-applepay').hide();
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-10-20 18:13:09 +01:00
|
|
|
|
|
|
|
reinit() {
|
|
|
|
if (!this.applePayConfig) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isInitialized = false;
|
|
|
|
this.init(this.applePayConfig);
|
|
|
|
}
|
|
|
|
|
2023-09-07 09:56:46 +02:00
|
|
|
async fetchTransactionInfo() {
|
|
|
|
this.transactionInfo = await this.contextHandler.transactionInfo();
|
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
|
2023-09-08 12:29:23 +02:00
|
|
|
/**
|
|
|
|
* Returns configurations relative to this button context.
|
|
|
|
*/
|
|
|
|
contextConfig() {
|
|
|
|
let config = {
|
|
|
|
wrapper: this.buttonConfig.button.wrapper,
|
|
|
|
ppcpStyle: this.ppcpConfig.button.style,
|
2023-11-22 17:27:21 +02:00
|
|
|
buttonStyle: this.buttonConfig.button.style,
|
2023-09-08 12:29:23 +02:00
|
|
|
ppcpButtonWrapper: this.ppcpConfig.button.wrapper
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.context === 'mini-cart') {
|
|
|
|
config.wrapper = this.buttonConfig.button.mini_cart_wrapper;
|
|
|
|
config.ppcpStyle = this.ppcpConfig.button.mini_cart_style;
|
|
|
|
config.buttonStyle = this.buttonConfig.button.mini_cart_style;
|
|
|
|
config.ppcpButtonWrapper = this.ppcpConfig.button.mini_cart_wrapper;
|
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
|
2023-09-08 12:29:23 +02:00
|
|
|
if (['cart-block', 'checkout-block'].indexOf(this.context) !== -1) {
|
2023-11-23 08:46:48 +02:00
|
|
|
config.ppcpButtonWrapper = '#express-payment-method-ppcp-gateway-paypal';
|
2023-09-08 12:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
|
2023-09-08 12:29:23 +02:00
|
|
|
initEventHandlers() {
|
|
|
|
const { wrapper, ppcpButtonWrapper } = this.contextConfig();
|
2023-09-18 12:34:34 +02:00
|
|
|
const wrapper_id = '#' + wrapper;
|
2023-09-08 12:29:23 +02:00
|
|
|
|
2024-06-07 21:12:34 +02:00
|
|
|
if (wrapper_id === ppcpButtonWrapper) {
|
|
|
|
throw new Error(`[ApplePayButton] "wrapper" and "ppcpButtonWrapper" values must differ to avoid infinite loop. Current value: "${wrapper_id}"`);
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:29:23 +02:00
|
|
|
const syncButtonVisibility = () => {
|
2023-12-21 12:01:01 +00:00
|
|
|
if (!this.isEligible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:29:23 +02:00
|
|
|
const $ppcpButtonWrapper = jQuery(ppcpButtonWrapper);
|
2023-09-18 12:34:34 +02:00
|
|
|
setVisible(wrapper_id, $ppcpButtonWrapper.is(':visible'));
|
|
|
|
setEnabled(wrapper_id, !$ppcpButtonWrapper.hasClass('ppcp-disabled'));
|
2023-09-08 12:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jQuery(document).on('ppcp-shown ppcp-hidden ppcp-enabled ppcp-disabled', (ev, data) => {
|
|
|
|
if (jQuery(data.selector).is(ppcpButtonWrapper)) {
|
|
|
|
syncButtonVisibility();
|
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
});
|
2023-09-08 12:29:23 +02:00
|
|
|
|
|
|
|
syncButtonVisibility();
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-09-08 12:29:23 +02:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
/**
|
|
|
|
* Starts an ApplePay session.
|
|
|
|
*/
|
2023-08-31 16:43:40 +02:00
|
|
|
applePaySession(paymentRequest) {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('applePaySession', paymentRequest);
|
2023-11-09 17:50:57 +00:00
|
|
|
const session = new ApplePaySession(4, paymentRequest);
|
|
|
|
session.begin();
|
2023-09-08 11:04:24 +02:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
if (this.shouldRequireShippingInButton()) {
|
|
|
|
session.onshippingmethodselected = this.onShippingMethodSelected(session);
|
|
|
|
session.onshippingcontactselected = this.onShippingContactSelected(session);
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
session.onvalidatemerchant = this.onValidateMerchant(session);
|
|
|
|
session.onpaymentauthorized = this.onPaymentAuthorized(session);
|
2023-09-21 09:53:17 +02:00
|
|
|
return session;
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-09 17:50:57 +00:00
|
|
|
* Adds an Apple Pay purchase button.
|
2023-08-31 12:48:01 +02:00
|
|
|
*/
|
2023-08-31 16:43:40 +02:00
|
|
|
addButton() {
|
2023-11-03 10:30:31 +00:00
|
|
|
this.log('addButton', this.context);
|
|
|
|
|
2023-11-22 17:27:21 +02:00
|
|
|
const { wrapper, ppcpStyle } = this.contextConfig();
|
|
|
|
|
2023-11-03 10:30:31 +00:00
|
|
|
const appleContainer = document.getElementById(wrapper);
|
2023-09-10 13:21:19 +02:00
|
|
|
const type = this.buttonConfig.button.type;
|
|
|
|
const language = this.buttonConfig.button.lang;
|
|
|
|
const color = this.buttonConfig.button.color;
|
|
|
|
const id = "apple-" + wrapper;
|
2023-10-12 15:27:55 +01:00
|
|
|
|
|
|
|
if (appleContainer) {
|
|
|
|
appleContainer.innerHTML = `<apple-pay-button id="${id}" buttonstyle="${color}" type="${type}" locale="${language}">`;
|
|
|
|
}
|
2023-09-10 13:21:19 +02:00
|
|
|
|
2023-12-07 18:20:58 +00:00
|
|
|
const $wrapper = jQuery('#' + wrapper);
|
|
|
|
$wrapper.addClass('ppcp-button-' + ppcpStyle.shape);
|
2023-11-22 17:27:21 +02:00
|
|
|
|
|
|
|
if (ppcpStyle.height) {
|
2023-12-07 18:20:58 +00:00
|
|
|
$wrapper.css('--apple-pay-button-height', `${ppcpStyle.height}px`)
|
|
|
|
$wrapper.css('height', `${ppcpStyle.height}px`)
|
2023-11-22 17:27:21 +02:00
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------
|
|
|
|
// Button click
|
|
|
|
//------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show Apple Pay payment sheet when Apple Pay payment button is clicked
|
|
|
|
*/
|
2023-09-21 09:53:17 +02:00
|
|
|
async onButtonClick() {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onButtonClick', this.context);
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
const paymentRequest = this.paymentRequest();
|
|
|
|
|
2024-01-24 08:47:48 +00:00
|
|
|
window.ppcpFundingSource = 'apple_pay'; // Do this on another place like on create order endpoint handler.
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
// Trigger woocommerce validation if we are in the checkout page.
|
2023-09-21 09:53:17 +02:00
|
|
|
if (this.context === 'checkout') {
|
|
|
|
const checkoutFormSelector = 'form.woocommerce-checkout';
|
|
|
|
const errorHandler = new ErrorHandler(
|
|
|
|
PayPalCommerceGateway.labels.error.generic,
|
|
|
|
document.querySelector('.woocommerce-notices-wrapper')
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
const formData = new FormData(document.querySelector(checkoutFormSelector));
|
2023-11-09 17:50:57 +00:00
|
|
|
this.formData = Object.fromEntries(formData.entries());
|
|
|
|
|
|
|
|
this.updateRequestDataWithForm(paymentRequest);
|
2023-09-21 09:53:17 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2023-11-06 10:15:56 +00:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
this.log('=== paymentRequest', paymentRequest);
|
2023-11-06 10:15:56 +00:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
const session = this.applePaySession(paymentRequest);
|
2023-09-21 09:53:17 +02:00
|
|
|
const formValidator = PayPalCommerceGateway.early_checkout_validation_enabled ?
|
|
|
|
new FormValidator(
|
|
|
|
PayPalCommerceGateway.ajax.validate_checkout.endpoint,
|
|
|
|
PayPalCommerceGateway.ajax.validate_checkout.nonce,
|
|
|
|
) : null;
|
|
|
|
if (formValidator) {
|
|
|
|
try {
|
|
|
|
const errors = await formValidator.validate(document.querySelector(checkoutFormSelector));
|
|
|
|
if (errors.length > 0) {
|
|
|
|
errorHandler.messages(errors);
|
|
|
|
jQuery( document.body ).trigger( 'checkout_error' , [ errorHandler.currentHtml() ] );
|
|
|
|
session.abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
|
|
|
|
// Default session initialization.
|
|
|
|
this.applePaySession(paymentRequest);
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 18:07:19 +00:00
|
|
|
/**
|
|
|
|
* If the button should show the shipping fields.
|
|
|
|
*
|
|
|
|
* @returns {false|*}
|
|
|
|
*/
|
2023-11-09 17:50:57 +00:00
|
|
|
shouldRequireShippingInButton() {
|
2023-11-08 18:07:19 +00:00
|
|
|
return this.contextHandler.shippingAllowed()
|
|
|
|
&& this.buttonConfig.product.needShipping
|
2023-11-09 17:50:57 +00:00
|
|
|
&& (this.context !== 'checkout' || this.shouldUpdateButtonWithFormData());
|
2023-11-08 18:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the button should be updated with the form addresses.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
shouldUpdateButtonWithFormData() {
|
2023-11-09 17:50:57 +00:00
|
|
|
if (this.context !== 'checkout') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.buttonConfig?.preferences?.checkout_data_mode === 'use_applepay';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates how payment completion should be handled if with the context handler default actions.
|
|
|
|
* Or with ApplePay module specific completion.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
shouldCompletePaymentWithContextHandler() {
|
|
|
|
// Data already handled, ex: PayNow
|
|
|
|
if (!this.contextHandler.shippingAllowed()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Use WC form data mode in Checkout.
|
|
|
|
if (this.context === 'checkout' && !this.shouldUpdateButtonWithFormData()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2023-11-08 18:07:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
/**
|
|
|
|
* Updates ApplePay paymentRequest with form data.
|
|
|
|
*/
|
|
|
|
updateRequestDataWithForm(paymentRequest) {
|
2023-11-08 18:07:19 +00:00
|
|
|
if (!this.shouldUpdateButtonWithFormData()) {
|
2023-09-21 09:53:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-11-08 18:07:19 +00:00
|
|
|
|
|
|
|
// Add billing address.
|
2023-11-09 17:50:57 +00:00
|
|
|
paymentRequest.billingContact = this.fillBillingContact(this.formData);
|
2023-11-06 10:15:56 +00:00
|
|
|
|
2023-11-08 18:07:19 +00:00
|
|
|
// Add custom data.
|
2023-11-06 10:15:56 +00:00
|
|
|
// "applicationData" is originating a "PayPalApplePayError: An internal server error has occurred" on paypal.Applepay().confirmOrder().
|
2023-11-09 17:50:57 +00:00
|
|
|
// paymentRequest.applicationData = this.fillApplicationData(this.formData);
|
2023-11-06 10:15:56 +00:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
if (!this.shouldRequireShippingInButton()) {
|
2023-09-21 09:53:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-11-08 18:07:19 +00:00
|
|
|
|
|
|
|
// Add shipping address.
|
2023-11-09 17:50:57 +00:00
|
|
|
paymentRequest.shippingContact = this.fillShippingContact(this.formData);
|
2023-11-06 10:15:56 +00:00
|
|
|
|
2023-11-08 18:07:19 +00:00
|
|
|
// Get shipping methods.
|
2023-11-06 10:15:56 +00:00
|
|
|
const rate = this.transactionInfo.chosenShippingMethods[0];
|
2023-11-09 17:50:57 +00:00
|
|
|
paymentRequest.shippingMethods = [];
|
2023-11-08 18:07:19 +00:00
|
|
|
|
|
|
|
// Add selected shipping method.
|
2023-11-06 10:15:56 +00:00
|
|
|
for (const shippingPackage of this.transactionInfo.shippingPackages) {
|
|
|
|
if (rate === shippingPackage.id) {
|
2023-11-09 17:50:57 +00:00
|
|
|
const shippingMethod = {
|
2023-11-06 10:15:56 +00:00
|
|
|
'label' : shippingPackage.label,
|
|
|
|
'detail' : '',
|
|
|
|
'amount' : shippingPackage.cost_str,
|
|
|
|
'identifier' : shippingPackage.id,
|
2023-11-09 17:50:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Remember this shipping method as the selected one.
|
|
|
|
this.selectedShippingMethod = shippingMethod;
|
|
|
|
|
|
|
|
paymentRequest.shippingMethods.push(shippingMethod);
|
2023-11-06 10:15:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:07:19 +00:00
|
|
|
// Add other shipping methods.
|
2023-11-06 10:15:56 +00:00
|
|
|
for (const shippingPackage of this.transactionInfo.shippingPackages) {
|
|
|
|
if (rate !== shippingPackage.id) {
|
2023-11-09 17:50:57 +00:00
|
|
|
paymentRequest.shippingMethods.push({
|
2023-11-06 10:15:56 +00:00
|
|
|
'label' : shippingPackage.label,
|
|
|
|
'detail' : '',
|
|
|
|
'amount' : shippingPackage.cost_str,
|
|
|
|
'identifier' : shippingPackage.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
// Store for reuse in case this data is not provided by ApplePay on authorization.
|
|
|
|
this.initialPaymentRequest = paymentRequest;
|
|
|
|
|
|
|
|
this.log('=== paymentRequest.shippingMethods', paymentRequest.shippingMethods);
|
2023-09-21 09:53:17 +02:00
|
|
|
}
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
paymentRequest() {
|
2023-08-31 16:43:40 +02:00
|
|
|
const applepayConfig = this.applePayConfig
|
|
|
|
const buttonConfig = this.buttonConfig
|
2023-09-07 09:56:46 +02:00
|
|
|
let baseRequest = {
|
|
|
|
countryCode: applepayConfig.countryCode,
|
|
|
|
merchantCapabilities: applepayConfig.merchantCapabilities,
|
|
|
|
supportedNetworks: applepayConfig.supportedNetworks,
|
2023-10-16 15:49:28 +02:00
|
|
|
requiredShippingContactFields: ["postalAddress", "email", "phone"],
|
2023-11-09 17:50:57 +00:00
|
|
|
requiredBillingContactFields: ["postalAddress"], // ApplePay does not implement billing email and phone fields.
|
2023-09-05 09:14:20 +02:00
|
|
|
}
|
2023-10-31 10:35:29 +00:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
if (!this.shouldRequireShippingInButton()) {
|
|
|
|
if (this.shouldCompletePaymentWithContextHandler()) {
|
|
|
|
// Data needs handled externally.
|
|
|
|
baseRequest.requiredShippingContactFields = [];
|
|
|
|
} else {
|
|
|
|
// Minimum data required for order creation.
|
|
|
|
baseRequest.requiredShippingContactFields = ["email", "phone"];
|
|
|
|
}
|
2023-10-31 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
const paymentRequest = Object.assign({}, baseRequest);
|
|
|
|
paymentRequest.currencyCode = buttonConfig.shop.currencyCode;
|
|
|
|
paymentRequest.total = {
|
2023-09-07 09:56:46 +02:00
|
|
|
label: buttonConfig.shop.totalLabel,
|
|
|
|
type: "final",
|
|
|
|
amount: this.transactionInfo.totalPrice,
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-09-05 09:14:20 +02:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
return paymentRequest;
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 17:32:21 +01:00
|
|
|
refreshContextData() {
|
|
|
|
switch (this.context) {
|
|
|
|
case 'product':
|
|
|
|
// Refresh product data that makes the price change.
|
2023-11-13 17:36:18 +00:00
|
|
|
this.productQuantity = document.querySelector('input.qty')?.value;
|
2023-10-25 17:52:00 +01:00
|
|
|
this.products = this.contextHandler.products();
|
|
|
|
this.log('Products updated', this.products);
|
2023-10-22 17:32:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
|
|
|
|
//------------------------
|
|
|
|
// Payment process
|
|
|
|
//------------------------
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
onValidateMerchant(session) {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onvalidatemerchant', this.buttonConfig.ajax_url);
|
2023-08-31 16:43:40 +02:00
|
|
|
return (applePayValidateMerchantEvent) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onvalidatemerchant call');
|
|
|
|
|
2023-11-03 10:30:31 +00:00
|
|
|
widgetBuilder.paypal.Applepay().validateMerchant({
|
2023-08-31 16:43:40 +02:00
|
|
|
validationUrl: applePayValidateMerchantEvent.validationURL
|
|
|
|
})
|
|
|
|
.then(validateResult => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onvalidatemerchant ok');
|
2023-08-31 16:43:40 +02:00
|
|
|
session.completeMerchantValidation(validateResult.merchantSession);
|
|
|
|
//call backend to update validation to true
|
2023-09-06 10:47:53 +02:00
|
|
|
jQuery.ajax({
|
|
|
|
url: this.buttonConfig.ajax_url,
|
|
|
|
type: 'POST',
|
|
|
|
data: {
|
|
|
|
action: 'ppcp_validate',
|
|
|
|
validation: true,
|
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
|
|
|
}
|
|
|
|
})
|
2023-08-31 16:43:40 +02:00
|
|
|
})
|
|
|
|
.catch(validateError => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onvalidatemerchant error', validateError);
|
2023-08-31 16:43:40 +02:00
|
|
|
console.error(validateError);
|
|
|
|
//call backend to update validation to false
|
2023-09-05 09:14:20 +02:00
|
|
|
jQuery.ajax({
|
|
|
|
url: this.buttonConfig.ajax_url,
|
|
|
|
type: 'POST',
|
|
|
|
data: {
|
|
|
|
action: 'ppcp_validate',
|
|
|
|
validation: false,
|
2023-09-06 10:47:53 +02:00
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
2023-09-05 09:14:20 +02:00
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
});
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onvalidatemerchant session abort');
|
2023-08-31 16:43:40 +02:00
|
|
|
session.abort();
|
2023-08-31 12:48:01 +02:00
|
|
|
});
|
2023-08-31 16:43:40 +02:00
|
|
|
};
|
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
|
|
|
|
onShippingMethodSelected(session) {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onshippingmethodselected', this.buttonConfig.ajax_url);
|
2023-11-09 17:50:57 +00:00
|
|
|
const ajax_url = this.buttonConfig.ajax_url;
|
2023-09-08 11:04:24 +02:00
|
|
|
return (event) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onshippingmethodselected call');
|
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
const data = this.getShippingMethodData(event);
|
2023-11-09 17:50:57 +00:00
|
|
|
|
2023-09-05 09:14:20 +02:00
|
|
|
jQuery.ajax({
|
2023-09-08 11:04:24 +02:00
|
|
|
url: ajax_url,
|
2023-09-05 09:14:20 +02:00
|
|
|
method: 'POST',
|
2023-09-08 11:04:24 +02:00
|
|
|
data: data,
|
2023-08-31 16:43:40 +02:00
|
|
|
success: (applePayShippingMethodUpdate, textStatus, jqXHR) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onshippingmethodselected ok');
|
2023-11-09 17:50:57 +00:00
|
|
|
let response = applePayShippingMethodUpdate.data;
|
2023-09-08 11:04:24 +02:00
|
|
|
if (applePayShippingMethodUpdate.success === false) {
|
2023-11-09 17:50:57 +00:00
|
|
|
response.errors = createAppleErrors(response.errors);
|
2023-09-08 11:04:24 +02:00
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
this.selectedShippingMethod = event.shippingMethod;
|
|
|
|
|
|
|
|
// Sort the response shipping methods, so that the selected shipping method is the first one.
|
|
|
|
response.newShippingMethods = response.newShippingMethods.sort((a, b) => {
|
2023-08-31 16:43:40 +02:00
|
|
|
if (a.label === this.selectedShippingMethod.label) {
|
2023-11-09 17:50:57 +00:00
|
|
|
return -1;
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
if (applePayShippingMethodUpdate.success === false) {
|
2023-11-09 17:50:57 +00:00
|
|
|
response.errors = createAppleErrors(response.errors);
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
session.completeShippingMethodSelection(response);
|
2023-08-31 16:43:40 +02:00
|
|
|
},
|
|
|
|
error: (jqXHR, textStatus, errorThrown) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onshippingmethodselected error', textStatus);
|
2023-11-09 17:50:57 +00:00
|
|
|
console.warn(textStatus, errorThrown);
|
|
|
|
session.abort();
|
2023-08-31 16:43:40 +02:00
|
|
|
},
|
2023-11-09 17:50:57 +00:00
|
|
|
});
|
2023-08-31 16:43:40 +02:00
|
|
|
};
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
|
|
|
|
onShippingContactSelected(session) {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onshippingcontactselected', this.buttonConfig.ajax_url);
|
2023-11-09 17:50:57 +00:00
|
|
|
|
|
|
|
const ajax_url = this.buttonConfig.ajax_url;
|
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
return (event) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onshippingcontactselected call');
|
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
const data = this.getShippingContactData(event);
|
2023-11-09 17:50:57 +00:00
|
|
|
|
2023-09-05 09:14:20 +02:00
|
|
|
jQuery.ajax({
|
2023-09-08 11:04:24 +02:00
|
|
|
url: ajax_url,
|
2023-09-05 09:14:20 +02:00
|
|
|
method: 'POST',
|
2023-09-08 11:04:24 +02:00
|
|
|
data: data,
|
2023-08-31 16:43:40 +02:00
|
|
|
success: (applePayShippingContactUpdate, textStatus, jqXHR) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onshippingcontactselected ok');
|
2023-11-09 17:50:57 +00:00
|
|
|
let response = applePayShippingContactUpdate.data;
|
|
|
|
this.updatedContactInfo = event.shippingContact;
|
2023-08-31 16:43:40 +02:00
|
|
|
if (applePayShippingContactUpdate.success === false) {
|
2023-11-09 17:50:57 +00:00
|
|
|
response.errors = createAppleErrors(response.errors);
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
|
|
|
if (response.newShippingMethods) {
|
2023-11-09 17:50:57 +00:00
|
|
|
this.selectedShippingMethod = response.newShippingMethods[0];
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
session.completeShippingContactSelection(response);
|
2023-08-31 16:43:40 +02:00
|
|
|
},
|
|
|
|
error: (jqXHR, textStatus, errorThrown) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onshippingcontactselected error', textStatus);
|
2023-11-09 17:50:57 +00:00
|
|
|
console.warn(textStatus, errorThrown);
|
|
|
|
session.abort();
|
2023-08-31 16:43:40 +02:00
|
|
|
},
|
2023-11-09 17:50:57 +00:00
|
|
|
});
|
2023-08-31 16:43:40 +02:00
|
|
|
};
|
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
getShippingContactData(event) {
|
|
|
|
const product_id = this.buttonConfig.product.id;
|
|
|
|
|
2023-10-22 17:32:21 +01:00
|
|
|
this.refreshContextData();
|
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
switch (this.context) {
|
|
|
|
case 'product':
|
|
|
|
return {
|
|
|
|
action: 'ppcp_update_shipping_contact',
|
|
|
|
product_id: product_id,
|
2023-10-25 17:52:00 +01:00
|
|
|
products: JSON.stringify(this.products),
|
2023-09-08 11:04:24 +02:00
|
|
|
caller_page: 'productDetail',
|
|
|
|
product_quantity: this.productQuantity,
|
|
|
|
simplified_contact: event.shippingContact,
|
2023-11-09 17:50:57 +00:00
|
|
|
need_shipping: this.shouldRequireShippingInButton(),
|
2023-09-08 11:04:24 +02:00
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
|
|
|
};
|
|
|
|
case 'cart':
|
|
|
|
case 'checkout':
|
|
|
|
case 'cart-block':
|
|
|
|
case 'checkout-block':
|
2023-09-10 13:21:19 +02:00
|
|
|
case 'mini-cart':
|
2023-09-08 11:04:24 +02:00
|
|
|
return {
|
|
|
|
action: 'ppcp_update_shipping_contact',
|
|
|
|
simplified_contact: event.shippingContact,
|
|
|
|
caller_page: 'cart',
|
2023-11-09 17:50:57 +00:00
|
|
|
need_shipping: this.shouldRequireShippingInButton(),
|
2023-09-08 11:04:24 +02:00
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
getShippingMethodData(event) {
|
|
|
|
const product_id = this.buttonConfig.product.id;
|
2023-10-22 17:32:21 +01:00
|
|
|
|
|
|
|
this.refreshContextData();
|
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
switch (this.context) {
|
|
|
|
case 'product': return {
|
|
|
|
action: 'ppcp_update_shipping_method',
|
|
|
|
shipping_method: event.shippingMethod,
|
2023-11-09 17:50:57 +00:00
|
|
|
simplified_contact: this.updatedContactInfo || this.initialPaymentRequest.shippingContact || this.initialPaymentRequest.billingContact,
|
2023-09-08 11:04:24 +02:00
|
|
|
product_id: product_id,
|
2023-10-25 17:52:00 +01:00
|
|
|
products: JSON.stringify(this.products),
|
2023-09-08 11:04:24 +02:00
|
|
|
caller_page: 'productDetail',
|
|
|
|
product_quantity: this.productQuantity,
|
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
|
|
|
}
|
|
|
|
case 'cart':
|
|
|
|
case 'checkout':
|
|
|
|
case 'cart-block':
|
|
|
|
case 'checkout-block':
|
2023-09-10 13:21:19 +02:00
|
|
|
case 'mini-cart':
|
2023-09-08 11:04:24 +02:00
|
|
|
return {
|
|
|
|
action: 'ppcp_update_shipping_method',
|
|
|
|
shipping_method: event.shippingMethod,
|
2023-11-09 17:50:57 +00:00
|
|
|
simplified_contact: this.updatedContactInfo || this.initialPaymentRequest.shippingContact || this.initialPaymentRequest.billingContact,
|
2023-09-08 11:04:24 +02:00
|
|
|
caller_page: 'cart',
|
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
onPaymentAuthorized(session) {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onpaymentauthorized');
|
2023-10-16 15:49:28 +02:00
|
|
|
return async (event) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onpaymentauthorized call');
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
function form() {
|
|
|
|
return document.querySelector('form.cart');
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-08-31 16:43:40 +02:00
|
|
|
const processInWooAndCapture = async (data) => {
|
2023-10-16 15:49:28 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
2023-11-09 17:50:57 +00:00
|
|
|
const billingContact = data.billing_contact || this.initialPaymentRequest.billingContact;
|
|
|
|
const shippingContact = data.shipping_contact || this.initialPaymentRequest.shippingContact;
|
|
|
|
const shippingMethod = this.selectedShippingMethod || (this.initialPaymentRequest.shippingMethods || [])[0];
|
|
|
|
|
2023-10-16 15:49:28 +02:00
|
|
|
let request_data = {
|
2023-08-31 16:43:40 +02:00
|
|
|
action: 'ppcp_create_order',
|
2023-10-16 15:49:28 +02:00
|
|
|
'caller_page': this.context,
|
|
|
|
'product_id': this.buttonConfig.product.id ?? null,
|
2023-10-25 17:52:00 +01:00
|
|
|
'products': JSON.stringify(this.products),
|
2023-10-16 15:49:28 +02:00
|
|
|
'product_quantity': this.productQuantity ?? null,
|
2023-08-31 16:43:40 +02:00
|
|
|
'shipping_contact': shippingContact,
|
|
|
|
'billing_contact': billingContact,
|
|
|
|
'token': event.payment.token,
|
2023-11-09 17:50:57 +00:00
|
|
|
'shipping_method': shippingMethod,
|
2023-10-16 15:49:28 +02:00
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
2023-08-31 16:43:40 +02:00
|
|
|
'funding_source': 'applepay',
|
|
|
|
'_wp_http_referer': '/?wc-ajax=update_order_review',
|
|
|
|
'paypal_order_id': data.paypal_order_id,
|
2023-10-16 15:49:28 +02:00
|
|
|
};
|
2023-10-20 18:13:09 +01:00
|
|
|
|
|
|
|
this.log('onpaymentauthorized request', this.buttonConfig.ajax_url, data);
|
|
|
|
|
2023-10-16 15:49:28 +02:00
|
|
|
jQuery.ajax({
|
|
|
|
url: this.buttonConfig.ajax_url,
|
|
|
|
method: 'POST',
|
|
|
|
data: request_data,
|
|
|
|
complete: (jqXHR, textStatus) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onpaymentauthorized complete');
|
2023-10-16 15:49:28 +02:00
|
|
|
},
|
|
|
|
success: (authorizationResult, textStatus, jqXHR) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onpaymentauthorized ok');
|
2023-11-09 17:50:57 +00:00
|
|
|
resolve(authorizationResult);
|
2023-10-16 15:49:28 +02:00
|
|
|
},
|
|
|
|
error: (jqXHR, textStatus, errorThrown) => {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onpaymentauthorized error', textStatus);
|
2023-10-16 15:49:28 +02:00
|
|
|
reject(new Error(errorThrown));
|
|
|
|
},
|
2023-11-09 17:50:57 +00:00
|
|
|
});
|
2023-10-16 15:49:28 +02:00
|
|
|
} catch (error) {
|
2023-10-20 18:13:09 +01:00
|
|
|
this.log('onpaymentauthorized catch', error);
|
2023-11-09 17:50:57 +00:00
|
|
|
console.log(error); // handle error
|
2023-10-16 15:49:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = await this.contextHandler.createOrder();
|
2023-10-20 18:13:09 +01:00
|
|
|
|
|
|
|
this.log('onpaymentauthorized paypal order ID', id, event.payment.token, event.payment.billingContact);
|
|
|
|
|
2023-10-16 15:49:28 +02:00
|
|
|
try {
|
2023-11-03 10:30:31 +00:00
|
|
|
const confirmOrderResponse = await widgetBuilder.paypal.Applepay().confirmOrder({
|
2023-10-16 15:49:28 +02:00
|
|
|
orderId: id,
|
|
|
|
token: event.payment.token,
|
|
|
|
billingContact: event.payment.billingContact,
|
|
|
|
});
|
2023-10-20 18:13:09 +01:00
|
|
|
|
|
|
|
this.log('onpaymentauthorized confirmOrderResponse', confirmOrderResponse);
|
|
|
|
|
2023-10-16 15:49:28 +02:00
|
|
|
if (confirmOrderResponse && confirmOrderResponse.approveApplePayPayment) {
|
|
|
|
if (confirmOrderResponse.approveApplePayPayment.status === "APPROVED") {
|
|
|
|
try {
|
2023-10-31 10:35:29 +00:00
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
if (this.shouldCompletePaymentWithContextHandler()) {
|
|
|
|
// No shipping, expect immediate capture, ex: PayNow, Checkout with form data.
|
2023-10-31 10:35:29 +00:00
|
|
|
|
|
|
|
let approveFailed = false;
|
|
|
|
await this.contextHandler.approveOrder({
|
|
|
|
orderID: id
|
|
|
|
}, { // actions mock object.
|
|
|
|
restart: () => new Promise((resolve, reject) => {
|
|
|
|
approveFailed = true;
|
|
|
|
resolve();
|
|
|
|
}),
|
|
|
|
order: {
|
|
|
|
get: () => new Promise((resolve, reject) => {
|
|
|
|
resolve(null);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!approveFailed) {
|
|
|
|
this.log('onpaymentauthorized approveOrder OK');
|
|
|
|
session.completePayment(ApplePaySession.STATUS_SUCCESS);
|
|
|
|
} else {
|
|
|
|
this.log('onpaymentauthorized approveOrder FAIL');
|
|
|
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
2023-11-09 17:50:57 +00:00
|
|
|
session.abort();
|
2023-10-31 10:35:29 +00:00
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
} else {
|
2023-10-31 10:35:29 +00:00
|
|
|
// Default payment.
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
billing_contact: event.payment.billingContact,
|
|
|
|
shipping_contact: event.payment.shippingContact,
|
|
|
|
paypal_order_id: id,
|
|
|
|
};
|
|
|
|
let authorizationResult = await processInWooAndCapture(data);
|
|
|
|
if (authorizationResult.result === "success") {
|
2023-11-09 17:50:57 +00:00
|
|
|
session.completePayment(ApplePaySession.STATUS_SUCCESS);
|
|
|
|
window.location.href = authorizationResult.redirect;
|
2023-10-31 10:35:29 +00:00
|
|
|
} else {
|
2023-11-09 17:50:57 +00:00
|
|
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
2023-10-31 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
2023-10-31 10:35:29 +00:00
|
|
|
|
2023-10-16 15:49:28 +02:00
|
|
|
} catch (error) {
|
|
|
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
2023-11-09 17:50:57 +00:00
|
|
|
session.abort();
|
2023-10-16 15:49:28 +02:00
|
|
|
console.error(error);
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
2023-10-16 15:49:28 +02:00
|
|
|
} else {
|
|
|
|
console.error('Error status is not APPROVED');
|
2023-08-31 16:43:40 +02:00
|
|
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
|
|
|
}
|
2023-10-16 15:49:28 +02:00
|
|
|
} else {
|
|
|
|
console.error('Invalid confirmOrderResponse');
|
|
|
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error confirming order with applepay token', error);
|
|
|
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
2023-11-09 17:50:57 +00:00
|
|
|
session.abort();
|
2023-10-16 15:49:28 +02:00
|
|
|
}
|
|
|
|
};
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
fillBillingContact(data) {
|
2023-09-21 09:53:17 +02:00
|
|
|
return {
|
2023-11-09 17:50:57 +00:00
|
|
|
givenName: data.billing_first_name ?? '',
|
|
|
|
familyName: data.billing_last_name ?? '',
|
|
|
|
emailAddress: data.billing_email ?? '',
|
|
|
|
phoneNumber: data.billing_phone ?? '',
|
|
|
|
addressLines: [data.billing_address_1, data.billing_address_2],
|
|
|
|
locality: data.billing_city ?? '',
|
|
|
|
postalCode: data.billing_postcode ?? '',
|
|
|
|
countryCode: data.billing_country ?? '',
|
|
|
|
administrativeArea: data.billing_state ?? '',
|
2023-09-21 09:53:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-09 17:50:57 +00:00
|
|
|
|
|
|
|
fillShippingContact(data) {
|
|
|
|
if (data.shipping_first_name === "") {
|
|
|
|
return this.fillBillingContact(data);
|
2023-09-21 09:53:17 +02:00
|
|
|
}
|
|
|
|
return {
|
2023-11-09 17:50:57 +00:00
|
|
|
givenName: (data?.shipping_first_name && data.shipping_first_name !== "") ? data.shipping_first_name : data?.billing_first_name,
|
|
|
|
familyName: (data?.shipping_last_name && data.shipping_last_name !== "") ? data.shipping_last_name : data?.billing_last_name,
|
|
|
|
emailAddress: (data?.shipping_email && data.shipping_email !== "") ? data.shipping_email : data?.billing_email,
|
|
|
|
phoneNumber: (data?.shipping_phone && data.shipping_phone !== "") ? data.shipping_phone : data?.billing_phone,
|
|
|
|
addressLines: [data.shipping_address_1 ?? '', data.shipping_address_2 ?? ''],
|
|
|
|
locality: (data?.shipping_city && data.shipping_city !== "") ? data.shipping_city : data?.billing_city,
|
|
|
|
postalCode: (data?.shipping_postcode && data.shipping_postcode !== "") ? data.shipping_postcode : data?.billing_postcode,
|
|
|
|
countryCode: (data?.shipping_country && data.shipping_country !== "") ? data.shipping_country : data?.billing_country,
|
|
|
|
administrativeArea: (data?.shipping_state && data.shipping_state !== "") ? data.shipping_state : data?.billing_state,
|
2023-09-21 09:53:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 17:50:57 +00:00
|
|
|
fillApplicationData(data) {
|
|
|
|
const jsonString = JSON.stringify(data);
|
2023-09-21 09:53:17 +02:00
|
|
|
let utf8Str = encodeURIComponent(jsonString).replace(/%([0-9A-F]{2})/g, (match, p1) => {
|
|
|
|
return String.fromCharCode('0x' + p1);
|
|
|
|
});
|
|
|
|
|
|
|
|
return btoa(utf8Str);
|
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ApplepayButton;
|