I'm using Stripe (advanced checkout) with PHP. I'm at a loss as to the Error 500 I'm receiving upon entering the test card details in.
I can confirm the PHP version is 7+
My code is:
<script>
var handler = StripeCheckout.configure({
key: 'XXXXXXXXXXXXXXXXXXXXXXX',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
token: function(token) {
var $theToken = $('<input type=hidden name=stripeToken />').val(token.id);
$('form').append($theToken);
$("#stripeAmount").val(Math.floor($("#stripePence").val() * 100));
var path = "<?php bloginfo('template_url');?>/charge.php";
$.post(path, $("form").serialize(), function (data) {
if (data == "success") {
alert("Success");
}
else {
alert(data);
}
});
}
});
$('#submit').on('click', function(e) {
var stripeAmount = Math.floor($("#stripePence").val() * 100);
var displayAmount = parseFloat(Math.floor($("#stripePence").val() * 100) / 100).toFixed(2);
var stripeEmail = $('#email').val();
handler.open({
name: 'Demo Site',
description: 'Custom amount ($' + displayAmount + ')',
currency: 'gbp',
email: stripeEmail,
amount: stripeAmount,
});
e.preventDefault();
});
$(window).on('popstate', function() {
handler.close();
});
</script>
Charge.php
<?php
try {
require_once('config.php');
$email = $_POST['stripeEmail'];
$token = $_POST['stripeToken'];
$total = $_POST['stripeAmount'],
$err = array();
$customer = \Stripe\Customer::create(array(
"email" => $email,
"source" => $token,
));
$charge = \Stripe\Charge::create(array(
"customer" => $customer->id,
"amount" => 1000,
"currency" => "gbp",
"description" => "Payment for Flowers"
));
} catch (\Stripe\Error\ApiConnection $e) {
$err[] = $e->getMessage();
} catch (\Stripe\Error\InvalidRequest $e) {
$err[] = $e->getMessage();
} catch (\Stripe\Error\Api $e) {
$err[] = $e->getMessage();
} catch (\Stripe\Error\Card $e) {
$err[] = $e->getMessage();
}
/*Count errors (if any)*/
if(count($err)) {
foreach($err as $one_er){
echo $one_er . "<br/>";
}
exit();
}
if (empty($err) === true) {
echo "success";
}
?>
Looking at the console.log, stripeName, stripeEmail, stripeToken and stripeAmount are all being sent to charge.php okay.
I'm trying to allow the user to select various options which change the price (so amount to be a variable).
Does anyone else see something I'm missing or have done incorrectly?
Many thanks