Create a separate billing event when EAP is applied

When EAP is involed we current have one billing event for domain create that
has the create fee and EAP fee lumped together. Change it to record two
separate billing events for each.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132335349
This commit is contained in:
jianglai 2016-09-06 10:28:56 -07:00 committed by Ben McIlwain
parent 969d9483ae
commit 6641f105b7
12 changed files with 196 additions and 72 deletions

View file

@ -14,11 +14,13 @@
package google.registry.flows.domain;
import static com.google.common.collect.Sets.union;
import static google.registry.flows.domain.DomainFlowUtils.validateFeeChallenge;
import static google.registry.model.domain.fee.Fee.FEE_CREATE_COMMAND_EXTENSIONS_IN_PREFERENCE_ORDER;
import static google.registry.model.index.DomainApplicationIndex.loadActiveApplicationsByDomainName;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import google.registry.flows.EppException;
@ -146,23 +148,42 @@ public class DomainCreateFlow extends DomainCreateOrAllocateFlow {
Registry registry = Registry.get(getTld());
// Bill for the create.
BillingEvent.OneTime createEvent = new BillingEvent.OneTime.Builder()
.setReason(Reason.CREATE)
.setTargetId(targetId)
.setClientId(getClientId())
.setPeriodYears(command.getPeriod().getValue())
// TODO(b/29089413): the EAP fee needs to be a separate billing event.
.setCost(commandOperations.getTotalCost())
.setEventTime(now)
.setBillingTime(now.plus(isAnchorTenant()
? registry.getAnchorTenantAddGracePeriodLength()
: registry.getAddGracePeriodLength()))
.setFlags(isAnchorTenant()
? ImmutableSet.of(BillingEvent.Flag.ANCHOR_TENANT)
: ImmutableSet.<BillingEvent.Flag>of())
.setParent(historyEntry)
.build();
BillingEvent.OneTime createEvent =
new BillingEvent.OneTime.Builder()
.setReason(Reason.CREATE)
.setTargetId(targetId)
.setClientId(getClientId())
.setPeriodYears(command.getPeriod().getValue())
.setCost(commandOperations.getCreateCost())
.setEventTime(now)
.setBillingTime(now.plus(isAnchorTenant()
? registry.getAnchorTenantAddGracePeriodLength()
: registry.getAddGracePeriodLength()))
.setFlags(isAnchorTenant()
? ImmutableSet.of(BillingEvent.Flag.ANCHOR_TENANT)
: ImmutableSet.<BillingEvent.Flag>of())
.setParent(historyEntry)
.build();
ofy().save().entity(createEvent);
// Bill for EAP cost, if any.
if (!commandOperations.getEapCost().isZero()) {
BillingEvent.OneTime eapEvent =
new BillingEvent.OneTime.Builder()
.setReason(createEvent.getReason())
.setTargetId(createEvent.getTargetId())
.setClientId(createEvent.getClientId())
.setPeriodYears(createEvent.getPeriodYears())
.setCost(commandOperations.getEapCost())
.setEventTime(createEvent.getEventTime())
.setBillingTime(createEvent.getBillingTime())
.setFlags(union(createEvent.getFlags(),
ImmutableSet.of(BillingEvent.Flag.EAP)).immutableCopy())
.setParent(createEvent.getParentKey())
.build();
ofy().save().entity(eapEvent);
}
builder.addGracePeriod(GracePeriod.forBillingEvent(GracePeriodStatus.ADD, createEvent));
if (launchCreate != null && (launchCreate.getNotice() != null || hasSignedMarks)) {
builder

View file

@ -42,6 +42,7 @@ import google.registry.model.domain.DomainRenewData;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.GracePeriod;
import google.registry.model.domain.Period;
import google.registry.model.domain.fee.BaseFee.FeeType;
import google.registry.model.domain.fee.Fee;
import google.registry.model.domain.fee.FeeTransformCommandExtension;
import google.registry.model.domain.rgp.GracePeriodStatus;
@ -174,13 +175,16 @@ public class DomainRenewFlow extends OwnedResourceMutateFlow<DomainResource, Ren
return createOutput(
Success,
DomainRenewData.create(
newResource.getFullyQualifiedDomainName(),
newResource.getRegistrationExpirationTime()),
(feeRenew == null) ? null : ImmutableList.of(
feeRenew.createResponseBuilder()
.setCurrency(renewCost.getCurrencyUnit())
.setFees(ImmutableList.of(Fee.create(renewCost.getAmount(), "renew")))
.build()));
newResource.getFullyQualifiedDomainName(), newResource.getRegistrationExpirationTime()),
(feeRenew == null)
? null
: ImmutableList.of(
feeRenew
.createResponseBuilder()
.setCurrency(renewCost.getCurrencyUnit())
.setFees(
ImmutableList.of(Fee.create(renewCost.getAmount(), FeeType.RENEW)))
.build()));
}
/** The domain has a pending transfer on it and so can't be explicitly renewed. */

View file

@ -38,6 +38,7 @@ import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingEvent.Reason;
import google.registry.model.domain.DomainCommand.Update;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.fee.BaseFee.FeeType;
import google.registry.model.domain.fee.Fee;
import google.registry.model.domain.fee.FeeTransformCommandExtension;
import google.registry.model.domain.rgp.GracePeriodStatus;
@ -187,13 +188,17 @@ public class DomainRestoreRequestFlow extends OwnedResourceMutateFlow<DomainReso
return createOutput(
Success,
null,
(feeUpdate == null) ? null : ImmutableList.of(
feeUpdate.createResponseBuilder()
.setCurrency(restoreCost.getCurrencyUnit())
.setFees(ImmutableList.of(
Fee.create(restoreCost.getAmount(), "restore"),
Fee.create(renewCost.getAmount(), "renew")))
.build()));
(feeUpdate == null)
? null
: ImmutableList.of(
feeUpdate
.createResponseBuilder()
.setCurrency(restoreCost.getCurrencyUnit())
.setFees(
ImmutableList.of(
Fee.create(restoreCost.getAmount(), FeeType.RESTORE),
Fee.create(renewCost.getAmount(), FeeType.RENEW)))
.build()));
}
/** Restore command cannot have other changes specified. */

View file

@ -36,6 +36,7 @@ import google.registry.model.billing.BillingEvent.Reason;
import google.registry.model.domain.DomainCommand.Transfer;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.Period;
import google.registry.model.domain.fee.BaseFee.FeeType;
import google.registry.model.domain.fee.Fee;
import google.registry.model.domain.fee.FeeTransformCommandExtension;
import google.registry.model.eppoutput.EppResponse.ResponseExtension;
@ -161,10 +162,12 @@ public class DomainTransferRequestFlow
@Override
protected ImmutableList<? extends ResponseExtension> getTransferResponseExtensions() {
if (feeTransfer != null) {
return ImmutableList.of(feeTransfer.createResponseBuilder()
.setCurrency(renewCost.getCurrencyUnit())
.setFees(ImmutableList.of(Fee.create(renewCost.getAmount(), "renew")))
.build());
return ImmutableList.of(
feeTransfer
.createResponseBuilder()
.setCurrency(renewCost.getCurrencyUnit())
.setFees(ImmutableList.of(Fee.create(renewCost.getAmount(), FeeType.RENEW)))
.build());
} else {
return null;
}

View file

@ -71,6 +71,7 @@ public abstract class BillingEvent extends ImmutableObject
ALLOCATION,
ANCHOR_TENANT,
AUTO_RENEW,
EAP,
LANDRUSH,
SUNRISE,
/**

View file

@ -15,6 +15,7 @@
package google.registry.model.domain.fee;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkState;
import google.registry.model.ImmutableObject;
import google.registry.xml.PeriodAdapter;
@ -38,6 +39,24 @@ public abstract class BaseFee extends ImmutableObject {
@XmlEnumValue("delayed")
DELAYED
}
/** Enum for the type of the fee. */
public enum FeeType {
CREATE("create"),
EAP("Early Access Period, fee expires: %s"),
RENEW("renew"),
RESTORE("restore");
private final String formatString;
FeeType(String formatString) {
this.formatString = formatString;
}
String renderDescription(Object... args) {
return String.format(formatString, args);
}
}
@XmlAttribute
String description;
@ -54,6 +73,9 @@ public abstract class BaseFee extends ImmutableObject {
@XmlValue
BigDecimal cost;
@XmlTransient
FeeType type;
public String getDescription() {
return description;
@ -81,6 +103,15 @@ public abstract class BaseFee extends ImmutableObject {
public BigDecimal getCost() {
return cost;
}
public FeeType getType() {
return type;
}
protected void generateDescription(Object... args) {
checkState(type != null);
description = type.renderDescription(args);
}
public boolean hasDefaultAttributes() {
return getGracePeriod().equals(Period.ZERO)

View file

@ -37,13 +37,17 @@ import google.registry.model.domain.fee12.FeeUpdateCommandExtensionV12;
import google.registry.model.eppcommon.ProtocolDefinition.ServiceExtension;
import java.math.BigDecimal;
/** A fee, in currency units specified elsewhere in the xml, and with an optional description. */
/**
* A fee, in currency units specified elsewhere in the xml, with type of the fee an optional fee
* description.
*/
public class Fee extends BaseFee {
public static Fee create(BigDecimal cost, String description) {
public static Fee create(BigDecimal cost, FeeType type, Object... descriptionArgs) {
Fee instance = new Fee();
instance.cost = checkNotNull(cost);
checkArgument(instance.cost.signum() >= 0);
instance.description = description;
instance.type = checkNotNull(type);
instance.generateDescription(descriptionArgs);
return instance;
}

View file

@ -26,6 +26,7 @@ import com.googlecode.objectify.Key;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.DomainCommand.Create;
import google.registry.model.domain.LrpToken;
import google.registry.model.domain.fee.BaseFee.FeeType;
import google.registry.model.domain.fee.EapFee;
import google.registry.model.domain.fee.Fee;
import google.registry.model.pricing.PremiumPricingEngine.DomainPrices;
@ -39,9 +40,6 @@ import org.joda.time.DateTime;
* implementations on a per-TLD basis.
*/
public final class TldSpecificLogicProxy {
private static final String EAP_DESCRIPTION_FORMAT = "Early Access Period, fee expires: %s";
/**
* A collection of fees for a specific event.
*/
@ -56,9 +54,18 @@ public final class TldSpecificLogicProxy {
this.fees = checkArgumentNotNull(fees, "Fees may not be null in EppCommandOperations.");
}
/**
* Returns the total cost of all fees for the event.
*/
private Money getTotalCostForType(FeeType type) {
Money result = Money.zero(currency);
checkArgumentNotNull(type);
for (Fee fee : fees) {
if (fee.getType() == type) {
result = result.plus(fee.getCost());
}
}
return result;
}
/** Returns the total cost of all fees for the event. */
public Money getTotalCost() {
Money result = Money.zero(currency);
for (Fee fee : fees) {
@ -67,6 +74,16 @@ public final class TldSpecificLogicProxy {
return result;
}
/** Returns the create cost for the event. */
public Money getCreateCost() {
return getTotalCostForType(FeeType.CREATE);
}
/** Returns the EAP cost for the event. */
public Money getEapCost() {
return getTotalCostForType(FeeType.EAP);
}
/**
* Returns all costs for the event as a list of fees.
*/
@ -94,7 +111,8 @@ public final class TldSpecificLogicProxy {
ImmutableList.Builder<Fee> feeBuilder = new ImmutableList.Builder<>();
// Add Create cost.
feeBuilder.add(Fee.create(prices.getCreateCost().multipliedBy(years).getAmount(), "create"));
feeBuilder.add(
Fee.create(prices.getCreateCost().multipliedBy(years).getAmount(), FeeType.CREATE));
// Add EAP Fee.
EapFee eapFee = registry.getEapFeeFor(date);
@ -103,8 +121,7 @@ public final class TldSpecificLogicProxy {
if (!eapFeeCost.getAmount().equals(Money.zero(currency).getAmount())) {
feeBuilder.add(
Fee.create(
eapFeeCost.getAmount(),
String.format(EAP_DESCRIPTION_FORMAT, eapFee.getPeriod().upperEndpoint())));
eapFeeCost.getAmount(), FeeType.EAP, eapFee.getPeriod().upperEndpoint()));
}
return new EppCommandOperations(currency, feeBuilder.build());
@ -119,7 +136,8 @@ public final class TldSpecificLogicProxy {
return new EppCommandOperations(
registry.getCurrency(),
ImmutableList.of(
Fee.create(prices.getRenewCost().multipliedBy(years).getAmount(), "renew")));
Fee.create(
prices.getRenewCost().multipliedBy(years).getAmount(), FeeType.RENEW)));
}
/**
@ -131,8 +149,9 @@ public final class TldSpecificLogicProxy {
return new EppCommandOperations(
registry.getCurrency(),
ImmutableList.of(
Fee.create(prices.getRenewCost().multipliedBy(years).getAmount(), "renew"),
Fee.create(registry.getStandardRestoreCost().getAmount(), "restore")));
Fee.create(
prices.getRenewCost().multipliedBy(years).getAmount(), FeeType.RENEW),
Fee.create(registry.getStandardRestoreCost().getAmount(), FeeType.RESTORE)));
}
/**