Fix: Use wp_redirect instead of wp_safe_redirect for Stripe checkout.
This commit is contained in:
parent
545bed1690
commit
2bbe7efdfe
1 changed files with 0 additions and 36 deletions
|
|
@ -24,16 +24,10 @@ class StripeGateway {
|
||||||
* @return WP_Error|void Returns WP_Error on failure. Redirects user on success.
|
* @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 ) {
|
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
|
// Retrieve Stripe Secret Key
|
||||||
$options = get_option('quiztech_settings');
|
$options = get_option('quiztech_settings');
|
||||||
$secret_key = isset($options['stripe_secret_key']) ? trim($options['stripe_secret_key']) : '';
|
$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 ) ) {
|
if ( empty( $secret_key ) ) {
|
||||||
error_log( 'Quiztech Stripe Error: Secret key is not configured.' );
|
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' ) );
|
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)
|
// 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
|
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.' );
|
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' ) );
|
return new WP_Error( 'stripe_lib_error', __( 'Payment processing library is missing. Please contact the site administrator.', 'quiztech' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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 );
|
Stripe::setApiKey( $secret_key );
|
||||||
|
|
||||||
// Get the URL for the Manage Credits page
|
// 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'
|
$manage_credits_page = get_page_by_path('manage-credits'); // Assumes page slug is 'manage-credits'
|
||||||
if (!$manage_credits_page) {
|
if (!$manage_credits_page) {
|
||||||
return new WP_Error('page_not_found', __('Manage Credits page not found.', 'quiztech'));
|
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 );
|
$success_url = add_query_arg( 'purchase_status', 'success', $base_url );
|
||||||
$cancel_url = add_query_arg( 'purchase_status', 'cancelled', $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
|
// Create a descriptive name for the line item
|
||||||
// Extract number from item_id like '10_credits'
|
// Extract number from item_id like '10_credits'
|
||||||
|
|
@ -77,9 +59,6 @@ class StripeGateway {
|
||||||
$credit_amount_display = isset($matches[1]) ? $matches[1] : $item_id;
|
$credit_amount_display = isset($matches[1]) ? $matches[1] : $item_id;
|
||||||
$item_name = sprintf( __( 'Quiztech Credits - %s Pack', 'quiztech' ), $credit_amount_display );
|
$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( [
|
$session = Session::create( [
|
||||||
'payment_method_types' => ['card'],
|
'payment_method_types' => ['card'],
|
||||||
'line_items' => [
|
'line_items' => [
|
||||||
|
|
@ -104,22 +83,7 @@ class StripeGateway {
|
||||||
|
|
||||||
// Redirect to Stripe Checkout
|
// Redirect to Stripe Checkout
|
||||||
if ( isset( $session->url ) ) {
|
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
|
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;
|
exit;
|
||||||
} else {
|
} else {
|
||||||
return new WP_Error( 'stripe_session_error', __( 'Could not create Stripe Checkout session.', 'quiztech' ) );
|
return new WP_Error( 'stripe_session_error', __( 'Could not create Stripe Checkout session.', 'quiztech' ) );
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue