Skip to Content
Docs

Accept payments in your Android app

Take a first payment in an Android app, with Pay.com's drop-in or your own screen.

To take a payment in the sandbox, your server creates a payment session, the app takes the payment, and your server confirms the result. Show the drop-in, which takes the least code, or build your own screen. The Android SDK reference describes the modules used here.

Drop-in (recommended)Your own UI
What you buildA button that presents Pay.com's sheetA screen, from the payment methods and the card fields
Payment methodsCards, redirect methods, and Google PayThe ones you show
Errors and retriesThe sheet shows themYou show them
LookMaterial 3, or your colors and shapesYours

Both show the customer's saved methods at checkout. The drop-in shows them in a carousel, and your own screen shows them as in Pay with a saved payment method.

  1. 1

    Create a payment session on your server

    Call Create a payment session with your sandbox key. amount, currency, and customer_reference_id (your own identifier for the customer) are required. Amounts are in minor units. For example, 1000 is $10.00.

    curl -X POST https://api.sandbox.pay.com/v1/sessions/payment \
      -u ':sk-pay-com-sandbox-…' \
      -H "Content-Type: application/json" \
      -d '{
        "amount": 1000,
        "currency": "usd",
        "customer_reference_id": "cust-001"
      }'

    Send only the client_secret from the response to the app. Never put your API key in the app.

  2. 2

    Add the SDK

    Add the modules for the UI you choose. See Install.

    • The drop-in: pay-dropin, which brings in the others it needs.
    • Your own UI: pay-elements and pay-threeds.
  3. 3

    Register the return URL

    Redirect payment methods return the customer to your app through a deep link or an App Link. Declare it on the activity that takes the payment, and keep one instance of that activity so the deep link reaches it:

    <activity
        android:name=".CheckoutActivity"
        android:exported="true"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="myshop" android:host="pay" />
        </intent-filter>
    </activity>
  4. 4

    Start a checkout

    PayCheckout.create checks the client_secret and loads the session. It's a suspend function, so call it from a coroutine, and it throws a PayError when it fails.

    import com.pay.sdk.core.PayCheckout
    import com.pay.sdk.core.PayError
    import com.pay.sdk.core.PayResult
    
    val checkout = PayCheckout.create(
        clientSecretFromYourServer,
        PayCheckout.Configuration(returnUrl = "myshop://pay"),
    )
    
    // The session's payment methods, in the order the API returns them.
    val methods = checkout.paymentMethods()

    Live and sandbox sessions use the same configuration.

  5. 5

    Take the payment

    Present the drop-in, or build your own card form and call pay.

    Present the sheet over your activity when the customer taps your checkout button. It shows the session's methods, the card form, and any failure. It also runs 3DS and closes when the payment is done.

    import com.pay.sdk.dropin.PayDropIn
    
    val dropIn = PayDropIn(checkout, PayDropIn.Configuration(merchantDisplayName = "My Shop"))
    
    lifecycleScope.launch {
        when (val result = dropIn.present(this@CheckoutActivity)) {
            is PayResult.Succeeded -> showReceipt(result.ids)   // A hint: your server verifies before fulfilling
            is PayResult.Processing -> showPending(result.ids)  // Your server decides
            PayResult.Canceled -> Unit                          // The customer closed the sheet
            is PayResult.Failed -> show(result.error)           // The session can't be paid any more
        }
    }

    To show Google Pay in the sheet, set the configuration's googlePay. Present the drop-in has the options and what the customer sees, and you can change its look and text.

  6. 6

    Pass return URLs to the SDK

    In onCreate and onNewIntent, pass the deep link back:

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        intent.data?.let { PayCheckout.handleReturn(it.toString()) }
    }

    It's safe to call with any URL. It returns true only when the URL belongs to a Pay.com flow in progress.

  7. 7

    Confirm the payment on your server

    PayResult reflects only client-side state. Before you fulfill the order, retrieve the session with Retrieve a payment session or wait for the signed payment_session.completed webhook. Use the session ID your server got when it created the session.

If you offer payment methods that redirect, your server must create the session with return_url set to the same URL as returnUrl.

Was this page helpful?