Last active
February 11, 2022 10:25
-
-
Save vanbo/4457efa3479b30dcdb6c95b8df24a5c7 to your computer and use it in GitHub Desktop.
paytrace-load-public-key
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
/** | |
* IMPORTANT: The code should be added to the "child-theme/functions.php" file or anywhere that you know it will be loaded | |
*/ | |
function vd_paytrace_load_js() { | |
if ( ! class_exists( '\WcPaytrace\Helpers\Factories' ) ) { | |
return; | |
} | |
$pt_gateway = \WcPaytrace\Helpers\Factories::get_gateway( 'paytrace' ); | |
if ( ! $pt_gateway && ! is_checkout() && ! is_add_payment_method_page() ) { | |
return; | |
} | |
?> | |
<script> | |
(function ($) { | |
$(document).ready(function () { | |
$(document.body).on('paytrace_validate_card_fields', function (none, form, PaytraceForm) { | |
PaytraceForm.setKey(PaytraceLoadKey.key); | |
}); | |
var PaytraceLoadKey = { | |
loadKey: function () { | |
if (null !== PaytraceLoadKey.key) { | |
return; | |
} | |
// Set the key from an AJAX call (in this case via a relative URL) | |
var location = checkout_params_paytrace.publicKeyUrl; | |
if ('' === location) { | |
location = '/public_key.pem' | |
} | |
PaytraceLoadKey.setKeyAjax(location); | |
}, | |
setKeyAjax: function (keyUrl) { | |
$.ajax({ | |
url : keyUrl, | |
dataType: 'text', | |
}) | |
.done(function (publicKeyPem) { | |
PaytraceLoadKey.key = publicKeyPem; | |
return true; | |
}) | |
.fail(function (jqXHR, textStatus) { | |
}); | |
}, | |
}; | |
PaytraceLoadKey.loadKey(); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
add_action( 'wp_footer', 'vd_paytrace_load_js', 999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment