Before you can add PayPal, you will need to:

  1. Create, verify, and link your PayPal account in the Braintree Control Panel
  2. Set up your Android SDK and obtain your client token

Initialization

To add PayPal to your app, first get a client token from your server and initialize Braintree.

Also register a listener to receive a nonce once the PayPal flow finishes.

Showing a PayPal button

Using PaymentButton

The PaymentButton handles displaying the available payment methods to your users. A payment method is available if it's configured, and the app is installed if the payment method requires app switch.

Java
Copy
Copied
PaymentButton paymentButton = (PaymentButton) findViewById(R.id.payment_button);
paymentButton.initialize(this, braintree);

Then handle the result:

Java
Copy
Copied
public void onActivityResult(int requestCode, int responseCode, Intent data){
  if (requestCode == PaymentButton.REQUEST_CODE){
    paymentButton.onActivityResult(requestCode, responseCode, data);
  }
}

A PaymentMethod will be returned to the Braintree.PaymentMethodCreatedListener that you implemented earlier.

Using a custom UI

You can also provide your own button that starts the PayPal flow.

Java
Copy
Copied
yourButton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    braintree.startPayWithPayPal(this, PAYPAL_REQUEST_CODE);
  }
});

When the PayPal activity completes you need to handle the result:

Java
Copy
Copied
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == PAYPAL_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {
      braintree.finishWithPayPal(resultCode, data);
    }
  }
}

Collecting additional data

There is additional data you can gather about your customers as they complete the payment process.

Billing address

To collect billing addresses you will need to enable the PayPal Billing Address Request feature in your PayPal account. To enable this feature, contact PayPal.

If you want to collect billing addresses, add the proper scopes when starting authorization:

Java
Copy
Copied
braintree.startPayWithPayPal(this, PAYPAL_REQUEST_CODE,
  Arrays.asList(PayPalOAuthScopes.PAYPAL_SCOPE_ADDRESS));

Once the scopes are set, the billingAddress property of the returned PayPalAccount should be populated with a PostalAddress. Make sure you implemented the Braintree.PaymentMethodCreatedListener to capture the PayPalAccount.

Java
Copy
Copied
PostalAddress postalAddress = paypalAccount.getBillingAddress();
String streetAddress = postalAddress.getStreetAddress();
String extendedAddress = postalAddress.getExtendedAddress();
String locality = postalAddress.getLocality();
String countryCodeAlpha2 = postalAddress.getCountryCodeAlpha2();
String postalCode = postalAddress.getPostalCode();
String region = postalAddress.getRegion();

If a credit card is not the underlying funding source of the transaction, the customer's primary account address will be returned instead.

Next Page: Vault →

Still have questions?

If you can’t find an answer, contact our Support team