Reconcile FeesAndCredits handling in price customization

Also adds a mechanism to ensure that fee extensions are included when custom
pricing logic adds a custom fee, and fixes up the domain restore flow to
properly use the restore price.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=142715136
This commit is contained in:
mcilwain 2016-12-21 16:44:08 -08:00 committed by Ben McIlwain
parent 720f03cc17
commit 9d9c527917
19 changed files with 312 additions and 182 deletions

View file

@ -19,7 +19,7 @@ import com.google.common.net.InternetDomainName;
import google.registry.flows.EppException;
import google.registry.flows.SessionMetadata;
import google.registry.flows.domain.DomainPricingLogic;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.flows.domain.FeesAndCredits;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.DomainApplication;
import google.registry.model.eppinput.EppInput;

View file

@ -51,7 +51,6 @@ import google.registry.flows.FlowModule.ClientId;
import google.registry.flows.FlowModule.Superuser;
import google.registry.flows.FlowModule.TargetId;
import google.registry.flows.TransactionalFlow;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.model.ImmutableObject;
import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingEvent.Flag;

View file

@ -63,7 +63,6 @@ import google.registry.flows.custom.DomainApplicationCreateFlowCustomLogic.After
import google.registry.flows.custom.DomainApplicationCreateFlowCustomLogic.BeforeResponseParameters;
import google.registry.flows.custom.DomainApplicationCreateFlowCustomLogic.BeforeResponseReturnData;
import google.registry.flows.custom.EntityChanges;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.DomainCommand.Create;
@ -223,7 +222,7 @@ public final class DomainApplicationCreateFlow implements TransactionalFlow {
}
FeeCreateCommandExtension feeCreate =
eppInput.getSingleExtension(FeeCreateCommandExtension.class);
validateFeeChallenge(targetId, tld, now, feeCreate, feesAndCredits.getTotalCost());
validateFeeChallenge(targetId, tld, now, feeCreate, feesAndCredits);
SecDnsCreateExtension secDnsCreate =
validateSecDnsExtension(eppInput.getSingleExtension(SecDnsCreateExtension.class));
customLogic.afterValidation(

View file

@ -54,8 +54,7 @@ import google.registry.flows.FlowModule.ClientId;
import google.registry.flows.FlowModule.Superuser;
import google.registry.flows.FlowModule.TargetId;
import google.registry.flows.TransactionalFlow;
import google.registry.flows.domain.DomainFlowUtils.FeesRequiredForNonFreeUpdateException;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.flows.domain.DomainFlowUtils.FeesRequiredForNonFreeOperationException;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.DomainCommand.Update;
@ -75,7 +74,6 @@ import google.registry.model.eppoutput.EppResponse;
import google.registry.model.registry.Registry;
import google.registry.model.reporting.HistoryEntry;
import javax.inject.Inject;
import org.joda.money.Money;
import org.joda.time.DateTime;
/**
@ -188,12 +186,11 @@ public class DomainApplicationUpdateFlow implements TransactionalFlow {
// If the fee extension is present, validate it (even if the cost is zero, to check for price
// mismatches). Don't rely on the the validateFeeChallenge check for feeUpdate nullness, because
// it throws an error if the name is premium, and we don't want to do that here.
Money totalCost = feesAndCredits.getTotalCost();
if (feeUpdate != null) {
validateFeeChallenge(targetId, tld, now, feeUpdate, totalCost);
} else if (!totalCost.isZero()) {
validateFeeChallenge(targetId, tld, now, feeUpdate, feesAndCredits);
} else if (!feesAndCredits.getTotalCost().isZero()) {
// If it's not present but the cost is not zero, throw an exception.
throw new FeesRequiredForNonFreeUpdateException();
throw new FeesRequiredForNonFreeOperationException(feesAndCredits.getTotalCost());
}
verifyNotInPendingDelete(
add.getContacts(),

View file

@ -61,7 +61,6 @@ import google.registry.flows.custom.DomainCreateFlowCustomLogic;
import google.registry.flows.custom.DomainCreateFlowCustomLogic.BeforeResponseParameters;
import google.registry.flows.custom.DomainCreateFlowCustomLogic.BeforeResponseReturnData;
import google.registry.flows.custom.EntityChanges;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.model.ImmutableObject;
import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingEvent.Flag;
@ -212,8 +211,7 @@ public class DomainCreateFlow implements TransactionalFlow {
FeeCreateCommandExtension feeCreate =
eppInput.getSingleExtension(FeeCreateCommandExtension.class);
FeesAndCredits feesAndCredits = pricingLogic.getCreatePrice(registry, targetId, now, years);
validateFeeChallenge(
targetId, registry.getTldStr(), now, feeCreate, feesAndCredits.getTotalCost());
validateFeeChallenge(targetId, registry.getTldStr(), now, feeCreate, feesAndCredits);
// Superusers can create reserved domains, force creations on domains that require a claims
// notice without specifying a claims key, ignore the registry phase, and override blocks on
// registering premium domains.

View file

@ -56,7 +56,6 @@ import google.registry.flows.EppException.ParameterValueSyntaxErrorException;
import google.registry.flows.EppException.RequiredParameterMissingException;
import google.registry.flows.EppException.StatusProhibitsOperationException;
import google.registry.flows.EppException.UnimplementedOptionException;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.flows.exceptions.ResourceAlreadyExistsException;
import google.registry.flows.exceptions.ResourceHasClientUpdateProhibitedException;
import google.registry.model.EppResource;
@ -674,8 +673,7 @@ public class DomainFlowUtils {
String tld,
DateTime priceTime,
final FeeTransformCommandExtension feeCommand,
Money cost,
Money... otherCosts)
FeesAndCredits feesAndCredits)
throws EppException {
Registry registry = Registry.get(tld);
if (registry.getPremiumPriceAckRequired()
@ -683,9 +681,17 @@ public class DomainFlowUtils {
&& feeCommand == null) {
throw new FeesRequiredForPremiumNameException();
}
// Check for the case where a fee command extension was required but not provided.
// This only happens when the total fees are non-zero and include custom fees requiring the
// extension.
if (feeCommand == null) {
return;
if (feesAndCredits.getTotalCost().isZero() || !feesAndCredits.isFeeExtensionRequired()) {
return;
}
throw new FeesRequiredForNonFreeOperationException(feesAndCredits.getTotalCost());
}
List<Fee> fees = feeCommand.getFees();
// The schema guarantees that at least one fee will be present.
checkState(!fees.isEmpty());
@ -710,16 +716,11 @@ public class DomainFlowUtils {
throw new CurrencyValueScaleException();
}
Money costTotal = cost;
for (Money otherCost : otherCosts) {
costTotal = costTotal.plus(otherCost);
}
if (!feeTotal.getCurrencyUnit().equals(costTotal.getCurrencyUnit())) {
if (!feeTotal.getCurrencyUnit().equals(feesAndCredits.getCurrency())) {
throw new CurrencyUnitMismatchException();
}
if (!feeTotal.equals(costTotal)) {
throw new FeesMismatchException(costTotal);
if (!feeTotal.equals(feesAndCredits.getTotalCost())) {
throw new FeesMismatchException(feesAndCredits.getTotalCost());
}
}
@ -1282,10 +1283,17 @@ public class DomainFlowUtils {
}
}
/** Fees must be explicitly acknowledged when performing an update which is not free. */
static class FeesRequiredForNonFreeUpdateException extends RequiredParameterMissingException {
FeesRequiredForNonFreeUpdateException() {
super("Fees must be explicitly acknowledged when performing an update which is not free.");
/** Fees must be explicitly acknowledged when performing an operation which is not free. */
static class FeesRequiredForNonFreeOperationException extends RequiredParameterMissingException {
FeesRequiredForNonFreeOperationException() {
super("Fees must be explicitly acknowledged when performing an operation which is not free.");
}
public FeesRequiredForNonFreeOperationException(Money expectedFee) {
super(
"Fees must be explicitly acknowledged when performing an operation which is not free."
+ " The total fee is: "
+ expectedFee);
}
}

View file

@ -14,16 +14,12 @@
package google.registry.flows.domain;
import static com.google.common.collect.Iterables.concat;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.pricing.PricingEngineProxy.getDomainCreateCost;
import static google.registry.pricing.PricingEngineProxy.getDomainFeeClass;
import static google.registry.pricing.PricingEngineProxy.getDomainRenewCost;
import static google.registry.util.CollectionUtils.nullToEmpty;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Key;
import google.registry.flows.EppException;
@ -35,12 +31,10 @@ import google.registry.flows.custom.DomainPricingCustomLogic.RenewPriceParameter
import google.registry.flows.custom.DomainPricingCustomLogic.RestorePriceParameters;
import google.registry.flows.custom.DomainPricingCustomLogic.TransferPriceParameters;
import google.registry.flows.custom.DomainPricingCustomLogic.UpdatePriceParameters;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.LrpTokenEntity;
import google.registry.model.domain.fee.BaseFee;
import google.registry.model.domain.fee.BaseFee.FeeType;
import google.registry.model.domain.fee.Credit;
import google.registry.model.domain.fee.Fee;
import google.registry.model.registry.Registry;
import javax.inject.Inject;
@ -61,83 +55,6 @@ public final class DomainPricingLogic {
@Inject
DomainPricingLogic() {}
/** A collection of fees and credits for a specific EPP transform. */
public static final class FeesAndCredits extends ImmutableObject {
private final CurrencyUnit currency;
private final ImmutableList<Fee> fees;
private final ImmutableList<Credit> credits;
/** Constructs a new instance. The currency must be the same across all fees and credits. */
public FeesAndCredits(CurrencyUnit currency, BaseFee... baseFees) {
this.currency = checkArgumentNotNull(currency, "Currency may not be null in FeesAndCredits.");
ImmutableList.Builder<Fee> feeBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<Credit> creditBuilder = new ImmutableList.Builder<>();
for (BaseFee feeOrCredit : baseFees) {
if (feeOrCredit instanceof Credit) {
creditBuilder.add((Credit) feeOrCredit);
} else {
feeBuilder.add((Fee) feeOrCredit);
}
}
this.fees = feeBuilder.build();
this.credits = creditBuilder.build();
}
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 and credits for the event. */
public Money getTotalCost() {
Money result = Money.zero(currency);
for (Fee fee : fees) {
result = result.plus(fee.getCost());
}
for (Credit credit : credits) {
result = result.plus(credit.getCost());
}
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 the list of fees for the event. */
public ImmutableList<Fee> getFees() {
return fees;
}
/** Returns the list of credits for the event. */
public ImmutableList<Credit> getCredits() {
return ImmutableList.copyOf(nullToEmpty(credits));
}
/** Returns the currency for all fees in the event. */
public final CurrencyUnit getCurrency() {
return currency;
}
/** Returns all fees and credits for the event. */
public ImmutableList<BaseFee> getFeesAndCredits() {
return ImmutableList.copyOf(concat(getFees(), getCredits()));
}
}
/** Returns a new create price for the Pricer. */
public FeesAndCredits getCreatePrice(
Registry registry, String domainName, DateTime date, int years) throws EppException {
@ -147,20 +64,18 @@ public final class DomainPricingLogic {
BaseFee createFeeOrCredit =
Fee.create(getDomainCreateCost(domainName, date, years).getAmount(), FeeType.CREATE);
FeesAndCredits feesAndCredits;
// Create fees for the cost and the EAP fee, if any.
Fee eapFee = registry.getEapFeeFor(date);
FeesAndCredits.Builder feesBuilder =
new FeesAndCredits.Builder().setCurrency(currency).addFeeOrCredit(createFeeOrCredit);
if (!eapFee.hasZeroCost()) {
feesAndCredits = new FeesAndCredits(currency, createFeeOrCredit, eapFee);
} else {
feesAndCredits = new FeesAndCredits(currency, createFeeOrCredit);
feesBuilder.addFeeOrCredit(eapFee);
}
// Apply custom logic to the create fee, if any.
return customLogic.customizeCreatePrice(
CreatePriceParameters.newBuilder()
.setFeesAndCredits(feesAndCredits)
.setFeesAndCredits(feesBuilder.build())
.setRegistry(registry)
.setDomainName(InternetDomainName.from(domainName))
.setAsOfDate(date)
@ -183,8 +98,10 @@ public final class DomainPricingLogic {
return customLogic.customizeRenewPrice(
RenewPriceParameters.newBuilder()
.setFeesAndCredits(
new FeesAndCredits(
registry.getCurrency(), Fee.create(renewCost.getAmount(), FeeType.RENEW)))
new FeesAndCredits.Builder()
.setCurrency(registry.getCurrency())
.addFeeOrCredit(Fee.create(renewCost.getAmount(), FeeType.RENEW))
.build())
.setRegistry(registry)
.setDomainName(InternetDomainName.from(domainName))
.setAsOfDate(date)
@ -197,10 +114,13 @@ public final class DomainPricingLogic {
public FeesAndCredits getRestorePrice(Registry registry, String domainName, DateTime date)
throws EppException {
FeesAndCredits feesAndCredits =
new FeesAndCredits(
registry.getCurrency(),
Fee.create(getDomainRenewCost(domainName, date, 1).getAmount(), FeeType.RENEW),
Fee.create(registry.getStandardRestoreCost().getAmount(), FeeType.RESTORE));
new FeesAndCredits.Builder()
.setCurrency(registry.getCurrency())
.addFeeOrCredit(
Fee.create(getDomainRenewCost(domainName, date, 1).getAmount(), FeeType.RENEW))
.addFeeOrCredit(
Fee.create(registry.getStandardRestoreCost().getAmount(), FeeType.RESTORE))
.build();
return customLogic.customizeRestorePrice(
RestorePriceParameters.newBuilder()
.setFeesAndCredits(feesAndCredits)
@ -221,8 +141,10 @@ public final class DomainPricingLogic {
return customLogic.customizeTransferPrice(
TransferPriceParameters.newBuilder()
.setFeesAndCredits(
new FeesAndCredits(
registry.getCurrency(), Fee.create(renewCost.getAmount(), FeeType.RENEW)))
new FeesAndCredits.Builder()
.setCurrency(registry.getCurrency())
.addFeeOrCredit(Fee.create(renewCost.getAmount(), FeeType.RENEW))
.build())
.setRegistry(registry)
.setDomainName(InternetDomainName.from(domainName))
.setAsOfDate(transferDate)
@ -238,7 +160,11 @@ public final class DomainPricingLogic {
Fee.create(Money.zero(registry.getCurrency()).getAmount(), FeeType.UPDATE);
return customLogic.customizeUpdatePrice(
UpdatePriceParameters.newBuilder()
.setFeesAndCredits(new FeesAndCredits(currency, feeOrCredit))
.setFeesAndCredits(
new FeesAndCredits.Builder()
.setCurrency(currency)
.addFeeOrCredit(feeOrCredit)
.build())
.setRegistry(registry)
.setDomainName(InternetDomainName.from(domainName))
.setAsOfDate(date)
@ -253,7 +179,11 @@ public final class DomainPricingLogic {
Fee.create(Money.zero(registry.getCurrency()).getAmount(), FeeType.UPDATE);
return customLogic.customizeApplicationUpdatePrice(
ApplicationUpdatePriceParameters.newBuilder()
.setFeesAndCredits(new FeesAndCredits(registry.getCurrency(), feeOrCredit))
.setFeesAndCredits(
new FeesAndCredits.Builder()
.setCurrency(registry.getCurrency())
.setFeesAndCredits(feeOrCredit)
.build())
.setRegistry(registry)
.setDomainApplication(application)
.setAsOfDate(date)

View file

@ -49,7 +49,6 @@ import google.registry.flows.custom.DomainRenewFlowCustomLogic.BeforeResponsePar
import google.registry.flows.custom.DomainRenewFlowCustomLogic.BeforeResponseReturnData;
import google.registry.flows.custom.DomainRenewFlowCustomLogic.BeforeSaveParameters;
import google.registry.flows.custom.EntityChanges;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.model.ImmutableObject;
import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingEvent.OneTime;
@ -140,8 +139,7 @@ public final class DomainRenewFlow implements TransactionalFlow {
eppInput.getSingleExtension(FeeRenewCommandExtension.class);
FeesAndCredits feesAndCredits =
pricingLogic.getRenewPrice(Registry.get(existingDomain.getTld()), targetId, now, years);
validateFeeChallenge(
targetId, existingDomain.getTld(), now, feeRenew, feesAndCredits.getTotalCost());
validateFeeChallenge(targetId, existingDomain.getTld(), now, feeRenew, feesAndCredits);
customLogic.afterValidation(
AfterValidationParameters.newBuilder()
.setExistingDomain(existingDomain)

View file

@ -42,7 +42,6 @@ import google.registry.flows.FlowModule.ClientId;
import google.registry.flows.FlowModule.Superuser;
import google.registry.flows.FlowModule.TargetId;
import google.registry.flows.TransactionalFlow;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.model.ImmutableObject;
import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingEvent.OneTime;
@ -128,17 +127,16 @@ public final class DomainRestoreRequestFlow implements TransactionalFlow {
Update command = (Update) resourceCommand;
DateTime now = ofy().getTransactionTime();
DomainResource existingDomain = loadAndVerifyExistence(DomainResource.class, targetId, now);
Money restoreCost = Registry.get(existingDomain.getTld()).getStandardRestoreCost();
FeesAndCredits feesAndCredits =
pricingLogic.getRenewPrice(Registry.get(existingDomain.getTld()), targetId, now, 1);
pricingLogic.getRestorePrice(Registry.get(existingDomain.getTld()), targetId, now);
FeeUpdateCommandExtension feeUpdate =
eppInput.getSingleExtension(FeeUpdateCommandExtension.class);
Money totalCost = feesAndCredits.getTotalCost();
verifyRestoreAllowed(command, existingDomain, restoreCost, totalCost, feeUpdate, now);
verifyRestoreAllowed(command, existingDomain, feeUpdate, feesAndCredits, now);
HistoryEntry historyEntry = buildHistory(existingDomain, now);
ImmutableSet.Builder<ImmutableObject> entitiesToSave = new ImmutableSet.Builder<>();
entitiesToSave.addAll(
createRestoreAndRenewBillingEvents(historyEntry, restoreCost, totalCost, now));
createRestoreAndRenewBillingEvents(
historyEntry, feesAndCredits.getRestoreCost(), feesAndCredits.getRenewCost(), now));
// We don't preserve the original expiration time of the domain when we restore, since doing so
// would require us to know if they received a grace period refund when they deleted the domain,
// and to charge them for that again. Instead, we just say that all restores get a fresh year of
@ -162,7 +160,9 @@ public final class DomainRestoreRequestFlow implements TransactionalFlow {
ofy().delete().key(existingDomain.getDeletePollMessage());
dnsQueue.addDomainRefreshTask(existingDomain.getFullyQualifiedDomainName());
return responseBuilder
.setExtensions(createResponseExtensions(restoreCost, totalCost, feeUpdate))
.setExtensions(
createResponseExtensions(
feesAndCredits.getRestoreCost(), feesAndCredits.getRenewCost(), feeUpdate))
.build();
}
@ -177,9 +177,8 @@ public final class DomainRestoreRequestFlow implements TransactionalFlow {
private void verifyRestoreAllowed(
Update command,
DomainResource existingDomain,
Money restoreCost,
Money renewCost,
FeeUpdateCommandExtension feeUpdate,
FeesAndCredits feesAndCredits,
DateTime now) throws EppException {
verifyOptionalAuthInfo(authInfo, existingDomain);
if (!isSuperuser) {
@ -196,7 +195,7 @@ public final class DomainRestoreRequestFlow implements TransactionalFlow {
throw new DomainNotEligibleForRestoreException();
}
checkAllowedAccessToTld(clientId, existingDomain.getTld());
validateFeeChallenge(targetId, existingDomain.getTld(), now, feeUpdate, restoreCost, renewCost);
validateFeeChallenge(targetId, existingDomain.getTld(), now, feeUpdate, feesAndCredits);
}
private ImmutableSet<BillingEvent.OneTime> createRestoreAndRenewBillingEvents(

View file

@ -44,7 +44,6 @@ import google.registry.flows.FlowModule.ClientId;
import google.registry.flows.FlowModule.Superuser;
import google.registry.flows.FlowModule.TargetId;
import google.registry.flows.TransactionalFlow;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.flows.exceptions.AlreadyPendingTransferException;
import google.registry.flows.exceptions.ObjectAlreadySponsoredException;
import google.registry.model.billing.BillingEvent;
@ -145,7 +144,7 @@ public final class DomainTransferRequestFlow implements TransactionalFlow {
FeeTransferCommandExtension feeTransfer =
eppInput.getSingleExtension(FeeTransferCommandExtension.class);
FeesAndCredits feesAndCredits = pricingLogic.getTransferPrice(registry, targetId, now, years);
validateFeeChallenge(targetId, tld, now, feeTransfer, feesAndCredits.getTotalCost());
validateFeeChallenge(targetId, tld, now, feeTransfer, feesAndCredits);
HistoryEntry historyEntry = buildHistory(period, existingDomain, now);
DateTime automaticTransferTime = now.plus(registry.getAutomaticTransferLength());
// The new expiration time if there is a server approval.

View file

@ -55,8 +55,7 @@ import google.registry.flows.custom.DomainUpdateFlowCustomLogic;
import google.registry.flows.custom.DomainUpdateFlowCustomLogic.AfterValidationParameters;
import google.registry.flows.custom.DomainUpdateFlowCustomLogic.BeforeSaveParameters;
import google.registry.flows.custom.EntityChanges;
import google.registry.flows.domain.DomainFlowUtils.FeesRequiredForNonFreeUpdateException;
import google.registry.flows.domain.DomainPricingLogic.FeesAndCredits;
import google.registry.flows.domain.DomainFlowUtils.FeesRequiredForNonFreeOperationException;
import google.registry.model.ImmutableObject;
import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingEvent.Reason;
@ -79,7 +78,6 @@ import google.registry.model.eppoutput.EppResponse;
import google.registry.model.registry.Registry;
import google.registry.model.reporting.HistoryEntry;
import javax.inject.Inject;
import org.joda.money.Money;
import org.joda.time.DateTime;
/**
@ -109,7 +107,7 @@ import org.joda.time.DateTime;
* @error {@link DomainFlowUtils.DuplicateContactForRoleException}
* @error {@link DomainFlowUtils.EmptySecDnsUpdateException}
* @error {@link DomainFlowUtils.FeesMismatchException}
* @error {@link DomainFlowUtils.FeesRequiredForNonFreeUpdateException}
* @error {@link DomainFlowUtils.FeesRequiredForNonFreeOperationException}
* @error {@link DomainFlowUtils.LinkedResourcesDoNotExistException}
* @error {@link DomainFlowUtils.LinkedResourceInPendingDeleteProhibitsOperationException}
* @error {@link DomainFlowUtils.MaxSigLifeChangeNotSupportedException}
@ -219,12 +217,11 @@ public final class DomainUpdateFlow implements TransactionalFlow {
// mismatches). Don't rely on the the validateFeeChallenge check for feeUpdate nullness, because
// it throws an error if the name is premium, and we don't want to do that here.
FeesAndCredits feesAndCredits = pricingLogic.getUpdatePrice(Registry.get(tld), targetId, now);
Money totalCost = feesAndCredits.getTotalCost();
if (feeUpdate != null) {
validateFeeChallenge(targetId, existingDomain.getTld(), now, feeUpdate, totalCost);
} else if (!totalCost.isZero()) {
validateFeeChallenge(targetId, existingDomain.getTld(), now, feeUpdate, feesAndCredits);
} else if (!feesAndCredits.getTotalCost().isZero()) {
// If it's not present but the cost is not zero, throw an exception.
throw new FeesRequiredForNonFreeUpdateException();
throw new FeesRequiredForNonFreeOperationException(feesAndCredits.getTotalCost());
}
verifyNotInPendingDelete(
add.getContacts(),

View file

@ -0,0 +1,176 @@
// Copyright 2016 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.flows.domain;
import static com.google.common.collect.Iterables.concat;
import static google.registry.util.CollectionUtils.nullToEmpty;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.collect.ImmutableList;
import google.registry.model.Buildable;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.fee.BaseFee;
import google.registry.model.domain.fee.BaseFee.FeeType;
import google.registry.model.domain.fee.Credit;
import google.registry.model.domain.fee.Fee;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
/** A collection of fees and credits for a specific EPP transform. */
public class FeesAndCredits extends ImmutableObject implements Buildable {
private CurrencyUnit currency;
private boolean feeExtensionRequired;
private ImmutableList<Fee> fees;
private ImmutableList<Credit> credits;
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 and credits for the event. */
public Money getTotalCost() {
Money result = Money.zero(currency);
for (Fee fee : fees) {
result = result.plus(fee.getCost());
}
for (Credit credit : credits) {
result = result.plus(credit.getCost());
}
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 the renew cost for the event. */
public Money getRenewCost() {
return getTotalCostForType(FeeType.RENEW);
}
/** Returns the restore cost for the event. */
public Money getRestoreCost() {
return getTotalCostForType(FeeType.RESTORE);
}
/** Returns the list of fees for the event. */
public ImmutableList<Fee> getFees() {
return fees;
}
/** Returns whether a custom fee is present that requires fee extension acknowledgement. */
public boolean isFeeExtensionRequired() {
return feeExtensionRequired;
}
/** Returns the list of credits for the event. */
public ImmutableList<Credit> getCredits() {
return ImmutableList.copyOf(nullToEmpty(credits));
}
/** Returns the currency for all fees in the event. */
public final CurrencyUnit getCurrency() {
return currency;
}
/** Returns all fees and credits for the event. */
public ImmutableList<BaseFee> getFeesAndCredits() {
return ImmutableList.copyOf(concat(getFees(), getCredits()));
}
@Override
public Builder asBuilder() {
return new Builder(clone(this));
}
/** A builder for constructing {@link FeesAndCredits} objects, since they are immutable. */
public static class Builder extends Buildable.Builder<FeesAndCredits> {
public Builder() {}
private Builder(FeesAndCredits instance) {
super(instance);
}
public Builder addFeeOrCredit(BaseFee feeOrCredit) {
if (feeOrCredit instanceof Credit) {
getInstance().credits =
new ImmutableList.Builder<Credit>()
.addAll(nullToEmptyImmutableCopy(getInstance().credits))
.add((Credit) feeOrCredit)
.build();
} else {
getInstance().fees =
new ImmutableList.Builder<Fee>()
.addAll(nullToEmptyImmutableCopy(getInstance().fees))
.add((Fee) feeOrCredit)
.build();
}
return this;
}
public Builder setFeesAndCredits(ImmutableList<BaseFee> feesAndCredits) {
ImmutableList.Builder<Fee> feeBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<Credit> creditBuilder = new ImmutableList.Builder<>();
for (BaseFee feeOrCredit : feesAndCredits) {
if (feeOrCredit instanceof Credit) {
creditBuilder.add((Credit) feeOrCredit);
} else {
feeBuilder.add((Fee) feeOrCredit);
}
}
getInstance().fees = feeBuilder.build();
getInstance().credits = creditBuilder.build();
return this;
}
public Builder setFeesAndCredits(BaseFee ... feesAndCredits) {
return setFeesAndCredits(ImmutableList.copyOf(feesAndCredits));
}
public Builder setCurrency(CurrencyUnit currency) {
getInstance().currency = currency;
return this;
}
public Builder setFeeExtensionRequired(boolean feeExtensionRequired) {
getInstance().feeExtensionRequired = feeExtensionRequired;
return this;
}
@Override
public FeesAndCredits build() {
checkArgumentNotNull(getInstance().currency, "Currency must be specified in FeesAndCredits");
getInstance().fees = nullToEmptyImmutableCopy(getInstance().fees);
getInstance().credits = nullToEmptyImmutableCopy(getInstance().credits);
return getInstance();
}
}
}