【问题标题】:How can I adapt this Stripe checkout code to work with cents as well as dollars?如何调整此 Stripe 结帐代码以使用美分和美元?
【发布时间】:2023-03-07 19:23:01
【问题描述】:

我正在处理来自 Stripe 网站的代码

网址https://stripe.com/docs/checkout/integration-builder

这是直接来自网站的,对我来说很好用

$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price_data' => [
      'currency' => 'usd',
      'unit_amount' => 2000,
      'product_data' => [
        'name' => 'Stubborn Attachments',
        'images' => ["https://i.imgur.com/EHyR2nP.png"],
      ],
    ],
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.html',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);

但是,当我将代码更改为以下代码以尝试使美元和美分发挥作用时(使用示例中的unit_amount ...或本页建议的unit_amount_decimal https://stripe.com/docs/billing/subscriptions/decimal-amounts)然后有我不太明白的问题,特别是使用unit_amount_decimal 时返回的错误,因为似乎在 Stripe 中没有办法处理小数位......希望不是这样。

如果不按照他们在该页面上的建议,我该怎么做......这似乎不起作用?

与 2000.55 而非 2000 相同的代码

$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price_data' => [
      'currency' => 'usd',
      'unit_amount_decimal' => 2000.55,
      'product_data' => [
        'name' => 'Stubborn Attachments',
        'images' => ["https://i.imgur.com/EHyR2nP.png"],
      ],
    ],
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.html',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);

网络标签错误

使用unit_amount 无效整数:2000.55

使用unit_amount_decimal Checkout 目前不支持line_items[0] 中小数点后两位以上的美元价格。你传递了2000.55对应$20.0055,不支持。

第二个错误似乎表明这是不可能的,但这是没有意义的,因为产品并不总是美元金额。

【问题讨论】:

  • 似乎使用 200055 作为 2000.55 美元的十进制价格应该可以使用,除非您需要两个以上的小数?

标签: php stripe-payments


【解决方案1】:

Stripe 以货币的最小单位衡量金额 - 例如美元的美分:

'unit_amount' => 2000, //2000 cents or $20

'unit_amount' => 2050, //2050 cents or $20.50

如果您需要使用小数最小单位 - 例如每个动作 1.5 美分,然后使用 'unit_amount_decimal'

'unit_amount_decimal' => 1.5 // 1.5¢ or $0.015 per action

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2016-01-04
    相关资源
    最近更新 更多