Skip to content
Advertisement

Stripe charge issue with stripe payment

What I’m doing is simple, using stripes JS code to charge a customer, like so.

<form method="post" action="?p=charge">
    <script 
        src="https://checkout.stripe.com/checkout.js" 
        class="stripe-button" 
        data-key="pk_test_ngaGkIg8PowWzIh5GRS18tRO" 
        data-image="img/logo.png" 
        data-name="Wine Glass Transport" 
        data-description="Transport Case (65.00/each + Shipping)" 
        data-amount="<?php echo $FinalTotal * 100; ?>" > 
    </script>
<input type="hidden" name="final" value="<?php echo $FinalTotal * 100; ?>" />
</form>

From there it gets sent to the charge script like so.

<?php

require_once('inc/stripe/lib/Stripe.php');
try {

    var_dump($_POST);
    Stripe::setApiKey("sk_test_");
    $charge = Stripe_Charge::create(array(
        "amount" => $_POST['final'] / 100,
        "currency" => "usd",
        "card" => $_POST['stripeToken'],
        "description" => "Wine Glass Transport Case"
    ));

    echo '<h1>Your payment has been completed. Thank You,<br><br>Click <a href="https://www.wineglasstransport.com/index.php">Here</a></h1>';

    //users email.
    echo $_POST['stripeEmail'];
} catch (Stripe_CardError $e) {
    echo 'There was an error with your card!<br><br>';
    echo $e;
}

//catch the errors in any way you like

catch (Stripe_InvalidRequestError $e) {
    echo 'We can not process your order there was something wrong with the information submitted, please go back and correct this error,if the error persists please contact the administrator<br><br>';
    echo $e;
    // Invalid parameters were supplied to stripe's API

} catch (Stripe_AuthenticationError $e) {
    echo 'Sorry, we can not connect to stripe, please contact Administrator.';
    // Authentication with stripe's API failed
    // (maybe you changed API keys recently)

} catch (Stripe_ApiConnectionError $e) {
    // Network communication with stripe failed
    echo 'sorry we cant connect to stripe please contact website administrator.';
} catch (Stripe_Error $e) {
    // Display a very generic error to the user, and maybe send
    // yourself an email
} catch (Exception $e) {
    echo 'UH OH, something Really went wrong here, Contact the system administrator ASAP!';
    // Something else happened, completely unrelated to stripe
}

at this point, I have no clue what is going on and I’m getting this error

exception ‘Stripe_InvalidRequestError’ with message ‘Unrecognized request URL (POST: /v1/stripecharges). Please see https://stripe.com/docs or we can help at https://support.stripe.com/.’ in /home/arthmael/PhpstormProjects/Vino/inc/stripe/lib/Stripe/ApiRequestor.php:147 Stack trace: #0 /home/arthmael/PhpstormProjects/Vino/inc/stripe/lib/Stripe/ApiRequestor.php(268): Stripe_ApiRequestor->handleApiError(‘{n “error”: {n…’, 404, Array)

1 /home/arthmael/PhpstormProjects/Vino/inc/stripe/lib/Stripe/ApiRequestor.php(109):

Stripe_ApiRequestor->_interpretResponse(‘{n “error”: {n…’, 404) #2 /home/arthmael/PhpstormProjects/Vino/inc/stripe/lib/Stripe/ApiResource.php(143): Stripe_ApiRequestor->request(‘post’, ‘/v1/stripecharg…’, Array, Array) #3 /home/arthmael/PhpstormProjects/Vino/inc/stripe/lib/Stripe/Charge.php(38): Stripe_ApiResource::_scopedCreate(‘Stripe_Charge’, Array, NULL) #4 /home/arthmael/PhpstormProjects/Vino/pages/charge.php(21): Stripe_Charge::create(Array) #5 /home/arthmael/PhpstormProjects/Vino/inc/content.php(6): include(‘/home/arthmael/…’) #6 /home/arthmael/PhpstormProjects/Vino/index.php(6): include(‘/home/arthmael/…’) #7 {main}

Thanks in advance

EDIT: I realized after jumping on the stripe irc, that I was using an outdated API, so I updated to that and here is my code now:

Advertisement

Answer

Here I found 3 problems in my code.

  1. I am using an outdated API 😛

  2. my hidden input for the amount was not being posted because it was after the embedded script. like this…

<form action="?p=charge" method="post">
    <script 
        src="https://checkout.stripe.com/checkout.js" 
        class="stripe-button" 
        data-key="pk_test_ngaGkIg8PowWzIh5GRS18tRO" 
        data-amount="<?php echo $FinalTotal * 100; ?>" 
        data-name="Wine Glass Transport" 
        data-description="Transport Case (65.00/each + Shipping)" 
        data-image="img/logo.png">
    </script>
    <input type="hidden" name="amount" value="<?php echo $FinalTotal; ?>">
</form>

so I just switched it up

<form action="?p=charge" method="post">
<input type="hidden" name="amount" value="<?php echo $FinalTotal; ?>">
    <script 
        src="https://checkout.stripe.com/checkout.js" 
        class="stripe-button" 
        data-key="pk_test_ngaGkIg8PowWzIh5GRS18tRO" 
        data-amount="<?php echo $FinalTotal * 100; ?>" 
        data-name="Wine Glass Transport" 
        data-description="Transport Case (65.00/each + Shipping)" 
        data-image="img/logo.png">
    </script>
</form>
  1. I also did not have the $_POST['stripeToken'] variable on my charge.php page.

thanks for the help though guys!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement