More Stripe Troubleshooting

This commit is contained in:
Ruben Ramirez 2025-04-04 03:35:46 -05:00
parent 9f8108caa0
commit 560a9be23d
2 changed files with 30 additions and 49 deletions

View file

@ -307,37 +307,4 @@ function quiztech_register_rest_routes() {
'permission_callback' => '__return_true' // Allow public access - Stripe needs to reach this
) );
}
add_action( 'rest_api_init', 'quiztech_register_rest_routes' );
/**
* Basic logging function for debugging. Allows passing of an object or array for the $data paramater
*
* Set the CUSTOM_DEBUG_LOG file in wp-config.php
*
*/
function log_to_file2($message, $data = false){
// Only proceed if a message is provided
if ($message) {
// Check if the custom log file constant is defined
if (defined('CUSTOM_DEBUG_LOG') && CUSTOM_DEBUG_LOG) {
$log_File = CUSTOM_DEBUG_LOG;
$date = new DateTime();
$date = $date->format("Y/m/d h:i:s");
// Convert arrays and objects to JSON format
if (is_array($data) || is_object($data)) {
$data = json_encode($data, JSON_PRETTY_PRINT);
$message = $message . "\r\n" . $data;
} else if ($data) {
$message = $message . " " . $data;
}
// Log the message to the specified file
error_log("[$date] " . $message ."\r\n", 3, $log_File);
}
// If CUSTOM_DEBUG_LOG is not defined, do nothing (fail silently)
// Alternatively, could log to default PHP error log:
// else { error_log("Quiztech Debug: " . $message); }
}
}
add_action( 'rest_api_init', 'quiztech_register_rest_routes' );

View file

@ -1,7 +1,3 @@
<?php
namespace Quiztech\AssessmentPlatform\Gateways;
@ -15,7 +11,6 @@ use WP_Error;
* Handles Stripe payment gateway interactions.
*/
class StripeGateway {
log_to_file2('StripeGateway::initiatePayment - START');
/**
* Initiate a payment process via Stripe (e.g., create Checkout Session).
@ -29,12 +24,16 @@ 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_to_file2('StripeGateway::initiatePayment - Secret key retrieved (empty=' . (empty($secret_key) ? 'yes' : 'no') . ')');
$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' ) );
@ -42,19 +41,22 @@ 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_to_file2('StripeGateway::initiatePayment - Stripe class check FAILED.');
$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_to_file2('StripeGateway::initiatePayment - Attempting to set API key.');
$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_to_file2('StripeGateway::initiatePayment - Attempting to get 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) {
@ -64,9 +66,9 @@ class StripeGateway {
$success_url = add_query_arg( 'purchase_status', 'success', $base_url );
$cancel_url = add_query_arg( 'purchase_status', 'cancelled', $base_url );
log_to_file2('StripeGateway::initiatePayment - Success URL: ' . $success_url);
log_to_file2('StripeGateway::initiatePayment - Cancel URL: ' . $cancel_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
@ -75,7 +77,8 @@ class StripeGateway {
$credit_amount_display = isset($matches[1]) ? $matches[1] : $item_id;
$item_name = sprintf( __( 'Quiztech Credits - %s Pack', 'quiztech' ), $credit_amount_display );
log_to_file2('StripeGateway::initiatePayment - Attempting Session::create.');
$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'],
@ -101,11 +104,22 @@ class StripeGateway {
// Redirect to Stripe Checkout
if ( isset( $session->url ) ) {
log_to_file2('StripeGateway::initiatePayment - Session created. Redirecting to: ' . $session->url);
error_log("Quiztech StripeGateway: Attempting redirect to Stripe URL: " . $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_safe_redirect( $session->url );
// If execution reaches here, exit() failed or was bypassed.
error_log("Quiztech StripeGateway: CRITICAL - Execution continued after wp_safe_redirect and before exit().");
$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' ) );