Skip to content

Commit

Permalink
Rename Google Pay APIs (#824)
Browse files Browse the repository at this point in the history
* Rename methods

* add CHANGELOG

* update migration guide

* update migration guide

* Fix demo integration

* Shorten variable names
  • Loading branch information
sarahkoop authored Nov 13, 2023
1 parent 639c776 commit 1567e76
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 274 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
* Add `VenmoClient#createPaymentAuthRequest`
* GooglePay
* Remove `GooglePayListener` and `GooglePayRequestPaymentCallback`
* Add `GooglePayLauncher`, `GooglePayIntentData`, `GooglePayIntentDataCallback`, `GooglePayResult`, and `GooglePayResultCallback`
* Add `GooglePayLauncher`, `GooglePayPaymentAuthRequest`,
`GooglePayPaymentAuthRequestCallback`, `GooglePayPaymentAuthResult`,
`GooglePayTokenizeCallback` and `GooglePayLauncherCallback`
* Remove overload constructors, `setListener, and `onActivityResult` from `GooglePayClient`
* Change `GooglePayClient#requestPayment` parameters
* Change `GooglePayClient#requestPayment` parameters and rename to
`GooglePayClient#createPaymentAuthRequest`
* Add `GooglePayClient#tokenize`
* ThreeDSecure
* Remove `ThreeDSecureListener`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public View onCreateView(@NonNull LayoutInflater inflater,
braintreeClient = getBraintreeClient();
googlePayClient = new GooglePayClient(braintreeClient);
googlePayLauncher = new GooglePayLauncher(this,
googlePayResult -> googlePayClient.tokenize(googlePayResult,
paymentAuthResult -> googlePayClient.tokenize(paymentAuthResult,
(paymentMethodNonce, error) -> {
if (error != null) {
handleError(error);
Expand Down Expand Up @@ -117,8 +117,8 @@ public void launchGooglePay(View v) {
.addAllowedCountryCodes(Settings.getGooglePayAllowedCountriesForShipping(activity))
.build());

googlePayClient.requestPayment(googlePayRequest,
(googlePayIntentData, error) -> googlePayLauncher.launch(googlePayIntentData));
googlePayClient.createPaymentAuthRequest(googlePayRequest,
(paymentAuthRequest, error) -> googlePayLauncher.launch(paymentAuthRequest));
}

private void handleGooglePayActivityResult(PaymentMethodNonce paymentMethodNonce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@
import com.google.android.gms.wallet.AutoResolveHelper;
import com.google.android.gms.wallet.PaymentData;

class GooglePayActivityResultContract extends ActivityResultContract<GooglePayIntentData, GooglePayResult> {
class GooglePayActivityResultContract extends ActivityResultContract<GooglePayPaymentAuthRequest, GooglePayPaymentAuthResult> {

@NonNull
@Override
public Intent createIntent(@NonNull Context context, GooglePayIntentData input) {
public Intent createIntent(@NonNull Context context, GooglePayPaymentAuthRequest input) {
return new Intent(context, GooglePayActivity.class)
.putExtra(EXTRA_ENVIRONMENT, input.getGooglePayEnvironment())
.putExtra(EXTRA_PAYMENT_DATA_REQUEST, input.getPaymentDataRequest());
}

@Override
public GooglePayResult parseResult(int resultCode, @Nullable Intent intent) {
public GooglePayPaymentAuthResult parseResult(int resultCode, @Nullable Intent intent) {
if (resultCode == RESULT_OK) {
if (intent != null) {
return new GooglePayResult(PaymentData.getFromIntent(intent), null);
return new GooglePayPaymentAuthResult(PaymentData.getFromIntent(intent), null);
}
} else if (resultCode == RESULT_CANCELED) {
return new GooglePayResult(null, new UserCanceledException("User canceled Google Pay.", true));
return new GooglePayPaymentAuthResult(null, new UserCanceledException("User canceled Google Pay.", true));
} else if (resultCode == RESULT_ERROR) {
if (intent != null) {
return new GooglePayResult(null, new GooglePayException("An error was encountered during the Google Pay " +
return new GooglePayPaymentAuthResult(null, new GooglePayException("An error was encountered during the Google Pay " +
"flow. See the status object in this exception for more details.",
AutoResolveHelper.getStatusFromIntent(intent)));
}
}
return new GooglePayResult(null, new BraintreeException("An unexpected error occurred."));
return new GooglePayPaymentAuthResult(null, new BraintreeException("An unexpected error occurred."));
}
}
205 changes: 93 additions & 112 deletions GooglePay/src/main/java/com/braintreepayments/api/GooglePayClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,46 +94,43 @@ public void isReadyToPay(@NonNull final FragmentActivity activity,
return;
}

braintreeClient.getConfiguration(new ConfigurationCallback() {
@Override
public void onResult(@Nullable Configuration configuration, @Nullable Exception e) {
if (configuration == null) {
callback.onResult(false, e);
return;
}

if (!configuration.isGooglePayEnabled()) {
callback.onResult(false, null);
return;
}

//noinspection ConstantConditions
if (activity == null) {
callback.onResult(false,
new IllegalArgumentException("Activity cannot be null."));
return;
}
braintreeClient.getConfiguration((configuration, e) -> {
if (configuration == null) {
callback.onResult(false, e);
return;
}

JSONObject json = new JSONObject();
JSONArray allowedCardNetworks = buildCardNetworks(configuration);
if (!configuration.isGooglePayEnabled()) {
callback.onResult(false, null);
return;
}

try {
json.put("apiVersion", 2).put("apiVersionMinor", 0).put("allowedPaymentMethods",
new JSONArray().put(new JSONObject().put("type", "CARD")
.put("parameters", new JSONObject().put("allowedAuthMethods",
new JSONArray().put("PAN_ONLY").put("CRYPTOGRAM_3DS"))
.put("allowedCardNetworks", allowedCardNetworks))));
//noinspection ConstantConditions
if (activity == null) {
callback.onResult(false,
new IllegalArgumentException("Activity cannot be null."));
return;
}

if (request != null) {
json.put("existingPaymentMethodRequired",
request.isExistingPaymentMethodRequired());
}
JSONObject json = new JSONObject();
JSONArray allowedCardNetworks = buildCardNetworks(configuration);

} catch (JSONException ignored) {
try {
json.put("apiVersion", 2).put("apiVersionMinor", 0).put("allowedPaymentMethods",
new JSONArray().put(new JSONObject().put("type", "CARD")
.put("parameters", new JSONObject().put("allowedAuthMethods",
new JSONArray().put("PAN_ONLY").put("CRYPTOGRAM_3DS"))
.put("allowedCardNetworks", allowedCardNetworks))));

if (request != null) {
json.put("existingPaymentMethodRequired",
request.isExistingPaymentMethodRequired());
}
IsReadyToPayRequest request = IsReadyToPayRequest.fromJson(json.toString());
internalGooglePayClient.isReadyToPay(activity, configuration, request, callback);

} catch (JSONException ignored) {
}
IsReadyToPayRequest request1 = IsReadyToPayRequest.fromJson(json.toString());
internalGooglePayClient.isReadyToPay(activity, configuration, request1, callback);
});
}

Expand All @@ -153,45 +150,37 @@ public void onResult(@Nullable Configuration configuration, @Nullable Exception
*/
public void getTokenizationParameters(
@NonNull final GooglePayGetTokenizationParametersCallback callback) {
braintreeClient.getAuthorization(new AuthorizationCallback() {
@Override
public void onAuthorizationResult(@Nullable final Authorization authorization,
@Nullable Exception error) {
if (authorization != null) {
braintreeClient.getConfiguration(new ConfigurationCallback() {
@Override
public void onResult(@Nullable Configuration configuration,
@Nullable Exception e) {
if (configuration == null) {
callback.onResult(null, null);
return;
}
callback.onResult(
getTokenizationParameters(configuration, authorization),
getAllowedCardNetworks(configuration));
}
});
} else {
callback.onResult(null, null);
}
braintreeClient.getAuthorization((authorization, error) -> {
if (authorization != null) {
braintreeClient.getConfiguration((configuration, e) -> {
if (configuration == null) {
callback.onResult(null, null);
return;
}
callback.onResult(
getTokenizationParameters(configuration, authorization),
getAllowedCardNetworks(configuration));
});
} else {
callback.onResult(null, null);
}
});
}

/**
* Start the Google Pay payment flow. This will return a {@link GooglePayIntentData} that will
* Start the Google Pay payment flow. This will return a {@link GooglePayPaymentAuthRequest} that will
* be used to present Google Pay payment sheet in
* {@link GooglePayLauncher#launch(GooglePayIntentData)}
* {@link GooglePayLauncher#launch(GooglePayPaymentAuthRequest)}
*
* @param request The {@link GooglePayRequest} containing options for the transaction.
* @param callback {@link GooglePayIntentDataCallback}
* @param callback {@link GooglePayPaymentAuthRequestCallback}
*/
public void requestPayment(@NonNull final GooglePayRequest request,
@NonNull final GooglePayIntentDataCallback callback) {
public void createPaymentAuthRequest(@NonNull final GooglePayRequest request,
@NonNull final GooglePayPaymentAuthRequestCallback callback) {
braintreeClient.sendAnalyticsEvent("google-payment.selected");

if (!validateManifest()) {
callback.onGooglePayIntentData(null, new BraintreeException(
callback.onResult(null, new BraintreeException(
"GooglePayActivity was " + "not found in the " + "Android " +
"manifest, or did not have a theme of R.style.bt_transparent_activity"));
braintreeClient.sendAnalyticsEvent("google-payment.failed");
Expand All @@ -200,63 +189,55 @@ public void requestPayment(@NonNull final GooglePayRequest request,

//noinspection ConstantConditions
if (request == null) {
callback.onGooglePayIntentData(null, new BraintreeException(
callback.onResult(null, new BraintreeException(
"Cannot pass null " + "GooglePayRequest to " + "requestPayment"));
braintreeClient.sendAnalyticsEvent("google-payment.failed");
return;
}

if (request.getTransactionInfo() == null) {
callback.onGooglePayIntentData(null, new BraintreeException(
callback.onResult(null, new BraintreeException(
"Cannot pass null " + "TransactionInfo to" + " requestPayment"));
braintreeClient.sendAnalyticsEvent("google-payment.failed");
return;
}

braintreeClient.getAuthorization(new AuthorizationCallback() {
@Override
public void onAuthorizationResult(@Nullable final Authorization authorization,
@Nullable Exception authError) {
if (authorization != null) {
braintreeClient.getConfiguration(new ConfigurationCallback() {
@Override
public void onResult(@Nullable Configuration configuration,
@Nullable Exception configError) {
if (configuration == null) {
callback.onGooglePayIntentData(null, configError);
return;
}

if (!configuration.isGooglePayEnabled()) {
callback.onGooglePayIntentData(null, new BraintreeException(
"Google Pay " +
"is not enabled for your Braintree account," +
" or Google Play Services are not configured correctly."));
return;
}

setGooglePayRequestDefaults(configuration, authorization, request);
braintreeClient.sendAnalyticsEvent("google-payment.started");

PaymentDataRequest paymentDataRequest =
PaymentDataRequest.fromJson(request.toJson());

GooglePayIntentData intent =
new GooglePayIntentData(getGooglePayEnvironment(configuration),
paymentDataRequest);
callback.onGooglePayIntentData(intent, null);

}
});
braintreeClient.getAuthorization((authorization, authError) -> {
if (authorization != null) {
braintreeClient.getConfiguration((configuration, configError) -> {
if (configuration == null) {
callback.onResult(null, configError);
return;
}

} else {
callback.onGooglePayIntentData(null, authError);
}
if (!configuration.isGooglePayEnabled()) {
callback.onResult(null, new BraintreeException(
"Google Pay " +
"is not enabled for your Braintree account," +
" or Google Play Services are not configured correctly."));
return;
}

setGooglePayRequestDefaults(configuration, authorization, request);
braintreeClient.sendAnalyticsEvent("google-payment.started");

PaymentDataRequest paymentDataRequest =
PaymentDataRequest.fromJson(request.toJson());

GooglePayPaymentAuthRequest intent =
new GooglePayPaymentAuthRequest(getGooglePayEnvironment(configuration),
paymentDataRequest);
callback.onResult(intent, null);

});

} else {
callback.onResult(null, authError);
}
});
}

void tokenize(PaymentData paymentData, GooglePayOnActivityResultCallback callback) {
void tokenize(PaymentData paymentData, GooglePayTokenizeCallback callback) {
try {
JSONObject result = new JSONObject(paymentData.toJson());
callback.onResult(GooglePayCardNonce.fromJSON(result), null);
Expand All @@ -278,25 +259,25 @@ void tokenize(PaymentData paymentData, GooglePayOnActivityResultCallback callbac

/**
* After a user successfully authorizes Google Pay payment via
* {@link GooglePayClient#requestPayment(GooglePayRequest, GooglePayIntentDataCallback)}, this
* {@link GooglePayClient#createPaymentAuthRequest(GooglePayRequest, GooglePayPaymentAuthRequestCallback)}, this
* method should be invoked to tokenize the payment method to retrieve a
* {@link PaymentMethodNonce}
*
* @param googlePayResult the result of {@link GooglePayLauncher#launch(GooglePayIntentData)}
* @param callback {@link GooglePayOnActivityResultCallback}
* @param paymentAuthResult the result of {@link GooglePayLauncher#launch(GooglePayPaymentAuthRequest)}
* @param callback {@link GooglePayTokenizeCallback}
*/
public void tokenize(GooglePayResult googlePayResult,
GooglePayOnActivityResultCallback callback) {
if (googlePayResult.getPaymentData() != null) {
public void tokenize(GooglePayPaymentAuthResult paymentAuthResult,
GooglePayTokenizeCallback callback) {
if (paymentAuthResult.getPaymentData() != null) {
braintreeClient.sendAnalyticsEvent("google-payment.authorized");
tokenize(googlePayResult.getPaymentData(), callback);
} else if (googlePayResult.getError() != null) {
if (googlePayResult.getError() instanceof UserCanceledException) {
tokenize(paymentAuthResult.getPaymentData(), callback);
} else if (paymentAuthResult.getError() != null) {
if (paymentAuthResult.getError() instanceof UserCanceledException) {
braintreeClient.sendAnalyticsEvent("google-payment.canceled");
} else {
braintreeClient.sendAnalyticsEvent("google-payment.failed");
}
callback.onResult(null, googlePayResult.getError());
callback.onResult(null, paymentAuthResult.getError());
}
}

Expand Down

This file was deleted.

Loading

0 comments on commit 1567e76

Please sign in to comment.