I've been working on integrating a Stripe checkout form into my website for customers to sign up for a subscription. I've downloaded the Stripe library via composer and it is up to date, here is my file structure
My Stripe API Keys have been replaced with Asterisks for obvious reasons.
The files are the same as the one in my test. The test worked and sent it to Stripe and everything worked. In this new site the form won't even open and I get the error:
{"error":{"type":"invalid_request","message":"Invalid
keyparameter."}}
When I do put in my stripe TEST keys, I copy and paste them and make sure there are no spaces between the key and the quotes. My thought is that something is wrong with a connection somewhere because the form won't even open. I've been working on this the last 3 days and have read all of Stripe's Docs. My questions are: Can a stripe subscription button be integrated if i'm using Bootstrap?, If I have Stripe in test mode and i'm using the test keys do I still have to have it under an SSL and HTTPS just to test it?, and is there an error in my code that would make the API Key Invalid? Here is the index.php and charge.php for the stripe checkout form.
Index.php
<?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-image="https://pixel.nymag.com/imgs/daily/science/2014/11/13
data-amount="1200"
data-name="Remember My People"
data-description="RMP Monthly subscription"
data-label="Sign Me Up!"
data-billing-address="true"
>
</script>
</form>
Charge.php
<?php
require_once('./config.php');
$token = $_POST['stripeToken'];
try {
$customer = \Stripe\Customer::create(array(
'email' => $_POST['stripeEmail'],
'source' => $_POST['stripeToken'],
'plan' => 'rmp_monthly'
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => 1200,
'currency' => 'usd'
));
header('Location: https://remembermypeople.com');
exit;
}
catch (Exception $e) {
// header('Location:oops.html');
error_log("unable to sign up customer" . $_POST['stripeEmail']. ", error" . $e->getMessage());
}
Stripe's support team emailed me back... it turned out all of my code was correct and the reason my PHP was not running was because I was viewing my website directly through the browser and my PHP never ran because it never reached a server... I forgot that since PHP is a server side language that it's only executed by the server once it reaches the server! Since it didn't reach the server, the browser just read it as text! I hope this helps anyone else who was as confused as I was! To set up a local php server on your Macbook (may be different on windows) open up the terminal, change into the directory your project is in, then run this code once you're in the directory
php -S localhost:3000
Then go into your browser and go to the url "localhost:3000"
When I did this, the code reached a server and was interpreted/executed!
I would bet you anything that your require_once('./config.php')
in index.html is not pointed correctly. The error you are getting is most likely because the data-key="<?php echo $stripe['publishable_key']; ?>"
is not able to echo the variable for the config.php file.
A quick solution would be to just replace <?php echo $stripe['publishable_key']; ?>"
with your public key.