Nazmul Hossain Pappu
Nazmul Hossain Pappu

Reputation: 131

Stripe payment issues, I can't store data into database but in stripe dashboard receiving test amount

I have setup the stripe payment system but i am getting Fatal error like this: {Fatal error: Uncaught (Status 400) (Request req_f142wGYY4VDGXx) You cannot use a Stripe token more than once: tok_1PPNrbGMcptyt0mO8IbOzi1T. thrown in C:\xampp\htdocs\join\stripe-php\lib\Exception\ApiErrorException.php on line 38}

I want to to store the stripe payment info from user, but the problem is I am receiving the amount into stripe dashboard but not into my database and getting this fatal errors!

here is my code, and it's started from line 38:

    // set API Key
    $stripe = array(
    "SecretKey"=>"sk_test_51PNnHTGMcptyt0mOGyt8G6VmAvXhlJ7ojBpzdlI1Ey2JLrDghSZ2Nz5UszwSJr9madoDm",
    "PublishableKey"=>"pk_test_51PNnHTGMcptyt0mOSgd8HrIaMr0mj0DM6lyd7W2EqlnzL3L4CHKJ0Yf2EPd9Qid8dOeGXNcwK8j"
    );

    \Stripe\Stripe::setApiKey($stripe['SecretKey']);

    // Add customer to stripe 
    $customer = \Stripe\Customer::create(array( 
        'email' => $email, 
        'source'  => $token,
        'name' => $name,
        'description'=>$des
    ));

    // Generate Unique order ID 
    $orderID = strtoupper(str_replace('.','',uniqid('', true)));
     
    // Convert price to cents 
    $itemPrice = ($price*100);
    $currency = "usd";

    // Charge a credit or a debit card 
    $charge = \Stripe\Charge::create(array( 
        'amount'   => $itemPrice, 
        'currency' => $currency, 
        'customer' => $customer->id, 
        'description' => $des
    ));

Anyone can able to solve and tell me where is the problem in this code? Thank you!

Upvotes: 0

Views: 102

Answers (1)

alex
alex

Reputation: 2784

Like what the error mentions, have you already used the token? One possibility is that you have already attached the token to a customer previously i.e. A token can only be attached to a customer once.

You can try looking through the logs in your Stripe Dashboard if you can't recall whether you did so : https://dashboard.stripe.com/test/logs

Charges and Tokens are legacy Stripe APIs, you really shouldn't be using them at this point. You'll want to look into PaymentMethods / PaymentIntents instead. You' can a look at these guides :

For how to save a PaymentMethod for future usage :

Upvotes: 0

Related Questions