App2App integration with android library

From Barion Documentation
Jump to navigation Jump to search

In this case of integration, you don't need to edit your server-side. In your mobile application, you don't have to implement network communication. We have already do it for you. If you use the Barion Android library you have to add a few lines of code to your existing codebase. The disadvantages are, that you have to notify your own server-side about the payments and your mobile app will contain the POSKey of the shop.

It is highly recommended to obfuscate client applications! We recommend using ProGuard or DexGuard.

Downloads, example

You can download the Barion Android Library from Barion's Github page.

The example project can be downloadable from Barion's Github page.

The library contains the necessary models, so you don't need to implement them.

Integration steps

1. step - Add the library to your project

Download the .aar file from Barion's Github page and put it into your libs directory!

2. step - Edit build.gradle file

   repositories {
      flatDir {
         dirs 'libs'
      }
   }

   dependencies {
      compile(name:'barionlibrary-1.0.2', ext:'aar')
   }

3. step - Edit the AndroidManifest.xml file

At first, you need to edit the AndroidManifest.xml file.

3.1. step - Providing internet access

For the communication, your application must have internet access. So add the internet access permission to your AndroidManifest file:

   <?xml version="1.0" encoding="utf-8"?>
   <manifest
      xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.barion.example.app2app.libraryintegration">

         <uses-permission android:name="android.permission.INTERNET" />

         <!-- The rest of your AndroidManifest.xml -->

   </manifest>

3.2. step - Add redirect scheme

In order to redirect from the Barion application into the integrator application, a redirect scheme is needed. You can choose anything, but it should be unique. In our example, this is myredirecturl. It is IMPORTANT, that this scheme must be the same as the redirectUrl property of the Payment/Start request. You need to add this scheme in intent-filter into your result activity section.

   <activity
      android:name=".activities.PaymentResultActivity"
      android:label="@string/app_name">
      <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="myredirecturl" />
      </intent-filter>
   </activity>

3.3. step - Add BarionActivity to your AndroidManifest.xml file

Add the BarionActivity class from our library to your AndroidManifest.xml file:

   <activity android:name="barion.com.barionlibrary.activities.BarionActivity"/>

4. step - Initiate payment with the library

At any point in the mobile application - where the products that you want to pay are available - you should initiate the payment in the Barion System. The Barion Android Library will check the parameters and if anything is wrong, it will return with an error. The possible error codes can be found on this page.

To initiate the payment you need to implement a few lines of code:

   ArrayList<BarionProduct> products;
   Intent intent = new Intent(getActivity(), BarionActivity.class);

   intent.putExtra(Barion.PRODUCTS, products);
   intent.putExtra(Barion.PAYMENT_SETTINGS, new PaymentSettingsModel.Builder()
      // the POSKey of the shop
      .posKey("f199e514-9199-40f9-a210-55a0c75be8cc")
      // immediate payment
      .paymentType(PaymentSettingsModel.PaymentTypeEnum.Immediate)
      .guestCheckOut(true)
      // if the debugMode is true the library will communicate with our sandbox environment.
      .debugMode(true)
      .locale("hu-HU")
      .fundingSources(PaymentSettingsModel.FundingSourceTypeEnum.All)
      // this scheme must be the same as in your AndroidManifest.xml file
      .redirectUrl("myredirecturl://")
      .build());

   intent.putExtra(Barion.TRANSACTION_SETTINGS, new TransactionModel.Builder()
      .posTransactionId("f192e544-9199-46f9-a240-55a0c75be8cc")
      .payee("[email protected]")
      .total(200)
      .comment("The new App2App Library")
      .build());

   startActivityForResult(intent, Barion.REQUEST_CODE);


  • The product list contain the products that Item type objects.
  • The PaymentSettingsModel object contains the settings, you can read about it on the Payment/Start page.
  • The TransactionModel object contains the settings of the transactions, you can read about it on the PaymentTransaction page.
  • If the debugMode is true the library will communicate with our sandbox environment.
  • If the user didn't installed the Barion app on their device, the library will show the webview to the user.

5. step - Get results

The library will return with the results in the onActivityResult method:

   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == Barion.REQUEST_CODE) {
         if (resultCode == Activity.RESULT_OK) {
            BarionGetPaymentStateResponse response = (BarionGetPaymentStateResponse) data.getSerializableExtra(Barion.BARION_PAYMENT_STATE_RESPONSE);
            if (response != null && getActivity() != null) {
               Intent intent = new Intent(getActivity(), PaymentResultActivity.class);
               intent.putExtra("PaymentState", response);
               startActivity(intent);
               finish();
            }
         } else if (resultCode == Activity.RESULT_CANCELED) {
            ArrayList<BarionError> errors = (ArrayList<BarionError>) data.getSerializableExtra(Barion.ERROR);
            processBarionErrors(errors);
         }
      }
   }

If everything went fine you can get every information about the payment from the response variable. If there was an error you will get it in the errors list.