w_lpz
w_lpz

Reputation: 613

Wordpress-Stripe Integration: Fatal error: Uncaught Error: Class 'Stripe' not found

I am getting the following error when trying to submit a test payment on my stripe account:

Fatal error: Uncaught Error: Class 'Stripe' not found in /home/dh_y3rvc7/vvnow.dreamhosters.com/wp-content/plugins/wp-stripe- integration/includes/process-payment.php:25 Stack trace: #0 /home/dh_y3rvc7/vvnow.dreamhosters.com/wp-includes/class-wp- hook.php(286): vvnow_stripe_process_payment('') #1 /home/dh_y3rvc7/vvnow.dreamhosters.com/wp-includes/class-wp- hook.php(310): WP_Hook->apply_filters(NULL, Array) #2 /home/dh_y3rvc7/vvnow.dreamhosters.com/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #3 /home/dh_y3rvc7/vvnow.dreamhosters.com/wp-settings.php(450): do_action('init') #4 /home/dh_y3rvc7/vvnow.dreamhosters.com/wp- config.php(89): require_once('/home/dh_y3rvc7...') #5 /home/dh_y3rvc7/vvnow.dreamhosters.com/wp-load.php(37): require_once('/home/dh_y3rvc7...') #6 /home/dh_y3rvc7/vvnow.dreamhosters.com/wp-blog-header.php(13): require_once('/home/dh_y3rvc7...') #7 /home/dh_y3rvc7/vvnow.dreamhosters.com/index.php(17): require('/home/dh_y3rvc7...') #8 {main} thrown in /home/dh_y3rvc7/vvnow.dreamhosters.com/wp-content/plugins/wp-stripe- integration/includes/process-payment.php on line 25

Line 25 is considered Stripe::setApiKey($secret_key); inside of process-payment.php:

<?php

function vvnow_stripe_process_payment() {
    if(isset($_POST['action']) && $_POST['action'] == 'stripe' && wp_verify_nonce($_POST['stripe_nonce'], 'stripe-nonce')) {

        global $stripe_options;

        // load the stripe libraries
        require_once(STRIPE_BASE_DIR . '/lib/Stripe.php');
        // require_once('./init.php');
        // require_once(realpath(dirname(__FILE__) . '/../includes/init.php'));

        // retrieve the token generated by stripe.js
        $token = $_POST['stripeToken'];

        // check if we are using test mode
        if(isset($stripe_options['test_mode']) && $stripe_options['test_mode']) {
            $secret_key = $stripe_options['test_secret_key'];
        } else {
            $secret_key = $stripe_options['live_secret_key'];
        }

        // attempt to charge the customer's card
        try {
            Stripe::setApiKey($secret_key);
            $charge = Stripe_Charge::create(array(
                    'amount' => 1000, // $10
                    'currency' => 'usd',
                    'card' => $token
                )
            );


            // redirect on successful payment
            $redirect = add_query_arg('payment', 'paid', $_POST['redirect']);

        } catch (Exception $e) {
            // redirect on failed payment
            $redirect = add_query_arg('payment', 'failed', $_POST['redirect']);
        }

        //will redirect back to our previous page with the added query variable
        wp_redirect($redirect); exit;
    }
}
add_action('init', 'vvnow_stripe_process_payment');

Been trying to figure out where the issue relies on since been following Stripe's API guideline but haven't been lucky. Any help will be appreciated.

Upvotes: 0

Views: 1760

Answers (2)

kristina
kristina

Reputation: 182

You need to require your Stripe php library, from whatever directory you have it in, at the beginning.

    require_once('stripe-php-6.28.0/init.php');

    function vvnow_stripe_process_payment() {
    if(isset($_POST['action']) && $_POST['action'] == 'stripe' && wp_verify_nonce($_POST['stripe_nonce'], 'stripe-nonce')) {

        global $stripe_options;

    //the rest of your code here....

Upvotes: 0

Cedric
Cedric

Reputation: 5303

It's because it uses a namespace. Try to add \ at before Stripe :

\Stripe\Stripe::setApiKey($stripe['secret_key']);

If it still fails, you actually want to include require_once('../vendor/stripe/init.php') instead of /lib/Stripe.php (or use Composer to install stripe and autoload the libraries).

Upvotes: 2

Related Questions