# Android SDK (Kotlin)

Take crypto payments in a native Android app written in Kotlin or Java. The checkout opens as a full-screen
activity in your app, with no browser bar, and returns a typed result through the Activity Result API.

- **Artifact:** `com.razcryptogateway:android-sdk:1.0.0` (Maven Central release coming soon; use the download today)
- **Requires:** minSdk 24, compileSdk 34+, JDK 17
- **Key in the app:** none (opens the `checkout_url` from your server), or the publishable key `pk_…`
- **Download:** [razcrypto-android-sdk.zip](https://razcryptogateway.com/downloads/razcrypto-android-sdk.zip) · MIT

Using Flutter? See the [Flutter SDK](/docs/flutter-sdk). React Native? See the [React Native SDK](/docs/react-native-sdk).

## How it works

1. The app asks **your server** to start a payment.
2. Your server creates it with the **secret key** and returns the `checkout_url`
   ([PHP / Laravel](/docs/php-sdk), [Node.js](/docs/node-sdk), [Python](/docs/python-sdk)).
3. The app launches the checkout. The customer pays.
4. The app gets `Success`, `Pending`, `Cancelled` or `Failed` and tells your server.
5. Your server verifies the signature, asks RazCrypto for the status and waits for the `payment.paid` webhook.
   **Only the server marks the order paid.**

## 1. Install

### Option A: add the module from the download (recommended today)

```bash
curl -L -o razcrypto-android-sdk.zip https://razcryptogateway.com/downloads/razcrypto-android-sdk.zip
unzip razcrypto-android-sdk.zip
# copy the library module into your project
cp -r razcrypto-android-sdk/razcrypto-checkout  <your-project>/razcrypto-checkout
```

```kotlin
// settings.gradle.kts
include(":app", ":razcrypto-checkout")
```

```kotlin
// app/build.gradle.kts
dependencies {
    implementation(project(":razcrypto-checkout"))
}
```

### Option B: build once and use Maven Local

```bash
cd razcrypto-android-sdk
./gradlew :razcrypto-checkout:publishToMavenLocal     # Windows: gradlew.bat …
```

```kotlin
// settings.gradle.kts → dependencyResolutionManagement { repositories { … } }
mavenLocal()

// app/build.gradle.kts
dependencies { implementation("com.razcryptogateway:android-sdk:1.0.0") }
```

### Option C: Maven Central (after release)

```kotlin
dependencies { implementation("com.razcryptogateway:android-sdk:1.0.0") }
```

### Manifest

Nothing to add. The SDK's own manifest already brings the checkout activity, the internet permission and the
`<queries>` entries that let Android 11+ open MetaMask, Trust and other wallet apps. They merge into your app
automatically.

## 2. Open the checkout

```kotlin
import com.razcryptogateway.checkout.CheckoutOptions
import com.razcryptogateway.checkout.CheckoutResult
import com.razcryptogateway.checkout.RazCryptoCheckout

class CartActivity : ComponentActivity() {

    // Register once, as a field
    private val checkout = registerForActivityResult(RazCryptoCheckout()) { result ->
        when (result) {
            is CheckoutResult.Success -> lifecycleScope.launch {
                // send to your server; it checks the signature and the real status
                val status = api.verifyRazCrypto(result.paymentId, result.referenceId, result.status, result.signature.orEmpty())
                if (status == "completed") showPaid() else waitForPaid(result.paymentId)
            }
            is CheckoutResult.Pending -> waitForPaid(result.paymentId)   // on-chain, confirming
            CheckoutResult.Cancelled -> Unit                             // closed; order stays pending
            is CheckoutResult.Failed -> showError(result.message)
        }
    }

    fun pay(amount: Int) = lifecycleScope.launch {
        val order = api.createRazCryptoPayment(amount)                   // your server → { payment_id, checkout_url }
        checkout.launch(
            CheckoutOptions.Builder()
                .checkoutUrl(order.checkoutUrl)
                .locale("en")                                            // or "hi"
                .build(),
        )
    }
}
```

With the publishable key instead of the URL:

```kotlin
CheckoutOptions.Builder().key("pk_live_…").paymentId("pay_…").build()  // a secret key is refused
```

`waitForPaid` polls your server for about 30 seconds (`GET /payments/razcrypto/status/{id}`), then tells the
customer the order will update shortly. See the Flutter page for the same logic in
[Dart](/docs/flutter-sdk#wait-for-paid-while-the-payment-confirms).

## 3. Server side

Your server needs four endpoints: create, verify, status and the webhook. A complete Laravel version (migration,
admin settings, controller) is on the [PHP SDK page](/docs/php-sdk#laravel-full-integration-for-a-mobile-app).

- `Success.signature` = hex HMAC-SHA256(webhook signing secret, `payment_id|reference_id|status`).
- Check it with `CheckoutSignature::isValid` (PHP), `isValidCheckoutSignature` (Node) or
  `is_valid_checkout_signature` (Python), then call `payments.retrieve(id)`.
- Fulfil only on `status == "paid"` or the verified `payment.paid` [webhook](/docs/webhooks).

## Reference

| `CheckoutOptions.Builder()` | Use |
|---|---|
| `.checkoutUrl(url)` | Recommended. The `checkout_url` from your server. |
| `.key(pk).paymentId(id)` | Publishable key + payment id. |
| `.locale("en" \| "hi")` | Checkout language. |
| `.payHost(url)` | Only for staging or a self-hosted gateway. |

| `CheckoutResult` | Fields |
|---|---|
| `Success` | `paymentId`, `referenceId`, `status`, `signature` |
| `Pending` | `paymentId` |
| `Cancelled` | — |
| `Failed` | `code`, `message` |

**What the customer sees:** network → coin → QR or "Open in MetaMask / Trust" (wallet links open the wallet app) →
green success screen that closes by itself. Back asks "Leave this payment?".

## Test and go live

1. Server on `sk_test_…`, open a payment from the app, then `simulate(paymentId, "paid")` on the server.
2. Check **Dashboard → API apps → Webhooks** for the delivery and your `200`.
3. Live: `sk_live_…` + live signing secret + live webhook URL, then one small real payment.

## Build the SDK yourself

```bash
./gradlew :razcrypto-checkout:testDebugUnitTest :razcrypto-checkout:assembleRelease
```

Android Studio's bundled JDK (JBR 17+) works.
