Add clientId and time parameters to pricing methods

This is a broken-out refactoring from []
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=121603352
This commit is contained in:
mcilwain 2016-05-05 12:19:50 -07:00 committed by Justine Tunney
parent 4a9be60e0b
commit 6f69e96465
18 changed files with 105 additions and 62 deletions

View file

@ -176,7 +176,8 @@ public abstract class BaseDomainCreateFlow<R extends DomainBase, B extends Build
tldState = registry.getTldState(now);
checkRegistryStateForTld(tld);
domainLabel = domainName.parts().get(0);
createCost = registry.getDomainCreateCost(targetId, command.getPeriod().getValue());
createCost =
registry.getDomainCreateCost(targetId, now, getClientId(), command.getPeriod().getValue());
// The TLD should always be the parent of the requested domain name.
isAnchorTenantViaReservation = matchesAnchorTenantReservation(
domainLabel, tld, command.getAuthInfo().getPw().getValue());
@ -201,7 +202,7 @@ public abstract class BaseDomainCreateFlow<R extends DomainBase, B extends Build
} else if (isClaimsCreate) {
throw new ClaimsPeriodEndedException(tld);
}
verifyPremiumNameIsNotBlocked(targetId, tld, getClientId());
verifyPremiumNameIsNotBlocked(targetId, now, getClientId(), tld);
}
verifyUnitIsYears(command.getPeriod());
verifyNotInPendingDelete(

View file

@ -103,7 +103,8 @@ public class DomainAllocateFlow extends DomainCreateOrAllocateFlow {
.setFlags(billingFlagsBuilder.add(Flag.ALLOCATION).build())
.setTargetId(targetId)
.setClientId(getClientId())
.setCost(registry.getDomainCreateCost(targetId, command.getPeriod().getValue()))
.setCost(registry
.getDomainCreateCost(targetId, now, getClientId(), command.getPeriod().getValue()))
.setPeriodYears(command.getPeriod().getValue())
.setEventTime(now)
// If there are no nameservers on the domain, then they get the benefit of the sunrush add

View file

@ -139,7 +139,7 @@ public class DomainApplicationCreateFlow extends BaseDomainCreateFlow<DomainAppl
@Override
protected void verifyDomainCreateIsAllowed() throws EppException {
validateFeeChallenge(targetId, getTld(), feeCreate, createCost);
validateFeeChallenge(targetId, getTld(), now, getClientId(), feeCreate, createCost);
if (tldState == TldState.LANDRUSH && !superuser) {
// Prohibit creating a landrush application in LANDRUSH (but not in SUNRUSH) if there is
// exactly one sunrise application for the same name.

View file

@ -77,7 +77,7 @@ public class DomainCheckFlow extends BaseDomainCheckFlow {
ReservationType reservationType = getReservationType(domainName);
Registry registry = Registry.get(domainName.parent().toString());
if (reservationType == UNRESERVED
&& registry.isPremiumName(domainName)
&& registry.isPremiumName(domainName, now, getClientId())
&& registry.getPremiumPriceAckRequired()
&& !nullToEmpty(sessionMetadata.getServiceExtensionUris()).contains(
ServiceExtension.FEE_0_6.getUri())) {
@ -114,7 +114,8 @@ public class DomainCheckFlow extends BaseDomainCheckFlow {
throw new OnlyCheckedNamesCanBeFeeCheckedException();
}
FeeCheck.Builder builder = new FeeCheck.Builder();
handleFeeRequest(domainCheck, builder, domainName, getTldFromSld(domainName), now);
handleFeeRequest(
domainCheck, builder, domainName, getTldFromSld(domainName), getClientId(), now);
feeChecksBuilder.add(builder.setName(domainName).build());
}
return ImmutableList.of(FeeCheckResponseExtension.create(feeChecksBuilder.build()));

View file

@ -105,7 +105,7 @@ public class DomainCreateFlow extends DomainCreateOrAllocateFlow {
@Override
protected final void verifyDomainCreateIsAllowed() throws EppException {
String tld = getTld();
validateFeeChallenge(targetId, tld, feeCreate, createCost);
validateFeeChallenge(targetId, tld, now, getClientId(), feeCreate, createCost);
if (!superuser) {
// Prohibit creating a domain if there is an open application for the same name.
for (DomainApplication application : loadActiveApplicationsByDomainName(targetId, now)) {

View file

@ -140,7 +140,7 @@ public class DomainDeleteFlow extends ResourceSyncDeleteFlow<DomainResource, Bui
checkNotNull(gracePeriod.getRecurringBillingEvent()).get().getRecurrenceTimeOfYear();
DateTime autoRenewTime = recurrenceTimeOfYear.getLastInstanceBeforeOrAt(now);
cost = Registry.get(existingResource.getTld())
.getDomainRenewCost(targetId, 1, autoRenewTime);
.getDomainRenewCost(targetId, autoRenewTime, getClientId(), 1);
} else {
cost = checkNotNull(gracePeriod.getOneTimeBillingEvent()).get().getCost();
}

View file

@ -384,9 +384,9 @@ public class DomainFlowUtils {
* where it would not be allowed is if domain name is premium, and premium names are blocked by
* this registrar.
*/
static void verifyPremiumNameIsNotBlocked(String domainName, String tld, String clientId)
throws EppException {
if (Registry.get(tld).isPremiumName(domainName)) {
static void verifyPremiumNameIsNotBlocked(
String domainName, DateTime priceTime, String clientId, String tld) throws EppException {
if (Registry.get(tld).isPremiumName(domainName, priceTime, clientId)) {
// NB: The load of the Registar object is transactionless, which means that it should hit
// memcache most of the time.
if (Registrar.loadByClientId(clientId).getBlockPremiumNames()) {
@ -561,6 +561,7 @@ public class DomainFlowUtils {
BaseFeeResponse.Builder<?, ?> builder,
String domainName,
String tld,
String clientIdentifier,
DateTime now) throws EppException {
InternetDomainName domain = InternetDomainName.from(domainName);
FeeCommandDescriptor feeCommand = feeRequest.getCommand();
@ -584,7 +585,7 @@ public class DomainFlowUtils {
.setPeriod(feeRequest.getPeriod())
// Choose from four classes: premium, premium-collision, collision, or null (standard case).
.setClass(emptyToNull(Joiner.on('-').skipNulls().join(
registry.isPremiumName(domainName) ? "premium" : null,
registry.isPremiumName(domainName, now, clientIdentifier) ? "premium" : null,
isNameCollisionInSunrise ? "collision" : null)));
switch (feeCommand.getCommand()) {
@ -595,7 +596,11 @@ public class DomainFlowUtils {
builder.setClass("reserved"); // Override whatever class we've set above.
} else {
builder.setFee(
Fee.create(registry.getDomainCreateCost(domainName, years).getAmount(), "create"));
Fee.create(
registry
.getDomainCreateCost(domainName, now, clientIdentifier, years)
.getAmount(),
"create"));
}
break;
case RESTORE:
@ -604,23 +609,32 @@ public class DomainFlowUtils {
}
// Restores have a "renew" and a "restore" fee.
builder.setFee(
Fee.create(registry.getDomainRenewCost(domainName, years, now).getAmount(), "renew"),
Fee.create(
registry.getDomainRenewCost(domainName, now, clientIdentifier, years).getAmount(),
"renew"),
Fee.create(registry.getStandardRestoreCost().getAmount(), "restore"));
break;
default:
// Anything else (transfer|renew) will have a "renew" fee.
builder.setFee(
Fee.create(registry.getDomainRenewCost(domainName, years, now).getAmount(), "renew"));
Fee.create(
registry.getDomainRenewCost(domainName, now, clientIdentifier, years).getAmount(),
"renew"));
}
}
static void validateFeeChallenge(
String domainName, String tld,
String domainName,
String tld,
DateTime priceTime,
String clientIdentifier,
final BaseFeeCommand feeCommand,
Money cost, Money... otherCosts) throws EppException {
Money cost,
Money... otherCosts)
throws EppException {
Registry registry = Registry.get(tld);
if (registry.getPremiumPriceAckRequired()
&& registry.isPremiumName(domainName)
&& registry.isPremiumName(domainName, priceTime, clientIdentifier)
&& feeCommand == null) {
throw new FeesRequiredForPremiumNameException();
}

View file

@ -87,7 +87,8 @@ public class DomainInfoFlow extends BaseDomainInfoFlow<DomainResource, Builder>
FeeInfoExtension feeInfo = eppInput.getSingleExtension(FeeInfoExtension.class);
if (feeInfo != null) { // Fee check was requested.
FeeInfoResponseExtension.Builder builder = new FeeInfoResponseExtension.Builder();
handleFeeRequest(feeInfo, builder, getTargetId(), existingResource.getTld(), now);
handleFeeRequest(
feeInfo, builder, getTargetId(), existingResource.getTld(), getClientId(), now);
extensions.add(builder.build());
}
return extensions.build();

View file

@ -110,8 +110,9 @@ public class DomainRenewFlow extends OwnedResourceMutateFlow<DomainResource, Ren
throw new IncorrectCurrentExpirationDateException();
}
renewCost = Registry.get(existingResource.getTld())
.getDomainRenewCost(targetId, command.getPeriod().getValue(), now);
validateFeeChallenge(targetId, existingResource.getTld(), feeRenew, renewCost);
.getDomainRenewCost(targetId, now, getClientId(), command.getPeriod().getValue());
validateFeeChallenge(
targetId, existingResource.getTld(), now, getClientId(), feeRenew, renewCost);
}
@Override

View file

@ -98,12 +98,12 @@ public class DomainRestoreRequestFlow extends OwnedResourceMutateFlow<DomainReso
checkAllowedAccessToTld(getAllowedTlds(), tld);
if (!superuser) {
verifyNotReserved(InternetDomainName.from(targetId), false);
verifyPremiumNameIsNotBlocked(targetId, tld, getClientId());
verifyPremiumNameIsNotBlocked(targetId, now, getClientId(), tld);
}
feeUpdate = eppInput.getSingleExtension(FeeUpdateExtension.class);
restoreCost = Registry.get(tld).getStandardRestoreCost();
renewCost = Registry.get(tld).getDomainRenewCost(targetId, 1, now);
validateFeeChallenge(targetId, tld, feeUpdate, restoreCost, renewCost);
renewCost = Registry.get(tld).getDomainRenewCost(targetId, now, getClientId(), 1);
validateFeeChallenge(targetId, tld, now, getClientId(), feeUpdate, restoreCost, renewCost);
}
@Override

View file

@ -78,8 +78,9 @@ public class DomainTransferApproveFlow extends
.setPeriodYears(extraYears)
.setCost(Registry.get(tld).getDomainRenewCost(
targetId,
extraYears,
transferData.getTransferRequestTime()))
transferData.getTransferRequestTime(),
getClientId(),
extraYears))
.setEventTime(now)
.setBillingTime(now.plus(Registry.get(tld).getTransferGracePeriodLength()))
.setParent(historyEntry)

View file

@ -113,7 +113,8 @@ public class DomainTransferRequestFlow
}
Registry registry = Registry.get(existingResource.getTld());
automaticTransferTime = now.plus(registry.getAutomaticTransferLength());
renewCost = registry.getDomainRenewCost(targetId, command.getPeriod().getValue(), now);
renewCost = registry
.getDomainRenewCost(targetId, now, getClientId(), command.getPeriod().getValue());
transferBillingEvent = new BillingEvent.OneTime.Builder()
.setReason(Reason.TRANSFER)
.setTargetId(targetId)
@ -151,9 +152,10 @@ public class DomainTransferRequestFlow
protected final void verifyTransferRequestIsAllowed() throws EppException {
verifyUnitIsYears(command.getPeriod());
if (!superuser) {
verifyPremiumNameIsNotBlocked(targetId, existingResource.getTld(), getClientId());
verifyPremiumNameIsNotBlocked(targetId, now, getClientId(), existingResource.getTld());
}
validateFeeChallenge(targetId, existingResource.getTld(), feeTransfer, renewCost);
validateFeeChallenge(
targetId, existingResource.getTld(), now, getClientId(), feeTransfer, renewCost);
checkAllowedAccessToTld(getAllowedTlds(), existingResource.getTld());
}

View file

@ -492,26 +492,32 @@ public class Registry extends ImmutableObject implements Buildable {
}
/** Returns true if the given domain name is on the premium price list. */
public boolean isPremiumName(String domainName) {
return isPremiumName(InternetDomainName.from(domainName));
public boolean isPremiumName(String domainName, DateTime priceTime, String clientIdentifier) {
return isPremiumName(InternetDomainName.from(domainName), priceTime, clientIdentifier);
}
/** Returns true if the given domain name is on the premium price list. */
public boolean isPremiumName(InternetDomainName domainName) {
@SuppressWarnings("unused")
public boolean isPremiumName(
InternetDomainName domainName, DateTime priceTime, String clientIdentifier) {
return getPremiumPriceForSld(domainName).isPresent();
}
/** Returns the billing cost for registering the specified domain name for this many years. */
public Money getDomainCreateCost(String domainName, int years) {
@SuppressWarnings("unused")
public Money getDomainCreateCost(
String domainName, DateTime priceTime, String clientIdentifier, int years) {
checkArgument(years > 0, "Number of years must be positive");
Money annualCost = getPremiumPriceForSld(domainName).or(getStandardCreateCost());
return annualCost.multipliedBy(years);
}
/** Returns the billing cost for renewing the specified domain name for this many years. */
public Money getDomainRenewCost(String domainName, int years, DateTime now) {
@SuppressWarnings("unused")
public Money getDomainRenewCost(
String domainName, DateTime priceTime, String clientIdentifier, int years) {
checkArgument(years > 0, "Number of years must be positive");
Money annualCost = getPremiumPriceForSld(domainName).or(getStandardRenewCost(now));
Money annualCost = getPremiumPriceForSld(domainName).or(getStandardRenewCost(priceTime));
return annualCost.multipliedBy(years);
}

View file

@ -17,6 +17,7 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static google.registry.model.registry.Registries.findTldForNameOrThrow;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.net.InternetDomainName;
import com.google.template.soy.data.SoyMapData;
@ -29,6 +30,7 @@ import google.registry.tools.Command.GtechCommand;
import google.registry.tools.soy.CreateAnchorTenantSoyInfo;
import org.joda.money.Money;
import org.joda.time.DateTime;
import javax.inject.Inject;
@ -86,7 +88,9 @@ final class CreateAnchorTenantCommand extends MutatingEppToolCommand implements
Money cost = null;
if (fee) {
cost = Registry.get(tld).getDomainCreateCost(domainName, DEFAULT_ANCHOR_TENANT_PERIOD_YEARS);
cost = Registry.get(tld)
.getDomainCreateCost(
domainName, DateTime.now(UTC), clientIdentifier, DEFAULT_ANCHOR_TENANT_PERIOD_YEARS);
}
setSoyTemplate(CreateAnchorTenantSoyInfo.getInstance(),

View file

@ -179,7 +179,8 @@ public class DomainCreateFlowTest extends ResourceFlowTestCase<DomainCreateFlow,
.setReason(Reason.CREATE)
.setTargetId(getUniqueIdFromCommand())
.setClientId("TheRegistrar")
.setCost(Registry.get(domainTld).isPremiumName(getUniqueIdFromCommand())
.setCost(Registry.get(domainTld)
.isPremiumName(getUniqueIdFromCommand(), clock.nowUtc(), "TheRegistrar")
? Money.of(USD, 200)
: Money.of(USD, 26))
.setPeriodYears(2)

View file

@ -185,6 +185,7 @@ public class DomainTransferFlowTestCase<F extends Flow, R extends EppResource>
return createBillingEventForTransfer(
domain,
historyEntry,
"NewRegistrar",
TRANSFER_REQUEST_TIME,
TRANSFER_EXPIRATION_TIME,
EXTENDED_REGISTRATION_YEARS);

View file

@ -311,9 +311,9 @@ public class RegistryTest extends EntityTestCase {
public void testIsPremiumDomain() throws Exception {
createTld("example");
Registry registry = Registry.get("example");
assertThat(registry.isPremiumName("poor.example")).isFalse();
assertThat(registry.isPremiumName("rich.example")).isTrue();
assertThat(registry.isPremiumName("richer.example")).isTrue();
assertThat(registry.isPremiumName("poor.example", clock.nowUtc(), "TheRegistrar")).isFalse();
assertThat(registry.isPremiumName("rich.example", clock.nowUtc(), "TheRegistrar")).isTrue();
assertThat(registry.isPremiumName("richer.example", clock.nowUtc(), "TheRegistrar")).isTrue();
}
public void testGetDomainCreateCost() throws Exception {
@ -321,10 +321,14 @@ public class RegistryTest extends EntityTestCase {
createTld("example");
Registry registry = Registry.get("example");
// The default value of 17 is set in createTld().
assertThat(registry.getDomainCreateCost("poor.example", 1)).isEqualTo(Money.of(USD, 13));
assertThat(registry.getDomainCreateCost("poor.example", 2)).isEqualTo(Money.of(USD, 26));
assertThat(registry.getDomainCreateCost("rich.example", 1)).isEqualTo(Money.of(USD, 100));
assertThat(registry.getDomainCreateCost("rich.example", 2)).isEqualTo(Money.of(USD, 200));
assertThat(registry.getDomainCreateCost("poor.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 13));
assertThat(registry.getDomainCreateCost("poor.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 26));
assertThat(registry.getDomainCreateCost("rich.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(registry.getDomainCreateCost("rich.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
}
@Test
@ -336,21 +340,21 @@ public class RegistryTest extends EntityTestCase {
START_OF_TIME, Money.of(USD, 8),
clock.nowUtc(), Money.of(USD, 10)))
.build();
assertThat(registry.getDomainRenewCost("poor.example", 1, START_OF_TIME))
assertThat(registry.getDomainRenewCost("poor.example", START_OF_TIME, "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 8));
assertThat(registry.getDomainRenewCost("poor.example", 2, START_OF_TIME))
assertThat(registry.getDomainRenewCost("poor.example", START_OF_TIME, "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 16));
assertThat(registry.getDomainRenewCost("poor.example", 1, clock.nowUtc()))
assertThat(registry.getDomainRenewCost("poor.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 10));
assertThat(registry.getDomainRenewCost("poor.example", 2, clock.nowUtc()))
assertThat(registry.getDomainRenewCost("poor.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 20));
assertThat(registry.getDomainRenewCost("rich.example", 1, START_OF_TIME))
assertThat(registry.getDomainRenewCost("rich.example", START_OF_TIME, "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(registry.getDomainRenewCost("rich.example", 2, START_OF_TIME))
assertThat(registry.getDomainRenewCost("rich.example", START_OF_TIME, "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
assertThat(registry.getDomainRenewCost("rich.example", 1, clock.nowUtc()))
assertThat(registry.getDomainRenewCost("rich.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(registry.getDomainRenewCost("rich.example", 2, clock.nowUtc()))
assertThat(registry.getDomainRenewCost("rich.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
}
@ -454,39 +458,41 @@ public class RegistryTest extends EntityTestCase {
@Test
public void testFailure_isPremiumNameForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
Registry.get("tld").isPremiumName("test.example");
Registry.get("tld").isPremiumName("test.example", clock.nowUtc(), "TheRegistrar");
}
@Test
public void testFailure_isPremiumNameForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
Registry.get("example").isPremiumName("rich.sld.example");
Registry.get("example").isPremiumName("rich.sld.example", clock.nowUtc(), "TheRegistrar");
}
@Test
public void testFailure_getCreateCostForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
Registry.get("tld").getDomainCreateCost("test.example", 1);
Registry.get("tld").getDomainCreateCost("test.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getCreateCostForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
Registry.get("example").getDomainCreateCost("rich.sld.example", 1);
Registry.get("example")
.getDomainCreateCost("rich.sld.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getRenewCostForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
Registry.get("tld").getDomainRenewCost("test.example", 1, clock.nowUtc());
Registry.get("tld").getDomainRenewCost("test.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getRenewCostForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
Registry.get("example").getDomainRenewCost("rich.sld.example", 1, clock.nowUtc());
Registry.get("example")
.getDomainRenewCost("rich.sld.example", clock.nowUtc(), "TheRegistrar", 1);
}
}

View file

@ -424,6 +424,7 @@ public class DatastoreHelper {
public static BillingEvent.OneTime createBillingEventForTransfer(
DomainResource domain,
HistoryEntry historyEntry,
String gainingClientId,
DateTime costLookupTime,
DateTime eventTime,
Integer extendedRegistrationYears) {
@ -437,8 +438,9 @@ public class DatastoreHelper {
.setPeriodYears(extendedRegistrationYears)
.setCost(Registry.get(domain.getTld()).getDomainRenewCost(
domain.getFullyQualifiedDomainName(),
extendedRegistrationYears,
costLookupTime))
costLookupTime,
gainingClientId,
extendedRegistrationYears))
.setParent(historyEntry)
.build();
}
@ -499,6 +501,7 @@ public class DatastoreHelper {
BillingEvent.OneTime transferBillingEvent = persistResource(createBillingEventForTransfer(
domain,
historyEntryDomainTransfer,
"NewRegistrar",
requestTime,
expirationTime,
extendedRegistrationYears));