elunez/eladmin · warning · BadRequestException

测试金额过大

Error message

测试金额过大

What it means

Thrown by AliPayServiceImpl.toPayAsWeb when Double.parseDouble(trade.getTotalAmount()) is <= 0 or >= 5000 (hard-coded maxMoney = 5000). It is a demo-safety guard for the sandbox payment feature: amounts must be strictly between 0 and 5000. A NumberFormatException would instead escape parseDouble if totalAmount is not numeric.

Source

Thrown at eladmin-tools/src/main/java/me/zhengjie/service/impl/AliPayServiceImpl.java:101

                "    \"sys_service_provider_id\":\""+alipay.getSysServiceProviderId()+"\"" +
                "    }"+
                "  }");//填充业务参数
        // 调用SDK生成表单, 通过GET方式,口可以获取url
        return alipayClient.pageExecute(request, "GET").getBody();

    }

    @Override
    public String toPayAsWeb(AlipayConfig alipay, TradeVo trade) throws Exception {
        if(alipay.getId() == null){
            throw new BadRequestException("请先添加相应配置,再操作");
        }
        AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());

        double money = Double.parseDouble(trade.getTotalAmount());
        double maxMoney = 5000;
        if(money <= 0 || money >= maxMoney){
            throw new BadRequestException("测试金额过大");
        }
        // 创建API对应的request(手机网页版)
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl(alipay.getReturnUrl());
        request.setNotifyUrl(alipay.getNotifyUrl());
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":"+trade.getTotalAmount()+"," +
                "    \"subject\":\""+trade.getSubject()+"\"," +
                "    \"body\":\""+trade.getBody()+"\"," +
                "    \"extend_params\":{" +
                "    \"sys_service_provider_id\":\""+alipay.getSysServiceProviderId()+"\"" +
                "    }"+
                "  }");
        return alipayClient.pageExecute(request, "GET").getBody();
    }
}

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Send totalAmount as a plain numeric string in yuan between 0 and 5000, e.g. "9.90".
  2. Fix the frontend unit (yuan, not cents) and strip thousands separators before submitting.
  3. If this is a real integration (not the demo), raise or make maxMoney configurable instead of editing the hard-coded 5000 inline.
  4. Validate the amount format client-side before calling the API to avoid NumberFormatException on non-numeric input.

Example fix

// before
double money = Double.parseDouble(trade.getTotalAmount());
double maxMoney = 5000;
if(money <= 0 || money >= maxMoney){
    throw new BadRequestException("测试金额过大");
}

// after: explicit message per branch
BigDecimal money = new BigDecimal(trade.getTotalAmount());
if(money.compareTo(BigDecimal.ZERO) <= 0){
    throw new BadRequestException("金额必须大于 0");
}
if(money.compareTo(new BigDecimal("5000")) >= 0){
    throw new BadRequestException("测试金额过大,请控制在 5000 以内");
}
Defensive patterns

Strategy: validation

Validate before calling

// mirror the server rule before submitting
const amount = Number(totalAmount);
if (!(amount > 0 && amount < 5000)) { showToast('金额必须在 0 到 5000 之间'); return; }

Try / catch

try { aliPayService.toPayAsWeb(alipay, trade); } catch (BadRequestException e) { if ("测试金额过大".equals(e.getMessage())) { /* clamp or ask for smaller amount; note boundary 5000 itself is rejected */ } }

Prevention

When it happens

Trigger: POST to the WAP pay endpoint with totalAmount "0", a negative value, exactly "5000" or above, or an empty string (parsed as 0.0 after trim? no — empty throws NumberFormatException). The boundary is exclusive: exactly 5000 also fails.

Common situations: Test orders with large amounts in demos; frontend sending amount in cents (e.g. "500000") instead of yuan; locale formatting ("1.299,00" or "1,299.00") that either throws NumberFormatException or inflates the value; copying production-like amounts into the demo pay feature.

Related errors


AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14). Data as JSON: /api/errors/becbbe58689f2c73. Report an issue: GitHub.