Android

Checkout workflow for Android

The Checkout workflow for Android is as follows:

  1. The Merchant Android App is integrated with Payabbhi Android SDK.

    • This generally means that your App includes Payabbhi Android SDK and implements PaymentCallback to receive payment response on successful Payment.
  2. The customer now clicks the Pay button in your App.

  3. This opens Payabbhi Checkout where the customer is able to provide his Card details or choose other payment options like NetBanking, Wallets etc.

  4. Payabbhi Checkout sends the payment details directly to Payabbhi Servers from the customer’s device (if it passes basic validation).

  5. Once the payment is successfully completed on the customer’s device, Checkout will return the signed payment response via callback to onPaymentSuccess

  6. Your mobile App should typically pass the signed payment response to your mobile backend (server-side code) for further processing.

  7. Your Server-side code validates the signed payment response (typically using the utility function of our Client Library ).

  8. After verification, your Mobile Backend (server-side code) communicates with your Mobile App for it to display appropriate success (or failure) message to your customer.


Installation

Requirements

  • minSdkVersion: 19

Our Android SDK is distributed via Maven Central.

To include our SDK in your Android project, add the following dependency in build.gradle.

dependencies {

    //Add dependency on Payabbhi Android SDK
    implementation 'com.payabbhi:payabbhi-android:1.0.7'

}

Using the SDK

Make sure you have signed up for your Payabbhi Account and downloaded the API keys from the Portal.

Step 1.1 : Add access_id to AndroidManifest.xml as meta-data

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application>
      ......
        <activity android:name=".MainActivity">
          .....
        </activity>
        <meta-data
            android:name="com.payabbhi.access_id"
            android:value="<your_access_id>"
            />
    </application>
</manifest>

Step 1.2 : Implement PaymentCallback in your Activity.

Your activity needs to implement PaymentCallback to receive callbacks for the Signed Payment Response.

  public class MainActivity extends Activity implements PaymentCallback {
    // your code

    @Override
    public void onPaymentSuccess(PaymentResponse paymentResponse) {
      /**
       * Add your logic here to process the Payment response on successful *payment.
       * It is expected that you would pass the Payment response to your
       * Mobile Backend (Server-side code) for further processing.
       **/
    }

    @Override
    public void onPaymentError(int code, String message) {
      /**
       * Add your logic here to handle scenarios where the
       * Checkout did not result in a successful Payment.
       */
     }

Step 2: Preload Payabbhi (optional)

We have provided a preload method to ensure faster loading of the Payabbhi Checkout form after initiation. This is especially useful in the case of network latency. We suggest that you invoke this method at an appropriate point in your App workflow, typically a few Activities below the Checkout activity in the Back Stack, to ensure optimal user experience.

public class BeforeCheckoutActivity extends Activity {

  // ...

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /**
     * Preload Payabbhi resources
     */
    Payabbhi.preload(getApplicationContext());

    // ...
  }

  // ...

}

Step 3: Initiate payment using Payabbhi

You will need to instantiate Payabbhi class and pass the Checkout Configuration options as a JSONObject. The configuration options in checkout.js are also available for Android.

It is expected that the Mobile Backend (server-side code) would create an Order using Payabbhi Order API and pass the unique order_id to the App. This unique order_id should be passed by your App to the Payabbhi object to initiate Checkout.

  Payabbhi payabbhi = new Payabbhi(); /* Instantiate Payabbhi */

  Activity activity = this;   /* Reference to current activity */

  /* Pass your configuration options to Payabbhi object as a JSONObject */
  try {

    JSONObject options = new JSONObject();

     /* order_id is a unique identifier of the Order created using Payabbhi Order API */
    options.put("order_id", "unique_order_id");

    /* Order amount in paisa (e.g., 5000 paisa = Rs 50.00) */
    options.put("amount", 500);

    options.put("name", "Merchant Name");
    JSONObject prefill = new JSONObject();
    prefill.put("contact", "9999999999");
    prefill.put("email", "bruce@wayneinc.com");
    options.put("prefill", prefill);


    /* Set optional notes */
    JSONObject notes = new JSONObject();
    notes.put("Address","1007 Mountain Drive");
    notes.put("City","Gotham");
    options.put("notes", notes);

    payabbhi.open(activity, options);

  } catch(Exception e) {
    Log.e(TAG, "Error in initiating payment", e);
  }

TIP: You can generate a unique order_id using curl and then pass it as param during development phase:

curl https://payabbhi.com/api/v1/orders \
  -u access_id:secret_key \
  -d amount=5000 \
  -d merchant_order_id=ordRefNo123456 \
  -d currency=INR

payabbhi.open() displays the Checkout form, where the customer can provide her/his Card details or choose other payment options like NetBanking, Wallets etc.

Step 4.1 : onPaymentSuccess

Once the payment is successfully completed on the customer’s device, Payabbhi will return the Signed Payment Response via callback, to onPaymentSuccess.

    @Override
    public void onPaymentSuccess(PaymentResponse paymentResponse) {

      paymentResponse.getPaymentID();
      paymentResponse.getOrderID();
      paymentResponse.getPaymentSignature();
      /**
       * Add your logic here for processing Payment response on successful
       * payment.
       * It is expected that you would pass the Payment response
       * to your Mobile Backend (Server-side code) for further processing
       */
    }

Step 4.2 : onPaymentError

Payabbhi may return an error code via a callback to onPaymentError.

    @Override
    public void onPaymentError(int code, String message) {
      /**
       * Add your logic here to handle scenarios where the
       * Checkout did not result in a successful Payment
       */
       if (Payabbhi.PAYMENT_CANCELED == code) {
           // Handle scenario when payment is canceled
        }
    }

Error Codes

The error codes that may be returned in the onPaymentError method are:

Error Code Description
Payabbhi.INVALID_ARGUMENTS Invalid or missing Checkout Configuration options
Payabbhi.PAYMENT_CANCELED User cancelled the payment (e.g., by pressing back button or tapping on back option)
Payabbhi.NETWORK_ERROR A network-related error occurred (e.g., loss of internet connectivity, connection timeout etc.)

Customization options

Disabling fullscreen mode for Checkout.

If your App has a fullscreen theme, Checkout Activity too will run in fullscreen mode. However, this may impact the UX (e.g. the customer may find it inconvenient to get the OTP by switching apps). This Checkout behaviour can be disabled by calling the following method before payabbhi.open().

payabbhi.setFullScreenDisable(true)