Android
Accept card payments and Google Pay in your Android app with the Cardencer payment sheet.
The Cardencer Android SDK shows a native payment sheet for cards and Google Pay, runs 3D Secure inside your app and returns the result to your activity.
Requirements#
- Android 7.0 (API level 24) or later
- Android Gradle Plugin 8.13+ and Gradle 8.13+
- AndroidX
1. Set up your server#
Your server creates the payment with your secret key and returns what the app needs. The app never sees your secret key.
app.post("/create-payment", async (req, res) => {
const payment = await fetch("https://sandbox.cardencer.com/payments", {
method: "POST",
headers: { "api-key": process.env.CARDENCER_API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ amount: 4900, currency: "EUR", customer_id: req.body.customerId, description: "Order 58211" }),
}).then((r) => r.json());
res.json({
sdkAuthorization: payment.sdk_authorization,
clientSecret: payment.client_secret,
publishableKey: process.env.CARDENCER_PUBLISHABLE_KEY,
profileId: process.env.CARDENCER_PROFILE_ID,
});
});@app.post("/create-payment")
def create_payment():
payment = requests.post(
"https://sandbox.cardencer.com/payments",
headers={"api-key": os.environ["CARDENCER_API_KEY"]},
json={"amount": 4900, "currency": "EUR", "customer_id": request.json.get("customerId"), "description": "Order 58211"},
).json()
return jsonify(
sdkAuthorization=payment["sdk_authorization"],
clientSecret=payment["client_secret"],
publishableKey=os.environ["CARDENCER_PUBLISHABLE_KEY"],
profileId=os.environ["CARDENCER_PROFILE_ID"],
)2. Add the SDK#
Add the Gradle plugin to your project-level build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "io.hyperswitch:hyperswitch-gradle-plugin:$latest_version"
}
}Apply it in your app-level build.gradle:
plugins {
id 'io.hyperswitch.plugin'
}The plugin pulls the latest SDK version by default. Check Maven Central for the current plugin version.
3. Initialise the SDK and create a payment session#
When the checkout screen opens, call your server, then create the SDK instance with the publishable key and profile ID it returned and start a session with the sdkAuthorization. Point the SDK at the Cardencer API: https://sandbox.cardencer.com for testing, https://api.cardencer.com in production.
class CheckoutActivity : AppCompatActivity() {
private var paymentSession: PaymentSession? = null
private fun startCheckout(response: CreatePaymentResponse) {
val sdk = Hyperswitch.init(
activity = this,
config = HyperswitchConfiguration(
publishableKey = response.publishableKey,
profileId = response.profileId,
customConfig = CustomEndpointConfiguration(
overrideEndpoints = OverrideEndpoints(
customBackendEndpoint = "https://sandbox.cardencer.com",
),
),
),
)
lifecycleScope.launch {
paymentSession = sdk.initPaymentSession(
PaymentSessionConfiguration(sdkAuthorization = response.sdkAuthorization),
)
payButton.isEnabled = true
}
}
}Create a new session for every payment.
4. Present the payment sheet#
private fun presentPaymentSheet() {
val session = paymentSession ?: return
val configuration = PaymentSheet.Configuration.Builder(merchantDisplayName = "Atelier Martin")
.primaryButtonLabel("Pay €49.00")
.build()
lifecycleScope.launch {
when (val result = session.presentPaymentSheet(configuration)) {
is PaymentResult.Completed -> showMessage("Payment completed")
is PaymentResult.Canceled -> showMessage("Payment cancelled")
is PaymentResult.Failed -> showMessage(result.throwable.message ?: "Payment failed")
}
}
}Completed means the sheet finished, not that you were paid. Ask your server to confirm succeeded through a webhook or GET /payments/{payment_id} before you deliver.
Google Pay#
Google Pay appears in the sheet automatically on supported devices once it is enabled for your account. Contact support to enable it.
Test it#
Use the test cards in the sheet. 4000 0038 0000 0446 shows a 3D Secure challenge inside the app.
Going live#
Replace the sandbox endpoint with https://api.cardencer.com and use your pk_prd_ key from your server. See Going live.