Fix: Correct credit purchase redirect hook and remove debug logs.

This commit is contained in:
Ruben Ramirez 2025-04-04 03:47:06 -05:00
parent 410b9c650e
commit b4638daa93

View file

@ -231,29 +231,21 @@ add_action( 'wp_ajax_send_job_invite', 'quiztech_ajax_send_job_invite' );
* Handle the submission from the "Buy Credits" forms on the Manage Credits page.
*/
function quiztech_handle_credit_purchase_submission() {
log_to_file("Stripe - Handler function started on template_redirect.");
log_to_file("Stripe - POST data inside handler:", $_POST);
// Only process if our action is set and we are on the frontend
if ( ! isset( $_POST['quiztech_action'] ) ) {
log_to_file("Stripe - quiztech_action unset");
return;
}
if ($_POST['quiztech_action'] !== 'initiate_credit_purchase') {
log_to_file("Stripe - invalid action");
return;
}
if (is_admin()) {
log_to_file("Stripe - is_admin");
return;
}
log_to_file("Stripe - Passed is_admin check.");
// Get the Manage Credits page URL reliably
$manage_credits_page = get_page_by_path('manage-credits'); // Assumes page slug is 'manage-credits'
if (!$manage_credits_page) {
// Fallback or error if page doesn't exist
log_to_file("Stripe - manage credits page does not exist");
wp_die(esc_html__('Manage Credits page not found. Cannot process purchase.', 'quiztech'));
exit;
}
@ -267,7 +259,6 @@ function quiztech_handle_credit_purchase_submission() {
'purchase_status' => 'error',
'message' => urlencode( __( 'Security check failed. Please try again.', 'quiztech' ) )
), $manage_credits_url ); // Use the stored URL
log_to_file("Stripe - Security check failed. Please try again");
wp_safe_redirect( $redirect_url );
exit;
}
@ -278,12 +269,10 @@ function quiztech_handle_credit_purchase_submission() {
'purchase_status' => 'error',
'message' => urlencode( __( 'You do not have permission to purchase credits.', 'quiztech' ) )
), $manage_credits_url );
log_to_file("Stripe - You do not have permission to purchase credits");
wp_safe_redirect( $redirect_url );
exit;
}
log_to_file("Stripe - Sanitize inputs");
// Sanitize inputs
$user_id = get_current_user_id();
$package_id = isset( $_POST['package_id'] ) ? sanitize_key( $_POST['package_id'] ) : '';
@ -296,7 +285,6 @@ function quiztech_handle_credit_purchase_submission() {
'purchase_status' => 'error',
'message' => urlencode( __( 'Invalid credit package selected.', 'quiztech' ) )
), $manage_credits_url );
log_to_file("Stripe - Invalid credit package selected");
wp_safe_redirect( $redirect_url );
exit;
}
@ -304,10 +292,8 @@ function quiztech_handle_credit_purchase_submission() {
// Check if the plugin function exists and call it
// Note: The plugin function expects (user_id, item_id, quantity)
if ( function_exists( '\Quiztech\AssessmentPlatform\Includes\quiztech_initiate_credit_purchase' ) ) {
log_to_file("Stripe - Function exists");
// Pass package_id as item_id, quantity is 1 for these packages
$result = \Quiztech\AssessmentPlatform\Includes\quiztech_initiate_credit_purchase( $user_id, $package_id, 1 );
log_to_file("Stripe - Result: ", $result);
// If the function returns an error, redirect back with the message
if ( is_wp_error( $result ) ) {
@ -315,7 +301,6 @@ function quiztech_handle_credit_purchase_submission() {
'purchase_status' => 'error',
'message' => urlencode( $result->get_error_message() )
), $manage_credits_url );
log_to_file("Stripe - Error: {$result->get_error_message()}");
wp_safe_redirect( $redirect_url );
exit;
}
@ -323,14 +308,12 @@ function quiztech_handle_credit_purchase_submission() {
// If it somehow didn't redirect AND didn't return an error, we might end up here.
// We could add a fallback redirect with 'initiated' status, but ideally the plugin handles it.
// For now, assume the plugin handles the success redirect or returns WP_Error.
log_to_file("Stripe - We shouldn't be here");
} else {
// Function doesn't exist - plugin likely inactive or issue
$redirect_url = add_query_arg( array(
'purchase_status' => 'error',
'message' => urlencode( __( 'Credit purchase functionality is currently unavailable.', 'quiztech' ) )
), get_permalink() );
log_to_file("Stripe - Credit purchase functionality is currently unavailable");
wp_safe_redirect( $redirect_url );
exit;
}