mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Full transaction with apple
This commit is contained in:
parent
327a87dfe8
commit
0882095602
10 changed files with 160 additions and 264 deletions
|
@ -27,25 +27,28 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.ppcp-has-googlepay-block {
|
.ppcp-has-applepay-block {
|
||||||
|
|
||||||
.wp-block-woocommerce-checkout {
|
.wp-block-woocommerce-checkout {
|
||||||
#applepay-container {
|
#applepay-container {
|
||||||
margin: 0;
|
--apple-pay-button-margin: 0;
|
||||||
height: 40px;
|
--apple-pay-button-height: 40px;
|
||||||
|
&.ppcp-button-pill {
|
||||||
|
--apple-pay-button-border-radius: 50px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.wp-block-woocommerce-cart {
|
.wp-block-woocommerce-cart {
|
||||||
#applepay-container {
|
#applepay-container {
|
||||||
margin: 0;
|
--apple-pay-button-margin: 0;
|
||||||
height: 40px;
|
--apple-pay-button-height: 40px;
|
||||||
}
|
}
|
||||||
/* Workaround for blocks grid */
|
/* Workaround for blocks grid */
|
||||||
.wc-block-components-express-payment__event-buttons {
|
.wc-block-components-express-payment__event-buttons {
|
||||||
display: block;
|
--apple-pay-button-display: block;
|
||||||
li[id*="express-payment-method-ppcp-"] {
|
li[id*="express-payment-method-ppcp-"] {
|
||||||
padding-bottom: 0;
|
--apple-pay-button-padding-bottom: 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,6 @@ class ApplepayButton {
|
||||||
}
|
}
|
||||||
|
|
||||||
init(config) {
|
init(config) {
|
||||||
console.log('[ApplePayButton] init', config);
|
|
||||||
if (this.isInitialized) {
|
if (this.isInitialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -180,7 +179,6 @@ class ApplepayButton {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
const session = this.applePaySession(paymentDataRequest)
|
const session = this.applePaySession(paymentDataRequest)
|
||||||
console.log("session", session)
|
|
||||||
const formValidator = PayPalCommerceGateway.early_checkout_validation_enabled ?
|
const formValidator = PayPalCommerceGateway.early_checkout_validation_enabled ?
|
||||||
new FormValidator(
|
new FormValidator(
|
||||||
PayPalCommerceGateway.ajax.validate_checkout.endpoint,
|
PayPalCommerceGateway.ajax.validate_checkout.endpoint,
|
||||||
|
@ -191,9 +189,7 @@ class ApplepayButton {
|
||||||
const errors = await formValidator.validate(document.querySelector(checkoutFormSelector));
|
const errors = await formValidator.validate(document.querySelector(checkoutFormSelector));
|
||||||
if (errors.length > 0) {
|
if (errors.length > 0) {
|
||||||
errorHandler.messages(errors);
|
errorHandler.messages(errors);
|
||||||
// fire WC event for other plugins
|
|
||||||
jQuery( document.body ).trigger( 'checkout_error' , [ errorHandler.currentHtml() ] );
|
jQuery( document.body ).trigger( 'checkout_error' , [ errorHandler.currentHtml() ] );
|
||||||
// stop Apple Pay payment sheet from showing
|
|
||||||
session.abort();
|
session.abort();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -222,8 +218,8 @@ class ApplepayButton {
|
||||||
countryCode: applepayConfig.countryCode,
|
countryCode: applepayConfig.countryCode,
|
||||||
merchantCapabilities: applepayConfig.merchantCapabilities,
|
merchantCapabilities: applepayConfig.merchantCapabilities,
|
||||||
supportedNetworks: applepayConfig.supportedNetworks,
|
supportedNetworks: applepayConfig.supportedNetworks,
|
||||||
requiredShippingContactFields: ["postalAddress"],
|
requiredShippingContactFields: ["postalAddress", "email", "phone"],
|
||||||
requiredBillingContactFields: ["postalAddress"]
|
requiredBillingContactFields: ["postalAddress", "email", "phone"],
|
||||||
}
|
}
|
||||||
const paymentDataRequest = Object.assign({}, baseRequest);
|
const paymentDataRequest = Object.assign({}, baseRequest);
|
||||||
paymentDataRequest.currencyCode = buttonConfig.shop.currencyCode;
|
paymentDataRequest.currencyCode = buttonConfig.shop.currencyCode;
|
||||||
|
@ -242,7 +238,6 @@ class ApplepayButton {
|
||||||
//------------------------
|
//------------------------
|
||||||
|
|
||||||
onvalidatemerchant(session) {
|
onvalidatemerchant(session) {
|
||||||
console.log("onvalidatemerchant")
|
|
||||||
return (applePayValidateMerchantEvent) => {
|
return (applePayValidateMerchantEvent) => {
|
||||||
paypal.Applepay().validateMerchant({
|
paypal.Applepay().validateMerchant({
|
||||||
validationUrl: applePayValidateMerchantEvent.validationURL
|
validationUrl: applePayValidateMerchantEvent.validationURL
|
||||||
|
@ -259,10 +254,8 @@ class ApplepayButton {
|
||||||
'woocommerce-process-checkout-nonce': this.nonce,
|
'woocommerce-process-checkout-nonce': this.nonce,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log('validated')
|
|
||||||
})
|
})
|
||||||
.catch(validateError => {
|
.catch(validateError => {
|
||||||
console.error(validateError);
|
|
||||||
//call backend to update validation to false
|
//call backend to update validation to false
|
||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
url: this.buttonConfig.ajax_url,
|
url: this.buttonConfig.ajax_url,
|
||||||
|
@ -279,7 +272,6 @@ class ApplepayButton {
|
||||||
}
|
}
|
||||||
onshippingmethodselected(session) {
|
onshippingmethodselected(session) {
|
||||||
const ajax_url = this.buttonConfig.ajax_url
|
const ajax_url = this.buttonConfig.ajax_url
|
||||||
console.log('[ApplePayButton] onshippingmethodselected');
|
|
||||||
return (event) => {
|
return (event) => {
|
||||||
const data = this.getShippingMethodData(event);
|
const data = this.getShippingMethodData(event);
|
||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
|
@ -291,7 +283,6 @@ class ApplepayButton {
|
||||||
if (applePayShippingMethodUpdate.success === false) {
|
if (applePayShippingMethodUpdate.success === false) {
|
||||||
response.errors = createAppleErrors(response.errors)
|
response.errors = createAppleErrors(response.errors)
|
||||||
}
|
}
|
||||||
console.log('shipping method update response', response, applePayShippingMethodUpdate)
|
|
||||||
this.selectedShippingMethod = event.shippingMethod
|
this.selectedShippingMethod = event.shippingMethod
|
||||||
//order the response shipping methods, so that the selected shipping method is the first one
|
//order the response shipping methods, so that the selected shipping method is the first one
|
||||||
let orderedShippingMethods = response.newShippingMethods.sort((a, b) => {
|
let orderedShippingMethods = response.newShippingMethods.sort((a, b) => {
|
||||||
|
@ -316,10 +307,8 @@ class ApplepayButton {
|
||||||
}
|
}
|
||||||
onshippingcontactselected(session) {
|
onshippingcontactselected(session) {
|
||||||
const ajax_url = this.buttonConfig.ajax_url
|
const ajax_url = this.buttonConfig.ajax_url
|
||||||
console.log('[ApplePayButton] onshippingcontactselected', ajax_url, session)
|
|
||||||
return (event) => {
|
return (event) => {
|
||||||
const data = this.getShippingContactData(event);
|
const data = this.getShippingContactData(event);
|
||||||
console.log('shipping contact selected', data, event)
|
|
||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
url: ajax_url,
|
url: ajax_url,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -327,7 +316,6 @@ class ApplepayButton {
|
||||||
success: (applePayShippingContactUpdate, textStatus, jqXHR) => {
|
success: (applePayShippingContactUpdate, textStatus, jqXHR) => {
|
||||||
let response = applePayShippingContactUpdate.data
|
let response = applePayShippingContactUpdate.data
|
||||||
this.updated_contact_info = event.shippingContact
|
this.updated_contact_info = event.shippingContact
|
||||||
console.log('shipping contact update response', response, applePayShippingContactUpdate, this.updated_contact_info)
|
|
||||||
if (applePayShippingContactUpdate.success === false) {
|
if (applePayShippingContactUpdate.success === false) {
|
||||||
response.errors = createAppleErrors(response.errors)
|
response.errors = createAppleErrors(response.errors)
|
||||||
}
|
}
|
||||||
|
@ -399,165 +387,90 @@ class ApplepayButton {
|
||||||
}
|
}
|
||||||
|
|
||||||
onpaymentauthorized(session) {
|
onpaymentauthorized(session) {
|
||||||
/*return (event) => {
|
return async (event) => {
|
||||||
function form() {
|
function form() {
|
||||||
return document.querySelector('form.cart');
|
return document.querySelector('form.cart');
|
||||||
}
|
}
|
||||||
|
|
||||||
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) => {
|
const processInWooAndCapture = async (data) => {
|
||||||
try {
|
return new Promise((resolve, reject) => {
|
||||||
console.log('processInWooAndCapture', data)
|
try {
|
||||||
const billingContact = data.billing_contact
|
const billingContact = data.billing_contact
|
||||||
const shippingContact = data.shipping_contact
|
const shippingContact = data.shipping_contact
|
||||||
jQuery.ajax({
|
let request_data = {
|
||||||
url: ajaxUrl,
|
|
||||||
method: 'POST',
|
|
||||||
data: {
|
|
||||||
action: 'ppcp_create_order',
|
action: 'ppcp_create_order',
|
||||||
'product_id': productId,
|
'caller_page': this.context,
|
||||||
'product_quantity': this.productQuantity,
|
'product_id': this.buttonConfig.product.id ?? null,
|
||||||
|
'product_quantity': this.productQuantity ?? null,
|
||||||
'shipping_contact': shippingContact,
|
'shipping_contact': shippingContact,
|
||||||
'billing_contact': billingContact,
|
'billing_contact': billingContact,
|
||||||
'token': event.payment.token,
|
'token': event.payment.token,
|
||||||
'shipping_method': selectedShippingMethod,
|
'shipping_method': this.selectedShippingMethod,
|
||||||
'woocommerce-process-checkout-nonce': nonce,
|
'woocommerce-process-checkout-nonce': this.nonce,
|
||||||
'funding_source': 'applepay',
|
'funding_source': 'applepay',
|
||||||
'_wp_http_referer': '/?wc-ajax=update_order_review',
|
'_wp_http_referer': '/?wc-ajax=update_order_review',
|
||||||
'paypal_order_id': data.paypal_order_id,
|
'paypal_order_id': data.paypal_order_id,
|
||||||
},
|
};
|
||||||
complete: (jqXHR, textStatus) => {
|
jQuery.ajax({
|
||||||
},
|
url: this.buttonConfig.ajax_url,
|
||||||
success: (authorizationResult, textStatus, jqXHR) => {
|
method: 'POST',
|
||||||
console.log('success authorizationResult', authorizationResult)
|
data: request_data,
|
||||||
if (authorizationResult.result === "success") {
|
complete: (jqXHR, textStatus) => {
|
||||||
redirectionUrl = authorizationResult.redirect;
|
},
|
||||||
//session.completePayment(ApplePaySession.STATUS_SUCCESS)
|
success: (authorizationResult, textStatus, jqXHR) => {
|
||||||
window.location.href = redirectionUrl
|
resolve(authorizationResult)
|
||||||
} else {
|
},
|
||||||
//session.completePayment(ApplePaySession.STATUS_FAILURE)
|
error: (jqXHR, textStatus, errorThrown) => {
|
||||||
}
|
reject(new Error(errorThrown));
|
||||||
},
|
},
|
||||||
error: (jqXHR, textStatus, errorThrown) => {
|
})
|
||||||
console.log('error authorizationResult', errorThrown)
|
} catch (error) {
|
||||||
session.completePayment(ApplePaySession.STATUS_FAILURE)
|
console.log(error) // handle error
|
||||||
console.warn(textStatus, errorThrown)
|
}
|
||||||
session.abort()
|
});
|
||||||
},
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error) // handle error
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
createOrderInPayPal([], []).then((orderId) => {
|
|
||||||
console.log('createOrderInPayPal', orderId)
|
let id = await this.contextHandler.createOrder();
|
||||||
paypal.Applepay().confirmOrder(
|
try {
|
||||||
{
|
const confirmOrderResponse = await paypal.Applepay().confirmOrder({
|
||||||
orderId: orderId,
|
orderId: id,
|
||||||
token: event.payment.token,
|
token: event.payment.token,
|
||||||
billingContact: event.payment.billingContact
|
billingContact: event.payment.billingContact,
|
||||||
}
|
});
|
||||||
).then(
|
if (confirmOrderResponse && confirmOrderResponse.approveApplePayPayment) {
|
||||||
() => {
|
if (confirmOrderResponse.approveApplePayPayment.status === "APPROVED") {
|
||||||
session.completePayment(ApplePaySession.STATUS_SUCCESS);
|
try {
|
||||||
let data = {
|
let data = {
|
||||||
billing_contact: event.payment.billingContact,
|
billing_contact: event.payment.billingContact,
|
||||||
shipping_contact: event.payment.shippingContact,
|
shipping_contact: event.payment.shippingContact,
|
||||||
paypal_order_id: orderId
|
paypal_order_id: id,
|
||||||
|
};
|
||||||
|
let authorizationResult = await processInWooAndCapture(data);
|
||||||
|
if (authorizationResult.result === "success") {
|
||||||
|
session.completePayment(ApplePaySession.STATUS_SUCCESS)
|
||||||
|
window.location.href = authorizationResult.redirect
|
||||||
|
} else {
|
||||||
|
session.completePayment(ApplePaySession.STATUS_FAILURE)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
||||||
|
session.abort()
|
||||||
|
console.error(error);
|
||||||
}
|
}
|
||||||
processInWooAndCapture(data)
|
} else {
|
||||||
}
|
console.error('Error status is not APPROVED');
|
||||||
).catch(err => {
|
|
||||||
console.error('Error confirming order with applepay token');
|
|
||||||
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
||||||
console.error(err);
|
|
||||||
}
|
}
|
||||||
);
|
} else {
|
||||||
}).catch((error) => {
|
console.error('Invalid confirmOrderResponse');
|
||||||
console.log(error)
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error confirming order with applepay token', error);
|
||||||
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
||||||
session.abort()
|
session.abort()
|
||||||
})
|
}
|
||||||
};*/
|
};
|
||||||
}
|
}
|
||||||
/* 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;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
fill_billing_contact(form_saved) {
|
fill_billing_contact(form_saved) {
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -8,7 +8,6 @@ class ApplepayManager {
|
||||||
this.buttonConfig = buttonConfig;
|
this.buttonConfig = buttonConfig;
|
||||||
this.ppcpConfig = ppcpConfig;
|
this.ppcpConfig = ppcpConfig;
|
||||||
this.ApplePayConfig = null;
|
this.ApplePayConfig = null;
|
||||||
console.log('Applepay manager', ppcpConfig, buttonConfig)
|
|
||||||
this.buttons = [];
|
this.buttons = [];
|
||||||
|
|
||||||
buttonModuleWatcher.watchContextBootstrap((bootstrap) => {
|
buttonModuleWatcher.watchContextBootstrap((bootstrap) => {
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
export const request = (countryCode, currencyCode, totalLabel, subtotal) => {
|
|
||||||
return {
|
|
||||||
countryCode: countryCode,
|
|
||||||
currencyCode: currencyCode,
|
|
||||||
supportedNetworks: ['amex', 'maestro', 'masterCard', 'visa', 'vPay'],
|
|
||||||
merchantCapabilities: ['supports3DS'],
|
|
||||||
shippingType: 'shipping',
|
|
||||||
requiredBillingContactFields: [
|
|
||||||
'postalAddress',
|
|
||||||
'email'
|
|
||||||
],
|
|
||||||
requiredShippingContactFields: [
|
|
||||||
'postalAddress',
|
|
||||||
'email'
|
|
||||||
],
|
|
||||||
total: {
|
|
||||||
label: totalLabel,
|
|
||||||
amount: subtotal,
|
|
||||||
type: 'final'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
import {buttonID} from "./utils";
|
|
||||||
export const maybeShowButton = () => {
|
|
||||||
const {ApplePaySession} = window
|
|
||||||
const applePayMethodElement = document.querySelector(
|
|
||||||
'#' + buttonID,
|
|
||||||
)
|
|
||||||
const canShowButton = applePayMethodElement && (ApplePaySession && ApplePaySession.canMakePayments())
|
|
||||||
if (!canShowButton) {
|
|
||||||
console.error('This device does not support Apple Pay');
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
|
@ -14,14 +14,7 @@ if (typeof window.PayPalCommerceGateway === 'undefined') {
|
||||||
window.PayPalCommerceGateway = ppcpConfig;
|
window.PayPalCommerceGateway = ppcpConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('ppcpData', ppcpData);
|
|
||||||
console.log('ppcpConfig', ppcpConfig);
|
|
||||||
console.log('buttonData', buttonData);
|
|
||||||
console.log('buttonConfig', buttonConfig);
|
|
||||||
|
|
||||||
const ApplePayComponent = () => {
|
const ApplePayComponent = () => {
|
||||||
console.log('ApplePayComponent render');
|
|
||||||
|
|
||||||
const [bootstrapped, setBootstrapped] = useState(false);
|
const [bootstrapped, setBootstrapped] = useState(false);
|
||||||
const [paypalLoaded, setPaypalLoaded] = useState(false);
|
const [paypalLoaded, setPaypalLoaded] = useState(false);
|
||||||
const [applePayLoaded, setApplePayLoaded] = useState(false);
|
const [applePayLoaded, setApplePayLoaded] = useState(false);
|
||||||
|
@ -30,6 +23,12 @@ const ApplePayComponent = () => {
|
||||||
const manager = new ApplepayManager(buttonConfig, ppcpConfig);
|
const manager = new ApplepayManager(buttonConfig, ppcpConfig);
|
||||||
manager.init();
|
manager.init();
|
||||||
};
|
};
|
||||||
|
useEffect(() => {
|
||||||
|
const bodyClass = 'ppcp-has-applepay-block';
|
||||||
|
if (!document.body.classList.contains(bodyClass)) {
|
||||||
|
document.body.classList.add(bodyClass);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Load ApplePay SDK
|
// Load ApplePay SDK
|
||||||
|
|
|
@ -25,7 +25,6 @@ import ApplepayManager from "./ApplepayManager";
|
||||||
}
|
}
|
||||||
const isMiniCart = ppcpConfig.mini_cart_buttons_enabled;
|
const isMiniCart = ppcpConfig.mini_cart_buttons_enabled;
|
||||||
const isButton = jQuery('#' + buttonConfig.button.wrapper).length > 0;
|
const isButton = jQuery('#' + buttonConfig.button.wrapper).length > 0;
|
||||||
console.log('isbutton' ,isButton, buttonConfig.button.wrapper)
|
|
||||||
// If button wrapper is not present then there is no need to load the scripts.
|
// If button wrapper is not present then there is no need to load the scripts.
|
||||||
// minicart loads later?
|
// minicart loads later?
|
||||||
if (!isMiniCart && !isButton) {
|
if (!isMiniCart && !isButton) {
|
||||||
|
|
|
@ -136,7 +136,8 @@ class ApplepayModule implements ModuleInterface {
|
||||||
assert( $button instanceof ApplePayButton );
|
assert( $button instanceof ApplePayButton );
|
||||||
$smart_button = $c->get( 'button.smart-button' );
|
$smart_button = $c->get( 'button.smart-button' );
|
||||||
assert( $smart_button instanceof SmartButtonInterface );
|
assert( $smart_button instanceof SmartButtonInterface );
|
||||||
if ( $smart_button->should_load_ppcp_script() ) {
|
$page_has_block = has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' );
|
||||||
|
if ( $smart_button->should_load_ppcp_script() || $page_has_block ) {
|
||||||
$button->enqueue();
|
$button->enqueue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,14 +232,6 @@ class ApplePayButton implements ButtonInterface {
|
||||||
'wp_ajax_nopriv_' . PropertiesDictionary::CREATE_ORDER,
|
'wp_ajax_nopriv_' . PropertiesDictionary::CREATE_ORDER,
|
||||||
array( $this, 'create_wc_order' )
|
array( $this, 'create_wc_order' )
|
||||||
);
|
);
|
||||||
add_action(
|
|
||||||
'wp_ajax_' . PropertiesDictionary::CREATE_ORDER_CART,
|
|
||||||
array( $this, 'create_wc_order_from_cart' )
|
|
||||||
);
|
|
||||||
add_action(
|
|
||||||
'wp_ajax_nopriv_' . PropertiesDictionary::CREATE_ORDER_CART,
|
|
||||||
array( $this, 'create_wc_order_from_cart' )
|
|
||||||
);
|
|
||||||
add_action(
|
add_action(
|
||||||
'wp_ajax_' . PropertiesDictionary::UPDATE_SHIPPING_CONTACT,
|
'wp_ajax_' . PropertiesDictionary::UPDATE_SHIPPING_CONTACT,
|
||||||
array( $this, 'update_shipping_contact' )
|
array( $this, 'update_shipping_contact' )
|
||||||
|
@ -400,17 +392,9 @@ class ApplePayButton implements ButtonInterface {
|
||||||
*/
|
*/
|
||||||
public function create_wc_order(): void {
|
public function create_wc_order(): void {
|
||||||
$applepay_request_data_object = $this->applepay_data_object_http();
|
$applepay_request_data_object = $this->applepay_data_object_http();
|
||||||
$applepay_request_data_object->order_data( 'productDetail' );
|
//phpcs:disable WordPress.Security.NonceVerification
|
||||||
$this->update_posted_data( $applepay_request_data_object );
|
$context = wc_clean( wp_unslash( $_POST['caller_page'] ?? '' ) );
|
||||||
$cart_item_key = $this->prepare_cart( $applepay_request_data_object );
|
if ( ! is_string( $context ) ) {
|
||||||
$cart = WC()->cart;
|
|
||||||
$address = $applepay_request_data_object->shipping_address();
|
|
||||||
$this->calculate_totals_single_product(
|
|
||||||
$cart,
|
|
||||||
$address,
|
|
||||||
$applepay_request_data_object->shipping_method()
|
|
||||||
);
|
|
||||||
if ( ! $cart_item_key ) {
|
|
||||||
$this->response_templates->response_with_data_errors(
|
$this->response_templates->response_with_data_errors(
|
||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
|
@ -421,29 +405,44 @@ class ApplePayButton implements ButtonInterface {
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->add_addresses_to_order( $applepay_request_data_object );
|
$applepay_request_data_object->order_data( $context );
|
||||||
add_filter(
|
$this->update_posted_data( $applepay_request_data_object );
|
||||||
'woocommerce_payment_successful_result',
|
if ( $context == 'product' ) {
|
||||||
function ( array $result ) use ( $cart, $cart_item_key ) : array {
|
$cart_item_key = $this->prepare_cart( $applepay_request_data_object );
|
||||||
if ( ! is_string( $cart_item_key ) ) {
|
$cart = WC()->cart;
|
||||||
|
$address = $applepay_request_data_object->shipping_address();
|
||||||
|
$this->calculate_totals_single_product(
|
||||||
|
$cart,
|
||||||
|
$address,
|
||||||
|
$applepay_request_data_object->shipping_method()
|
||||||
|
);
|
||||||
|
if ( ! $cart_item_key ) {
|
||||||
|
$this->response_templates->response_with_data_errors(
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
'errorCode' => 'unableToProcess',
|
||||||
|
'message' => 'Unable to process the order',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
add_filter(
|
||||||
|
'woocommerce_payment_successful_result',
|
||||||
|
function ( array $result ) use ( $cart, $cart_item_key ) : array {
|
||||||
|
if ( ! is_string( $cart_item_key ) ) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
$this->clear_current_cart( $cart, $cart_item_key );
|
||||||
|
$this->reload_cart( $cart );
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
$this->clear_current_cart( $cart, $cart_item_key );
|
);
|
||||||
$this->reload_cart( $cart );
|
}
|
||||||
return $result;
|
$this->add_addresses_to_order( $applepay_request_data_object );
|
||||||
}
|
|
||||||
);
|
|
||||||
WC()->checkout()->process_checkout();
|
WC()->checkout()->process_checkout();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method to create a WC order from the data received from the ApplePay JS
|
|
||||||
* On error returns an array of errors to be handled by the script
|
|
||||||
* On success returns the new order data
|
|
||||||
*/
|
|
||||||
public function create_wc_order_from_cart(): void {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the nonce in the data object is valid
|
* Checks if the nonce in the data object is valid
|
||||||
|
@ -655,10 +654,10 @@ class ApplePayButton implements ButtonInterface {
|
||||||
$packages[0]['contents'] = WC()->cart->cart_contents;
|
$packages[0]['contents'] = WC()->cart->cart_contents;
|
||||||
$packages[0]['contents_cost'] = $total;
|
$packages[0]['contents_cost'] = $total;
|
||||||
$packages[0]['applied_coupons'] = WC()->session->applied_coupon;
|
$packages[0]['applied_coupons'] = WC()->session->applied_coupon;
|
||||||
$packages[0]['destination']['country'] = $customer_address['country'];
|
$packages[0]['destination']['country'] = $customer_address['country'] ?? '';
|
||||||
$packages[0]['destination']['state'] = '';
|
$packages[0]['destination']['state'] = '';
|
||||||
$packages[0]['destination']['postcode'] = $customer_address['postcode'];
|
$packages[0]['destination']['postcode'] = $customer_address['postcode'] ?? '';
|
||||||
$packages[0]['destination']['city'] = $customer_address['city'];
|
$packages[0]['destination']['city'] = $customer_address['city'] ?? '';
|
||||||
$packages[0]['destination']['address'] = '';
|
$packages[0]['destination']['address'] = '';
|
||||||
$packages[0]['destination']['address_2'] = '';
|
$packages[0]['destination']['address_2'] = '';
|
||||||
|
|
||||||
|
@ -1016,6 +1015,12 @@ class ApplePayButton implements ButtonInterface {
|
||||||
'wc_ppcp_applepay',
|
'wc_ppcp_applepay',
|
||||||
$this->script_data()
|
$this->script_data()
|
||||||
);
|
);
|
||||||
|
add_action(
|
||||||
|
'wp_enqueue_scripts',
|
||||||
|
function () {
|
||||||
|
wp_enqueue_script( 'wc-ppcp-applepay' );
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -71,14 +71,14 @@ class ApplePayDataObjectHttp {
|
||||||
*
|
*
|
||||||
* @var string[]
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
protected $billing_address = array();
|
protected $billing_contact = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The shipping address.
|
* The shipping address.
|
||||||
*
|
*
|
||||||
* @var string[]
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
protected $shipping_address = array();
|
protected $shipping_contact = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of errors.
|
* The list of errors.
|
||||||
|
@ -217,17 +217,11 @@ class ApplePayDataObjectHttp {
|
||||||
* @param string $caller_page The caller page.
|
* @param string $caller_page The caller page.
|
||||||
*/
|
*/
|
||||||
public function order_data( string $caller_page ): void {
|
public function order_data( string $caller_page ): void {
|
||||||
$nonce = filter_input( INPUT_POST, 'woocommerce-process-checkout-nonce', FILTER_SANITIZE_SPECIAL_CHARS );
|
|
||||||
if ( ! $nonce ) {
|
if ( ! $this->is_nonce_valid() ) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
$is_nonce_valid = wp_verify_nonce(
|
|
||||||
$nonce,
|
|
||||||
'woocommerce-process_checkout'
|
|
||||||
);
|
|
||||||
if ( ! $is_nonce_valid ) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
//phpcs:disable WordPress.Security.NonceVerification
|
||||||
$data = filter_var_array( $_POST, FILTER_SANITIZE_SPECIAL_CHARS );
|
$data = filter_var_array( $_POST, FILTER_SANITIZE_SPECIAL_CHARS );
|
||||||
if ( ! $data ) {
|
if ( ! $data ) {
|
||||||
return;
|
return;
|
||||||
|
@ -238,6 +232,7 @@ class ApplePayDataObjectHttp {
|
||||||
PropertiesDictionary::CREATE_ORDER_SINGLE_PROD_REQUIRED_FIELDS,
|
PropertiesDictionary::CREATE_ORDER_SINGLE_PROD_REQUIRED_FIELDS,
|
||||||
PropertiesDictionary::CREATE_ORDER_CART_REQUIRED_FIELDS
|
PropertiesDictionary::CREATE_ORDER_CART_REQUIRED_FIELDS
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( ! $result ) {
|
if ( ! $result ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -254,12 +249,12 @@ class ApplePayDataObjectHttp {
|
||||||
}
|
}
|
||||||
|
|
||||||
$filtered_shipping_contact = $data[ PropertiesDictionary::SHIPPING_CONTACT ];
|
$filtered_shipping_contact = $data[ PropertiesDictionary::SHIPPING_CONTACT ];
|
||||||
$this->shipping_address = $this->complete_address(
|
$this->shipping_contact = $this->complete_address(
|
||||||
$filtered_shipping_contact,
|
$filtered_shipping_contact,
|
||||||
PropertiesDictionary::SHIPPING_CONTACT_INVALID
|
PropertiesDictionary::SHIPPING_CONTACT_INVALID
|
||||||
);
|
);
|
||||||
$filtered_billing_contact = $data[ PropertiesDictionary::BILLING_CONTACT ];
|
$filtered_billing_contact = $data[ PropertiesDictionary::BILLING_CONTACT ];
|
||||||
$this->billing_address = $this->complete_address(
|
$this->billing_contact = $this->complete_address(
|
||||||
$filtered_billing_contact,
|
$filtered_billing_contact,
|
||||||
PropertiesDictionary::BILLING_CONTACT_INVALID
|
PropertiesDictionary::BILLING_CONTACT_INVALID
|
||||||
);
|
);
|
||||||
|
@ -310,6 +305,7 @@ class ApplePayDataObjectHttp {
|
||||||
}
|
}
|
||||||
$this->$key = $value;
|
$this->$key = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -436,7 +432,7 @@ class ApplePayDataObjectHttp {
|
||||||
$required_fields = $required_product_fields;
|
$required_fields = $required_product_fields;
|
||||||
if (
|
if (
|
||||||
isset( $data[ PropertiesDictionary::CALLER_PAGE ] )
|
isset( $data[ PropertiesDictionary::CALLER_PAGE ] )
|
||||||
&& $data[ PropertiesDictionary::CALLER_PAGE ] === 'cart'
|
&& $data[ PropertiesDictionary::CALLER_PAGE ] !== 'product'
|
||||||
) {
|
) {
|
||||||
$required_fields = $required_cart_fields;
|
$required_fields = $required_cart_fields;
|
||||||
}
|
}
|
||||||
|
@ -490,7 +486,7 @@ class ApplePayDataObjectHttp {
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public function billing_address(): array {
|
public function billing_address(): array {
|
||||||
return $this->billing_address;
|
return $this->billing_contact;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -499,7 +495,7 @@ class ApplePayDataObjectHttp {
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public function shipping_address(): array {
|
public function shipping_address(): array {
|
||||||
return $this->shipping_address;
|
return $this->shipping_contact;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -607,4 +603,20 @@ class ApplePayDataObjectHttp {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns if the nonce is valid.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function is_nonce_valid():bool {
|
||||||
|
$nonce = filter_input( INPUT_POST, 'woocommerce-process-checkout-nonce', FILTER_SANITIZE_SPECIAL_CHARS );
|
||||||
|
if ( ! $nonce ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return (bool) wp_verify_nonce(
|
||||||
|
$nonce,
|
||||||
|
'woocommerce-process_checkout'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue