How do I know if I should enqueue JS code or just include it in ONE PHP function?

Best practice in this case would be to enqueue the js file inside the WP functions.php file. Are the braintree submit pages on their own unique templates? If so you could add something like this to your functions file:

function register_scripts() {

if(is_page_template('template-parts/page-braintree.php')):
    wp_register_script( 'braintree-js', get_template_directory_uri() . '/js/braintree.js', array( 'jquery' ), false, true );
    wp_enqueue_script( 'braintree-js' );
endif;


}
add_action( 'wp_enqueue_scripts', 'register_scripts' );

There is a few assumptions here:

  • You have created a page template for the braintree pages, you could use is_page and put in the array of pages it occurs on
  • Im assuming you put your js file in a ‘js’ folder inside your theme and that the js file is called braintree.js

Why enqueue scripts in functions?
I use to ask myself this question, it easy enough to manage 2-3 js files on a small project but on larger projects it becomes difficulat to manage the loading (enqueuing) of all of your js files. Putting all of the files inside functions makes it really easy to manage. Want to remove a js file from your project? Just comment it out from your functions file.