I am a definite PHP noob, so please bear with me. I am trying to set up a simple donation payment option for my fund raising website. I've got a paypal option, but was looking to add a stripe option as well.
I'm using there simple Checkout process to collect card info and set up the stripe token. I've set up a pay.php file to handle the card info on the server.
<!DOCTYPE html>
<html>
<body>
<?php
// secret key
***Stripe::setApiKey("sk_test_##########");***
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
//Create the charge on Stripe's servers
try {
$charge = Stripe_Charge::create(array(
"amount" => 500, // amount in cents, again
"currency" => "aud",
"card" => $token,
"description" => "payinguser@example.com")
);
} catch(Stripe_CardError $e) {
// card decline
}
?>
</body>
</html>
Obviously I use the right secret key, I've just blocked it out here. Everything seems to work with the form, but when it POSTS to pay.php it throws up this error.
Fatal error: Class 'Stripe' not found in /home/munkeychunk/public_html/pay.php on line 8
Line 8, as highlighted above is the secret key component/API key. As I said, I'm a bit of a PHP novice, and i'm not sure how or what to set the class 'Stripe' to!
Most of the PHP is lifted straight from stripe's own documentation, but it doesn't seem to work straight out of the box. My attempt to solve was to try to 'include Stripe.php'
, using an external stripe.php page which is used if you process payments using javascript/jquery rather than stripe's checkout option.
Any help would be greatly appreciated - please be gentle with your comments!
I am guessing you haven't included Stripe library as given in Stripe Documentation. So it should be like
<!DOCTYPE html>
<html>
<body>
<?php
//Include stripe library first before doing anything related to stripe here
require_once('./lib/Stripe.php');
// secret key
***Stripe::setApiKey("sk_test_##########");***
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
//Create the charge on Stripe's servers
try {
$charge = Stripe_Charge::create(array(
"amount" => 500, // amount in cents, again
"currency" => "aud",
"card" => $token,
"description" => "payinguser@example.com")
);
} catch(Stripe_CardError $e) {
// card decline
}
?>
</body>
</html>
Documentation : https://stripe.com/docs/checkout/guides/php
Note : A PHP >= 5.2 environment is required
Library download: https://code.stripe.com/stripe-php-latest.zip
I was having the same problem but then I installed Stripe using Composer. Then in my PHP file I require_once the autoload.php file and that seemed to pull in the Stripe library
<?php
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account
require_once('/home2/username/vendor/autoload.php');
\Stripe\Stripe::setApiKey("sk_test_########");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array(
"amount" => 1000, // amount in cents, again
"currency" => "usd",
"source" => $token,
"description" => "payinguser@example.com")
);
} catch(\Stripe\Error\Card $e) {
// The card has been declined
};
?>
</div>
For those of you who still get the error message
Class 'Stripe' not found in /home/munkeychunk/public_html/pay.php on line 8
I'm with you. I got around this problem by doing require_once to all the necessary Stripe files in /lib/. Order matters, here's what worked for me
require_once ('./stripe-php-2.1.1/lib/Stripe.php') ;
require_once ('./stripe-php-2.1.1/lib/Util/Set.php') ;
require_once ('./stripe-php-2.1.1/lib/Util/RequestOptions.php') ;
require_once ('./stripe-php-2.1.1/lib/Util/Util.php') ;
require_once ('./stripe-php-2.1.1/lib/Error/Base.php') ;
require_once ('./stripe-php-2.1.1/lib/Error/InvalidRequest.php') ;
require_once ('./stripe-php-2.1.1/lib/Object.php') ;
require_once ('./stripe-php-2.1.1/lib/ApiRequestor.php') ;
require_once ('./stripe-php-2.1.1/lib/ApiResource.php') ;
require_once ('./stripe-php-2.1.1/lib/SingletonApiResource.php') ;
require_once ('./stripe-php-2.1.1/lib/Charge.php') ;
$files = glob('./stripe-php-2.1.1/lib/*.php');
foreach ($files as $file) {
require_once($file);
}
$files = glob('./stripe-php-2.1.1/lib/Error/*.php');
foreach ($files as $file) {
require_once($file);
}
$files = glob('./stripe-php-2.1.1/lib/Util/*.php');
foreach ($files as $file) {
require_once($file);
}
require_once('/home2/username/vendor/autoload.php');
Instead of autoload.php
use init.php
Depending on where your Stripe files are, you may have to edit the path statements on each line of init.php
The use of autoload.php
is for when Stripe is installed with Composer. When you wish to use Stripe with a manual install, use init.php
where the Stripe devs have provided all the functionality contained in the answer above.