I have a wordpress site, created a plugin that creates a URL and then it's supposed to go to that URL. I created a shortcode for it as well. I have a pop-up that runs when I click a button and inside that pop-up I have the shortcode. It works if my plugin code is just a simple return 'Hello World', however with my code (below) as soon as I load the page it will go to the URL. I need it to only run when I open my pop-up. Been trying to fix this for over a day and it's driving me nuts.
<?php
/*
Plugin Name: Keystone AR Payments
Description: Allows customers to make online payments
Version: 1.0
Author: Jeff Margel
*/
function keystone_ar_payments(){
    global $wpdb;
    $table = 'tblAR';
    $current_user = wp_get_current_user(); 
    $user_id      = $current_user->user_login;
$db = new mysqli('localhost', 'wp_user', 'wp_password', 'wp_database');
// Check for connection errors
if ($db->connect_error) {
    die('Connection failed: ' . $db->connect_error);
}
// Query the tblAR table for invoice numbers and amount
$sql = "SELECT * FROM $table WHERE customer_number = '$user_id' AND select_invoice = 'YES'";
$result = $db->query($sql);
// Check for query errors
if (!$result) {
    die('Query failed: ' . $db->error);
}
    $invoices = '';
    $amount_total = 0;
    $cus_name = '';
    $cus_address = '';
    $cus_city = '';
    $cus_state = '';
    $cus_zip = '';
        while ($row = $result->fetch_assoc()) {
             $invoices .= $row['invoice_number'] . ',';
             $amount_total += $row['amount'];
             $cus_name = $row['cus_name'];
             $cus_address = $row['cus_address'];
             $cus_city = $row['cus_city'];
             $cus_state = $row['cus_state'];
             $cus_zip = $row['cus_zip'];
        }
        $cus_name = trim($cus_name);
        $cus_address = trim($cus_address);
        $cus_city = trim($cus_city);
        $cus_state = trim($cus_state);
        $cus_zip = trim($cus_zip);
        
        $invoices = rtrim($invoices, ',');
        
        $url = 'https://cphppdemo.securepayments.cardpointe.com/?cf_CustomField0=' . urlencode($invoices) . '&total=' . urlencode($amount_total) . '&customerId=' . urlencode($user_id) . '&billCompany=' . urlencode($cus_name) . '&billAddress1=' . urlencode($cus_address) . '&billCity=' . urlencode($cus_city) . '&billState=' . urlencode($cus_state) . '&billZip=' . urlencode($cus_zip);
wp_redirect( $url );
exit;
}
add_shortcode('keystone_ar_payments', 'keystone_ar_payments');
?>
It works if I change the code to a simple return 'Hello World' so I know the pop-up is working and can run shortcodes. For some reason the browser is running the wp_redirect code on pageload and which it should only fire during the button click.
 
    