0
votes

I have created a very simple website which has the default stripe checkout that submits to a charging page. I have followed the PHP instructions on the stripe website and I am utterly confused as to why it does not work. The checkout page is working perfectly:

<div class="container">
            <form action="charge.php" method="POST">
                <h6> amount </h6>
                <input type="text" name="amount" />
                <h6> test-info </h6>
                <br /> <input type="text" name="pubkey" />
                <script
                    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="pk_test_NC2CbWJLgm82SMzbxRX4JBLo"
                    data-amount="2000"
                    data-name="Demo Site"
                    data-description="2 widgets ($20.00)"
                    data-image="/128x128.png"
                    data-bitcoin="true">
                </script>
            </form>
        </div>

All the inputs from the checkout such as the email, the token and pubkey are transferred to the charge.php page perfectly. In charge.php:

<?php
    require_once('config.php');

    $token = $_POST ['stripeToken'];
    $email = $_POST['stripeEmail'];
    $pubkey = $_POST['pubkey'];
    $amount = $_POST['amount'];
    ?>
    <h3> Public key: <?php echo $pubkey; ?> </h3> 
    <h3> Token: <?php echo $token; ?> </h3> 
    <h3> Amount: <?php echo $amount; ?> </h3> 
    <h3> Email: <?php echo $email; ?> </h3>

    <?php
        try {
            $charge = \Stripe\Charge::create(array(
                "amount" => 1000, // amount in cents, again
                "currency" => "gbp",
                "source" => $token,
                "description" => "Example charge"
            ));
            } catch(\Stripe\Error\Card $e) {

            }


    ?>

    <h6> You've been charged </h6>

all of the variables from the form submission are printed out. However, for some reason the actual charging does not work and the header (i.e. you've been charged) is not printed out. The token is correct so I'm really not sure how it is possible for it not to be charging. Why am I wrong and how could I debug this to find out?

Config.php

<?php 
    require_once('vendor/autoload.php');
    $stripe = array(
        "secret_key" => "sk_test_**********************",
        "publishable_key" => "pk_test_NC2CbWJLgm82SMzbxRX4JBLo"
    );
    \Stripe\Stripe::setApiKey($stripe['secret_key']);
    echo 'this is from config.php';

?>
1
You realise the data in your form is different from the data in your PHP try/catch. This difference is probably why Stripe is not completing the transaction.Martin
Also what does your error $e exception output? It will tell you why the try/catch failed.Martin
The data in the form is just for show, it doesn't actually matter as far as I'm aware (it seems to work now with them being different).user2850249
Please roll your private key, you should never share it in publicMatthew Arkin

1 Answers

0
votes

It seems the problem was data-bitcoin="true". Getting rid of it meant the payments went through.