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 build | A button that presents Pay.com's sheet | A screen, from the payment methods and the card fields |
| Payment methods | Cards, redirect methods, and Google Pay | The ones you show |
| Errors and retries | The sheet shows them | You show them |
| Look | Material 3, or your colors and shapes | Yours |
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
Create a payment session on your server
Call Create a payment session with your sandbox key.
amount,currency, andcustomer_reference_id(your own identifier for the customer) are required. Amounts are in minor units. For example,1000is $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_secretfrom the response to the app. Never put your API key in the app. - 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-elementsandpay-threeds.
- The drop-in:
- 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
Start a checkout
PayCheckout.createchecks theclient_secretand loads the session. It's asuspendfunction, so call it from a coroutine, and it throws aPayErrorwhen 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
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
Pass return URLs to the SDK
In
onCreateandonNewIntent, 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
trueonly when the URL belongs to a Pay.com flow in progress. - 7
Confirm the payment on your server
PayResultreflects only client-side state. Before you fulfill the order, retrieve the session with Retrieve a payment session or wait for the signedpayment_session.completedwebhook. Use the session ID your server got when it created the session.