https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.ZketYD&treeId=338&articleId=106464&docType=1
https://docs.open.alipay.com/277/106748/
package com.baozun.store.util; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.baozun.store.constants.GucciAlipayConstants; /** * 花呗分期工具 * @author binrui.dong * 2017年7月25日 下午5:58:08 */ public class InstallmentUtil{ private static final Logger LOG = LoggerFactory.getLogger(InstallmentUtil.class); private static final BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100); /** * 1、获取用户每期本金 * 2、获取用户每期手续费 * 3、获取用户每期总费用 * @param payAmount 总额 * @param periods 期数 * @return * <p>binrui.dong * <p>2017年7月25日 下午6:03:38 */ public static Map<String,BigDecimal> getInstallmentCost(BigDecimal payAmount, Integer periods){ Map<String,BigDecimal> map = new HashMap<String,BigDecimal>(); BigDecimal payAmountCent = payAmount.multiply(ONE_HUNDRED);//转化为分 BigDecimal totalFeeInDecimal = null; switch (periods) { case 3: totalFeeInDecimal = payAmountCent.setScale(2, RoundingMode.HALF_UP) .multiply(GucciAlipayConstants.HB_FQ_THREE_FEE_RATE).setScale(2, RoundingMode.HALF_UP); break; case 6: totalFeeInDecimal = payAmountCent.setScale(2, RoundingMode.HALF_UP) .multiply(GucciAlipayConstants.HB_FQ_SIX_FEE_RATE).setScale(2, RoundingMode.HALF_UP); break; case 12: totalFeeInDecimal = payAmountCent.setScale(2, RoundingMode.HALF_UP) .multiply(GucciAlipayConstants.HB_FQ_TWELVE_FEE_RATE).setScale(2, RoundingMode.HALF_UP); break; } map.put("allRateFee", totalFeeInDecimal.divide(ONE_HUNDRED,RoundingMode.HALF_EVEN).setScale(2, RoundingMode.HALF_EVEN)); map.put("allFee", payAmountCent.add(totalFeeInDecimal).divide(ONE_HUNDRED).setScale(2, RoundingMode.HALF_EVEN)); LOG.debug("总费用:" + map.get("allFee")); BigDecimal eachFee = map.get("allRateFee").setScale(2, RoundingMode.HALF_DOWN).divide(BigDecimal.valueOf(periods), RoundingMode.HALF_DOWN);//文档里是ROUND_DOWN map.put("eachFee", eachFee); LOG.debug("每期手续费:" + map.get("eachFee")); BigDecimal prinAndFee = map.get("allFee").setScale(2, RoundingMode.HALF_DOWN).divide(BigDecimal.valueOf(periods), RoundingMode.HALF_DOWN);//文档里是ROUND_DOWN map.put("prinAndFee", prinAndFee); LOG.debug("每期费用:" + map.get("prinAndFee")); map.put("periods", BigDecimal.valueOf(periods)); return map; } public static Map<String,BigDecimal> getInstallmentCost2(BigDecimal payAmount, Integer periods){ Map<String,BigDecimal> map = new HashMap<String,BigDecimal>(); BigDecimal payAmountCent = payAmount.multiply(ONE_HUNDRED);//转化为分 BigDecimal totalFeeInDecimal = null; switch (periods) { case 3: totalFeeInDecimal = payAmountCent.setScale(2, RoundingMode.HALF_UP) .multiply(new BigDecimal(0.023)).setScale(2, RoundingMode.HALF_UP); break; case 6: totalFeeInDecimal = payAmountCent.setScale(2, RoundingMode.HALF_UP) .multiply(new BigDecimal(0.045)).setScale(2, RoundingMode.HALF_UP); break; case 12: totalFeeInDecimal = payAmountCent.setScale(2, RoundingMode.HALF_UP) .multiply(new BigDecimal(0.075)).setScale(2, RoundingMode.HALF_UP); break; } map.put("allRateFee", totalFeeInDecimal.divide(ONE_HUNDRED,RoundingMode.HALF_EVEN).setScale(2, RoundingMode.HALF_EVEN)); map.put("allFee", payAmountCent.add(totalFeeInDecimal).divide(ONE_HUNDRED).setScale(2, RoundingMode.HALF_EVEN)); LOG.debug("总费用:" + map.get("allFee")); BigDecimal eachFee = map.get("allRateFee").setScale(2, RoundingMode.HALF_DOWN).divide(BigDecimal.valueOf(periods), RoundingMode.HALF_DOWN);//文档里是ROUND_DOWN map.put("eachFee", eachFee); LOG.debug("每期手续费:" + map.get("eachFee")); BigDecimal prinAndFee = map.get("allFee").setScale(2, RoundingMode.HALF_DOWN).divide(BigDecimal.valueOf(periods), RoundingMode.HALF_DOWN);//文档里是ROUND_DOWN map.put("prinAndFee", prinAndFee); LOG.debug("每期费用:" + map.get("prinAndFee")); map.put("periods", BigDecimal.valueOf(periods)); return map; } public static void main(String[] args){ System.err.println(getInstallmentCost2(new BigDecimal(1111.11), 3)); } }