You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce支付API开发问题:WC_Gateway_Stripe类未找到

Fixing "WC_Gateway_Stripe not found" Error in WooCommerce Payment API Development

Let's break down why you're hitting this error and walk through actionable fixes step by step:

1. The Official WooCommerce Stripe Plugin Isn't Installed/Activated

The WC_Gateway_Stripe class doesn't come with core WooCommerce—it's part of the official WooCommerce Stripe Payment Gateway plugin.

  • Quick Check: Head to your WordPress admin → Plugins. Look for "WooCommerce Stripe Payment Gateway":
    • If it’s missing, install it from the WordPress plugin repository.
    • If it’s installed, make sure it’s toggled to "Active".
  • Class Location: Once activated, the class lives in wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-gateway-stripe.php.

2. Your Code Runs Before the Stripe Plugin Loads

WordPress loads plugins in a specific sequence. If your REST API callback executes before the Stripe plugin finishes initializing, the class won’t exist yet.

Fix: Register Your Route at the Right Time

Always use the rest_api_init action hook to register your REST routes. This ensures all plugins (including WooCommerce and Stripe) are fully loaded before your code runs. Here’s how to adjust your route registration:

add_action('rest_api_init', function() {
    register_rest_route(
        'codestage/v1', 
        '/process-payment', 
        array(
            'methods' => WP_REST_Server::CREATABLE, // Uses POST method
            'callback' => 'CodestagePay',
            'permission_callback' => function() {
                // Add security checks here (e.g., verify order ownership)
                return true; // Update this for production use
            }
        )
    );
});

3. Check for Stripe Plugin Version Compatibility

Older versions of the Stripe plugin might use outdated class names or file structures. Ensure you’re running the latest version of the WooCommerce Stripe Payment Gateway plugin to avoid compatibility gaps.

4. Double-Check Class Name Spelling

PHP class names are case-sensitive on most servers. Confirm you’re using the exact capitalization: WC_Gateway_Stripe (capital W, C, G, S). Typos like wc_gateway_stripe or WC_Gateway_Stripe_ will trigger the "not found" error.

Bonus: Fix Other Bugs in Your Code

I spotted a couple of issues in your CodestagePay function that need addressing:

  • The line return new WP_REST_Response( array("c"), 123 ); in the failure block exits the function early, so the subsequent $response setup never runs—remove this line.
  • The final return uses status code 123, which isn’t a valid HTTP status. Use 200 for success, and standard 4xx codes for errors.

Here’s the cleaned-up Stripe payment block:

if ( $payment_method === "stripe" ) {
    $wc_gateway_stripe = new WC_Gateway_Stripe();
    $_POST['stripe_token'] = $payment_token;
    $payment_result = $wc_gateway_stripe->process_payment( $order_id );
    
    if ( $payment_result['result'] === "success" ) {
        $response['code'] = 200;
        $response['message'] = __( "Your Payment was Successful", "wc-rest-payment" );
        $order = wc_get_order( $order_id );
        // Update order status to completed if it's processing
        if( $order->get_status() === 'processing' ) {
            $order->update_status( 'completed' );
        }
        return new WP_REST_Response( $response, 200 );
    } else {
        $response['code'] = 400;
        $response['message'] = __( "Please enter valid card details", "wc-rest-payment" );
        return new WP_REST_Response( $response, 400 );
    }
}

内容的提问来源于stack exchange,提问作者Călin Marin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 16:32:40