Drop-in UI
Setup and Integration
Configuration
To use the Drop-in UI, you'll need to get a tokenization key from the Control Panel or you can generate a client token on your server.
Setup
Gradle
Use Gradle to integrate with the Braintree Android SDK.
In your app's build.gradle, add the following:
- Groovy
dependencies {
implementation 'com.braintreepayments.api:drop-in:6.13.0'
}
Client-side implementation
Starting Drop-in
First, create a DropInClient in the onCreate method of an Android Activity or Fragment. Implement the DropInListener interface as well to receive callback notifications from the SDK.
Then, to launch the Drop-in UI call DropInClient#launchDropIn(DropInRequest):
- Java
- Kotlin
public class MyActivity extends AppCompatActivity implements DropInListener {
private DropInClient dropInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// DropInClient can also be instantiated with a tokenization key
dropInClient = new DropInClient(this, new ExampleClientTokenProvider());
// Make sure to register listener in onCreate
dropInClient.setListener(this);
}
private void launchDropIn() {
DropInRequest dropInRequest = new DropInRequest();
dropInClient.launchDropIn(dropInRequest);
}
@Override
public void onDropInSuccess(@NonNull DropInResult dropInResult) {
String paymentMethodNonce = dropInResult.getPaymentMethodNonce().getString();
// use the result to update your UI and send the payment method nonce to your server
}
@Override
public void onDropInFailure(@NonNull Exception error) {
if (error instanceof UserCanceledException) {
// the user canceled
} else {
// handle error
}
}
}
Configuring payment methods
Additional steps are required for the Drop-in UI to accept payment methods other than cards. After completing the Drop-in setup instructions, follow the steps below for each payment method type.
PayPal
Construct a PayPalCheckoutRequest or PayPalVaultRequest, and add it to your DropInRequest:
- Java
- Kotlin
PayPalVaultRequest payPalRequest = new PayPalVaultRequest();
dropInRequest.setPayPalRequest(payPalRequest);
Google Pay
In order for Drop-in to support Google Pay, you must ensure you've added the required meta-data tag in your AndroidManifest.xml.
Construct a GooglePayRequest
, and add it to your DropInRequest
:
- Java
- Kotlin
GooglePayRequest googlePayRequest = new GooglePayRequest();
googlePayRequest.setTransactionInfo(TransactionInfo.newBuilder()
.setTotalPrice("1.00")
.setTotalPriceStatus(WalletConstants.TOTAL_PRICE_STATUS_FINAL)
.setCurrencyCode("USD")
.build());
googlePayRequest.setBillingAddressRequired(true);
dropInRequest.setGooglePayRequest(googlePayRequest);
If using a client token with a customer id, the Google Pay card will not automatically be vaulted. You can use the payment method nonce to create a payment method on your server.
Venmo
To support Venmo payments in the Drop-in UI, make sure to follow the browser switch setup instructions in the Client SDK Setup.
Construct a VenmoRequest, and add it to your DropInRequest:
- Java
- Kotlin
VenmoRequest venmoRequest = new VenmoRequest(VenmoPaymentMethodUsage.MULTI_USE);
dropInRequest.setVenmoRequest(venmoRequest);
3D Secure
Drop-in supports 3D Secure verification. To use 3D Secure in your integration, construct a ThreeDSecureRequest with an amount and add it to your DropInRequest:
- Java
- Kotlin
ThreeDSecureRequest threeDSecureRequest = new ThreeDSecureRequest();
threeDSecureRequest.setAmount("10");
dropInRequest.setThreeDSecureRequest(threeDSecureRequest);
Once you have added 3D Secure to Drop-in, you will need to complete the server-side implementation for 3D Secure.
Displaying the most recently added payment method
If your user already has an existing payment method, you may not need to show Drop-in. You can check if they have an existing payment method using DropInClient#fetchMostRecentPaymentMethod
. A payment method will only be returned when using a client token created with a customer_id
.
- Java
- Kotlin
DropInClient dropInClient = new DropInClient(this, new ExampleClientTokenProvider());
dropInClient.fetchMostRecentPaymentMethod(this, (dropInResult, error) -> {
if (error != null) {
// an error occurred
} else if (dropInResult != null) {
if (dropInResult.getPaymentMethodType() != null) {
DropInPaymentMethod paymentMethodType = dropInResult.getPaymentMethodType();
// use the icon and name to show in your UI
int icon = paymentMethodType.getDrawable();
int name = paymentMethodType.getLocalizedName();
if (paymentMethodType == DropInPaymentMethod.GOOGLE_PAY) {
// The last payment method the user used was Google Pay.
// The Google Pay flow will need to be performed by the
// user again at the time of checkout.
} else {
// Use the payment method show in your UI and charge the user
// at the time of checkout.
PaymentMethodNonce paymentMethod = dropInResult.getPaymentMethodNonce();
}
} else {
// there was no existing payment method
}
}
});
Browser Switch
The Drop-in, in most cases, handles browser switching internally. Specifying an <intent-filter />
in AndroidManifest.xml
is most often no longer required. Any payment method that requires a browser switch will work automatically.
On the other hand, there are some scenarios that will require you to set a custom URL scheme for browser switch deep linking. For example, if your applicationId
contains uppercase letters, you will need to override the default deep link configuration set by the DropIn library.
Internally we use a manifest placeholder to support deep linking into DropInActivity
. You can override the deep link intent filter for DropInActivity
by placing the following xml snippet in your app's AndroidManifest.xml
:
- XML
<activity android:name="com.braintreepayments.api.DropInActivity"
android:exported="true"
tools:node="merge"
>
<intent-filter tools:node="removeAll" />
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="my-custom-url-scheme" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
You can then set the custom url scheme on DropInRequest
to align with the overridden AndroidManifest.xml
value:
- Java
- Kotlin
dropInRequest.setCustomUrlScheme("my-custom-url-scheme");
Once set, the DropIn SDK will use this custom url scheme when deep linking instead of the default one.
Next steps
- Read the Set Up Your Server guide to learn about our server SDKs and how to send a nonce to your server
- Explore the ways to customize the appearance and functionality of Drop-in
- Learn how to manage different payment methods
Next Page: Customization →