DiscountPromotionHandler.java 3.79 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.collections.Predicate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.commerce.impl.promotion;

import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceSession;
import com.adobe.cq.commerce.api.PriceInfo;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.PromotionHandler;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Currency;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;

@Component(metatype=0)
@Service
@Properties(value={@Property(name="service.description", value={"Promotion handler which applies an absolute or percentage discount to the cart"}), @Property(name="commerce.promotion.type", value={"/libs/commerce/components/promotion/discount"})})
public class DiscountPromotionHandler
implements PromotionHandler {
    @Override
    public PriceInfo applyCartEntryPromotion(CommerceSession commerceSession, Promotion promotion, CommerceSession.CartEntry cartEntry) throws CommerceException {
        return null;
    }

    @Override
    public PriceInfo applyOrderPromotion(CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        PriceInfo cartTotal = commerceSession.getCartPriceInfo(null).get(0);
        ValueMap config = promotion.getConfig();
        String discountType = (String)config.get("discountType", (Object)"");
        BigDecimal discountValue = (BigDecimal)config.get("discountValue", (Object)BigDecimal.ZERO);
        BigDecimal discount = BigDecimal.ZERO;
        if (discountType.equals("percentage")) {
            discountValue = discountValue.divide(BigDecimal.valueOf(100));
            discount = cartTotal.getAmount().multiply(discountValue);
        } else if (discountType.equals("absolute")) {
            discount = discountValue;
        }
        return new PriceInfo(discount, cartTotal.getCurrency());
    }

    @Override
    public PriceInfo applyShippingPromotion(CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        return null;
    }

    @Deprecated
    @Override
    public String getMessage(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        return null;
    }

    @Override
    public String getDescription(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        return promotion.getDescription();
    }

    @Override
    public Map<Integer, String> getMessages(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        HashMap<Integer, String> messages = new HashMap<Integer, String>();
        ValueMap config = promotion.getConfig();
        String messageTemplate = (String)config.get("message", String.class);
        if (messageTemplate != null) {
            messages.put(-1, MessageFormat.format(messageTemplate, "", this.applyOrderPromotion(commerceSession, promotion).getFormattedString()));
        }
        return messages;
    }

    @Override
    public void invalidateCaches() {
    }
}