Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single Result Objects for Public Methods #857

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public void getRewardsBalance(@NonNull String nonce, @NonNull String currencyIso
try {
AmericanExpressRewardsBalance rewardsBalance =
AmericanExpressRewardsBalance.fromJson(responseBody);
callback.onResult(rewardsBalance, null);
callback.onAmericanExpressResult(new AmericanExpressResult.Success(rewardsBalance));
} catch (JSONException e) {
braintreeClient.sendAnalyticsEvent("amex.rewards-balance.parse.failed");
callback.onResult(null, e);
callback.onAmericanExpressResult(new AmericanExpressResult.Failure(e));
}
} else {
callback.onResult(null, httpError);
} else if (httpError != null) {
callback.onAmericanExpressResult(new AmericanExpressResult.Failure(httpError));
braintreeClient.sendAnalyticsEvent("amex.rewards-balance.error");
}
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.braintreepayments.api

/**
* Callback for receiving result of
* [AmericanExpressClient.getRewardsBalance].
*/
fun interface AmericanExpressGetRewardsBalanceCallback {
/**
* @param americanExpressResult the [AmericanExpressResult]
*/
fun onAmericanExpressResult(americanExpressResult: AmericanExpressResult)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.braintreepayments.api

/**
* Result of fetching American Express rewards balance
*/
sealed class AmericanExpressResult {

/**
* The [rewardsBalance] was successfully fetched
*/
class Success(val rewardsBalance: AmericanExpressRewardsBalance) : AmericanExpressResult()

/**
* There was an [error] fetching rewards balance
*/
class Failure(val error: Exception) : AmericanExpressResult()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -48,11 +49,13 @@ public void getRewardsBalance_callsListenerWithRewardsBalanceOnSuccess() {
AmericanExpressClient sut = new AmericanExpressClient(braintreeClient);
sut.getRewardsBalance("fake-nonce", "USD", amexRewardsCallback);

ArgumentCaptor<AmericanExpressRewardsBalance> amexRewardsCaptor =
ArgumentCaptor.forClass(AmericanExpressRewardsBalance.class);
verify(amexRewardsCallback).onResult(amexRewardsCaptor.capture(), (Exception) isNull());
ArgumentCaptor<AmericanExpressResult> amexRewardsCaptor =
ArgumentCaptor.forClass(AmericanExpressResult.class);
verify(amexRewardsCallback).onAmericanExpressResult(amexRewardsCaptor.capture());

AmericanExpressRewardsBalance rewardsBalance = amexRewardsCaptor.getValue();
AmericanExpressResult result = amexRewardsCaptor.getValue();
assertTrue(result instanceof AmericanExpressResult.Success);
AmericanExpressRewardsBalance rewardsBalance = ((AmericanExpressResult.Success) result).getRewardsBalance();
assertNotNull(rewardsBalance);
assertEquals("0.0070", rewardsBalance.getConversionRate());
assertEquals("316795.03", rewardsBalance.getCurrencyAmount());
Expand All @@ -73,11 +76,13 @@ public void getRewardsBalance_callsListenerWithRewardsBalanceWithErrorCode_OnIne
AmericanExpressClient sut = new AmericanExpressClient(braintreeClient);
sut.getRewardsBalance("fake-nonce", "USD", amexRewardsCallback);

ArgumentCaptor<AmericanExpressRewardsBalance> amexRewardsCaptor =
ArgumentCaptor.forClass(AmericanExpressRewardsBalance.class);
verify(amexRewardsCallback).onResult(amexRewardsCaptor.capture(), isNull());
ArgumentCaptor<AmericanExpressResult> amexRewardsCaptor =
ArgumentCaptor.forClass(AmericanExpressResult.class);
verify(amexRewardsCallback).onAmericanExpressResult(amexRewardsCaptor.capture());

AmericanExpressRewardsBalance rewardsBalance = amexRewardsCaptor.getValue();
AmericanExpressResult result = amexRewardsCaptor.getValue();
assertTrue(result instanceof AmericanExpressResult.Success);
AmericanExpressRewardsBalance rewardsBalance = ((AmericanExpressResult.Success) result).getRewardsBalance();
assertNotNull(rewardsBalance);
assertNull(rewardsBalance.getConversionRate());
assertNull(rewardsBalance.getCurrencyAmount());
Expand All @@ -98,11 +103,14 @@ public void getRewardsBalance_callsListenerWithRewardsBalanceWithErrorCode_OnIns
AmericanExpressClient sut = new AmericanExpressClient(braintreeClient);
sut.getRewardsBalance("fake-nonce", "USD", amexRewardsCallback);

ArgumentCaptor<AmericanExpressRewardsBalance> amexRewardsCaptor =
ArgumentCaptor.forClass(AmericanExpressRewardsBalance.class);
verify(amexRewardsCallback).onResult(amexRewardsCaptor.capture(), (Exception) isNull());
ArgumentCaptor<AmericanExpressResult> amexRewardsCaptor =
ArgumentCaptor.forClass(AmericanExpressResult.class);
verify(amexRewardsCallback).onAmericanExpressResult(amexRewardsCaptor.capture());

AmericanExpressRewardsBalance rewardsBalance = amexRewardsCaptor.getValue();
AmericanExpressResult result = amexRewardsCaptor.getValue();
assertTrue(result instanceof AmericanExpressResult.Success);

AmericanExpressRewardsBalance rewardsBalance = ((AmericanExpressResult.Success) result).getRewardsBalance();
assertNotNull(rewardsBalance);
assertNull(rewardsBalance.getConversionRate());
assertNull(rewardsBalance.getCurrencyAmount());
Expand All @@ -114,6 +122,27 @@ public void getRewardsBalance_callsListenerWithRewardsBalanceWithErrorCode_OnIns
assertEquals("Insufficient points on card", rewardsBalance.getErrorMessage());
}

@Test
public void getRewardsBalance_callsBackFailure_OnHttpError() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love that we're adding this test coverage. Question: do we have testing around analytic events being sent?

Exception expectedError = new Exception("error");
BraintreeClient braintreeClient = new MockBraintreeClientBuilder()
.sendGETErrorResponse(expectedError)
.build();

AmericanExpressClient sut = new AmericanExpressClient(braintreeClient);
sut.getRewardsBalance("fake-nonce", "USD", amexRewardsCallback);

ArgumentCaptor<AmericanExpressResult> amexRewardsCaptor =
ArgumentCaptor.forClass(AmericanExpressResult.class);
verify(amexRewardsCallback).onAmericanExpressResult(amexRewardsCaptor.capture());

AmericanExpressResult result = amexRewardsCaptor.getValue();
assertTrue(result instanceof AmericanExpressResult.Failure);
Exception actualError = ((AmericanExpressResult.Failure) result).getError();
assertEquals(expectedError, actualError);
}


@Test
public void getRewardsBalance_sendsAnalyticsEventOnSuccess() {
BraintreeClient braintreeClient = new MockBraintreeClientBuilder()
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@
* Remove `GooglePayListener` and `GooglePayRequestPaymentCallback`
* Add `GooglePayLauncher`, `GooglePayPaymentAuthRequest`,
`GooglePayPaymentAuthRequestCallback`, `GooglePayPaymentAuthResult`,
`GooglePayTokenizeCallback` and `GooglePayLauncherCallback`
`GooglePayTokenizeCallback`, `GooglePayTokenizationParameters` and `GooglePayLauncherCallback`
* Remove overload constructors, `setListener, and `onActivityResult` from `GooglePayClient`
* Change `GooglePayClient#requestPayment` parameters and rename to
`GooglePayClient#createPaymentAuthRequest`
* Add `GooglePayClient#tokenize`
* Remove `merchantId` from `GooglePayRequest`
* Change `GooglePayGetTokenizationParametersCallback` parameters
* ThreeDSecure
* Remove `ThreeDSecureListener`
* Add `ThreeDSecureLauncher`, `ThreeDSecurePaymentAuthResult`,
Expand Down Expand Up @@ -105,6 +106,13 @@
modify parameters
* Replace `SEPADirectDebitClient#tokenize` with`SEPADirectDebitClient#createPaymentAuthRequest`
and modify parameters
* American Express
* Change parameters of `AmericanExpressGetRewardsBalanceCallback`
* Add `AmericanExpressResult`
* Visa Checkout
* Change parameters of `VisaCheckoutCreateProfileBuilderCallback` and
`VisaCheckoutTokenizeCallback`
* Add `VisaCheckoutProfileBuilderResult` and `VisaCheckoutTokenizeResult`

## 4.40.1 (2023-12-13)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,27 @@ public void collectDeviceData(@NonNull final Context context, @NonNull final Dat
* @param callback {@link DataCollectorCallback}
*/
public void collectDeviceData(@NonNull final Context context, @Nullable final String riskCorrelationId, @NonNull final DataCollectorCallback callback) {
braintreeClient.getConfiguration(new ConfigurationCallback() {
@Override
public void onResult(@Nullable Configuration configuration, @Nullable Exception error) {
if (configuration != null) {
final JSONObject deviceData = new JSONObject();
try {
DataCollectorRequest request = new DataCollectorRequest()
.setApplicationGuid(getPayPalInstallationGUID(context));
if (riskCorrelationId != null) {
request.setRiskCorrelationId(riskCorrelationId);
}

String correlationId =
magnesInternalClient.getClientMetadataId(context, configuration, request);
if (!TextUtils.isEmpty(correlationId)) {
deviceData.put(CORRELATION_ID_KEY, correlationId);
}
} catch (JSONException ignored) {
braintreeClient.getConfiguration((configuration, error) -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work need to be in the CHANGELOG?

if (configuration != null) {
final JSONObject deviceData = new JSONObject();
try {
DataCollectorRequest request = new DataCollectorRequest()
.setApplicationGuid(getPayPalInstallationGUID(context));
if (riskCorrelationId != null) {
request.setRiskCorrelationId(riskCorrelationId);
}
callback.onResult(deviceData.toString(), null);

} else {
callback.onResult(null, error);
String correlationId =
magnesInternalClient.getClientMetadataId(context, configuration, request);
if (!TextUtils.isEmpty(correlationId)) {
deviceData.put(CORRELATION_ID_KEY, correlationId);
}
} catch (JSONException ignored) {
}
callback.onDataCollectorResult(new DataCollectorResult.Success(deviceData.toString()));

} else if (error != null) {
callback.onDataCollectorResult(new DataCollectorResult.Failure(error));
}
});
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.braintreepayments.api

/**
* Callback for receiving result of [DataCollector.collectDeviceData]
*/
fun interface DataCollectorCallback {
/**
* @param dataCollectorResult the [DataCollectorResult] of collecting device data
*/
fun onDataCollectorResult(dataCollectorResult: DataCollectorResult)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.braintreepayments.api

/**
* Result of collecting device data for fraud detection
*/
sealed class DataCollectorResult {

/**
* The device information was collected for fraud detection. Send [deviceData] to your server
*/
class Success(val deviceData: String) : DataCollectorResult()

/**
* There was an [error] during device data collection
*/
class Failure(val error: Exception) : DataCollectorResult()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.braintreepayments.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.same;
Expand Down Expand Up @@ -113,7 +114,12 @@ public void collectDeviceData_forwardsConfigurationFetchErrors() {
DataCollectorCallback callback = mock(DataCollectorCallback.class);
sut.collectDeviceData(context, callback);

verify(callback).onResult(null, configError);
ArgumentCaptor<DataCollectorResult> deviceDataCaptor = ArgumentCaptor.forClass(DataCollectorResult.class);
verify(callback).onDataCollectorResult(deviceDataCaptor.capture());

DataCollectorResult result = deviceDataCaptor.getValue();
assertTrue(result instanceof DataCollectorResult.Failure);
assertEquals(configError, ((DataCollectorResult.Failure) result).getError());
}

@Test
Expand Down Expand Up @@ -175,10 +181,12 @@ public void collectDeviceData_getsDeviceDataJSONWithCorrelationIdFromPayPal() th
DataCollectorCallback callback = mock(DataCollectorCallback.class);
sut.collectDeviceData(context, callback);

ArgumentCaptor<String> deviceDataCaptor = ArgumentCaptor.forClass(String.class);
verify(callback).onResult(deviceDataCaptor.capture(), (Exception) isNull());
ArgumentCaptor<DataCollectorResult> deviceDataCaptor = ArgumentCaptor.forClass(DataCollectorResult.class);
verify(callback).onDataCollectorResult(deviceDataCaptor.capture());

String deviceData = deviceDataCaptor.getValue();
DataCollectorResult result = deviceDataCaptor.getValue();
assertTrue(result instanceof DataCollectorResult.Success);
String deviceData = ((DataCollectorResult.Success) result).getDeviceData();
JSONObject json = new JSONObject(deviceData);
assertEquals("paypal-clientmetadata-id", json.getString("correlation_id"));
}
Expand Down
21 changes: 14 additions & 7 deletions Demo/src/main/java/com/braintreepayments/demo/CardFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import androidx.navigation.fragment.NavHostFragment;

import com.braintreepayments.api.AmericanExpressClient;
import com.braintreepayments.api.AmericanExpressResult;
import com.braintreepayments.api.AmericanExpressRewardsBalance;
import com.braintreepayments.api.Card;
import com.braintreepayments.api.CardClient;
import com.braintreepayments.api.CardNonce;
import com.braintreepayments.api.CardResult;
import com.braintreepayments.api.DataCollector;
import com.braintreepayments.api.DataCollectorCallback;
import com.braintreepayments.api.DataCollectorResult;
import com.braintreepayments.api.PaymentMethodNonce;
import com.braintreepayments.api.ThreeDSecureAdditionalInformation;
import com.braintreepayments.api.ThreeDSecureClient;
Expand Down Expand Up @@ -147,8 +150,11 @@ private void configureCardForm() {
.setup(activity);

if (getArguments().getBoolean(MainFragment.EXTRA_COLLECT_DEVICE_DATA, false)) {
dataCollector.collectDeviceData(activity,
(deviceData, e) -> this.deviceData = deviceData);
dataCollector.collectDeviceData(activity, dataCollectorResult -> {
if (dataCollectorResult instanceof DataCollectorResult.Success) {
deviceData = ((DataCollectorResult.Success) dataCollectorResult).getDeviceData();
}
});
}
}

Expand Down Expand Up @@ -240,12 +246,13 @@ private void handlePaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNon
getString(R.string.loading), true, false);
String nonce = paymentMethodNonce.getString();

americanExpressClient.getRewardsBalance(nonce, "USD", (rewardsBalance, error) -> {
if (rewardsBalance != null) {
americanExpressClient.getRewardsBalance(nonce, "USD", (americanExpressResult) -> {
if (americanExpressResult instanceof AmericanExpressResult.Success) {
safelyCloseLoadingView();
showDialog(getAmexRewardsBalanceString(rewardsBalance));
} else if (error != null) {
handleError(error);
showDialog(getAmexRewardsBalanceString(
((AmericanExpressResult.Success) americanExpressResult).getRewardsBalance()));
} else if (americanExpressResult instanceof AmericanExpressResult.Failure) {
handleError(((AmericanExpressResult.Failure) americanExpressResult).getError());
}
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import androidx.navigation.fragment.NavHostFragment;

import com.braintreepayments.api.DataCollector;
import com.braintreepayments.api.DataCollectorResult;
import com.braintreepayments.api.PayPalClient;
import com.braintreepayments.api.PayPalLauncher;
import com.braintreepayments.api.PayPalPaymentAuthRequest;
Expand Down Expand Up @@ -79,9 +80,9 @@ private void launchPayPal(boolean isBillingAgreement) {
dataCollector = new DataCollector(requireContext(), super.getAuthStringArg());

if (Settings.shouldCollectDeviceData(requireActivity())) {
dataCollector.collectDeviceData(requireActivity(), (deviceDataResult, error) -> {
if (deviceDataResult != null) {
deviceData = deviceDataResult;
dataCollector.collectDeviceData(requireActivity(), (dataCollectorResult) -> {
if (dataCollectorResult instanceof DataCollectorResult.Success) {
deviceData = ((DataCollectorResult.Success) dataCollectorResult).getDeviceData();
}
launchPayPal(activity, isBillingAgreement, amount);
});
Expand Down
Loading
Loading