-
-
Save maccman/2f110e12237b4d83ae8c to your computer and use it in GitHub Desktop.
<?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 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.
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.
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.
In charge.php, are you charging the user $5 or $50?
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?
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?
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?
How do you actually pass the data-description string into the Stripe system?
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
Is it possible to use this with the custom button?
I keep getting "This card was declined"!
I am using a valid good card!
Could you do a tutorial for the custom checkout where billing and shipping address are activated please
In this example the amount being charged is fixed in the code. How do you make it so that the amount is a variable?
I use Codeigniter. A MVC PHP framework. I need my form to post csrf token to controller. How do I get it working?
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?
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.
Another way would be like this:
index.php:
>charge.php:
'amount' => $_POST['amount']
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?
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 ?
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... !!
Not sure why, but I could only get this to work by removing getenv from the array code in config.php.