-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started on Android
A sample app showcasing the use of the MobilePay SDK can be found here.
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')
}
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 want to switch to MobilePay FI please use "APPFI0000000000".
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(BigDecimal.valueOf(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);
}
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(String orderId) {
// The payment was cancelled.
}
});
}
}