Skip to content

Instantly share code, notes, and snippets.

@maccman
Last active December 21, 2021 09:09
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save maccman/2f110e12237b4d83ae8c to your computer and use it in GitHub Desktop.
Save maccman/2f110e12237b4d83ae8c to your computer and use it in GitHub Desktop.
Stripe Button PHP example
<?php
require_once(dirname(__FILE__) . '/config.php');
$token = $_POST['stripeToken'];
$customer = Stripe_Customer::create(array(
'email' => 'customer@example.com',
'card' => $token
));
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => 5000,
'currency' => 'usd'
));
echo '<h1>Successfully charged $5!</h1>';
?>
<?php
require_once('./lib/Stripe.php');
$stripe = array(
secret_key => getenv('secret_key'),
publishable_key => getenv('publishable_key')
);
Stripe::setApiKey($stripe['secret_key']);
?>
<?php require_once('./config.php'); ?>
<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount="5000" data-description="One year's subscription"></script>
</form>
@tylermcole
Copy link

Not sure why, but I could only get this to work by removing getenv from the array code in config.php.

@edpeciulis
Copy link

@tylermcole I had the exact same problem. The chrome console says:
Uncaught Error: You did not set a valid publishable key.
Call Stripe.setPublishableKey() with your publishable key.
For more info, see https://stripe.com/docs/stripe.js

When I remove getenv and the parentheses everything works fine and the test payment goes through.

@edpeciulis
Copy link

I had the same problem. Chrome console shows the following error:

Uncaught Error: You did not set a valid publishable key.
Call Stripe.setPublishableKey() with your publishable key.
For more info, see https://stripe.com/docs/stripe.js

When I remove getenv and the parentheses it works fine.

@briermay
Copy link

Your getting that error because getenv returns an environment variable of the user the webserver runs under. If you own the server you could set it by logging in as the user the webserver runs under and adding it, if its a hosting provider that's impossible, but the much easier approach is to just change it from getenv('secret_key') to 'secret_key'

because the environment variable doesn't exist its returning a null string.
Hope that explains why you get the error from web browsers.

@jeffehobbs
Copy link

In charge.php, are you charging the user $5 or $50?

@anonify
Copy link

anonify commented May 22, 2013

jeffehobbs, in Stripe you express charges in cents. So a value of 5000 would be equal to $50.

Question of my own here, is it possible to add fields to the stripe button? Such as a field for users to enter in coupon codes?

@aarontbarksdale
Copy link

Any particular reason for this?

Fatal error: Uncaught exception 'Stripe_InvalidRequestError' with message 'You cannot use a stripe token more than once: tok_1zq4dfgwtrt7Ft' in /home/content/w/e/b/webmasterbark/html/csac/login/stripe/lib/Stripe/ApiRequestor.php:86 Stack trace: #0 /home/content/w/e/b/webmasterbark/html/csac/login/stripe/lib/Stripe/ApiRequestor.php(136): Stripe_ApiRequestor->handleApiError('{? "error": {?...', 400, Array) #1 /home/content/w/e/b/webmasterbark/html/csac/login/stripe/lib/Stripe/ApiRequestor.php(74): Stripe_ApiRequestor->_interpretResponse('{? "error": {?...', 400) #2 /home/content/w/e/b/webmasterbark/html/csac/login/stripe/lib/Stripe/ApiResource.php(76): Stripe_ApiRequestor->request('post', '/v1/customers', Array) #3 /home/content/w/e/b/webmasterbark/html/csac/login/stripe/lib/Stripe/Customer.php(26): Stripe_ApiResource::_scopedCreate('Stripe_Customer', Array, NULL) #4 /home/content/w/e/b/webmasterbark/html/csac/charge.php(163): Stripe_Customer::create(Array) #5 {main} thrown in /home/content/w/e/b/webmasterbark/html/csac/login/stripe/lib/Stripe/ApiRequestor.php on line 86

Clearly there's an error...all seems to be centered around the ApiRequestor.php error line...but I'm not sure what I did...any suggestions?

@PHPNovice
Copy link

I've had success on MY LAPTOP at processing payments with the following credentials:

"secret_key" => "sk_test_6NWSdHKNi2bMmFEC97KNdSE4",
"publishable_key" => "pk_test_y0Anew0bC8EzOqY0l23HuMvb"

Now, I'd like for the user, after successfully completing a payment, to get directed to some "member's only page" in which that page requires a webhook, API element, etc.

What must I do for this?

@jeffehobbs
Copy link

How do you actually pass the data-description string into the Stripe system?

@roroland
Copy link

It's possible to pass custom label names to the payment form ? (I'm testing the spanish beta of stripe, the form is in english)

Thanks

@BN83
Copy link

BN83 commented Jan 1, 2014

Is it possible to use this with the custom button?

@behnampmdg3
Copy link

I keep getting "This card was declined"!

I am using a valid good card!

@iwarner
Copy link

iwarner commented Feb 9, 2014

Could you do a tutorial for the custom checkout where billing and shipping address are activated please

@drdan18
Copy link

drdan18 commented Feb 10, 2014

In this example the amount being charged is fixed in the code. How do you make it so that the amount is a variable?

@hussaintamboli
Copy link

I use Codeigniter. A MVC PHP framework. I need my form to post csrf token to controller. How do I get it working?

@delay
Copy link

delay commented Apr 10, 2014

I don't understand how this is suppose to be a real example. You are hard coding both the email and the amount in your charge.php. Shouldn't you be getting this from the result of the form that checkout.js creates? As it stands now this example doesn't really explain anything about how to actually get the results from your checkout form in a real situation. How can I pass the payers actual email address and amount and receive it into charge.php in your example?

@kpetrillo
Copy link

You can pass variables just find from this? Inside the form field instead of hard coding, for example, 5000, have it echo whatever amount you're trying to get and add a hidden input with the amount:

index.php:
data-amount= ...
<input type="hidden" name="amount" value=>

charge.php:
amount => $_POST['amount']

That method could be one solutions, seeing how nobody is bothering to answer any of these questions.

@kpetrillo
Copy link

Another way would be like this:

index.php:

>

charge.php:

'amount' => $_POST['amount']

@tobywallis
Copy link

When I run this I receive a PHP error:

"Fatal error: Class 'Stripe_Customer' not found in /home/sites/_domain_.com/public_html/charge.php on line 13"

Any ideas, anybody, please?

@jshort2
Copy link

jshort2 commented Jan 16, 2015

i use wordpress ... trying the simple install , keep getting the error "stripe needs multibyte string php to get to the library" .... im assuming that ive installed the code in the wrong places .... where are they installed ? in my domain url root ? in the sub domains wordpress install root or in the subdomain theme root thats using the paybutton ?

@mohamamdkalim
Copy link

After Getting the new updated version 3.9.2

got an error in config.php so i just replace

require_once('./lib/Stripe.php');
with this:
require_once('stripe-php/init.php');

work awesome thanks... !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment