I uploaded the code Stripe instructed in Step 1: Embed the Checkout on my PHP webpage:
<form action="" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_ghkfasasgipnDFA6fAB"
data-amount="2000"
data-name="Demo Site"
data-description="2 widgets ($20.00)"
data-image="/128x128.png">
</script>
</form>
Then I uploaded the PHP code on the same web page below the code mentioned above:
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account
\Stripe\Stripe::setApiKey("sk_test_ghkfasasgipnDFA6fAB");
// 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
}
After bringing the web page onto my FTP server the first code worked but the second did not. If anyone may know why any help would be greatly appreciated. Thank you.
Try adding the second code in <?php
?>
tags so your page would be.
somepage.php
<form action="" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_ghkfasasgipnDFA6fAB"
data-amount="2000"
data-name="Demo Site"
data-description="2 widgets ($20.00)"
data-image="/128x128.png">
</script>
</form>
<?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
\Stripe\Stripe::setApiKey("sk_test_ghkfasasgipnDFA6fAB");
// 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
}
?>
The thing is, the line \Stripe\Stripe::setApiKey("...")
is referring to a PHP Class, that is not in the script above. This means there is some other code some where, that should be included in the page as well. Do you have the stripe php library installed as well? https://stripe.com/docs/checkout/guides/php