reactjs - Stripe 参数_invalid_integer

我正在尝试从 Stripe 设置 PaymentIntents API,需要传递给 API 的金额非常困惑。

Stripe 文档: 所有 API 请求都希望以货币的最小单位提供金额。例如,要收取 10 美元,请提供 1000 的金额值(即 1000 美分)。对于零小数货币,仍提供整数金额,但不乘以 100。例如,要收取 500 日元,请提供 500 的金额值。

我的前端代码将价格传递给我的后端:

const { data: clientSecret } = await axios.post("http://127.0.0.1:8000/paymentIntent/", {
        amount: price * 100
      });

我的后端:

@api_view(['POST'])
def payment(request):
    try:
        amount = request.amount
        paymentIntent = stripe.PaymentIntent.create(
            amount = amount,
            currency = "usd",
            # capture_method='manual',
            # metadata={'integration_check': 'accept_a_payment'},
        ) 

        data = paymentIntent.client_secret

如果我输入 amount = 45(例如随机数),付款意向有效并且一切顺利。如果我将金额作为从我的前端发送的请求的价格,它会显示此错误消息:

{
  "error": {
    "code": "parameter_invalid_integer",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-invalid-integer",
    "message": "Invalid integer: {\"amount\":4300}",
    "param": "amount",
    "type": "invalid_request_error"
  }
}

在上面的错误中,商品的价格是 43 美元,所以我只输入 43 x 100,这不正确吗?也许金额没有作为整数传递,但我觉得没问题。

最佳答案

错误是说你已经传递了 {\"amount\":4300}——看起来你不小心传递了一些对象的整数值。

例如:您发送的内容类似于 amount={\"amount\":4300} 而不是预期的 amount=4300

您需要查看 amount 是如何传递并提供给 Payment Intent 创建调用的。

更新(不是修复): 尝试使用不同的变量名称:

requestAmount = request.amount
paymentIntent = stripe.PaymentIntent.create(
  amount = requestAmount,
  currency = "usd",
) 

更新 2:可能需要解析 json 主体:

data = json.loads(request.body)
paymentIntent = stripe.PaymentIntent.create(
  amount = data.amount,
  currency = "usd",
) 

关于reactjs - Stripe 参数_invalid_integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64485515/

相关文章:

docker - 使用 .Net Core 3.1 api (docker) 运行 Heroku 时

reactjs - 如何使用代理将 Flask API 和 React 前端微服务部署到 Googl

python - 异常值处理部分零值过多怎么办?

reactjs - React craco tailwind postcss 集成

python - pygame双显示器和全屏

java - 对于 IntelliJ 中的 Java 项目,Run 命令究竟做了什么?从 ./mvn

git - 无法推送到 bitbucket 403 "authentication failed"

html - 输入类型 "email"在:focus state时变为 "text"

python - 如何为带有输入的代码编写单元测试 (Python)

graphql - 在 Gatsby 上渲染 Contentful Reference (many)