Invalid array or Invalid integer in using Stripe "unit_amount"

Viewed 26

I have a problem when I want to integer Stripe on my project in Symfony. I use Stripe v.9.5.0 and I have a different errors when I implement "unit_amount" for Stripe

I have a OrderController:

$product_for_stripe = []; $YOUR_DOMAIN = 'https://localhost:8000/';

        //Enregister sur mon entity Order_details

        foreach ($cart->getFull() as $product) {
            $orderDetails = new OrderDetails();
            $orderDetails->setMyOrder($order);
            $orderDetails->setProduct($product['product']);
            $orderDetails->setQuantity($product['quantity']);
            $orderDetails->setPrice($product['product']->getPrix());
            $orderDetails->setTotal($product['product']->getPrix() * $product['quantity']);
            $this->entityManager->persist($orderDetails);

            $product_for_stripe[] = [
                'price_data' => [
                    'currency' => 'eur',
                    'product_data' => [
                        'name' => $product['product']->getName(),
                        'images' => $product['product']->getImage()
                    ],
                    'unit_amount' => $orderDetails->getPrice(),
                ],
                'quantity' => $product['quantity'],
            ];

        }


        //$this->entityManager->flush();

        Stripe::setApiKey('sk_test_51LiLoMDGYOFHKYepnY3xMBT5vwMJWH2XR3ntN9GpHXYtapN29AvQVty21GPUx0qVa2J6MWFr69ke3Yq1p3MJL1yV00kCU59YvE');

        $checkout_session = Session::create([
            'line_items' =>
                [$product_for_stripe],
            'mode' => 'payment',
            'success_url' => $YOUR_DOMAIN . '/success.html',
            'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
        ]);

        dump($checkout_session->id);
        dd($checkout_session);

When I want debug $checkout_session I have this error:

"Invalid integer: 532.46"

I have tried "round($orderDetails->getPrice())" for "unit_amount" and I have this error:

"Invalid array"

I'm blocked, it's a project for my degree in school, if somebody can help me and have a solution , I would be happy.

Thank you

I'm sorry for my bad english

1 Answers

unit_amount should be a non-negative integer in a currency’s smallest unit also called as minor unit.

For example, EUR's smallest unit is cents. EUR 10 is equivalent to 1000 cents. In Checkout Session creation request, unit_amount will then be set to 1000 with currency='eur'.

Related