Frog82
Frog82

Reputation: 464

Class 'Stripe\StripeCharge' not found

I've had a look at a few answer to this quesition on Stack Overflow, but there solutions didnt work for me. Im receiving the following error whilst testing the Stripe API

Class 'Stripe\StripeCharge' not found

This is the code that I am using:

require_once('app/init.php');

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

if(isset($_POST['stripeToken'])){

    $token = $_POST['stripeToken'];

    try {

        \Stripe\StripeCharge::create(array(
            "amount" => 2000,
            "currency" => "gbp",
            "card" => "$token",
            "description" => $Email
            ));

    } catch(Stripe_CardError $e){

        //Error Payment

    }
}

echo $_POST['stripeToken'];

/* Stripe Vairables */
$stripe = [
    'publishable' => 'hidden',
    'private' => 'hidden'
];

I didn't use composer to pull this as it works with the include of the "init" file (supposedly). Any help would be great!

Upvotes: 1

Views: 1997

Answers (1)

Ywain
Ywain

Reputation: 17503

The correct name of the class is \Stripe\Charge, not \Stripe\StripeCharge. You can see an example of a charge creation request in the API documentation: https://stripe.com/docs/api/php#create_charge

Also, the card parameter was renamed to source in the 2015-02-18 API version.

Another problem is that you assign the $stripe array with your API keys at the end. You need to assign it before you can use it in the call to \Stripe\Stripe::setApiKey().

There were a few other minor mistakes. Here is a corrected version of your code:

require_once('app/init.php');

/* Stripe variables */
$stripe = [
    'publishable' => 'hidden',
    'private' => 'hidden'
];

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

if(isset($_POST['stripeToken'])) {

    $token = $_POST['stripeToken'];
    $email = $_POST['stripeEmail'];

    try {

        $charge = \Stripe\Charge::create(array(
            "amount" => 2000,
            "currency" => "gbp",
            "source" => $token,
            "description" => $email
        ));

        // echo $charge->id;

    } catch(\Stripe\Error\Card $e) {

        // Card error

    }
}

Upvotes: 1

Related Questions