【问题标题】:Invalid array or Invalid integer in using Stripe "unit_amount"使用 Stripe \"unit_amount\" 时数组无效或整数无效
【发布时间】:2023-01-14 11:47:21
【问题描述】:

当我想在 Symfony 的项目中对 Stripe 进行整数处理时,我遇到了问题。 我使用 Stripe v.9.5.0,当我为 Stripe 实现“unit_amount”时出现不同的错误

我有一个订单控制器:

$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);

当我想要调试 $checkout_session 时,出现以下错误:

“无效整数:532.46”

我已经为“unit_amount”尝试了“round($orderDetails->getPrice())”,但出现此错误:

“无效数组”

我被阻止了,这是我在学校获得学位的项目,如果有人可以帮助我并提供解决方案,我会很高兴。

谢谢

我很抱歉我的英语不好

【问题讨论】:

    标签: php symfony stripe-payments payment


    【解决方案1】:

    unit_amount 应该是currency’s smallest unit 中的非负整数,也称为次要单位。

    例如,欧元的最小单位是分。 10 欧元相当于 1000 欧分。在 Checkout Session 创建请求中,unit_amount 将被设置为 1000currency='eur'

    【讨论】:

    • 感谢您的答复。但我一直不明白。例如我的价格是 $orderDetails->getPrice() 并且当我写 $orderDetails->getPrice()*100 以获得 53246(美分)​​时我有这个错误 - >“无效数组”
    • 我终于修复了错误,问题不是 unit_amount 而是不应该出现的 "'images' => $product['product']->getImage()"
    【解决方案2】:

    您可以使用以下方法,因为它会帮助您使用十进制值作为金额,并在发票和付款期间显示相同的金额。

    数量:Math.round(49.99 * 100)

    这对我有用

    【讨论】:

      猜你喜欢
      • 2016-08-18
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 2023-01-19
      相关资源
      最近更新 更多