Skip to content

Instantly share code, notes, and snippets.

@vanbo
Last active October 19, 2020 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanbo/8240e3ec04609f3e9d57142dc9cc3d8d to your computer and use it in GitHub Desktop.
Save vanbo/8240e3ec04609f3e9d57142dc9cc3d8d to your computer and use it in GitHub Desktop.
WC Paytrace block card types on checkout
/**
* 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
}
@vanbo
Copy link
Author

vanbo commented Oct 16, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment