Validate individual fee types

Currently we validate the fee extension by summing up all fees present in the extension and comparing it against the total fee to be charged. While this works in most cases, we'd like the ability to individually validate each fee. This is especially useful during EAP when two fees are charged, a regular "create" fee that would also be amount we charge during renewal, and a one time "EAP" fee.

Because we can only distinguish fees by their descriptions, we try to match the description to the format string of the fee type enums. We also only require individual fee matches when we are charging more than one type of fees, which makes the change compatible with most existing use cases where only one fees is charged and the description field is ignored in the extension.

We expect the workflow to be that a registrar sends a domain check, and we reply with exactly what fees we are expecting, and then it will use the descriptions in the response to send us a domain create with the correct fees.

Note that we aggregate fees within the same FeeType together. Normally there will only be one fee per type, but in case of custom logic there could be more than one fee for the same type. There is no way to distinguish them as they both use the same description. So it is simpler to just aggregate them.

This CL also includes some reformatting that conforms to google-java-format output.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=186530316
This commit is contained in:
jianglai 2018-02-21 15:34:13 -08:00
parent 1965c0a0aa
commit ff221fba96
13 changed files with 355 additions and 106 deletions

View file

@ -16,11 +16,15 @@ package google.registry.model.domain.fee;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import google.registry.model.ImmutableObject;
import google.registry.xml.PeriodAdapter;
import java.math.BigDecimal;
import java.util.stream.Stream;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlEnumValue;
import javax.xml.bind.annotation.XmlTransient;
@ -68,29 +72,27 @@ public abstract class BaseFee extends ImmutableObject {
String renderDescription(Object... args) {
return String.format(formatString, args);
}
boolean matchFormatString(String description) {
return Ascii.toLowerCase(formatString).contains(Ascii.toLowerCase(description));
}
}
@XmlAttribute
String description;
@XmlAttribute String description;
@XmlAttribute
AppliedType applied;
@XmlAttribute AppliedType applied;
@XmlAttribute(name = "grace-period")
@XmlJavaTypeAdapter(PeriodAdapter.class)
Period gracePeriod;
@XmlAttribute
Boolean refundable;
@XmlAttribute Boolean refundable;
@XmlValue
BigDecimal cost;
@XmlValue BigDecimal cost;
@XmlTransient
FeeType type;
@XmlTransient FeeType type;
@XmlTransient
Range<DateTime> validDateRange;
@XmlTransient Range<DateTime> validDateRange;
public String getDescription() {
return description;
@ -113,8 +115,8 @@ public abstract class BaseFee extends ImmutableObject {
* must always be negative. Essentially, they are the same thing, just with different sign.
* However, we need them to be separate classes for proper JAXB handling.
*
* @see <a href="https://tools.ietf.org/html/draft-brown-epp-fees-03#section-2.4">
* Registry Fee Extension for EPP - Fees and Credits</a>
* @see <a href="https://tools.ietf.org/html/draft-brown-epp-fees-03#section-2.4">Registry Fee
* Extension for EPP - Fees and Credits</a>
*/
public BigDecimal getCost() {
return cost;
@ -142,4 +144,19 @@ public abstract class BaseFee extends ImmutableObject {
&& getApplied().equals(AppliedType.IMMEDIATE)
&& getRefundable();
}
/**
* Parses the description field and returns {@link FeeType}s that match the description.
*
* <p>A {@link FeeType} is a match when its {@code formatString} contains the description, case
* insensitively.
*/
public ImmutableList<FeeType> parseDescriptionForTypes() {
if (description == null) {
return ImmutableList.of();
}
return Stream.of(FeeType.values())
.filter(feeType -> feeType.matchFormatString(description))
.collect(toImmutableList());
}
}