> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pesaswap.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin with Node Backend

Integrate hyper SDK to your Kotlin App using Pesaswap-node

## Requirements

* Android 6.0 (API level 23) and above
* Android Gradle Plugin 8.5+
* Gradle 8.8+
* AndroidX

## 1. Setup the Server

```bash theme={"system"}
npm install @Pesaswap-/Pesaswap-node
```

Follow the [Server Setup section](server-setup).

## 2. Build Checkout Page on Your App

### 2.1 Add the Buildscript Classpath

To start integrating the Pesaswap SDK, add the following classpath to the `buildscript` block of your project-level `build.gradle` file:

```gradle theme={"system"}
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "io.Pesaswap:Pesaswap-gradle-plugin:$latest_version"
    }
}
```

### 2.2 Add the Plugin

Add the following plugin to the `plugins` block of your app-level `build.gradle` file:

```gradle theme={"system"}
plugins {
    // Apply Pesaswap Plugin
    id 'io.Pesaswap.plugin'
}
```

### 2.3 Implement the HyperInterface

Next, implement the `HyperInterface` in your `CheckoutActivity`. This involves extending `FragmentActivity` and implementing the `HyperInterface`:

```kotlin theme={"system"}
class CheckoutActivity : AppCompatActivity(), HyperInterface {
    // ...
}
```

<Note> Note : `PaymentSession` is designed to work with AndroidX activities. Ensure that your `CheckoutActivity` extends `FragmentActivity` or its subclass from the AndroidX library.</Note>

### 2.4 Setup the SDK and Fetch a Payment

Set up the SDK using your publishable key. This is essential for initializing a `PaymentSession`:

```kotlin theme={"system"}
val paymentSession = PaymentSession(applicationContext, "YOUR_PUBLISHABLE_KEY");
```

<Note> Note : PaymentSession needs to be initialized in onCreate method of your `FragmentActivity`.</Note>

<Note> Note : For an open-source setup, use the following parameters:</Note>

> ```kotlin theme={"system"}
> val paymentSession = PaymentSession(applicationContext, "YOUR_PUBLISHABLE_KEY", "YOUR_CUSTOM_BACKEND_URL", "YOUR_CUSTOM_LOG_URL")
> ```

#### Fetch a Payment

Request your server to fetch a payment as soon as your view is loaded. Store the `client_secret` returned by your server. The `PaymentSession` will use this secret to complete the payment process.

## 3. Complete the Payment on Your App

### Initialize Payment Session

Initialize the payment session with the `client_secret`:

```kotlin theme={"system"}
paymentSession.initPaymentSession(paymentIntentClientSecret)
```

### Handle Payment Result

Handle the payment result in the completion block. Display appropriate messages to your customer based on the outcome of the payment:

```kotlin theme={"system"}
private fun onPaymentSheetResult(paymentResult: PaymentSheetResult) {
    when (paymentResult) {
        is PaymentSheetResult.Completed -> {
            showToast("Payment complete!")
        }
        is PaymentSheetResult.Canceled -> {
            Log.i(TAG, "Payment canceled!")
        }
        is PaymentSheetResult.Failed -> {
            showAlert("Payment failed", paymentResult.error.localizedMessage)
        }
    }
}
```

<Danger> Important : Please retrieve the payment status from the Pesaswap backend to get the terminal status of the payment. Do not rely solely on the status returned by the SDK, as it may not always reflect the final state of the transaction.</Danger>

### Present the Payment Page

Create a configuration object to customize the payment sheet and present the payment page:

```kotlin theme={"system"}
val configuration = PaymentSheet.Configuration("Your_app, Inc.")

// Present Payment Page
paymentSession.presentPaymentSheet(configuration, ::onPaymentSheetResult)
```

## Final Step

Congratulations! You have successfully integrated the Pesaswap Android SDK into your app. You can now customize the payment sheet to match the look and feel of your app.
