'"A token may not be passed in as a PaymentMethod. Instead, use payment_method_data with type=card Stripe Error in Laravel

My Stripe Controller :-

 try {
        $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
        $customer = $stripe->customers->create([
            // 'email' => '[email protected]',
            'email' => $user->email,
            'name' => $user->name
            // 'name' => 'test'
        ]);
        $payemntid = $stripe->paymentIntents->create([
            'amount' => 100 * $total_amount,
            'currency' => 'usd',
            'payment_method_types' => ['card'],
            'customer' => $customer->id,
            'payment_method' => $request->stripeToken
        ]);

The Error Comes :- {"success":false,"message":"A token may not be passed in as a PaymentMethod. Instead, use payment_method_data with type=card and card[token]=tok_1KtVZxSF6gyJgm24q5ZpEFWD."}

Any Idea How to Fix This. Thanks In Advance



Solution 1:[1]

As the error states it's not allowed to pass the token into the payment_method. You have to use payment_method_data.


Your code should look something like this:

try {
        $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
        $customer = $stripe->customers->create([
            // 'email' => '[email protected]',
            'email' => $user->email,
            'name' => $user->name
            // 'name' => 'test'
        ]);
        $payemntid = $stripe->paymentIntents->create([
            'amount' => 100 * $total_amount,
            'currency' => 'usd',
            'payment_method_types' => ['card'],
            'customer' => $customer->id,
            'payment_method_data' => [
                'type' => 'card',
                'card' => [
                    'token' => $request->stripeToken
                ]
            ]
        ]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1