In order to detect when there are errors in the form after the user clicks "pay now" button, you need to use the solution below.
Since "subscription" will be triggered on every interaction with every field, we need to make sure that it only triggers once, after the "pay" button is clicked. I left a comment in its place.
let pay_btn_clicked = false;
// here you need to write a function that changes the value of "pay_btn_clicked" to true, when the button is clicked
const { subscribe, select } = wp.data;
subscribe(() => {
const checkoutData = select('wc/store/checkout');
if ( pay_btn_clicked ) {
if ( checkoutData.hasError() ) {
console.error('There was an error in checkout');
} else {
console.error('There were no errors in checkout');
}
}
pay_btn_clicked = false;
});