0

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

gromey
  • 97
  • 2
  • 11
  • Check your server error logs for hints to the 500 error. Here's a first hint: You have a comma on line 7 of your php script when you should have a semi-colon. – aynber Dec 28 '17 at 18:48
  • 2
    You got a `,` instead of a `;` `$total = $_POST['stripeAmount'],` - Use a proper editor! – Lawrence Cherone Dec 28 '17 at 18:49
  • @LawrenceCherone, you're amazing... What editor would you suggest? I'm using Notepad++ and couldn't see this for the life of me! – gromey Dec 28 '17 at 18:52
  • @DavidPottrell anything but Notepad++, [Atom](https://atom.io/) or [Brackets](http://brackets.io/), even [Ace](https://ace.c9.io/) shows basic syntax errors.. to name a few.. – Lawrence Cherone Dec 28 '17 at 18:56

0 Answers0