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

如何修正Razorpay金额显示错误:将小数转为实际支付金额

问题描述

我正在集成Razorpay支付功能,目前功能可正常运行,但存在金额显示异常问题:获取用户需支付的总金额后,界面显示为小数647.99,而实际应支付金额为64799,请问该如何修复此问题?

相关代码

Django后端代码

def Checkout(request):
    amount_str = request.POST.get('amount')
    amount_float = float(amount_str)
    amount = int(amount_float)
    cart = request.session.get('cart')
    tax = sum(i.get('tax', 0) for i in cart.values() if i)


    payment = client.order.create(
        {
            "amount": amount, 
            "currency": "INR",
            "payment_capture" : "1"
        }
    )
    
    order_id = payment['id']

    context = {
        'tax' : tax,
        'order_id' : order_id,
        'payment' : payment,
    }
    return render(request, 'checkout/checkout.html', context)

前端HTML/JS代码

<form action="{% url 'success' %}" method="post">
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
    "key": "rzp_test_05AdCyeNjGQqYO", 
    "amount": "50000", 
    "currency": "INR",
    "name": "Acme Corp", //your business name
    "description": "Test Transaction",
    "image": "https://example.com/your_logo",
    "order_id": "{{order_id}}", 
    "callback_url": "http://localhost:8000/success/",
    "prefill": { //We recommend using the prefill parameter to auto-fill customer's contact information especially their phone number
        "name": "Gaurav Kumar", //your customer's name
        "email": "gaurav.kumar@example.com",
        "contact": "9000090000" //Provide the customer's phone number for better conversion rates 
    },
    "notes": {
        "address": "Razorpay Corporate Office"
    },
    "theme": {
        "color": "#3399cc"
    }
};
var rzp1 = new Razorpay(options);
document.getElementById('rzp-button1').onclick = function(e){
    rzp1.open();
    e.preventDefault();
}
</script>
</form>
解决方案

核心问题是Razorpay的amount参数要求传入最小货币单位的整数(INR对应派萨,1卢比=100派萨),你的代码直接将浮点金额转成整数,导致单位错误,金额被缩小了100倍。

1. 修复后端金额计算逻辑

将获取到的卢比金额乘以100转成派萨单位的整数,修改后端代码中的金额计算部分:

def Checkout(request):
    amount_str = request.POST.get('amount')
    amount_float = float(amount_str)
    amount = int(amount_float * 100)  # 关键修改:乘以100转换为派萨
    cart = request.session.get('cart')
    tax = sum(i.get('tax', 0) for i in cart.values() if i)


    payment = client.order.create(
        {
            "amount": amount, 
            "currency": "INR",
            "payment_capture" : "1"
        }
    )
    
    order_id = payment['id']

    context = {
        'tax' : tax,
        'order_id' : order_id,
        'payment' : payment,
    }
    return render(request, 'checkout/checkout.html', context)

2. 同步前端金额参数

如果前端的amount不是直接使用后端返回的payment.amount,需要确保传入的也是派萨单位的整数,推荐直接绑定后端返回的支付金额,避免手动维护:

var options = {
    "key": "rzp_test_05AdCyeNjGQqYO", 
    "amount": "{{payment.amount}}",  // 使用后端返回的正确金额
    "currency": "INR",
    // 其他参数不变
};

3. 进阶:避免浮点精度问题

如果担心浮点数运算的精度误差,可以改用字符串分割的方式计算金额:

amount_str = request.POST.get('amount')
if '.' in amount_str:
    rupees, paise = amount_str.split('.', 1)
    paise = paise.ljust(2, '0')[:2]  # 确保派萨是两位,不足补0、超过截断
    amount = int(rupees) * 100 + int(paise)
else:
    amount = int(amount_str) * 100

内容的提问来源于stack exchange,提问作者vivi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 03:23:15