Use capture for 3ds in google

This commit is contained in:
carmenmaymo 2025-03-03 15:34:45 +01:00
parent d66a7521f5
commit ca76d33aa3
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
8 changed files with 333 additions and 2 deletions

View file

@ -55,6 +55,53 @@ class BaseHandler {
return this.actionHandler().configuration().onApprove( data, actions );
}
captureOrder( data, actions ) {
return fetch( this.ppcpConfig.ajax.get_order.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'same-origin',
body: JSON.stringify( {
nonce: this.ppcpConfig.ajax.get_order.nonce,
order_id: data.orderID,
} ),
} )
.then( ( order ) => {
console.log( 'order', order );
const orderResponse = order.json();
console.log(
orderResponse?.payment_source?.google_pay?.card
?.authentication_result
);
return fetch( this.ppcpConfig.ajax.capture_order.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'same-origin',
body: JSON.stringify( {
nonce: this.ppcpConfig.ajax.capture_order.nonce,
order_id: data.orderID,
} ),
} );
} )
.then( ( response ) => response.json() )
.then( ( captureResponse ) => {
console.log( 'Capture response:', captureResponse );
const orderReceivedUrl =
captureResponse.data?.order_received_url;
console.log( 'orderReceivedUrl', orderReceivedUrl );
setTimeout( () => {
window.location.href = orderReceivedUrl;
}, 200 );
} )
.catch( ( error ) => {
console.error( 'Error:', error );
} );
}
actionHandler() {
return new CartActionHandler( this.ppcpConfig, this.errorHandler() );
}

View file

@ -193,6 +193,7 @@ class GooglepayButton extends PaymentButton {
this.onButtonClick = this.onButtonClick.bind( this );
this.log( 'Create instance' );
this.log( this.ppcpConfig );
}
/**
@ -847,7 +848,31 @@ class GooglepayButton extends PaymentButton {
this.log( 'confirmOrder', confirmOrderResponse );
return 'APPROVED' === confirmOrderResponse?.status;
switch ( confirmOrderResponse?.status ) {
case 'APPROVED':
return true;
case 'PAYER_ACTION_REQUIRED':
return 'action_required';
default:
return false;
}
};
/**
* Initiates payer action and handles the 3DS contingency.
*
* @param {string} orderID
*/
const initiatePayerAction = async ( orderID ) => {
this.log( 'initiatePayerAction', orderID );
this.log(
'==== Confirm Payment Completed Payer Action Required ====='
);
await widgetBuilder.paypal
.Googlepay()
.initiatePayerAction( { orderId: orderID } );
this.log( '===== Payer Action Completed =====' );
};
/**
@ -887,6 +912,28 @@ class GooglepayButton extends PaymentButton {
return isApproved;
};
const captureOrderServerSide = async ( orderID ) => {
let isCaptured = true;
this.log( 'context', this.contextHandler );
await this.contextHandler.captureOrder(
{ orderID, payer },
{
restart: () =>
new Promise( ( resolve ) => {
isCaptured = false;
resolve();
} ),
order: {
get: () =>
new Promise( ( resolve ) => {
resolve( null );
} ),
},
}
);
return isCaptured;
};
// Add billing data to session.
moduleStorage.setPayer( payer );
setPayerData( payer );
@ -899,6 +946,14 @@ class GooglepayButton extends PaymentButton {
if ( ! isApprovedByPayPal ) {
result = paymentError( 'TRANSACTION FAILED' );
} else if ( isApprovedByPayPal === 'action_required' ) {
await initiatePayerAction( orderId );
const success = await captureOrderServerSide( orderId );
if ( success ) {
result = paymentResponse( 'SUCCESS' );
} else {
result = paymentError( 'FAILED TO APPROVE' );
}
} else {
const success = await approveOrderServerSide( orderId );