Fixes ApplePay payment process

This commit is contained in:
Pedro Silva 2023-10-20 18:13:09 +01:00
parent b4271a6782
commit 7f2836845e
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
5 changed files with 64 additions and 10 deletions

View file

@ -43,6 +43,8 @@ class ApplepayButton {
if (this.isInitialized) {
return;
}
this.log('Init', this.context);
this.initEventHandlers();
this.isInitialized = true;
this.applePayConfig = config;
@ -76,6 +78,16 @@ class ApplepayButton {
});
}
}
reinit() {
if (!this.applePayConfig) {
return;
}
this.isInitialized = false;
this.init(this.applePayConfig);
}
async fetchTransactionInfo() {
this.transactionInfo = await this.contextHandler.transactionInfo();
}
@ -123,6 +135,7 @@ class ApplepayButton {
}
applePaySession(paymentRequest) {
this.log('applePaySession', paymentRequest);
const session = new ApplePaySession(4, paymentRequest)
session.begin()
@ -172,6 +185,8 @@ class ApplepayButton {
* Show Apple Pay payment sheet when Apple Pay payment button is clicked
*/
async onButtonClick() {
this.log('onButtonClick', this.context);
const paymentDataRequest = this.paymentDataRequest();
// trigger woocommerce validation if we are in the checkout page
if (this.context === 'checkout') {
@ -247,11 +262,15 @@ class ApplepayButton {
//------------------------
onvalidatemerchant(session) {
this.log('onvalidatemerchant', this.buttonConfig.ajax_url);
return (applePayValidateMerchantEvent) => {
this.log('onvalidatemerchant call');
paypal.Applepay().validateMerchant({
validationUrl: applePayValidateMerchantEvent.validationURL
})
.then(validateResult => {
this.log('onvalidatemerchant ok');
session.completeMerchantValidation(validateResult.merchantSession);
//call backend to update validation to true
jQuery.ajax({
@ -265,6 +284,7 @@ class ApplepayButton {
})
})
.catch(validateError => {
this.log('onvalidatemerchant error', validateError);
console.error(validateError);
//call backend to update validation to false
jQuery.ajax({
@ -276,19 +296,24 @@ class ApplepayButton {
'woocommerce-process-checkout-nonce': this.nonce,
}
})
this.log('onvalidatemerchant session abort');
session.abort();
});
};
}
onshippingmethodselected(session) {
this.log('onshippingmethodselected', this.buttonConfig.ajax_url);
const ajax_url = this.buttonConfig.ajax_url
return (event) => {
this.log('onshippingmethodselected call');
const data = this.getShippingMethodData(event);
jQuery.ajax({
url: ajax_url,
method: 'POST',
data: data,
success: (applePayShippingMethodUpdate, textStatus, jqXHR) => {
this.log('onshippingmethodselected ok');
let response = applePayShippingMethodUpdate.data
if (applePayShippingMethodUpdate.success === false) {
response.errors = createAppleErrors(response.errors)
@ -309,6 +334,7 @@ class ApplepayButton {
session.completeShippingMethodSelection(response)
},
error: (jqXHR, textStatus, errorThrown) => {
this.log('onshippingmethodselected error', textStatus);
console.warn(textStatus, errorThrown)
session.abort()
},
@ -316,14 +342,18 @@ class ApplepayButton {
};
}
onshippingcontactselected(session) {
this.log('onshippingcontactselected', this.buttonConfig.ajax_url);
const ajax_url = this.buttonConfig.ajax_url
return (event) => {
this.log('onshippingcontactselected call');
const data = this.getShippingContactData(event);
jQuery.ajax({
url: ajax_url,
method: 'POST',
data: data,
success: (applePayShippingContactUpdate, textStatus, jqXHR) => {
this.log('onshippingcontactselected ok');
let response = applePayShippingContactUpdate.data
this.updated_contact_info = event.shippingContact
if (applePayShippingContactUpdate.success === false) {
@ -332,9 +362,9 @@ class ApplepayButton {
if (response.newShippingMethods) {
this.selectedShippingMethod = response.newShippingMethods[0]
}
session.completeShippingContactSelection(response)
},
error: (jqXHR, textStatus, errorThrown) => {
this.log('onshippingcontactselected error', textStatus);
console.warn(textStatus, errorThrown)
session.abort()
},
@ -397,7 +427,10 @@ class ApplepayButton {
}
onpaymentauthorized(session) {
this.log('onpaymentauthorized');
return async (event) => {
this.log('onpaymentauthorized call');
function form() {
return document.querySelector('form.cart');
}
@ -420,32 +453,45 @@ class ApplepayButton {
'_wp_http_referer': '/?wc-ajax=update_order_review',
'paypal_order_id': data.paypal_order_id,
};
this.log('onpaymentauthorized request', this.buttonConfig.ajax_url, data);
jQuery.ajax({
url: this.buttonConfig.ajax_url,
method: 'POST',
data: request_data,
complete: (jqXHR, textStatus) => {
this.log('onpaymentauthorized complete');
},
success: (authorizationResult, textStatus, jqXHR) => {
this.log('onpaymentauthorized ok');
resolve(authorizationResult)
},
error: (jqXHR, textStatus, errorThrown) => {
this.log('onpaymentauthorized error', textStatus);
reject(new Error(errorThrown));
},
})
} catch (error) {
this.log('onpaymentauthorized catch', error);
console.log(error) // handle error
}
});
}
let id = await this.contextHandler.createOrder();
this.log('onpaymentauthorized paypal order ID', id, event.payment.token, event.payment.billingContact);
try {
const confirmOrderResponse = await paypal.Applepay().confirmOrder({
orderId: id,
token: event.payment.token,
billingContact: event.payment.billingContact,
});
this.log('onpaymentauthorized confirmOrderResponse', confirmOrderResponse);
if (confirmOrderResponse && confirmOrderResponse.approveApplePayPayment) {
if (confirmOrderResponse.approveApplePayPayment.status === "APPROVED") {
try {

View file

@ -35,6 +35,12 @@ class ApplepayManager {
})();
}
reinit() {
for (const button of this.buttons) {
button.reinit();
}
}
/**
* Gets ApplePay configuration of the PayPal merchant.
* @returns {Promise<null>}

View file

@ -8,11 +8,19 @@ import ApplepayManager from "./ApplepayManager";
jQuery
}) {
let manager;
const bootstrap = function () {
const manager = new ApplepayManager(buttonConfig, ppcpConfig);
manager = new ApplepayManager(buttonConfig, ppcpConfig);
manager.init();
};
jQuery(document.body).on('updated_cart_totals updated_checkout', () => {
if (manager) {
manager.reinit();
}
});
document.addEventListener(
'DOMContentLoaded',
() => {

View file

@ -12,23 +12,16 @@ namespace WooCommerce\PayPalCommerce\Applepay\Assets;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Cart;
use WC_Checkout;
use WC_Order;
use WC_Session_Handler;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Session\MemoryWcSession;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\Webhooks\Handler\RequestHandlerTrait;
/**
* Class PayPalPaymentMethod
* Class ApplePayButton
*/
class ApplePayButton implements ButtonInterface {
use RequestHandlerTrait;

View file

@ -300,6 +300,7 @@ class ApplePayDataObjectHttp {
*/
protected function assign_data_object_values( array $data ): void {
foreach ( $data as $key => $value ) {
// Null values may give origin to type errors. If necessary replace condition this with a specialized field filter.
if ( null === $value ) {
continue;
}