Skip to content

Getting started on Android

Per Thomsen edited this page Dec 27, 2016 · 7 revisions

Android

A sample app showcasing the use of the MobilePay SDK can be found here.

1. Add the SDK to your project

Download the latest SDK and add the .jar to your libs folder. The SDK can be found here.

Make sure to include .jar files under dependencies in your build.gradle file:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

2. Initialize the SDK

Initialize the MobilePay SDK with your MerchantId before using it. This has to be done before using the rest of the SDK. Since this only has to be done once in the application lifecycle the Application or MainActivity classes are good candidates.

Keep in mind that each time you call init() it resets all parameters to their default value.

MobilePay.getInstance().init("APPDK0000000000", Country.DENMARK);

NOTE: The MerchantId "APPDK0000000000" can be used for testing purposes, and will not withdraw any money when completing a transaction through MobilePay. It should be replaced with your own MerchantId in a production environment. If you are targeting MobilePay NO through the SDK use "APPNO0000000000", and if you want to switch to MobilePay FI please use "APPFI0000000000".

3. Create your first payment

To start a payment, first check if the MobilePay app is installed using isMobilePayInstalled. If MobilePay is pressent, create a payment Intent with the createPaymentIntent method and start an AppSwitch to MobilePay using Androids startActivityForResult. Supply it with the created Intent and a request code of your choice.

If MobilePay is not installed, you can create an Intent to take the user to Google Play by calling createDownloadMobilePayIntent.

int MOBILEPAY_PAYMENT_REQUEST_CODE = 1337;

// Check if the MobilePay app is installed on the device.
boolean isMobilePayInstalled = MobilePay.getInstance().isMobilePayInstalled(getApplicationContext());

if (isMobilePayInstalled) {
    // MobilePay is present on the system. Create a Payment object.
    Payment payment = new Payment();
    payment.setProductPrice(new BigDecimal(10.0));
    payment.setOrderId("86715c57-8840-4a6f-af5f-07ee89107ece");

    // Create a payment Intent using the Payment object from above.
    Intent paymentIntent = MobilePay.getInstance().createPaymentIntent(payment);

    // We now jump to MobilePay to complete the transaction. Start MobilePay and wait for the result using an unique result code of your choice.
    startActivityForResult(paymentIntent, MOBILEPAY_PAYMENT_REQUEST_CODE);
} else {
    // MobilePay is not installed. Use the SDK to create an Intent to take the user to Google Play and download MobilePay.
    Intent intent = MobilePay.getInstance().createDownloadMobilePayIntent(getApplicationContext());
    startActivity(intent);
}

4. Handle callbacks from MobilePay

Since we launched MobilePay using startActivityForResult the callback from MobilePay is handled in onActivityResult of your Fragment or Activity.

First check if the request code matches the one you started the payment with. If it is a result from MobilePay call handleResult in the MobilePay SDK to handle the result.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == MOBILEPAY_PAYMENT_REQUEST_CODE) {
    // The request code matches our MobilePay Intent
    MobilePay.getInstance().handleResult(resultCode, data, new ResultCallback() {
      @Override
      public void onSuccess(SuccessResult result) {
        // The payment succeeded - you can deliver the product.
      }
      @Override
      public void onFailure(FailureResult result) {
        // The payment failed - show an appropriate error message to the user. Consult the MobilePay class documentation for possible error codes.
      }
      @Override
      public void onCancel() {
        // The payment was cancelled.
      }
    });
  }
}

###Examples of customizing different properties/settings of the SDK

####Server Callback URL

Set the optional Server Callback URL for server-to-server communication in order to receive instant feedback on current transaction. This enables merchants to react accordingly.

Max 255 characters and it must be an HTTPS:// endpoint

// Create a new payment object (as described in "3. Create your first payment")
Payment payment = new Payment();

// Set the callback URL
payment.setServerCallbackUrl("https://example.com/appswitchstatus");

Upon completion the status of the payment will be posted to the callbacl URL (using http POST). Here's an example of the posted variables:

TimeStamp:2016-11-30T12:42:02.536
OrderId:2016-11-30 Duy 3
MerchantId:APPDK8474110008
TransactionId:1556046783
Amount:00000001
Currency:DKK
Country:DK
PaymentStatus:RES
ReturnCode:00
ReasonCode:00

For the value of ReturnCode and ReasonCode, please refer to this page: https://github.com/MobilePayDev/MobilePay-AppSwitch-API/tree/master/SOAP%20APIs/GetStatus