From 2bbe7efdfeaa09cb2d330e1ebd3f7f70a7095bf7 Mon Sep 17 00:00:00 2001 From: Ruben Ramirez Date: Fri, 4 Apr 2025 03:47:21 -0500 Subject: [PATCH] Fix: Use wp_redirect instead of wp_safe_redirect for Stripe checkout. --- src/Gateways/StripeGateway.php | 36 ---------------------------------- 1 file changed, 36 deletions(-) diff --git a/src/Gateways/StripeGateway.php b/src/Gateways/StripeGateway.php index 4866984..ea26769 100644 --- a/src/Gateways/StripeGateway.php +++ b/src/Gateways/StripeGateway.php @@ -24,16 +24,10 @@ class StripeGateway { * @return WP_Error|void Returns WP_Error on failure. Redirects user on success. */ public function initiatePayment( $user_id, $item_id, $quantity, $price_in_cents, $currency, $metadata ) { - $log_message_start = 'StripeGateway::initiatePayment - START'; - if (function_exists('log_to_file')) { log_to_file($log_message_start); } else { error_log($log_message_start); } - // Retrieve Stripe Secret Key $options = get_option('quiztech_settings'); $secret_key = isset($options['stripe_secret_key']) ? trim($options['stripe_secret_key']) : ''; - $log_message_key = 'StripeGateway::initiatePayment - Secret key retrieved (empty=' . (empty($secret_key) ? 'yes' : 'no') . ')'; - if (function_exists('log_to_file')) { log_to_file($log_message_key); } else { error_log($log_message_key); } - if ( empty( $secret_key ) ) { error_log( 'Quiztech Stripe Error: Secret key is not configured.' ); return new WP_Error( 'stripe_config_error', __( 'Stripe payment gateway is not configured correctly. Please contact the site administrator.', 'quiztech' ) ); @@ -41,23 +35,14 @@ class StripeGateway { // Ensure Stripe library is loaded (via Composer autoload) if ( ! class_exists( '\Stripe\Stripe' ) ) { // Check with leading slash here is okay as it's a general check - $log_message_lib_fail = 'StripeGateway::initiatePayment - Stripe class check FAILED.'; - if (function_exists('log_to_file')) { log_to_file($log_message_lib_fail); } else { error_log($log_message_lib_fail); } - error_log( 'Quiztech Stripe Error: Stripe PHP library not found. Ensure composer install was run.' ); return new WP_Error( 'stripe_lib_error', __( 'Payment processing library is missing. Please contact the site administrator.', 'quiztech' ) ); } try { - $log_message_set_key = 'StripeGateway::initiatePayment - Attempting to set API key.'; - if (function_exists('log_to_file')) { log_to_file($log_message_set_key); } else { error_log($log_message_set_key); } - Stripe::setApiKey( $secret_key ); // Get the URL for the Manage Credits page - $log_message_get_page = 'StripeGateway::initiatePayment - Attempting to get manage-credits page.'; - if (function_exists('log_to_file')) { log_to_file($log_message_get_page); } else { error_log($log_message_get_page); } - $manage_credits_page = get_page_by_path('manage-credits'); // Assumes page slug is 'manage-credits' if (!$manage_credits_page) { return new WP_Error('page_not_found', __('Manage Credits page not found.', 'quiztech')); @@ -67,9 +52,6 @@ class StripeGateway { $success_url = add_query_arg( 'purchase_status', 'success', $base_url ); $cancel_url = add_query_arg( 'purchase_status', 'cancelled', $base_url ); - $log_message_urls = 'StripeGateway::initiatePayment - Success URL: ' . $success_url . ' | Cancel URL: ' . $cancel_url; - if (function_exists('log_to_file')) { log_to_file($log_message_urls); } else { error_log($log_message_urls); } - // Create a descriptive name for the line item // Extract number from item_id like '10_credits' @@ -77,9 +59,6 @@ class StripeGateway { $credit_amount_display = isset($matches[1]) ? $matches[1] : $item_id; $item_name = sprintf( __( 'Quiztech Credits - %s Pack', 'quiztech' ), $credit_amount_display ); - $log_message_create_session = 'StripeGateway::initiatePayment - Attempting Session::create.'; - if (function_exists('log_to_file')) { log_to_file($log_message_create_session); } else { error_log($log_message_create_session); } - $session = Session::create( [ 'payment_method_types' => ['card'], 'line_items' => [ @@ -104,22 +83,7 @@ class StripeGateway { // Redirect to Stripe Checkout if ( isset( $session->url ) ) { - $log_message_redirect = "Quiztech StripeGateway: Attempting redirect to Stripe URL: " . $session->url; - if (function_exists('log_to_file')) { - log_to_file($log_message_redirect); - } else { - error_log($log_message_redirect); - } - wp_redirect( $session->url ); // Use direct redirect - - // If execution reaches here, exit() failed or was bypassed. - $log_message_critical = "Quiztech StripeGateway: CRITICAL - Execution continued after wp_safe_redirect and before exit()."; - if (function_exists('log_to_file')) { - log_to_file($log_message_critical); - } else { - error_log($log_message_critical); - } exit; } else { return new WP_Error( 'stripe_session_error', __( 'Could not create Stripe Checkout session.', 'quiztech' ) );