Skip to content

Commit

Permalink
Revert to Braintree Merchant Server (#1173)
Browse files Browse the repository at this point in the history
* Revert additional pieces to bt server style.

* Update test environment credentials.

* Add amount field to createTransaction method overloads.
  • Loading branch information
sshropshire authored Oct 1, 2024
1 parent dddbcc5 commit 64fded9
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package com.braintreepayments.api.test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;

import com.braintreepayments.demo.ClientTokenRequest;
import com.braintreepayments.demo.Settings;
import com.braintreepayments.demo.TransactionRequest;
import com.braintreepayments.demo.internal.ApiClient;
import com.braintreepayments.demo.internal.ApiClientRequestInterceptor;
import com.braintreepayments.demo.models.ClientToken;
Expand All @@ -24,6 +17,11 @@
import retrofit.RetrofitError;
import retrofit.client.Response;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;

public class ApiClientTest {

private CountDownLatch countDownLatch;
Expand All @@ -42,11 +40,10 @@ public void setup() {

@Test(timeout = 10000)
public void getClientToken_returnsAClientToken() throws InterruptedException {
ClientTokenRequest request = new ClientTokenRequest();
apiClient.getClientToken(request, new Callback<ClientToken>() {
apiClient.getClientToken(null, null, new Callback<ClientToken>() {
@Override
public void success(ClientToken clientToken, Response response) {
assertNotNull(clientToken.getValue());
assertNotNull(clientToken.getClientToken());
countDownLatch.countDown();
}

Expand All @@ -61,11 +58,10 @@ public void failure(RetrofitError retrofitError) {

@Test(timeout = 10000)
public void getClientToken_returnsAClientTokenForACustomer() throws InterruptedException {
ClientTokenRequest request = new ClientTokenRequest("customer");
apiClient.getClientToken(request, new Callback<ClientToken>() {
apiClient.getClientToken("customer", null, new Callback<ClientToken>() {
@Override
public void success(ClientToken clientToken, Response response) {
assertNotNull(clientToken.getValue());
assertNotNull(clientToken.getClientToken());
countDownLatch.countDown();
}

Expand All @@ -80,12 +76,10 @@ public void failure(RetrofitError retrofitError) {

@Test(timeout = 10000)
public void getClientToken_returnsAClientTokenForAMerchantAccount() throws InterruptedException {
ClientTokenRequest request =
new ClientTokenRequest(null, "fake_switch_usd");
apiClient.getClientToken(request, new Callback<ClientToken>() {
apiClient.getClientToken(null, "fake_switch_usd", new Callback<ClientToken>() {
@Override
public void success(ClientToken clientToken, Response response) {
assertNotNull(clientToken.getValue());
assertNotNull(clientToken.getClientToken());
countDownLatch.countDown();
}

Expand All @@ -100,8 +94,7 @@ public void failure(RetrofitError retrofitError) {

@Test(timeout = 10000)
public void createTransaction_createsATransaction() throws InterruptedException {
TransactionRequest request = new TransactionRequest("1.00", "fake-valid-nonce");
apiClient.createTransaction(request, new Callback<Transaction>() {
apiClient.createTransaction("fake-valid-nonce", new Callback<Transaction>() {
@Override
public void success(Transaction transaction, Response response) {
assertTrue(transaction.getMessage().contains("created") && transaction.getMessage().contains("authorized"));
Expand All @@ -119,9 +112,7 @@ public void failure(RetrofitError error) {

@Test(timeout = 10000)
public void createTransaction_createsATransactionWhenMerchantAccountIsNull() throws InterruptedException {
TransactionRequest request =
new TransactionRequest("2.00", "fake-valid-nonce", null);
apiClient.createTransaction(request, new Callback<Transaction>() {
apiClient.createTransaction("fake-valid-nonce", null, new Callback<Transaction>() {
@Override
public void success(Transaction transaction, Response response) {
assertTrue(transaction.getMessage().contains("created") && transaction.getMessage().contains("authorized"));
Expand All @@ -139,9 +130,7 @@ public void failure(RetrofitError error) {

@Test(timeout = 10000)
public void createTransaction_createsATransactionWhenMerchantAccountIsEmpty() throws InterruptedException {
TransactionRequest request =
new TransactionRequest("3.00", "fake-valid-nonce", "");
apiClient.createTransaction(request, new Callback<Transaction>() {
apiClient.createTransaction("fake-valid-nonce", "", new Callback<Transaction>() {
@Override
public void success(Transaction transaction, Response response) {
assertTrue(transaction.getMessage().contains("created") && transaction.getMessage().contains("authorized"));
Expand All @@ -159,9 +148,7 @@ public void failure(RetrofitError error) {

@Test(timeout = 10000)
public void createTransaction_failsWhenNonceIsAlreadyConsumed() throws InterruptedException {
TransactionRequest request =
new TransactionRequest("4.00", "fake-consumed-nonce");
apiClient.createTransaction(request, new Callback<Transaction>() {
apiClient.createTransaction("fake-consumed-nonce", new Callback<Transaction>() {
@Override
public void success(Transaction transaction, Response response) {
assertEquals("Cannot use a payment_method_nonce more than once.", transaction.getMessage());
Expand All @@ -179,9 +166,7 @@ public void failure(RetrofitError error) {

@Test(timeout = 10000)
public void createTransaction_failsWhenThreeDSecureIsRequired() throws InterruptedException {
TransactionRequest request =
new TransactionRequest("5.00", "fake-valid-nonce", null, true);
apiClient.createTransaction(request, new Callback<Transaction>() {
apiClient.createTransaction("fake-valid-nonce", null, true, new Callback<Transaction>() {
@Override
public void success(Transaction transaction, Response response) {
assertEquals("Gateway Rejected: three_d_secure", transaction.getMessage());
Expand All @@ -196,4 +181,23 @@ public void failure(RetrofitError error) {

countDownLatch.await();
}

@Test(timeout = 10000)
public void createTransaction_createsAUnionPayTransaction() throws InterruptedException {
apiClient.createTransaction("fake-valid-unionpay-credit-nonce", "fake_switch_usd",
new Callback<Transaction>() {
@Override
public void success(Transaction transaction, Response response) {
assertTrue(transaction.getMessage().contains("created") && transaction.getMessage().contains("authorized"));
countDownLatch.countDown();
}

@Override
public void failure(RetrofitError error) {
fail(error.getMessage());
}
});

countDownLatch.await();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PayPalBrowserSwitchTest extends TestHelper {
@Before
public void setup() {
super.setup();
launchApp("Mock PayPal");
launchApp("Sandbox");
onDevice(withText("PayPal Web")).waitForEnabled().perform(click());
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.braintreepayments.api.card.CardNonce;
import com.braintreepayments.api.core.PaymentMethodNonce;
import com.braintreepayments.demo.models.Transaction;

Expand All @@ -24,6 +25,8 @@

public class CreateTransactionFragment extends Fragment {

public static final String EXTRA_PAYMENT_METHOD_NONCE = "nonce";

private ProgressBar loadingSpinner;

@Override
Expand Down Expand Up @@ -58,7 +61,7 @@ public void onResponse(Call<Transaction> call, Response<Transaction> response) {
if (response.isSuccessful()) {
Transaction transaction = response.body();
if (transaction.getMessage() != null &&
transaction.getMessage().startsWith("created")) {
transaction.getMessage().startsWith("created")) {
setStatus(R.string.transaction_complete);
setMessage(transaction.getMessage());
} else {
Expand All @@ -73,7 +76,7 @@ public void onResponse(Call<Transaction> call, Response<Transaction> response) {
setStatus(R.string.transaction_failed);
if (response.body() != null) {
setMessage("Unable to create a transaction. Response Code: " +
response.code() + " Response body: " + response.body());
response.code() + " Response body: " + response.body());
} else {
setMessage("Unable to create a transaction - no response body");
}
Expand All @@ -90,18 +93,35 @@ public void onFailure(Call<Transaction> call, Throwable throwable) {
String nonceString = nonce.getString();
AppCompatActivity activity = (AppCompatActivity) getActivity();

TransactionRequest transactionRequest;
if (Settings.isThreeDSecureEnabled(activity)) {
String threeDSecureMerchantId = Settings.getThreeDSecureMerchantAccountId(activity);
boolean threeDSecureRequired = Settings.isThreeDSecureRequired(activity);
transactionRequest = new TransactionRequest(amount, nonceString, threeDSecureMerchantId, threeDSecureRequired);
if (Settings.isThreeDSecureEnabled(activity) && Settings.isThreeDSecureRequired(activity)) {
DemoApplication
.getApiClient(activity)
.createTransaction(
nonceString,
amount,
Settings.getThreeDSecureMerchantAccountId(activity),
true
)
.enqueue(callback);
} else if (Settings.isThreeDSecureEnabled(activity)) {
DemoApplication
.getApiClient(activity)
.createTransaction(
nonceString,
amount,
Settings.getThreeDSecureMerchantAccountId(activity)
)
.enqueue(callback);
} else {
String merchantAccountId = Settings.getMerchantAccountId(activity);
transactionRequest = new TransactionRequest(amount, nonceString, merchantAccountId, false);
DemoApplication
.getApiClient(activity)
.createTransaction(
nonceString,
amount,
Settings.getMerchantAccountId(activity)
)
.enqueue(callback);
}

DemoApplication.getApiClient(activity).createTransaction(transactionRequest)
.enqueue(callback);
}

private void setStatus(int message) {
Expand Down
70 changes: 35 additions & 35 deletions Demo/src/main/java/com/braintreepayments/demo/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,44 @@ public void fetchClientToken(Context context, final FetchClientTokenCallback lis
String customerId = Settings.getCustomerId(context);
String merchantAccountId = Settings.getMerchantAccountId(context);

ClientTokenRequest request = new ClientTokenRequest(customerId, merchantAccountId);

DemoApplication.getApiClient(context).getClientToken(request)
.enqueue(new Callback<>() {
@Override
public void onResponse(Call<ClientToken> call, Response<ClientToken> response) {
if (response.isSuccessful()) {
String token = response.body().getValue();
if (TextUtils.isEmpty(token)) {
listener.onResult(new FetchClientTokenResult.Error(
new Exception("Client token was empty")
));
} else {
listener.onResult(new FetchClientTokenResult.Success(token));
}
} else {
String responseBody;
if (response.body() != null) {
responseBody = response.body().toString();
DemoApplication
.getApiClient(context)
.getClientToken(customerId, merchantAccountId)
.enqueue(new Callback<>() {
@Override
public void onResponse(Call<ClientToken> call, Response<ClientToken> response) {
if (response.isSuccessful()) {
String token = response.body().getClientToken();
if (TextUtils.isEmpty(token)) {
listener.onResult(new FetchClientTokenResult.Error(
new Exception("Client token was empty")
));
} else {
listener.onResult(new FetchClientTokenResult.Success(token));
}
} else {
responseBody = "empty response body";
}
String responseBody;
if (response.body() != null) {
responseBody = response.body().toString();
} else {
responseBody = "empty response body";
}

String errorFormat =
"Unable to get a client token. Response Code: %d Response body: %s";
String errorMessage = String.format(
Locale.US, errorFormat, response.code(), responseBody);

String errorFormat =
"Unable to get a client token. Response Code: %d Response body: %s";
String errorMessage = String.format(
Locale.US, errorFormat, response.code(), responseBody);

listener.onResult(new FetchClientTokenResult.Error(
new Exception(errorMessage))
);
listener.onResult(new FetchClientTokenResult.Error(
new Exception(errorMessage))
);
}
}
}

@Override
public void onFailure(Call<ClientToken> call, Throwable throwable) {
listener.onResult(new FetchClientTokenResult.Error(new Exception(throwable)));
}
});
@Override
public void onFailure(Call<ClientToken> call, Throwable throwable) {
listener.onResult(new FetchClientTokenResult.Error(new Exception(throwable)));
}
});
}
}
16 changes: 3 additions & 13 deletions Demo/src/main/java/com/braintreepayments/demo/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ public class Settings {
private static final String ENVIRONMENT = "environment";

static final String SANDBOX_ENV_NAME = "Sandbox";
static final String MOCKED_PAY_PAL_ENV_NAME = "Mock PayPal";
static final String PRODUCTION_ENV_NAME = "Production";

private static final String MERCHANT_SERVER_URL = "https://sdk-sample-merchant-server.herokuapp.com";

private static final String SANDBOX_BASE_SERVER_URL = MERCHANT_SERVER_URL + "/braintree/sandbox/";
private static final String PRODUCTION_BASE_SERVER_URL = MERCHANT_SERVER_URL + "/braintree/production/";
private static final String MOCKED_PAY_PAL_SANDBOX_SERVER_URL = MERCHANT_SERVER_URL + "/braintree/mock_pay_pal/";
private static final String PRODUCTION_BASE_SERVER_URL = "https://executive-sample-merchant.herokuapp.com";
private static final String PRODUCTION_TOKENIZATION_KEY = "production_t2wns2y2_dfy45jdj3dxkmz5m";

private static final String SANDBOX_BASE_SERVER_URL = "https://braintree-sample-merchant.herokuapp.com";
private static final String SANDBOX_TOKENIZATION_KEY = "sandbox_tmxhyf7d_dcpspy2brwdjr3qn";
private static final String PRODUCTION_TOKENIZATION_KEY = "production_t2wns2y2_dfy45jdj3dxkmz5m";
private static final String MOCKED_PAY_PAL_SANDBOX_TOKENIZATION_KEY = "sandbox_q7v35n9n_555d2htrfsnnmfb3";

static final String LOCAL_PAYMENTS_TOKENIZATION_KEY = "sandbox_f252zhq7_hh4cpc39zq4rgjcg";

private static final String XO_SANDBOX_TOKENIZATION_KEY = "sandbox_rz48bqvw_jcyycfw6f9j4nj9c";

private static SharedPreferences sharedPreferences;
Expand Down Expand Up @@ -60,8 +54,6 @@ public static String getEnvironmentUrl(Context context) {
String environment = getEnvironment(context);
if (SANDBOX_ENV_NAME.equals(environment)) {
return SANDBOX_BASE_SERVER_URL;
} else if (MOCKED_PAY_PAL_ENV_NAME.equals(environment)) {
return MOCKED_PAY_PAL_SANDBOX_SERVER_URL;
} else if (PRODUCTION_ENV_NAME.equals(environment)) {
return PRODUCTION_BASE_SERVER_URL;
} else {
Expand Down Expand Up @@ -102,8 +94,6 @@ public static String getTokenizationKey(Context context) {

if (SANDBOX_ENV_NAME.equals(environment)) {
return SANDBOX_TOKENIZATION_KEY;
} else if (MOCKED_PAY_PAL_ENV_NAME.equals(environment)) {
return MOCKED_PAY_PAL_SANDBOX_TOKENIZATION_KEY;
} else if (PRODUCTION_ENV_NAME.equals(environment)) {
return PRODUCTION_TOKENIZATION_KEY;
} else {
Expand Down

This file was deleted.

Loading

0 comments on commit 64fded9

Please sign in to comment.