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-08-31 12:48:01 +02:00
|
|
|
|
|
|
|
class ApplepayButton {
|
|
|
|
|
|
|
|
constructor(context, externalHandler, buttonConfig, ppcpConfig) {
|
|
|
|
this.isInitialized = false;
|
|
|
|
|
|
|
|
this.context = context;
|
|
|
|
this.externalHandler = externalHandler;
|
|
|
|
this.buttonConfig = buttonConfig;
|
|
|
|
this.ppcpConfig = ppcpConfig;
|
|
|
|
this.paymentsClient = null;
|
|
|
|
|
|
|
|
this.contextHandler = ContextHandlerFactory.create(
|
|
|
|
this.context,
|
|
|
|
this.buttonConfig,
|
|
|
|
this.ppcpConfig
|
|
|
|
);
|
2023-08-31 16:43:40 +02:00
|
|
|
|
|
|
|
//PRODUCT DETAIL PAGE
|
2023-09-03 11:04:17 +02:00
|
|
|
if(this.context === 'product') {
|
|
|
|
this.productQuantity = document.querySelector('input.qty').value
|
|
|
|
}
|
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
this.updated_contact_info = []
|
2023-08-31 16:43:40 +02:00
|
|
|
this.selectedShippingMethod = []
|
|
|
|
this.nonce = document.getElementById('woocommerce-process-checkout-nonce').value
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init(config) {
|
|
|
|
console.log('[ApplePayButton] init', config);
|
|
|
|
if (this.isInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
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-08-31 16:43:40 +02:00
|
|
|
const isEligible = this.applePayConfig.isEligible;
|
|
|
|
if (isEligible) {
|
2023-09-07 09:56:46 +02:00
|
|
|
this.fetchTransactionInfo().then(() => {
|
2023-09-08 12:52:19 +02:00
|
|
|
const isSubscriptionProduct = this.ppcpConfig.data_client_id.has_subscriptions === true;
|
|
|
|
if(isSubscriptionProduct) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-07 09:56:46 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
if(this.context === 'mini-cart') {
|
|
|
|
document.querySelector(id_minicart).addEventListener('click', (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
this.onButtonClick();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
document.querySelector(id).addEventListener('click', (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
this.onButtonClick();
|
|
|
|
});
|
|
|
|
}
|
2023-09-07 09:56:46 +02:00
|
|
|
});
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
2023-09-06 10:47:53 +02:00
|
|
|
console.log('[ApplePayButton] init done', this.buttonConfig.ajax_url);
|
|
|
|
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-09-07 09:56:46 +02:00
|
|
|
async fetchTransactionInfo() {
|
|
|
|
this.transactionInfo = await this.contextHandler.transactionInfo();
|
|
|
|
}
|
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,
|
|
|
|
//buttonStyle: this.buttonConfig.button.style,
|
|
|
|
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) {
|
|
|
|
config.ppcpButtonWrapper = '#express-payment-method-ppcp-gateway';
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
initEventHandlers() {
|
|
|
|
const { wrapper, ppcpButtonWrapper } = this.contextConfig();
|
|
|
|
|
|
|
|
const syncButtonVisibility = () => {
|
|
|
|
const $ppcpButtonWrapper = jQuery(ppcpButtonWrapper);
|
|
|
|
setVisible(wrapper, $ppcpButtonWrapper.is(':visible'));
|
|
|
|
setEnabled(wrapper, !$ppcpButtonWrapper.hasClass('ppcp-disabled'));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-31 16:43:40 +02:00
|
|
|
applePaySession(paymentRequest) {
|
|
|
|
const session = new ApplePaySession(4, paymentRequest)
|
|
|
|
session.begin()
|
2023-09-08 11:04:24 +02:00
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
if (this.buttonConfig.product.needShipping) {
|
2023-09-08 11:04:24 +02:00
|
|
|
session.onshippingmethodselected = this.onshippingmethodselected(session)
|
|
|
|
session.onshippingcontactselected = this.onshippingcontactselected(session)
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
|
|
|
session.onvalidatemerchant = this.onvalidatemerchant(session);
|
2023-09-08 11:04:24 +02:00
|
|
|
session.onpaymentauthorized = this.onpaymentauthorized(session);
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-31 12:48:01 +02:00
|
|
|
/**
|
|
|
|
* Add a Apple Pay purchase button
|
|
|
|
*/
|
2023-08-31 16:43:40 +02:00
|
|
|
addButton() {
|
2023-09-12 14:05:06 +02:00
|
|
|
console.log('[ApplePayButton] context', this.context);
|
2023-08-31 12:48:01 +02:00
|
|
|
const wrapper =
|
|
|
|
(this.context === 'mini-cart')
|
|
|
|
? this.buttonConfig.button.mini_cart_wrapper
|
|
|
|
: this.buttonConfig.button.wrapper;
|
2023-09-12 14:05:06 +02:00
|
|
|
console.log('[ApplePayButton] wrapper', wrapper)
|
2023-09-12 12:08:08 +02:00
|
|
|
const shape =
|
2023-08-31 12:48:01 +02:00
|
|
|
(this.context === 'mini-cart')
|
|
|
|
? this.ppcpConfig.button.mini_cart_style.shape
|
2023-09-12 12:08:08 +02:00
|
|
|
: this.ppcpConfig.button.style.shape;
|
2023-09-10 13:21:19 +02:00
|
|
|
const appleContainer = this.context === 'mini-cart' ? document.getElementById("applepay-container-minicart") : document.getElementById("applepay-container");
|
2023-09-12 14:05:06 +02:00
|
|
|
console.log('[ApplePayButton] shape', shape)
|
|
|
|
console.log('[ApplePayButton] container', appleContainer)
|
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;
|
|
|
|
appleContainer.innerHTML = `<apple-pay-button id="${id}" buttonstyle="${color}" type="${type}" locale="${language}">`;
|
|
|
|
|
2023-09-12 14:05:06 +02:00
|
|
|
jQuery('#' + wrapper).addClass('ppcp-button-' + shape);
|
2023-08-31 16:43:40 +02:00
|
|
|
jQuery(wrapper).append(appleContainer);
|
2023-09-10 13:21:19 +02:00
|
|
|
console.log('[ApplePayButton] addButton', wrapper, appleContainer);
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------
|
|
|
|
// Button click
|
|
|
|
//------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show Apple Pay payment sheet when Apple Pay payment button is clicked
|
|
|
|
*/
|
2023-08-31 16:43:40 +02:00
|
|
|
onButtonClick() {
|
|
|
|
const paymentDataRequest = this.paymentDataRequest();
|
2023-08-31 12:48:01 +02:00
|
|
|
console.log('[ApplePayButton] onButtonClick: paymentDataRequest', paymentDataRequest, this.context);
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
this.applePaySession(paymentDataRequest)
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
paymentDataRequest() {
|
|
|
|
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,
|
|
|
|
requiredShippingContactFields: ["name", "phone",
|
|
|
|
"email", "postalAddress"],
|
|
|
|
requiredBillingContactFields: ["name", "phone", "email",
|
|
|
|
"postalAddress"]
|
2023-09-05 09:14:20 +02:00
|
|
|
}
|
2023-09-07 09:56:46 +02:00
|
|
|
console.log('[ApplePayButton] paymentDataRequest', applepayConfig, buttonConfig);
|
|
|
|
const paymentDataRequest = Object.assign({}, baseRequest);
|
|
|
|
paymentDataRequest.currencyCode = buttonConfig.shop.currencyCode;
|
|
|
|
paymentDataRequest.total = {
|
|
|
|
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-09-07 09:56:46 +02:00
|
|
|
return paymentDataRequest
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------
|
|
|
|
// Payment process
|
|
|
|
//------------------------
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
onvalidatemerchant(session) {
|
|
|
|
return (applePayValidateMerchantEvent) => {
|
2023-09-05 09:14:20 +02:00
|
|
|
paypal.Applepay().validateMerchant({
|
2023-08-31 16:43:40 +02:00
|
|
|
validationUrl: applePayValidateMerchantEvent.validationURL
|
|
|
|
})
|
|
|
|
.then(validateResult => {
|
|
|
|
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
|
|
|
console.log('validated')
|
|
|
|
})
|
|
|
|
.catch(validateError => {
|
|
|
|
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-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-09-08 11:04:24 +02:00
|
|
|
onshippingmethodselected(session) {
|
|
|
|
const ajax_url = this.buttonConfig.ajax_url
|
|
|
|
console.log('[ApplePayButton] onshippingmethodselected');
|
2023-09-05 09:14:20 +02:00
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
return (event) => {
|
|
|
|
const data = this.getShippingMethodData(event);
|
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) => {
|
|
|
|
let response = applePayShippingMethodUpdate.data
|
2023-09-08 11:04:24 +02:00
|
|
|
if (applePayShippingMethodUpdate.success === false) {
|
|
|
|
response.errors = createAppleErrors(response.errors)
|
|
|
|
}
|
|
|
|
console.log('shipping method update response', response, applePayShippingMethodUpdate)
|
2023-08-31 16:43:40 +02:00
|
|
|
this.selectedShippingMethod = event.shippingMethod
|
|
|
|
//order the response shipping methods, so that the selected shipping method is the first one
|
|
|
|
let orderedShippingMethods = response.newShippingMethods.sort((a, b) => {
|
|
|
|
if (a.label === this.selectedShippingMethod.label) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
})
|
|
|
|
//update the response.newShippingMethods with the ordered shipping methods
|
|
|
|
response.newShippingMethods = orderedShippingMethods
|
|
|
|
if (applePayShippingMethodUpdate.success === false) {
|
|
|
|
response.errors = createAppleErrors(response.errors)
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-09-08 11:04:24 +02:00
|
|
|
session.completeShippingMethodSelection(response)
|
2023-08-31 16:43:40 +02:00
|
|
|
},
|
|
|
|
error: (jqXHR, textStatus, errorThrown) => {
|
|
|
|
console.warn(textStatus, errorThrown)
|
|
|
|
session.abort()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
};
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-09-08 11:04:24 +02:00
|
|
|
onshippingcontactselected(session) {
|
|
|
|
const ajax_url = this.buttonConfig.ajax_url
|
2023-09-05 09:14:20 +02:00
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
return (event) => {
|
|
|
|
const data = this.getShippingContactData(event);
|
|
|
|
console.log('shipping contact selected', data, event)
|
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) => {
|
|
|
|
let response = applePayShippingContactUpdate.data
|
2023-09-08 11:04:24 +02:00
|
|
|
this.updated_contact_info = event.shippingContact
|
|
|
|
console.log('shipping contact update response', response, applePayShippingContactUpdate, this.updated_contact_info)
|
2023-08-31 16:43:40 +02:00
|
|
|
if (applePayShippingContactUpdate.success === false) {
|
|
|
|
response.errors = createAppleErrors(response.errors)
|
|
|
|
}
|
|
|
|
if (response.newShippingMethods) {
|
|
|
|
this.selectedShippingMethod = response.newShippingMethods[0]
|
|
|
|
}
|
2023-09-08 11:04:24 +02:00
|
|
|
session.completeShippingContactSelection(response)
|
2023-08-31 16:43:40 +02:00
|
|
|
},
|
|
|
|
error: (jqXHR, textStatus, errorThrown) => {
|
|
|
|
console.warn(textStatus, errorThrown)
|
|
|
|
session.abort()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
2023-09-08 11:04:24 +02:00
|
|
|
getShippingContactData(event) {
|
|
|
|
const product_id = this.buttonConfig.product.id;
|
|
|
|
|
|
|
|
switch (this.context) {
|
|
|
|
case 'product':
|
|
|
|
return {
|
|
|
|
action: 'ppcp_update_shipping_contact',
|
|
|
|
product_id: product_id,
|
|
|
|
caller_page: 'productDetail',
|
|
|
|
product_quantity: this.productQuantity,
|
|
|
|
simplified_contact: event.shippingContact,
|
|
|
|
need_shipping: this.buttonConfig.product.needShipping,
|
|
|
|
'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',
|
|
|
|
need_shipping: this.buttonConfig.product.needShipping,
|
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getShippingMethodData(event) {
|
|
|
|
const product_id = this.buttonConfig.product.id;
|
|
|
|
switch (this.context) {
|
|
|
|
case 'product': return {
|
|
|
|
action: 'ppcp_update_shipping_method',
|
|
|
|
shipping_method: event.shippingMethod,
|
|
|
|
product_id: product_id,
|
|
|
|
caller_page: 'productDetail',
|
|
|
|
product_quantity: this.productQuantity,
|
|
|
|
simplified_contact: this.updated_contact_info,
|
|
|
|
'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,
|
|
|
|
caller_page: 'cart',
|
|
|
|
simplified_contact: this.updated_contact_info,
|
|
|
|
'woocommerce-process-checkout-nonce': this.nonce,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
|
2023-09-08 11:04:24 +02:00
|
|
|
onpaymentauthorized(session) {
|
2023-08-31 16:43:40 +02:00
|
|
|
/*return (event) => {
|
|
|
|
function form() {
|
|
|
|
return document.querySelector('form.cart');
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
const errorHandler = new ErrorHandler(
|
|
|
|
PayPalCommerceGateway.labels.error.generic,
|
|
|
|
document.querySelector('.woocommerce-notices-wrapper')
|
|
|
|
);
|
|
|
|
const actionHandler = new SingleProductActionHandler(
|
|
|
|
PayPalCommerceGateway,
|
|
|
|
new UpdateCart(
|
|
|
|
PayPalCommerceGateway.ajax.change_cart.endpoint,
|
|
|
|
PayPalCommerceGateway.ajax.change_cart.nonce,
|
|
|
|
),
|
|
|
|
form(),
|
|
|
|
errorHandler,
|
|
|
|
);
|
|
|
|
|
|
|
|
let createOrderInPayPal = actionHandler.createOrder()
|
|
|
|
const processInWooAndCapture = async (data) => {
|
|
|
|
try {
|
|
|
|
console.log('processInWooAndCapture', data)
|
|
|
|
const billingContact = data.billing_contact
|
|
|
|
const shippingContact = data.shipping_contact
|
|
|
|
jQuery.ajax({
|
|
|
|
url: ajaxUrl,
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
action: 'ppcp_create_order',
|
|
|
|
'product_id': productId,
|
2023-09-05 09:14:20 +02:00
|
|
|
'product_quantity': this.productQuantity,
|
2023-08-31 16:43:40 +02:00
|
|
|
'shipping_contact': shippingContact,
|
|
|
|
'billing_contact': billingContact,
|
|
|
|
'token': event.payment.token,
|
|
|
|
'shipping_method': selectedShippingMethod,
|
|
|
|
'woocommerce-process-checkout-nonce': nonce,
|
|
|
|
'funding_source': 'applepay',
|
|
|
|
'_wp_http_referer': '/?wc-ajax=update_order_review',
|
|
|
|
'paypal_order_id': data.paypal_order_id,
|
|
|
|
},
|
|
|
|
complete: (jqXHR, textStatus) => {
|
|
|
|
},
|
|
|
|
success: (authorizationResult, textStatus, jqXHR) => {
|
|
|
|
console.log('success authorizationResult', authorizationResult)
|
|
|
|
if (authorizationResult.result === "success") {
|
|
|
|
redirectionUrl = authorizationResult.redirect;
|
|
|
|
//session.completePayment(ApplePaySession.STATUS_SUCCESS)
|
|
|
|
window.location.href = redirectionUrl
|
|
|
|
} else {
|
|
|
|
//session.completePayment(ApplePaySession.STATUS_FAILURE)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: (jqXHR, textStatus, errorThrown) => {
|
|
|
|
console.log('error authorizationResult', errorThrown)
|
|
|
|
session.completePayment(ApplePaySession.STATUS_FAILURE)
|
|
|
|
console.warn(textStatus, errorThrown)
|
|
|
|
session.abort()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error) // handle error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createOrderInPayPal([], []).then((orderId) => {
|
|
|
|
console.log('createOrderInPayPal', orderId)
|
2023-09-05 09:14:20 +02:00
|
|
|
paypal.Applepay().confirmOrder(
|
2023-08-31 16:43:40 +02:00
|
|
|
{
|
|
|
|
orderId: orderId,
|
|
|
|
token: event.payment.token,
|
|
|
|
billingContact: event.payment.billingContact
|
|
|
|
}
|
|
|
|
).then(
|
|
|
|
() => {
|
|
|
|
session.completePayment(ApplePaySession.STATUS_SUCCESS);
|
|
|
|
let data = {
|
|
|
|
billing_contact: event.payment.billingContact,
|
|
|
|
shipping_contact: event.payment.shippingContact,
|
|
|
|
paypal_order_id: orderId
|
|
|
|
}
|
|
|
|
processInWooAndCapture(data)
|
|
|
|
}
|
|
|
|
).catch(err => {
|
|
|
|
console.error('Error confirming order with applepay token');
|
|
|
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}).catch((error) => {
|
|
|
|
console.log(error)
|
|
|
|
session.abort()
|
|
|
|
})
|
|
|
|
};*/
|
2023-08-31 12:48:01 +02:00
|
|
|
}
|
2023-08-31 16:43:40 +02:00
|
|
|
/* onPaymentAuthorized(paymentData) {
|
|
|
|
console.log('[ApplePayButton] onPaymentAuthorized', this.context);
|
|
|
|
return this.processPayment(paymentData);
|
|
|
|
}
|
|
|
|
|
|
|
|
async processPayment(paymentData) {
|
|
|
|
console.log('[ApplePayButton] processPayment', this.context);
|
|
|
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
let id = await this.contextHandler.createOrder();
|
|
|
|
|
|
|
|
console.log('[ApplePayButton] processPayment: createOrder', id, this.context);
|
|
|
|
|
|
|
|
const confirmOrderResponse = await paypal.Applepay().confirmOrder({
|
|
|
|
orderId: id,
|
|
|
|
paymentMethodData: paymentData.paymentMethodData
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('[ApplePayButton] processPayment: confirmOrder', confirmOrderResponse, this.context);
|
|
|
|
|
|
|
|
/!** Capture the Order on the Server *!/
|
|
|
|
if (confirmOrderResponse.status === "APPROVED") {
|
|
|
|
|
|
|
|
let approveFailed = false;
|
|
|
|
await this.contextHandler.approveOrderForContinue({
|
|
|
|
orderID: id
|
|
|
|
}, {
|
|
|
|
restart: () => new Promise((resolve, reject) => {
|
|
|
|
approveFailed = true;
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!approveFailed) {
|
|
|
|
resolve(this.processPaymentResponse('SUCCESS'));
|
|
|
|
} else {
|
|
|
|
resolve(this.processPaymentResponse('ERROR', 'PAYMENT_AUTHORIZATION', 'FAILED TO APPROVE'));
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
resolve(this.processPaymentResponse('ERROR', 'PAYMENT_AUTHORIZATION', 'TRANSACTION FAILED'));
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
resolve(this.processPaymentResponse('ERROR', 'PAYMENT_AUTHORIZATION', err.message));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
processPaymentResponse(state, intent = null, message = null) {
|
|
|
|
let response = {
|
|
|
|
transactionState: state,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intent || message) {
|
|
|
|
response.error = {
|
|
|
|
intent: intent,
|
|
|
|
message: message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('[ApplePayButton] processPaymentResponse', response, this.context);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}*/
|
2023-08-31 12:48:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ApplepayButton;
|