Alipay Huabei Amount Calculation Tool

https://docs.open.alipay.com/60/104790/
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;

/**
 * Huabei installment tool
 * @author binrui.dong
 * July 25, 2017 at 5:58:08 pm
 */
public class InstallmentUtil{
    
    private static final Logger     LOG     = LoggerFactory.getLogger(InstallmentUtil.class);
    
    private static final BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100);
    
    /**
     * 1. Obtain the principal of each period of the user
     * 2. Get the user fee for each period
     * 3. Get the user's total cost per period
     * @param payAmount total
     * @param periods number of periods
     *	@return
     *	<p>binrui.dong
     * <p>July 25, 2017 at 6:03:38 pm
     */
    public static Map<String,BigDecimal> getInstallmentCost(BigDecimal payAmount, Integer periods){
        Map<String,BigDecimal> map = new HashMap<String,BigDecimal>();
        BigDecimal payAmountCent = payAmount.multiply(ONE_HUNDRED);//Convert to points
        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("Total Fee:" + 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("Fees per period:" + 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("Fee per period: " + 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);//Convert to points
        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("Total Fee:" + 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("Fees per period:" + 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("Fee per period: " + 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));
    }
    
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326249587&siteId=291194637