Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe Error 400 - Cannot use stripe token more than once

Tags:

I keep receiving error code 400 on my stripe dashboard. It seems like im using the same stripe token more than once and this produces an error. Below is my code.

Js:

    <script src="https://checkout.stripe.com/checkout.js"></script>     <script>      var handler = StripeCheckout.configure({         key: 'pk_test_******************',         image: '/img/documentation/checkout/marketplace.png',         token: function(token) {           /*$.post("php/charge.php",{stripeToken:token.id},function(data,status){               console.log("Data: "+ data+"\nStatus: "+status);           });*/           alert(token.used);//alerts false           $.post("php/charge.php",{stripeToken:token.id});           alert(token.used);// still alerts false         }       });        $('#myButton').on('click', function(e) {         // Open Checkout with further options         handler.open({           name: 'Demo Site',           description: '2 widgets',           currency: "cad",           amount: 2000         });         e.preventDefault();       });        // Close Checkout on page navigation       $(window).on('popstate', function() {         handler.close();       });     </script> 

Php:

<?php   require_once('config.php');    $token  = $_POST['stripeToken'];    $customer = \Stripe\Customer::create(array(       'email' => '[email protected]',       'card'  => $token   ));    //try {     $charge = \Stripe\Charge::create(array(       "amount" => 1000, // amount in cents, again       "currency" => "cad",       "source" => $token,       "description" => "Example charge")     );     //}catch(\Stripe\Error\Card $e) {       // The card has been declined     //} ?> 

Can anyone tell my why I cant charge a customer? How am I using a key multiple times?

like image 753
alaboudi Avatar asked Jun 10 '15 02:06

alaboudi


People also ask

Do Stripe tokens expire?

The card token is valid for a few minutes (usually up to 10). What Stripe recommends in that case is to use the token now to create a customer via the API first to save its card and then let your background job handle the charge part after the fact.

What does Stripe token mean?

Tokenization is the process Stripe uses to collect sensitive card or bank account details, or personally identifiable information (PII), directly from your customers in a secure manner. A token representing this information is returned to your server to use.

What does Stripe error mean?

The customer must use another card or method of payment. processing_error. An error occurred while processing the card. The payment needs to be attempted again. If it still can't be processed, try again later.


1 Answers

You do use the token twice.

First, when creating the customer. Second, when trying to charge the card.

Instead, you can create a customer and and then pass $customer->id to Stripe when you create the charge:

$charge = \Stripe\Charge::create(array(   "amount" => 1000, // amount in cents, again   "currency" => "cad",   "customer" => $customer->id,   "description" => "Example charge") ); 
like image 52
Tushar Avatar answered Sep 17 '22 15:09

Tushar