Last active
October 19, 2020 17:48
-
-
Save vanbo/8240e3ec04609f3e9d57142dc9cc3d8d to your computer and use it in GitHub Desktop.
WC Paytrace block card types on checkout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* NOTE: Add this snippet to your "child-theme\functions.php" file | |
*/ | |
add_action( 'wp_footer', 'vanbo_add_block_script_to_footer', 9999 ); | |
function vanbo_add_block_script_to_footer() { | |
?> | |
<script> | |
(function ($) { | |
/** | |
* Load the javascript after 'jquery' and 'paytrace-js' handles | |
*/ | |
var vanboPaytraceAcceptedCards = { | |
init: function () { | |
$(document.body).on('paytrace_validate_card_fields', vanboPaytraceAcceptedCards.validateCardFields); | |
}, | |
validateCardFields: function (e, form) { | |
form = $(form); | |
// Validate the payment form | |
if (vanboPaytraceAcceptedCards.isPayingWithNewCard()) { | |
/** | |
* Card types: | |
* 'amex', | |
* 'maestro', | |
* 'visa', | |
* 'mastercard', | |
* 'dinersclub', | |
* 'discover', | |
* 'jcb', | |
*/ | |
if ('amex' == $.payment.cardType(form.find('input#paytrace-card-number').val())) { | |
throw new vanboPaytraceAcceptedCards.error('American Express cards are not supported', 'card', form.find('input#paytrace-card-number')); | |
} | |
} | |
}, | |
isPayingWithNewCard: function () { | |
var newCardField = $('#wc-paytrace-payment-token-card-new'); | |
return 0 < newCardField.length && newCardField.is(':checked'); | |
}, | |
error: function (message, form, element) { | |
this.message = message; | |
this.form = form || 'card'; | |
this.appendTo = element || false; | |
}, | |
}; | |
$(document).ready(function () { | |
vanboPaytraceAcceptedCards.init(); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script will block the checkout action if the card type is used. The example uses American Express 'amex' card type, but any card type can be checked for and used by following the same example.