Using Braintree SDK on Android for Paypal and Credit Card Payments

Leave a comment

Previously it was just using Paypal SDK to accept Paypal payments but now it requires you to use Braintree SDK to accept Paypal, Credit card or other types of payments. Accepting a payment through Braintree SDK is a multistep process.

Requirements:
1. Braintree live account (you need to apply for it, they will review and approve)
2. Braintree Sandbox account for testing
3. Paypal Business Account
4. Your own server to host server side code (can use localhost and Sandbox for testing though)
5. Linking Paypal with Braintree

These two tutorials shows you how to do using localhost for Android, Braintree SDK and PHP:

1. Braintree Android Integration Tutorial
2. Using Braintree Payment in Android

While these tutorials will give you a head start, none of them fully work. I came across some issues, here’s how I have solved them. Also, not only sandbox , I have tested for production environment as well.

1. After adding Braintree SDK as Module dependency in your Android project there might be mismatch due to SDK version differences

It is annoying specially because you have no idea which library versions are used internally by another library.

The best way to solve the problem is implement all ‘com.android.support:…’ suggested by android studio
(doesn’t matter which support versions you are using — 27.1.1, 28.0.0 etc..)

place the cursor to the error line e.g:
implementation ‘com.android.support:appcompat-v7:28.0.0’
android studio will suggest you which ‘com.android.support:…’ is different version than ‘com.android.support:appcompat-v7:28.0.0’

example

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes).
Found versions 28.0.0, 27.1.0, 27.0.2. Examples include com.android.support:animated-vector-drawable:28.0.0 and com.android.support:exifinterface:27.1.0

so add

com.android.support:animated-vector-drawable:28.0.0
com.android.support:exifinterface:28.0.0. 

now sync gradle file.

one by one try to implement all the suggested ‘com.android.support:…’ till there is no error in this line implementation ‘com.android.support:appcompat-v7:28.0.0’

In my case, my dependencies were like this:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:28.0.0'
compile 'com.android.support:support-v4:28.0.0'
compile 'com.android.support:animated-vector-drawable:28.0.0'
compile 'com.android.support:asynclayoutinflater:28.0.0'
compile 'com.android.support:design:28.0.0'
compile 'com.braintreepayments.api:drop-in:3.+'
compile 'com.braintreepayments.api:braintree:2.+'
compile 'com.loopj.android:android-async-http:1.4.9'
compile 'com.google.android.gms:play-services-ads:8.4.0'
testCompile 'junit:junit:4.12'
}

2. Don’t forget to declare Activity for Paypal payments in the Android Manifest for BrowserSwitchActivity, otherwise it will do nothing on Paypal option selection in the Braintree Drop-In and you will have no idea why it’s doing nothing!

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

3. Volley RequestQueue failed to do HTTP POST for some reason and AsyncHttpClient worked for me

4. Going from Sandbox to Production Environment
After testing in Sandbox Environment to go in Production Environment you just need to update the Merchand Id, Private Key and Public Key in server side PHP.
Still don’t forget to enable Paypal Processing option in Braintree Control Panel, otherwise Paypal option just won’t show up in the Android app UI!

5. Use 10.0.2.2 to access your local machine from Android emulator when running local server like XAMPP/WAMPP
Use 10.0.2.2 to access your actual machine. As I’ve learned, when you use the emulator, localhost ( 127.0.0.1 ) refers to the device’s own loopback service, not the one on your machine as you may expect. You can use 10.0.2.2 to access your actual machine, it is an alias set up to help in development.

6. Sending a mail from PHP after successful transaction
[PHP Warning: mail(): “ sendmail_from” not set in php.ini or custom “From:” header missing

if you’re using you own PC/localhost/127.0.0.1 as server you can’t connect to SMTP server. You can only send mail from live server.
Also check with headers in PHP

$headers =  'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: Your name &lt;info@address.com&gt;' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

mail($to, $subject, $body, $headers);

Leave a Reply