0

I am attempting to set up the Stripe checkout payment system, but am having trouble with the PHP. When I run the console log, everything executes, except the piece that actually posts the charge to my Stripe dashboard, which is crucial. Below I highlighted some of the potential areas that error my lie. Please let me know if you need more information, as I am glad to help.

Note: the line below "//charge card is where I think the problem is, but I may be mistaken.

`

    require_once('Stripe.php'); 

    $stripe = array(
        **'secret_key' => 'secret key',**
        'publishable_key' => 'publishable key'
        );

        **\Stripe\Stripe::setApiKey($stripe['secret key']);**


    if($_POST) {
        $error = NULL;
        try{
            if (isset($_POST['stripeToken'])) 
                throw new Exception("The Stripe Token was not generated correctly");



        //charge the card
        **$charge = \Stripe\Charge::create(array(**
            'card' => $_POST['stripeToken'],
            'amount' => 2000,
            'currency' => 'usd'
            ));

            }

            catch(Exception $e) {
                $error = $e->getMessage(); 
            }

        $quotes= array(
            "A",
             "B",
             "C" 
            );

            echo "<h1>Here is your quote!</h1>";
            echo "<h2>" . $quotes[array_rand($quotes)]. "</h2>";


    } else{ 
        //show the welcome screen
        ?>
    <form action="index.php" method="POST">
      <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="publishable key"
        data-amount="2000"
        data-name="BB Traders"
        data-description="Wiping Rags Order"
        data-image="/128x128.png">
      </script>
    </form>

`

2 Answers2

1

You are getting Undefined index notice as a result of accessing to unexisting array element. Just check all of your accesses to arrays - $_POST['stripeToken'] or $stripe['secret key'] does not exist.

Also, it's a very common question, for example here you can get a realy clear explanation of how to overcome this notice.

Community
  • 1
  • 1
SilverFire
  • 1,582
  • 13
  • 22
1

I don't really know php but won't

isset($_POST['stripeToken'])

return true if the token IS generated correctly? It looks like you may be throwing an exception for the opposite case that you want.

Try removing that check temporarily or using

isset($_POST['stripeToken'])==FALSE

or whatever the best PHP way of doing that is.