More Stripe Troubleshooting

This commit is contained in:
Ruben Ramirez 2025-04-04 03:28:01 -05:00
parent 993ad7d5bb
commit 257ff33d83

View file

@ -1,3 +1,7 @@
<?php
namespace Quiztech\AssessmentPlatform\Gateways;
@ -11,6 +15,7 @@ use WP_Error;
* Handles Stripe payment gateway interactions.
*/
class StripeGateway {
\log_to_file('StripeGateway::initiatePayment - START');
/**
* Initiate a payment process via Stripe (e.g., create Checkout Session).
@ -24,6 +29,8 @@ 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_file('StripeGateway::initiatePayment - Secret key retrieved (empty=' . (empty($secret_key) ? 'yes' : 'no') . ')');
// Retrieve Stripe Secret Key
$options = get_option('quiztech_settings');
$secret_key = isset($options['stripe_secret_key']) ? trim($options['stripe_secret_key']) : '';
@ -35,14 +42,20 @@ 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_file('StripeGateway::initiatePayment - Stripe class check FAILED.');
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_file('StripeGateway::initiatePayment - Attempting to set API key.');
Stripe::setApiKey( $secret_key );
// Get the URL for the Manage Credits page
\log_to_file('StripeGateway::initiatePayment - Attempting to get manage-credits 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'));
@ -51,6 +64,10 @@ class StripeGateway {
$success_url = add_query_arg( 'purchase_status', 'success', $base_url );
$cancel_url = add_query_arg( 'purchase_status', 'cancelled', $base_url );
\log_to_file('StripeGateway::initiatePayment - Success URL: ' . $success_url);
\log_to_file('StripeGateway::initiatePayment - Cancel URL: ' . $cancel_url);
// Create a descriptive name for the line item
// Extract number from item_id like '10_credits'
@ -58,6 +75,7 @@ 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_file('StripeGateway::initiatePayment - Attempting Session::create.');
$session = Session::create( [
'payment_method_types' => ['card'],
@ -83,7 +101,11 @@ class StripeGateway {
// Redirect to Stripe Checkout
if ( isset( $session->url ) ) {
\log_to_file('StripeGateway::initiatePayment - Session created. Redirecting to: ' . $session->url);
error_log("Quiztech StripeGateway: Attempting redirect to Stripe URL: " . $session->url);
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().");
exit;
} else {
return new WP_Error( 'stripe_session_error', __( 'Could not create Stripe Checkout session.', 'quiztech' ) );
@ -143,4 +165,4 @@ class StripeGateway {
error_log('StripeGateway::handleWebhook called - This method is deprecated.');
return false; // Indicate not handled here
}
}
}