# Gift Voucher Pro - Stripe Library Conflict Resolution

## Problem Description

The Gift Voucher Pro plugin was experiencing a critical Stripe payment error when used alongside the "traveler-code" plugin:

```
PHP Fatal error: Uncaught Stripe\Error\InvalidRequest: Unrecognized request URL (POST: /v1/sessions)
```

This error occurred because both plugins were loading different versions of the Stripe PHP library, causing a version conflict where the older library from "traveler-code" was being used instead of the newer library required by Gift Voucher Pro for Checkout Sessions.

## Root Cause Analysis

1. **Library Version Conflict**: Gift Voucher Pro requires a newer version of the Stripe PHP library that supports Checkout Sessions (`/v1/sessions` endpoint), while the traveler-code plugin loads an older, incompatible version.

2. **Loading Order Issue**: The traveler-code plugin was loading its Stripe library after Gift Voucher Pro, overriding the newer classes with older ones.

3. **Namespace Pollution**: Both plugins were using the same `\Stripe\` namespace, causing class conflicts.

## Comprehensive Solution Implemented

### 1. Force Loading System (`include/stripe-force-loader.php`)

Created a robust force loading system to prevent conflicts:

```php
// Force load our Stripe library and prevent any interference
function wpgv_force_load_stripe_library() {
    // Clear any existing autoloaders that might interfere
    wpgv_clear_autoloaders();

    // Force load our specific Stripe library
    $stripe_path = WPGIFT__PLUGIN_DIR . '/vendor/stripe/stripe-php/init.php';

    if (file_exists($stripe_path)) {
        // Clear any existing Stripe classes
        wpgv_clear_stripe_classes();

        // Force load our Stripe library
        require_once($stripe_path);

        // Verify that the Checkout Session class is available
        if (class_exists('\Stripe\Checkout\Session')) {
            return true;
        }
    }
    return false;
}
```

**Key Features:**

- **Autoloader Clearing**: Removes conflicting autoloaders
- **Class Clearing**: Attempts to clear existing Stripe classes
- **Direct Path Loading**: Loads from specific vendor path
- **Verification**: Checks for required Checkout Session class
- **Comprehensive Logging**: Detailed error logging for debugging

### 2. Early Loading Integration (`gift-voucher-pro.php`)

Integrated force loading into the main plugin file:

```php
// Force load Stripe library immediately to prevent conflicts
require_once(WPGIFT__PLUGIN_DIR . '/include/stripe-force-loader.php');

// Early Stripe library loading to prevent conflicts with other plugins
function wpgv_early_stripe_loader() {
    wpgv_force_load_stripe_library();
}
// Load Stripe library as early as possible
wpgv_early_stripe_loader();

// Hook to load Stripe library before other plugins
add_action('plugins_loaded', 'wpgv_early_stripe_loader', 1);
```

### 3. Updated Payment Processing Files

Modified all payment processing files to use force loader functions:

- `include/wpgv_item_pdf.php`
- `include/wpgv_voucher_pdf.php`
- `include/wpgv_giftcard_pdf.php`

Changed from:

```php
wpgv_init_isolated_stripe($stripe['secret_key'])
wpgv_create_isolated_stripe_session($session_data)
```

To:

```php
wpgv_init_force_stripe($stripe['secret_key'])
wpgv_create_force_stripe_session($session_data)
```

### 4. Updated Webhook (`include/stripewebhook.php`)

Updated webhook to use force loader:

```php
//include Stripe PHP library using force loader
require_once( WPGIFT__PLUGIN_DIR .'/include/stripe-force-loader.php');
wpgv_force_load_stripe_library();
```

### 5. Debug Framework (`include/stripe-debug.php`)

Added comprehensive debugging capabilities:

- **Library Loading Tests**: Verifies force loading works
- **Class Availability Tests**: Checks for required classes and methods
- **Initialization Tests**: Tests Stripe API key setting
- **Session Creation Tests**: Tests session creation with dummy key
- **Debug Access**: Admin-only test endpoint for troubleshooting

## Files Modified

1. **`gift-voucher-pro.php`** - Added force loading integration
2. **`include/stripe-force-loader.php`** - New force loading system
3. **`include/stripe-debug.php`** - New debug framework
4. **`include/wpgv_item_pdf.php`** - Updated to use force loader functions
5. **`include/wpgv_voucher_pdf.php`** - Updated to use force loader functions
6. **`include/wpgv_giftcard_pdf.php`** - Updated to use force loader functions
7. **`include/stripewebhook.php`** - Updated to use force loader


## Benefits

1. **Conflict Prevention**: Prevents other plugins from overriding the Stripe library
2. **Version Compatibility**: Ensures the correct Stripe library version is used
3. **Error Isolation**: Better error handling and logging
4. **Testing Capability**: Built-in testing framework for troubleshooting
5. **Backward Compatibility**: Maintains existing functionality while adding isolation

## Testing

To test the Stripe library force loading, administrators can access:

```
https://yoursite.com/?wpgv_debug_stripe=1
```

This will run comprehensive tests and log the results to the WordPress error log.

## Error Logging

All Stripe-related operations now include detailed logging with the "WPGV:" prefix for easy identification in logs.

## Future Maintenance

- Monitor for new Stripe library versions
- Update library paths if needed
- Test compatibility with new WordPress versions
- Consider implementing automatic library updates
