# SDK API Reference ### PayKKa.useEnv() Configure the payment environment used by the SDK. - **Type** ```java public static void useEnv(PayKKaEnvironment env) ``` - **Details** This method is used to set the payment environment before initiating a payment. It supports a sandbox environment and multiple production environments. If this method is not called, `PayKKaEnv.SANDBOX` is used by default. - **Example** ```java import com.paykka.android.checkout.PayKKa; import com.paykka.android.checkout.PayKKaEnv; // Use production environment (Hong Kong) PayKKa.useEnv(PayKKaEnv.PROD_HK); ``` ### PayKKa.goPay() Initiate the payment process. - **Type** ```java public static void goPay( Activity context, String sessionId, PaymentResultCallback onPaymentResultCallback ) public static void goPay( Activity context, String sessionId, PaymentResultCallback onPaymentResultCallback, JSEventCallback onCloseTappedCallback ) ``` - **Details** - `context`: The Activity currently initiating the payment. - `sessionId`: The payment session ID, retrieved from the server. - `onPaymentResultCallback`: Payment result callback. - `onCloseTappedCallback` (optional): Callback when the user taps the close button on the payment page. This method checks if the device supports `PaymentRequest`. If supported, it opens `PayKKaWKWebViewActivity` within the app for payment; otherwise, it redirects to an external browser (Chrome preferred) to complete the payment. **Note**: When using this method, you must call `PayKKa.onPaymentResult` in the `onActivityResult` of the initiating Activity to ensure the callback is triggered. - **Example** ```java PayKKa.goPay(this, "your_session_id", result -> { switch (result.getStatus()) { case SUCCESS: // Payment successful break; case ERROR: // Payment failed break; } }); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); PayKKa.onPaymentResult(requestCode, resultCode, data); } ``` ### PayKKa.registerForActivityResult() Register a payment result listener in modern Android development (ComponentActivity). - **Type** ```java public static PayKKaActivityResultLauncher registerForActivityResult( ComponentActivity context, PaymentResultCallback onPaymentResultCallback ) ``` - **Details** This is the recommended integration method, utilizing Android's `ActivityResultLauncher` API. It registers the callback when the Activity is created and returns a `PayKKaActivityResultLauncher` instance. - **Example** ```java public class MyActivity extends AppCompatActivity { private PayKKaActivityResultLauncher launcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Must be registered during onCreate or the initialization phase launcher = PayKKa.registerForActivityResult(this, result -> { // Handle payment result }); } private void startPayment() { launcher.goPay("your_session_id"); } } ``` ## Data Types ### PaymentResult Payment result object. - **Properties** - `getStatus()`: Returns the `PaymentResult.Status` enum. - `getResult()`: Returns a `JSONObject` containing detailed payment result data. - `getMessage()`: Returns result description information. - **PaymentResult.Status Enum** - `SUCCESS`: Payment successful. - `EXPIRED`: Payment expired. - `ERROR`: Payment error. - `UNKNOWN`: Unknown status. ### JSEvent JavaScript events from the payment page. - **Properties** - `getType()`: Returns `JSEvent.JSEventType`. - `getData()`: Returns the `JSONObject` data carried by the event. - **JSEvent.JSEventType Enum** - `CLOSE_TAPPED`: User tapped the close/back button on the payment page. - `UNKNOWN`: Unknown event. ### PayKKaEnv Predefined payment environments. - **Enum Values** - `SANDBOX`: Sandbox test environment. - `PROD_EU`: European production environment. - `PROD_HK`: Hong Kong production environment.