10 min Android Braintree (Paypal) integration

Braintree is a paypal payment portal which provides sdks for android, ios, .net core to help you pay via paypal.

How you do it? There are 3 main steps to do it

1. Add BrainTree Drop-In
2. setup onActivityResult
3. Add Braintree activity to your manifest file.


1. Drop In

Drop-in is a quickest way to enable paypal in your app. It handle UI and application from.
Say a user clicks on "pay using paypal", it should run the following codes



private void OpenDropIn()
{
          DropInRequest dropInRequest = new DropInRequest()
            .clientToken(mAuthorization)
            .amount("1.00")
            .requestThreeDSecureVerification(Settings.isThreeDSecureEnabled(this))
            .collectDeviceData(Settings.shouldCollectDeviceData(this))
            .androidPayCart(getAndroidPayCart())
            .androidPayShippingAddressRequired(Settings.isAndroidPayShippingAddressRequired(this))
            .androidPayPhoneNumberRequired(Settings.isAndroidPayPhoneNumberRequired(this))
            .androidPayAllowedCountriesForShipping(Settings.getAndroidPayAllowedCountriesForShipping(this));

    if (Settings.isPayPalAddressScopeRequested(this)) {
        dropInRequest.paypalAdditionalScopes(Collections.singletonList(PayPal.SCOPE_ADDRESS));
    }

    startActivityForResult(dropInRequest.getIntent(this), DROP_IN_REQUEST);
}


Next you will handle results (after Braintree release control back to your app) using sample code below

2. onActivityResult


@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        PaymentConfirmation confirm = data.getParcelableExtra(PaymentlActivity.EXTRA_PAYMENT);
        if (confirm != null) {
            try {
                Log.i("paymentExample", confirm.toJSONObject().toString(4));

                // TODO: send 'confirm' to your server for verification.                // see https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/                // for more details.
            } catch (JSONException e) {
                Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
            }
        }
    }
    else if (resultCode == Activity.RESULT_CANCELED) {
        Log.i("paymentExample", "The user canceled.");
    }
    else if (resultCode == PaymentlActivity.RESULT_EXTRAS_INVALID) {
        Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
    }
}

3. Manifest

Yeap this is required. Otherwise you will not be able see Drop-In application flow

<activity android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="${applicationId}.braintree" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
</activity>


Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm