You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Razorpay Subscriptions API商品金额设置报错求助

Razorpay API 创建订阅计划时金额报错的解决办法

问题现象

  • 输入数字类型金额(如0.05)时,报错:Amount should be atleast 0.10 USD
  • 输入小数类型金额(如0.10)时,报错:Amount should be integer

相关代码:

try{
  const data = await razorpay.items.create(body);
  res.status(200).send(data);
}catch(err){
  console.log(err);
  res.status(400).send({
    message:err?.error?.description
  });
}

问题原因

Razorpay API的金额规则是必须传入对应货币最小单位的整数:

  • 以美元为例,最小单位是美分,0.10美元对应10美分,必须传整数10
  • 直接传小数会触发“金额需为整数”的校验,传小于10的整数(对应不足0.10美元)会触发金额下限报错

解决方案

  1. 先校验金额不低于平台要求的下限(此处为0.10 USD)
  2. 将金额转换为最小货币单位的整数(乘以100,用Math.round规避浮点精度问题)

修改后的代码示例:

// 处理金额:转换为最小货币单位整数,同时做下限校验
const processPaymentAmount = (amountInUSD) => {
  if (amountInUSD < 0.10) {
    throw new Error('Amount should be atleast 0.10 USD');
  }
  // 用Math.round避免0.10*100出现9.999999999999998这类精度问题
  return Math.round(amountInUSD * 100);
};

try{
  // 处理请求体中的金额字段(假设字段名为amount)
  const processedBody = {
    ...body,
    amount: processPaymentAmount(body.amount)
  };
  const data = await razorpay.items.create(processedBody);
  res.status(200).send(data);
}catch(err){
  console.log(err);
  res.status(400).send({
    message: err?.error?.description || err.message
  });
}

额外说明

  • 不同货币的最小单位不同:比如印度卢比是派萨(同样乘100),日元无小数直接传原金额整数
  • 务必用Math.round或Number.toFixed(0)处理浮点转换,避免精度丢失导致的整数异常

内容的提问来源于stack exchange,提问作者Mr.Vibe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 03:42:07