Set up your Android client
Last updated: Sept 23rd, 6:18pm
Set up your Android client-side SDK as part of the Android SDK workflow. The PayPal native Android SDK uses the Braintree client SDK with PayPal's Orders v2 API.
Know before you code
You need the following:
- Android Studio 4.0+.
- Min Android SDK Version API 21 (5.0 / Lollipop).
- PayPal API credentials.
Step 1: Update Gradle dependencies
You can integrate the Android SDK using Gradle. See the GitHub repository to access the SDK source code.
Add the following to your app’s build.gradle file:
1dependencies {2 ...3 implementation 'com.paypal:android-sdk:1.0.0'45 // This example uses the Retrofit http client, feel free6 // to use your favorite http client in its place7 implementation 'com.squareup.retrofit2:retrofit:2.9.0'8}
Step 2: Set up browser switching
For payment flows that switch to a Web Browser/Chrome Custom Tab, register a custom URL scheme to allow web applications to deep link back into your app.
These flows include PayPal checkout and card checkout flows that result in a 3D Secure challenge. The SDK uses a BrowserSwitchActivity to route the app link back to your app.
In Android Studio, open your app’s AnroidManifest.xml, and add the following:
1<activity2 android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"3 android:launchMode="singleTask">4 <intent-filter>5 <action android:name="android.intent.action.VIEW" />6 <category android:name="android.intent.category.DEFAULT" />7 <category android:name="android.intent.category.BROWSABLE" />8 <data android:scheme="${applicationId}.braintree" />9 </intent-filter>10</activity>
Step 3: Set up an HTTP client
In order to perform checkout, you will need to communicate with your server.
Here is an example HttpClient that can fetch both a client token and an order ID:
1public interface MyServer {2 @POST("client_id_token")3 Call<String> fetchClientToken();45 @POST("order_id")6 Call<String> fetchOrderId();7}89public class HttpClient {1011 Retrofit retrofit = new Retrofit.Builder()12 .baseUrl("https://my-merchant-server.com/")13 .build();1415 MyServer myServer = retrofit.create(MyServer.class);1617 public void createClientToken(Callback<String> callback) {18 myServer.fetchClientToken().enqueue(callback);19 }2021 public void createOrderId(Callback<String> callback) {22 myServer.fetchOrderId().enqueue(callback);23 }24}
Step 4: Fetch a client token to initialize the SDK
Send the client-side token that was generated from your server to your client.
1public class MyActivity extends Activity {23 private String clientToken = null;4 private HttpClient httpClient = new HttpClient();56 protected void onCreate(Bundle savedInstanceState) {7 httpClient.createClientToken(new Callback<String>() {8 @Override9 public void onResponse(Call<String> call, Response<String> response) {10 if (response.isSuccessful()) {11 // obtain the token12 clientToken = response.body();13 } else {14 // handle errors15 }16 }17 });18 }19}
Step 5: Create a CheckoutClient
Use the client token to create a CheckoutClient.
1public class MyActivity extends Activity {2 ...34 private void createCheckoutClient() {5 CheckoutClient checkoutClient = new CheckoutClient(clientToken, this);6 }7}