mobile: Android integration

The easiest way to start accepting payments on Android devices using ProcessOut is to use the ProcessOut Android SDK. The SDK will take care of tokenizing your customers’ card numbers, so that you can send those generated tokens to your backend and process payments ↗.


Requirements

The ProcessOut Android SDK is available on Android SDK 14+.

ProcessOut is available through Jitpack. To install it, simply add the following line to your build file:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

as well as this dependency in your build.gradle file:

dependencies {
    compile 'com.github.processout:processout-android:2.6'
}

You’ll then be able to import Processout in your code base and configure the SDK with your ProcessOut API credentials.

// You can find your project ID in your dashboard
final ProcessOut client = new ProcessOut(this, 
    "test-proj_gAO1Uu0ysZJvDuUpOGPkUBeE3pGalk3x");

Tokenize a card

Once all set up, you’ll be able to tokenize the card numbers. The returned token is a simple string.

// First create a card object containing the card details
Card card = new Card("Jeremy lejoux", "4242424242424242", 11, 19, "123");

// And then send the card data to ProcessOut to tokenize
client.tokenize(card, new TokenCallback() {
    @Override
    public void onSuccess(String token) {
        // Send the card token to your backend for charging
    }

    @Override
    public void onError(Exception error) {
        Log.e("ProcessOut", error.toString());
    }
});

Alternative payment methods

ProcessOut also supports alternative payment methods with a redirection flow.

First you’ll need to generate an invoice in your backend and retrieve that invoice ID in order to fetch all the active configurations available for that invoice. You’ll also need to provide this invoice with a specific return url of this form: yourapp://processout.return

curl -X POST https://api.processout.com/invoices \
    -u test-proj_gAO1Uu0ysZJvDuUpOGPkUBeE3pGalk3x:key_sandbox_mah31RDFqcDxmaS7MvhDbJfDJvjtsFTB \
    -d name="Awesome invoice" \
    -d amount="9.99" \
    -d currency=USD 
    -d return_url="yourapp://processout.return"
var ProcessOut = require("processout");
var client = new ProcessOut(
    "test-proj_gAO1Uu0ysZJvDuUpOGPkUBeE3pGalk3x",
    "key_sandbox_mah31RDFqcDxmaS7MvhDbJfDJvjtsFTB");

client.newInvoice().create({
    name:     "Amazing item",
    amount:   "4.99",
    currency: "USD",
    return_url: "yourapp://processout.return"
}).then(function(invoice) {
    // invoice is our newly created resource

}, function(err) {
    // An error occured

});
import processout
client = processout.ProcessOut(
    "test-proj_gAO1Uu0ysZJvDuUpOGPkUBeE3pGalk3x", 
    "key_sandbox_mah31RDFqcDxmaS7MvhDbJfDJvjtsFTB")

invoice = client.new_invoice().create({
    "name":     "Amazing item",
    "amount":   "4.99",
    "currency": "USD",
    "return_url": "yourapp://processout.return"
})
require "processout"
client = ProcessOut::Client.new(
    "test-proj_gAO1Uu0ysZJvDuUpOGPkUBeE3pGalk3x", 
    "key_sandbox_mah31RDFqcDxmaS7MvhDbJfDJvjtsFTB")

invoice = client.invoice.create(
    name:     "Amazing item",
    amount:   "4.99",
    currency: "USD",
    return_url: "yourapp://processout.return"
)
<?php
$client = new \ProcessOut\ProcessOut(
    "test-proj_gAO1Uu0ysZJvDuUpOGPkUBeE3pGalk3x", 
    "key_sandbox_mah31RDFqcDxmaS7MvhDbJfDJvjtsFTB");

$invoice = $client->newInvoice()->create(array(
    "name"     => "Amazing item",
    "amount"   => "4.99",
    "currency" => "USD"
    "return_url" => "yourapp://processout.return"
));
import "gopkg.in/processout.v4"
client := processout.New(
    "test-proj_gAO1Uu0ysZJvDuUpOGPkUBeE3pGalk3x", 
    "key_sandbox_mah31RDFqcDxmaS7MvhDbJfDJvjtsFTB")

iv, err := client.NewInvoice().Create(processout.InvoiceCreateParameters{
    Invoice: &processout.Invoice{
        Name:     processout.String("Amazing item"),
        Amount:   processout.String("4.99"),
        Currency: processout.String("USD"),
        ReturnUrl: processout.String("yourapp://processout.return")
    },
})

Then, you’ll be able to display your APM to your users.

final ProcessOut p = new ProcessOut(this, "test-proj_OET06P4r3J8A1jT1XnaEZjWNp6zKFBZ5");
p.listAlternativeMethods("invoice-id", new ListAlternativeMethodsCallback() {
    @Override
    public void onSuccess(ArrayList<AlternativeGateway> gateways) {
        for (AlternativeGateway g :
            gateways) {
            // display the gateway to your user
        }
    }

    @Override
    public void onError(Exception e) {
        e.printStackTrace();
    }
});

In order to use a specific alternative payment method configuration simply use its redirect method.

Then you’ll need to handle your customers returning to your app. To do so, add an intent-filter to your payment activity like so:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter android:label="@string/processout_return">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="your_app"
            android:host="processout.return" />
    </intent-filter>
</activity>

This will ensure that your app catches all the your_app://processout.com urls. Once done, you’ll be able to retrieve ProcessOut data from a return url:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    Uri data = intent.getData();
    if (data == null)
        this.initiatePayment();
    else {
        // Check if the activity has been opened from ProcessOut
        WebViewReturnAction returnAction = ProcessOut.handleProcessOutReturn(data);
        if (returnAction == null) {
            // Opening URI is not from ProcessOut
        } else {
            switch (returnAction.getType()) {
                case APMAuthorization:
                    // Value contains the APM token
                    if (returnAction.isSuccess())
                        Log.d("PROCESSOUT", returnAction.getValue());
                    break;
                case ThreeDSVerification:   
                    // Value contains the invoice_id    
                    if (returnAction.isSuccess())   
                        Log.d("PROCESSOUT", returnAction.getValue());   
                    break;
            }
        }
    }
}

There are two cases where your app could be opened from a return URL: 1- From an Alternative Payment Method authorization request in which case a APM token is returned 2- From a ThreeDS1 check in which case the invoice ID is returned (please note that even when performing a 3DS2 challenge, in some cases if the 3DS2 is not supported a fallback to 3DS1 will be performed.)

In either case you should send these information back to your backend to create the charge.

Next steps

You’re all set! You can now send the newly created token to your backend to:

If you’re currently setting up payments for your mobile application, you’re also most likely developping it for the iOS platform as well. We have a very similar SDK available for Apple’s devices, available here ↗. Feel free to check it out!