Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WooPay pre-checking place order bug when buying a subscription #9450

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/fix-woopay-pre-checking-save-my-info-place-order
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix WooPay pre-checking place order bug when buying a subscription.
73 changes: 36 additions & 37 deletions client/components/woopay/hooks/use-selected-payment-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,22 @@
* External dependencies
*/
import { useEffect, useState } from 'react';
import { useSelect } from '@wordpress/data';
import { PAYMENT_STORE_KEY } from '@woocommerce/block-data'; // eslint-disable-line import/no-unresolved

const getWCPayRadioButtonStatus = ( isBlocksCheckout ) =>
isBlocksCheckout
? document.querySelector(
'#radio-control-wc-payment-method-options-woocommerce_payments'
)?.checked
: document.querySelector( '#payment_method_woocommerce_payments' )
?.checked;

const getNewPaymentTokenRadioButtonStatus = ( isBlocksCheckout ) =>
isBlocksCheckout
? document.querySelector(
'#radio-control-wc-payment-method-options-woocommerce_payments'
)?.checked
: document.querySelector( '#wc-woocommerce_payments-payment-token-new' )
?.checked ||
! document.querySelector(
'[type=radio][name="wc-woocommerce_payments-payment-token"]'
);
const getWCPayRadioButtonStatus = () => {
return document.querySelector( '#payment_method_woocommerce_payments' )
?.checked;
};

const getPaymentMethods = ( isBlocksCheckout ) => {
if ( isBlocksCheckout ) {
// For blocks checkout there is no common selector to find all the payment methods including the
// saved tokens. Thus need to concate them here to make a whole list.
return [
...document.querySelectorAll(
'[type=radio][name="radio-control-wc-payment-method-options"]'
),
...document.querySelectorAll(
'[type=radio][name="radio-control-wc-payment-method-saved-tokens"]'
),
];
}
// for classic checkout
const getNewPaymentTokenRadioButtonStatus = () =>
document.querySelector( '#wc-woocommerce_payments-payment-token-new' )
?.checked ||
! document.querySelector(
'[type=radio][name="wc-woocommerce_payments-payment-token"]'
);

const getPaymentMethods = () => {
return document.querySelectorAll( '[type=radio][name="payment_method"]' );
};

Expand All @@ -51,15 +33,28 @@ const getPaymentTokens = ( isBlocksCheckout ) => {

// hook for checking if WCPay is selected.
const useSelectedPaymentMethod = ( isBlocksCheckout ) => {
// For blocks checkout, we use the store to get the active payment method.
const { isWCPayChosenOnBlocksCheckout } = useSelect( ( select ) => {
const store = select( PAYMENT_STORE_KEY );
return {
isWCPayChosenOnBlocksCheckout:
store.getActivePaymentMethod() === 'woocommerce_payments',
};
} );

const [ isWCPayChosen, setIsWCPayChosen ] = useState(
getWCPayRadioButtonStatus( isBlocksCheckout )
! isBlocksCheckout && getWCPayRadioButtonStatus()
);

const [ isNewPaymentTokenChosen, setNewPaymentTokenChosen ] = useState(
getNewPaymentTokenRadioButtonStatus( isBlocksCheckout )
! isBlocksCheckout && getNewPaymentTokenRadioButtonStatus()
);

useEffect( () => {
if ( isBlocksCheckout ) {
return;
}

// hides the `Save payment information to my account for future purchases` checkbox.
const hideCheckbox = () => {
const checkbox = document.querySelector(
Expand Down Expand Up @@ -87,7 +82,7 @@ const useSelectedPaymentMethod = ( isBlocksCheckout ) => {
);
};

const paymentMethods = getPaymentMethods( isBlocksCheckout );
const paymentMethods = getPaymentMethods();

paymentMethods.forEach( ( paymentMethod ) => {
paymentMethod.addEventListener( 'change', updateIsWCPayChosen );
Expand Down Expand Up @@ -119,8 +114,12 @@ const useSelectedPaymentMethod = ( isBlocksCheckout ) => {
}, [ isBlocksCheckout ] );

return {
isWCPayChosen,
isNewPaymentTokenChosen,
isWCPayChosen: isBlocksCheckout
? isWCPayChosenOnBlocksCheckout
: isWCPayChosen,
isNewPaymentTokenChosen: isBlocksCheckout
? isWCPayChosenOnBlocksCheckout
: isNewPaymentTokenChosen,
};
};

Expand Down
Loading