Creating SDK Module Class

Create a module class PayContactlessSDKModule in the android directory extending ReactContextBaseJavaModule and expose all public APIs using @ReactMethod.

All methods return Promises and JSON responses (where applicable). Some methods like doSale or payWithLink require currencyCode, e.g 566 is Naira Code.

A simple usage is highlighted below;

circle-info

Please note that SDK Initialization API is different from the start method, and it has to be called before every other public APIs (e.g initialize, doSale).

import androidx.fragment.app.FragmentActivity
import com.facebook.react.bridge.*
import com.google.gson.Gson
import com.prophius.paycontactlesssdk.*
import java.util.HashMap

class PayContactlessSDKModule(
    reactContext: ReactApplicationContext
) : ReactContextBaseJavaModule(reactContext) {

    override fun getName(): String = "PayContactlessSDKModule"

    private val activity: FragmentActivity?
        get() = reactApplicationContext.currentActivity as? FragmentActivity

    /* --------------------INIT -------------------- */

    @ReactMethod
    fun initialize(apiKey: String, merchantInfo: ReadableMap, promise: Promise) {
        android.util.Log.d("PayContactless", "BRIDGE_HIT: Method entered")

        val merchant = MerchantInfo(
            merchantInfo.getString("merchantId") ?: "",
            merchantInfo.getString("terminalId") ?: "",
            merchantInfo.getString("phoneNumber") ?: "",
            merchantInfo.getString("businessName") ?: ""
        )

        val act = reactApplicationContext.currentActivity as? FragmentActivity
        if (act == null) {
            android.util.Log.e("PayContactless", "ERROR: Activity is null")
            promise.reject("NO_ACTIVITY", "Current activity is null")
            return
        }

        act.runOnUiThread {
            android.util.Log.d("PayContactless", "UI_THREAD: Executing SDK Init")
            try {
                // Your existing MerchantInfo setup...
                PayContactlessEmbed.getInstance().initialize(apiKey, merchant, object : InitializeListener {
                    override fun onSuccess(result: String?) {
                        val out = result ?: "INITIALIZED_BUT_STRING_WAS_NULL"
                        android.util.Log.d("PayContactless", "SDK_SUCCESS: $out")
                        promise.resolve(out)
                    }
                    override fun onFailure(error: Error) {
                        android.util.Log.e("PayContactless", "SDK_FAILURE: ${error.message}")
                        promise.reject("INIT_FAILED", error.message)
                    }
                })
            } catch (e: Exception) {
                promise.reject("CRASH", e.message)
            }
        }
    }

    /* -------------------- DO SALE /PayWithCard -------------------- */

    @ReactMethod
    fun doSale(
        amount: String,
        description: String,
        currencyCode: String,
        promise: Promise
    ) {
        val act = activity
        if (act == null) {
            promise.reject("NO_ACTIVITY", "Current activity is null")
            return
        }

        act.runOnUiThread {
        //SDK call
            PayContactlessEmbed.getInstance().doSale(
                amount,
                description,
                currencyCode,
                act,
                HashMap(),
                TransactionResultListener { result ->
                    promise.resolve(Gson().toJson(result))
                }
            )
        }
    }

Last updated