Skip to main content

Steps to start accepting payments via SADAD Android SDK

👉 DOWNLOAD SAMPLE CODE
  • Merchant have to configure PHP code in the server before starting Android SDK.

SDK Installation and Setup​

Install SDK​

Install Sadad Android SDK using Android Studio. To add the SDK to your app, add the following dependency in your build.gradle:

Steps to Integration :​

Step 1. Add the JitPack repository to your project level build.gradle file

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

Step 2. Add the dependency

dependencies {
implementation 'com.github.SadadDeveloper:Sadad_SDK_Android:v1.1.6'
}

How to Use ?​

  1. Prepare Order for purchase through the SADAD.

  2. Bundle bundle = new Bundle();
    bundle.putString(SadadOrder.ACCESS_TOKEN, token);
    bundle.putString(SadadOrder.CUST_ID, "123456789");
    bundle.putString(SadadOrder.ORDER_ID, "528963147");
    bundle.putString(SadadOrder.TXN_AMOUNT, "100.00");
    bundle.putString(SadadOrder.MOBILE_NO, "9824672292");

    JSONArray productDetails = getProductDetails();

    if (productDetails.length() > 0) {
    bundle.putString(SadadOrder.ORDER_DETAIL,String.valueOf(productDetails));
    }

    //building product details e.g. product name, product quantity, product amount
    JSONArray getProductDetails() {
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    try {
    jsonObject.put("itemname", "Gucci perfume");
    jsonObject.put("quantity", 1);
    jsonObject.put("amount", 1.0);
    } catch (JSONException e) {
    e.printStackTrace();
    }
    jsonArray.put(jsonObject);
    return jsonArray;
    }

    SadadOrder sadadOrder = new SadadOrder();
    sadadOrder.setRequestParamMap(bundle);
  3. Initiate Sadad SDK service.

  4. SadadService.getProductionService();
  5. Create Transaction.

  6. SadadService.createTransaction(HomeActivity.this, sadadOrder, new TransactionCallBack() {
    @Override
    public void onTransactionResponse(String inResponse){

    }

    @Override
    public void onBackPressedCancelTransaction() {

    }

    @Override
    public void onTransactionCancel(String errorJson) {

    }

    @Override
    public void onTransactionFailed(String errorJson) {

    }
    });

    After downloading and setting up SDK, below are some code snippets which might be helpful to build the checkout flow.​

  7. Generating Access Token: public void

  8.     public void generateToken() {
    String url;
    url = ServerConfig.SERVER_TOKEN_URL;
    RestClient.getInstance().post(HomeActivity.this, url, RequestMethod.POST, true, RequestCode.GENERATE_TOKEN, false, new DataObserver() {
    @Override
    public void onSuccess(RequestCode mRequestCode, Object mObject) {
    try {
    JSONObject jsonObject = new JSONObject(mObject.toString());
    String token = jsonObject.optString(Constant.ACCESS_TOKEN, ""); startNewActivity(token);
    }
    catch (JSONException e) {
    e.printStackTrace();
    showSnackBar(rootView, "Invalid Token", getString(R.string.str_ok), true, null);
    }
    }
    @Override
    public void onFailure(RequestCode mRequestCode, String mError, int errorCode) {
    showSnackBar(rootView, mError, getString(R.string.str_ok), true, null);
    }
    )};
    }
  9. Getting transaction response using SDK:

  10. @Override
    public void onTransactionResponse(String inResponse) {
    int transactionStatusId = Constant.TRANSACTION_STATUS_ID_FAILED;
    double amount = totalAmount;
    String transactionNumber = "";

    JSONObject jsonObject;
    try {
    jsonObject = new JSONObject(inResponse);

    if (jsonObject.has("data") && !jsonObject.isNull("data")) {
    JSONObject dataJson = jsonObject.optJSONObject("data");
    }

    if (!dataJson.isNull("transactionstatus")) {
    transactionStatusId = dataJson.optInt("transactionstatus");
    }
    else {
    transactionStatusId = dataJson.optInt("transactionstatusId");
    }

    amount = dataJson.optDouble("amount");

    if (!dataJson.isNull("transactionnumber")) {
    transactionNumber = dataJson.optString("transactionnumber");
    }
    else {
    transactionNumber = dataJson.optString("invoicenumber");
    }
    }
    }
    catch (JSONException e) {
    e.printStackTrace();
    }

    Intent intent = new Intent(HomeActivity.this, TransactionStatusActivity.class);
    intent.putExtra(Constant.TRANSACTION_STATUS, transactionStatusId);
    intent.putExtra(Constant.AMOUNT, amount);
    intent.putExtra(Constant.TRANSACTION_ID, transactionNumber);
    startActivitywithAnimation(intent, false);
    }

In the above public void onTransactionResponse(String inResponse) you will notice the transactionStatusId variable. Your transaction is successful or failed is depends on it. The codes are stated belows :​

  1. ransactionStatusId = 3 means the transaction got success
  2. transactionStatusId = 2 means the transaction got failed

Steps to confiqure PHP code in server​

Create a config.php file over your server and write the following code

<?php 
if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
echo 'access denied';
die;
}
/*
* Set the credentials in below places.
*/
$secretkey = '5rQRZSC4t7Ze42Cb'; // Secret key of the merchant
$sadadid = 7648426; // Sadad id of the merchant
$registered_domain = 'www.some.com'; // Registered domain of the merchant
$type = 'sandbox'; // 'live' - For live SDK,'sandbox' - For Sandbox
?>

Create a index.php file over your server and write the following code. Replace the SadadId, SecretKey and Domain with your original details.

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'GET' && realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
echo 'access denied';
die;
}

include 'config.php';
$wspath = 'https://api.sadadqatar.com/api-v4/';

$requestdata = array();
$requestdata = '{
"sadadId": "<SadadId>",
"secretKey": "<SecretKey>",
"domain": "<Domain>"
}';

$ch = curl_init($wspath . 'userbusinesses/getsdktoken');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, @$requestdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Content-Length: ' . strlen(@$requestdata)));
$returndata = curl_exec($ch) . "\n";
curl_close($ch);

echo $returndata;
?>