From 3eef39126d88889c66b654a63a0eae3593b683fb Mon Sep 17 00:00:00 2001 From: jart Date: Wed, 9 Mar 2016 09:09:46 -0800 Subject: [PATCH] Add Registrar client ID to Braintree payment info The client ID is passed as the "customer ID" to Braintree. In order for this to work, someone needs to go into the Braintree website beforehand, click on "New Customer" link, and create the customer record with a matching ID. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=116769955 --- .../frontend/FrontendRequestComponent.java | 2 ++ .../registrar/RegistrarPaymentAction.java | 3 ++ .../server/registrar/RegistrarUserModule.java | 36 +++++++++++++++++++ .../ui/server/registrar/SessionUtils.java | 2 ++ .../registrar/RegistrarPaymentActionTest.java | 9 +++++ 5 files changed, 52 insertions(+) create mode 100644 java/com/google/domain/registry/ui/server/registrar/RegistrarUserModule.java diff --git a/java/com/google/domain/registry/module/frontend/FrontendRequestComponent.java b/java/com/google/domain/registry/module/frontend/FrontendRequestComponent.java index c0ca470bf..26168b5de 100644 --- a/java/com/google/domain/registry/module/frontend/FrontendRequestComponent.java +++ b/java/com/google/domain/registry/module/frontend/FrontendRequestComponent.java @@ -28,6 +28,7 @@ import com.google.domain.registry.request.RequestModule; import com.google.domain.registry.request.RequestScope; import com.google.domain.registry.ui.server.registrar.RegistrarPaymentAction; import com.google.domain.registry.ui.server.registrar.RegistrarPaymentSetupAction; +import com.google.domain.registry.ui.server.registrar.RegistrarUserModule; import com.google.domain.registry.whois.WhoisHttpServer; import com.google.domain.registry.whois.WhoisModule; import com.google.domain.registry.whois.WhoisServer; @@ -39,6 +40,7 @@ import dagger.Subcomponent; @Subcomponent( modules = { RdapModule.class, + RegistrarUserModule.class, RequestModule.class, WhoisModule.class, }) diff --git a/java/com/google/domain/registry/ui/server/registrar/RegistrarPaymentAction.java b/java/com/google/domain/registry/ui/server/registrar/RegistrarPaymentAction.java index 06433f19c..61b20a631 100644 --- a/java/com/google/domain/registry/ui/server/registrar/RegistrarPaymentAction.java +++ b/java/com/google/domain/registry/ui/server/registrar/RegistrarPaymentAction.java @@ -24,6 +24,7 @@ import static java.util.Arrays.asList; import com.google.common.base.Function; import com.google.common.collect.ImmutableMap; import com.google.domain.registry.config.ConfigModule.Config; +import com.google.domain.registry.model.registrar.Registrar; import com.google.domain.registry.request.Action; import com.google.domain.registry.request.JsonActionRunner; import com.google.domain.registry.request.JsonActionRunner.JsonAction; @@ -145,6 +146,7 @@ public final class RegistrarPaymentAction implements Runnable, JsonAction { @Inject BraintreeGateway braintreeGateway; @Inject JsonActionRunner jsonActionRunner; + @Inject Registrar registrar; @Inject @Config("braintreeMerchantAccountIds") ImmutableMap accountIds; @Inject RegistrarPaymentAction() {} @@ -183,6 +185,7 @@ public final class RegistrarPaymentAction implements Runnable, JsonAction { .amount(amount.getAmount()) .paymentMethodNonce(paymentMethodNonce) .merchantAccountId(merchantAccountId) + .customerId(registrar.getClientIdentifier()) .options() .submitForSettlement(true) .done()); diff --git a/java/com/google/domain/registry/ui/server/registrar/RegistrarUserModule.java b/java/com/google/domain/registry/ui/server/registrar/RegistrarUserModule.java new file mode 100644 index 000000000..32bbf236c --- /dev/null +++ b/java/com/google/domain/registry/ui/server/registrar/RegistrarUserModule.java @@ -0,0 +1,36 @@ +// Copyright 2016 Google Inc. 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 com.google.domain.registry.ui.server.registrar; + +import com.google.domain.registry.model.registrar.Registrar; +import com.google.domain.registry.request.HttpException.ForbiddenException; + +import dagger.Module; +import dagger.Provides; + +import javax.servlet.http.HttpServletRequest; + +/** Registrar Console module providing reference to logged-in {@link Registrar}. */ +@Module +public final class RegistrarUserModule { + + @Provides + static Registrar provideRegistrarUser(SessionUtils sessionUtils, HttpServletRequest req) { + if (!sessionUtils.checkRegistrarConsoleLogin(req)) { + throw new ForbiddenException("Not authorized to access Registrar Console"); + } + return Registrar.loadByClientId(sessionUtils.getRegistrarClientId(req)); + } +} diff --git a/java/com/google/domain/registry/ui/server/registrar/SessionUtils.java b/java/com/google/domain/registry/ui/server/registrar/SessionUtils.java index 8d4db074b..6a228c93c 100644 --- a/java/com/google/domain/registry/ui/server/registrar/SessionUtils.java +++ b/java/com/google/domain/registry/ui/server/registrar/SessionUtils.java @@ -33,6 +33,7 @@ import com.google.domain.registry.util.FormattingLogger; import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.concurrent.Immutable; +import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @@ -47,6 +48,7 @@ public class SessionUtils { private final UserService userService; + @Inject public SessionUtils(UserService userService) { this.userService = checkNotNull(userService); } diff --git a/javatests/com/google/domain/registry/ui/server/registrar/RegistrarPaymentActionTest.java b/javatests/com/google/domain/registry/ui/server/registrar/RegistrarPaymentActionTest.java index b3c7bc513..0d59158cd 100644 --- a/javatests/com/google/domain/registry/ui/server/registrar/RegistrarPaymentActionTest.java +++ b/javatests/com/google/domain/registry/ui/server/registrar/RegistrarPaymentActionTest.java @@ -22,6 +22,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.google.common.collect.ImmutableMap; +import com.google.domain.registry.model.registrar.Registrar; +import com.google.domain.registry.testing.AppEngineRule; import com.braintreegateway.BraintreeGateway; import com.braintreegateway.Result; @@ -35,6 +37,7 @@ import com.braintreegateway.ValidationErrors; import org.joda.money.CurrencyUnit; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -49,6 +52,9 @@ import java.math.BigDecimal; @RunWith(MockitoJUnitRunner.class) public class RegistrarPaymentActionTest { + @Rule + public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); + @Mock private BraintreeGateway braintreeGateway; @@ -71,6 +77,7 @@ public class RegistrarPaymentActionTest { @Before public void before() throws Exception { + paymentAction.registrar = Registrar.loadByClientId("TheRegistrar"); paymentAction.accountIds = ImmutableMap.of( CurrencyUnit.USD, "merchant-account-usd", @@ -106,6 +113,8 @@ public class RegistrarPaymentActionTest { .isEqualTo(BigDecimal.valueOf(123.4).setScale(2)); assertThat(extractField(String.class, transactionRequest, "merchantAccountId")) .isEqualTo("merchant-account-usd"); + assertThat(extractField(String.class, transactionRequest, "customerId")) + .isEqualTo("TheRegistrar"); } @Test