Documentation

Borgun, all rights reserved. 2017 ©

Introduction

Borgun MobilePay for Android is a library that supports accepting payments on mobile using RPG.

Installation

Step 1 - Import the library

Add the library in the project:

Add Library

In the build.gradle file add the following instruction:

repositories {
  jcenter()
  flatDir {
    dirs 'libs'
  }
}
dependencies {
  compile (name: 'borgun', ext:'aar')
}

Step 2 - Add a FrameLayout to the Activity that will show the payment screens

Give it the id: borgun_fragment_container as the following example.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".PaymentActivity">
  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/borgun_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

public class PaymentActivity extends ActionBarActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_payment);
    // Step 3
    // Step 4
  }

  // Step 5
}
  

Step 3 - Set up the library configuration

// 1) The library support a customisable theme. It’s possible to set up two colors for the text, two
// colors for the background or and even set an image as background (then the background color
// isn’t taken into account). It’s also possible to set new fonts.

LibraryConfig configuration = new LibraryConfig();
configuration.setMainColor(getResources().getColor(R.color.main_color));
configuration.setSecondaryTextColor(getResources().getColor(R.color.secondary_color));
configuration.setBackgroundColor(getResources().getColor(R.color.background_color));
configuration.setBackgroundContainerColor(getResources().getColor(R.color.background_container_color));
configuration.setBackgroundImage(getResources().getDrawable(R.drawable.background));
configuration.setShowLinesBetweenReceiptItems(false);
configuration.setMainTypeface(Typeface.createFromAsset(getAssets(), "fonts/RalewayRegular.ttf"));
configuration.setSecondTypeface(Typeface.createFromAsset(getAssets(), "fonts/RalewayBold.ttf"));

// 2) Set the Authorization Token provided by Borgun.
configuration.setAppAuthorizationToken(“project_token”);

// 3) Set the language. All language available are provided through an Enum: Language.
configuration.setLanguage(Language.IS);

// 4) Chose a list of available cards for the customer. All cards are provided through an Enum:
//CardType.
// 4) Option 1
configuration.useAllCardTypes();
// 4) Option 2
ArrayList<CardType> cards = new ArrayList<CardType>();
cards.add(CardType.MASTERCARD);
cards.add(CardType.VISA);
configuration.setSelectedCards(cards);

// Provide the created configuration to the library
BorgunLibraryConfiguration.config().setLibraryConfig(configuration);

Step 4 - Set up the Payment configuration

// Make sure the FrameLayout was inflated correctly
if(findViewById(R.id.borgun_fragment_container) != null) {
  // Here is how to create a configuration for one payment

  // The orderId should be unique to identify one payment
  String orderId = "the order id for the following payment";

  // The userId is used to save the card informations for the express checkout
  // A user can have a maximum of one card saved
  String userId = "the user id for the following payment";

  PaymentConfig paymentConfig = new PaymentConfig(10000, Currency.ISK, orderId, userId, TransactionType.SALE);
  final BorgunFragment borgunFragment = new BorgunFragment();
  Bundle args = new Bundle();
  args.putParcelable(BorgunKeys.PAYMENT_CONFIG, paymentConfig);
  borgunFragment.setArguments(args);
  borgunFragment.setBorgunPaymentListener(new
  BorgunFragment.BorgunPaymentListener() {
    @Override
    public void onPaymentCancelled() {
      Log.d(TAG, "onPaymentCancelled");
      // Handle cancellation
    }

    @Override
    public void onPaymentSuccessful(PaymentResponse response) {
      Log.d(TAG, "onPaymentSuccessful");
      // Handle the response
      //If it's pre auth, it's possible to capture, cancel or refund
      //capture(response.getTransactionId());
      //cancel(response.getTransactionId());
      //refund(response.getTransactionId());
      finish();
    }

    @Override
    public void onPaymentFailed() {
      Log.d(TAG, "onPaymentFailed");
      // Handle
    }
  );

  getSupportFragmentManager().beginTransaction().add(R.id.borgun_fragment_container, borgunFragment).commit();
}

Step 5 - Actions possible if the transaction type is Pre_Authorization

After the pre authorization was successful, the amount can be captured, cancelled or refunded by calling the API:


private void capture(String transactionId){
  captureSubscription = BorgunClient.sharedClient().capture(transactionId)
      .subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(new Subscriber<ActionResponse>() {
        @Override
        public void onCompleted() {
          Log.d(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable e) {
          Log.d(TAG, "onError");
          Log.d(TAG, e.getMessage());
        }

        @Override
        public void onNext(ActionResponse actionResponse) {
          Log.d(TAG, "onNextCapture");
          if(actionResponse.getActionCode().equals("000")) {
            finish();
          } else {
          }
        }
      });
}

private void cancel(String transactionId){
  cancelSubscription = BorgunClient.sharedClient().cancel(transactionId)
      .subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(new Subscriber<ActionResponse>() {
        @Override
        public void onCompleted() {
          Log.d(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable e) {
          Log.d(TAG, "onError");
          Log.d(TAG, e.getMessage());
        }

        @Override
        public void onNext(ActionResponse actionResponse) {
          Log.d(TAG, "onNextCancel");
          if(actionResponse.getActionCode().equals("000")) {
            // Everything went well
            finish();
          } else {
            // Read action code to define where the problem is from
          }
        }
      });
}
  

private void refund(String transactionId){
  refundSubscription = BorgunClient.sharedClient().refund(transactionId)
      .subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(new Subscriber<ActionResponse>() {
        @Override
        public void onCompleted() {
          Log.d(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable e) {
          Log.d(TAG, "onError");
          Log.d(TAG, e.getMessage());
        }

        @Override
        public void onNext(ActionResponse actionResponse) {
          Log.d(TAG, "onNextRefund");
          if(actionResponse.getActionCode().equals("000")) {
            // Everything went well
            finish();
          } else {
            // Read action code to define where the problem is from
          }
        }
      });
}