Skip to content

Quickstart

Before reading this document, make sure you have completed the Integration Guide and understand the APIs for creating checkout sessions.

The Android SDK provided by PayKKa allows you to natively embed multiple payment methods in your app with ease. You only need to pass a sessionId to the SDK to open the checkout page in your app, receive user payments, and customize and handle related payment callbacks. The latest SDK download is available in Android SDK Release History and Changelog.

Step 1: Add the SDK to Your Project

After downloading the SDK, you will get a .zip file with a directory structure similar to the following:

.
├── libs
│   ├── alipayplusclient-2.2.0-release.aar
│   ├── cardinalmobilesdk-2.2.7-7.aar
│   ├── paykka-checkout-payments-1.1-Release.aar
│   └── ...
└── PayKKaCheckoutApp-Android
    ├── JavaCheckoutDemo
    │   ├── app
    │   ├── build.gradle
    │   ├── gradle
    │   ├── gradle.properties
    │   ├── gradlew
    │   ├── gradlew.bat
    │   └── settings.gradle
    └── KotlinCheckoutDemo
        ├── app
        ├── build.gradle.kts
        ├── gradle
        ├── gradle.properties
        ├── gradlew
        ├── gradlew.bat
        └── settings.gradle.kts

PayKKaCheckoutApp-Android contains sample Android projects written in Java and Kotlin. Developers can use the sample code to learn how to use the PayKKa SDK APIs.

The paykka-checkout-payments-X.X-Release.aar file in the libs folder is the core PayKKa SDK. Copy the libs folder from the downloaded zip file into the app/ folder of your Android project, for example YourAppProject/app/. Then edit app/build.gradle.kts or app/build.gradle and add the following code to import and use the SDK.

dependencies {
    // Required: include all libraries and dependencies in the libs directory
    // paykka-checkout-payments-x.x-Release.aar: required, the core PayKKa SDK
    // 
    // alipayplusclient-x.x.x-release.aar: required for [Alipay+], provides Alipay+ capabilities
    // cardinalmobilesdk-x.x.x-x.aar: required for [BankCardPay], used for anti-fraud, risk control, and related 3DS flows
    implementation(
        fileTree(
            mapOf(
                "dir" to "libs",
                "include" to listOf("*.aar", "*.jar")
            )
        )
    )
    // Required: the SDK uses Gson for JSON serialization/deserialization
    implementation("com.google.code.gson:gson:2.13.2")
    // Required: related SDK UI styles and component implementations are based on this library
    implementation("com.google.android.material:material:1.12.0")
    // Required: related SDK API requests are based on okhttp
    implementation("com.squareup.okhttp3:okhttp:4.12.0")
    // Required: the SDK uses some WebKit features
    implementation("androidx.webkit:webkit:1.14.0")

    // Required for [GooglePay]: Google Pay native SDK
    implementation("com.google.android.gms:play-services-wallet:19.5.0")
    // Required for [BankCardPay, GooglePay]: used for anti-fraud, risk control, and related 3DS flows
    implementation("com.stripe:payments-core:22.6.1")
    // Required for [WeChatPay]: WeChat Pay native SDK
    implementation("com.tencent.mm.opensdk:wechat-sdk-android:6.8.34")
}

Step 2: Obtain AppCode

During initialization, the SDK verifies whether the package name and signing certificate hash of the host app match the information stored in the PayKKa database, in order to confirm that SDK calls are coming from the app officially packaged by the merchant. Therefore, you need to provide the following information about your app:

  • App package name
  • App signing certificate fingerprint
Tip

If your app has already been uploaded to Google Play, you can directly go to Test and release -> App integrity -> Play app signing -> Settings to view the certificate fingerprint.

The app package name can be found directly in your app project. The signing certificate fingerprint can be obtained using Android App: LibChecker or by running the following commands:

# Export the certificate first
keytool -exportcert -alias {YourKeyAlias} -keystore {PathToYour}.keystore -rfc > cert.pem
# Calculate the md5 with openssl
openssl x509 -in cert.pem -noout -fingerprint -md5

OpenSSL will output a hash value like the following:

md5 Fingerprint=7A:3F:9C:10:B4:6D:2E:XX:XX:XX:CC:9A:3D:77:E8:21

Provide your app package name and signing certificate hash to our integration contact, and we will generate PayKKaAppCode for you.

Then add the following metadata to AndroidManifest.xml (replace {YourPayKKaAppCode} below with your actual PayKKaAppCode):

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <application ...>
        ...
        <meta-data android:name="paykka_appcode" android:value="{YourPayKKaAppCode}"/>
    </application>
</manifest>

Step 3: Integrate and Use

After the SDK has been added and configured, you can start using PayKKa's payment SDK in your app. The SDK mainly provides two integration modes: Drop-In (an embedded checkout with a complete payment form popup component that includes multiple payment methods) or Component (a component-based checkout that includes only a single payment method).

For each payment method, some key Java/Kotlin code snippets are listed below. By calling the SDK methods, you can customize callbacks and launch payment forms or popups to guide users through the payment process.

Define Your Merchant Configuration

// ...
open class AppConf : YourMerchantConf() {
    // Optional. If not specified, the display language follows the device system setting.
    override fun getLocale(): String = "en-US"
}
open class YourMerchantConf : PayKKaConfiguration {
    // Required. Fill in your PayKKa merchant ID.
    override fun getGatewayMerchantId(): String = "{YOUR_GATEWAY_MERCHANT_ID}"
    // Required. Fill in your client key.
    override fun getClientKey(): String = "{YOUR_CLIENT_KEY}"
    // Optional. Your WeChat Open Platform App ID.
    override fun getWeChatPayAppId(): String = "{YOUR_WECHAT_PAY_APP_ID}"
    // Optional. Your `Participant ID` in Alipay+ Developer Center.
    override fun getAlipayPlusAcquirerId(): String = "{YOUR_ALIPAY_PARTICIPANT_ID}"
}

Option 1: Integrate in Drop-In Mode

The checkout integrated in Drop-In mode includes multiple payment methods (for example, Google Pay, bank card/credit card, and more), and the integration is relatively simple:

After your backend calls PayKKa's Initialize Drop-In Checkout SessionID API, the app only needs to pass the SessionID to the SDK's payment form component to launch a popup that contains multiple PayKKa payment forms, guide users through payment, and complete the payment collection flow.

Paymentsheet examples
package com.paykka.kotlincheckoutdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.paykka.android.checkout.PayKKa
import com.paykka.android.checkout.PayKKaEnv
import com.paykka.android.checkout.PaymentResult
import com.paykka.android.checkout.PaymentResultCallback
import com.paykka.android.checkout.ui.component.PaymentBottomSheet
import com.paykka.kotlincheckoutdemo.YourMerchantConf.Companion.EXAMPLE_CHECKOUT_SESSION_ID
import com.paykka.kotlincheckoutdemo.ui.theme.KotlinCheckoutDemoTheme
import com.paykka.kotlincheckoutdemo.views.ConfirmOrderScreen

class DropInCheckoutConfirmOrderActivity : ComponentActivity() {
    val TAG = "DropInCheckoutConfirmOrderActivity"

    private var isPaying by mutableStateOf(false)

    // Initialize your merchant configuration
    private val APP_CONF = AppConf()

    private lateinit var paymentSheet: PaymentBottomSheet

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // (1) You must initialize the PayKKa SDK in advance
        PayKKa.init(this, APP_CONF, PayKKaEnv.SANDBOX)

        // (2) Make sure to initialize the `PaymentBottomSheet` instance before Activity onCreate
        paymentSheet = PaymentBottomSheet(this, object : PaymentResultCallback {
            override fun onResult(paymentResult: PaymentResult) {
                // (3) Define your payment callback in onResult; this callback is triggered only after EXPIRED/ SUCCESS
                handlePaymentResult(paymentResult)
            }

            override fun onError(e: Throwable) {
                // (4) You can capture errors in the payment flow here
                // By default, the SDK prints errors in the UI and shows a user-friendly message explaining the cause
                // You can focus on your own error-handling logic in this callback
                Log.e(TAG, "onError: ", e)
                toast(e.message ?: "Unknown error")
                hideButtonLoading()
            }
        })

        setContent {
            KotlinCheckoutDemoTheme {
                Box(modifier = Modifier.fillMaxSize()) {
                    ConfirmOrderScreen {
                        openCheckout()
                    }

                    if (isPaying) {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .background(Color.Black.copy(alpha = 0.25f)),
                            contentAlignment = Alignment.Center
                        ) {
                            CircularProgressIndicator(modifier = Modifier.size(44.dp))
                        }
                    }
                }
            }
        }
    }

    private fun openCheckout() {
        if (isPaying) return

        // Optional. Whether to display your merchant name in the form
        paymentSheet.showMerchantName()
        showButtonLoading()
        // (5) At the appropriate time (for example, when the user taps the "Pay Now" button),
        // call the popup form's init method to initialize it, then call show to display the popup
        paymentSheet
            .init(EXAMPLE_CHECKOUT_SESSION_ID)
            .show { _ ->
                // Callback after the form is fully displayed
                hideButtonLoading()
            }
    }

    private fun hideCheckout() {
        hideButtonLoading()
        // Manually close the form popup
        paymentSheet.hide()
    }

    private fun showButtonLoading() {
        isPaying = true
    }

    private fun hideButtonLoading() {
        isPaying = false
    }

    private fun handlePaymentResult(paymentResult: PaymentResult) {
        // Close BottomSheetDialog before leaving the page to avoid WindowLeaked
        if (this::paymentSheet.isInitialized && paymentSheet.isShowing) {
            paymentSheet.dismiss()
        }

        val intent = Intent(this, PaymentCompleteActivity::class.java)
        intent.putExtra("paymentResult", paymentResult.toString())
        startActivity(intent)
        finish()
    }

    override fun onDestroy() {
        // Close BottomSheetDialog before leaving the page to avoid WindowLeaked
        if (this::paymentSheet.isInitialized && paymentSheet.isShowing) {
            paymentSheet.dismiss()
        }
        super.onDestroy()
    }

    private fun toast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}

Option 2: Integrate in Component Mode

In Component mode, PayKKa currently provides multiple options such as the card payment component, Google Pay, and WeChat Pay component. You can embed each payment method component independently anywhere in your app, with a high degree of customization:

After your backend calls PayKKa's Initialize Component Checkout SessionID API, the app passes the SessionID to the SDK's single payment component to complete the payment collection flow.

Card Payment

Card payment form examples
  1. Declare the card payment form layout
package com.paykka.kotlincheckoutdemo.views

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.paykka.android.checkout.ui.component.card.CardPaymentForm


@Composable
fun BankCardPayConfirmOrderScreen(
    isPayButtonEnabled: Boolean,
    onPayClick: () -> Unit,
    onFormCreated: (CardPaymentForm) -> Unit
) {
    Scaffold(
        bottomBar = {
            Surface(shadowElevation = 8.dp) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 12.dp, vertical = 10.dp),
                    contentAlignment = Alignment.Center
                ) {
                    Button(
                        onClick = onPayClick,
                        enabled = isPayButtonEnabled,
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text(text = "Pay Now", fontWeight = FontWeight.Bold)
                    }
                }
            }
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding)
                .verticalScroll(rememberScrollState())
                .fillMaxSize()
                .padding(vertical = 12.dp)
        ) {
            OrderCardView()

            AndroidView(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 12.dp)
                    .padding(horizontal = 12.dp),
                factory = { context ->
                    // Import and declare the card payment form layout
                    CardPaymentForm(context).also { onFormCreated(it) }
                }
            )
        }
    }
}
  1. Initialize and integrate BankCardPaymentSDK
package com.paykka.kotlincheckoutdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.paykka.android.checkout.PayKKa
import com.paykka.android.checkout.PayKKaEnv
import com.paykka.android.checkout.PaymentResult
import com.paykka.android.checkout.PaymentResultCallback
import com.paykka.android.checkout.api.CheckoutApi.CCSInfoResponse
import com.paykka.android.checkout.model.dto.enums.CheckoutStatus
import com.paykka.android.checkout.payments.card.BankCardPayment
import com.paykka.android.checkout.payments.card.BankCardPayment.CardPaymentFormInitCallback
import com.paykka.android.checkout.ui.component.card.CardPaymentForm
import com.paykka.kotlincheckoutdemo.ui.theme.KotlinCheckoutDemoTheme
import com.paykka.kotlincheckoutdemo.views.BankCardPayConfirmOrderScreen

class BankCardPayConfirmOrderActivity : ComponentActivity() {
    val TAG = "BankCardPayConfirmOrderActivity"

    private var isPaying by mutableStateOf(false)
    private var isPayEnabled by mutableStateOf(false)
    // Initialize your merchant configuration
    private val APP_CONF = AppConf()

    private lateinit var cardPaymentForm: CardPaymentForm
    private lateinit var bankCardPayment: BankCardPayment

    private val payCallback = object : PaymentResultCallback {
        override fun onResult(paymentResult: PaymentResult) {
            // Callback after payment is completed; triggered only after EXPIRED/ SUCCESS
            handlePaymentResult(paymentResult)
        }

        override fun onError(e: Throwable) {
            // You can capture errors in the payment flow here
            // By default, the SDK prints errors in the UI and shows a user-friendly message explaining the cause
            // You can focus on your own error-handling logic in this callback
            Log.e(TAG, "onError: ", e)
            toast(e.message ?: "Unknown error")
            hideLoadingOverlay()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // (1) You must initialize the PayKKa SDK in advance
        PayKKa.init(this, APP_CONF, PayKKaEnv.SANDBOX)

        setContent {
            KotlinCheckoutDemoTheme {
                Box(modifier = Modifier.fillMaxSize()) {
                    BankCardPayConfirmOrderScreen(
                        isPayButtonEnabled = isPayEnabled && !isPaying,
                        onPayClick = { onClickBtnPay() },
                        onFormCreated = { form ->
                            cardPaymentForm = form
                            initCardPaymentForm(form)
                            initBankCardPayment(form)
                        }
                    )

                    if (isPaying) {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .background(Color.Black.copy(alpha = 0.25f)),
                            contentAlignment = Alignment.Center
                        ) {
                            CircularProgressIndicator(modifier = Modifier.size(44.dp))
                        }
                    }
                }
            }
        }
    }

    private fun initCardPaymentForm(form: CardPaymentForm) {
        // (2) Initialize the card component and set form validation callbacks
        form.init(this).setInputValidationListener(object : CardPaymentForm.InputValidationListener {
            override fun onAllFieldsValid() {
                // Triggered when all form fields are filled in correctly
                isPayEnabled = true
                Log.i(TAG, "All form fields are valid")
            }

            override fun onFieldInputInvalid(throwable: Throwable) {
                // Triggered when any form field is filled in incorrectly
                isPayEnabled = false
                Log.e(TAG, "Some form field(s) invalid")
            }
        })
        form.isEnabled = false
    }

    private fun initBankCardPayment(form: CardPaymentForm) {
        // (3) Initialize the card component PaymentSDK
        // At this stage, the form structure and form field interactions are initialized based on the checkout configuration
        bankCardPayment = BankCardPayment(this).init(
            form,
            YourMerchantConf.EXAMPLE_CHECKOUT_SESSION_ID,
            object : CardPaymentFormInitCallback {
                override fun onComplete(ccsInfoResponse: CCSInfoResponse?) {
                    // Triggered when initialization is complete
                    if (ccsInfoResponse?.data?.status == CheckoutStatus.EXPIRED) {
                        toast("Expired")
                        return
                    }
                    form.isEnabled = true
                }

                override fun onFailed(t: Throwable) {
                    // Triggered when initialization fails
                    toast(t.message!!)
                }
            }
        )
    }

    private fun onClickBtnPay() {
        if (isPaying) return
        if (!this::bankCardPayment.isInitialized || !this::cardPaymentForm.isInitialized) return

        showLoadingOverlay()
        cardPaymentForm.hideKeyboard()
        cardPaymentForm.clearAllInputFocus()
        // (4) Initiate payment
        bankCardPayment.requestPayment(payCallback)
    }

    private fun showLoadingOverlay() {
        isPaying = true
    }

    private fun hideLoadingOverlay() {
        isPaying = false
    }

    private fun handlePaymentResult(paymentResult: PaymentResult) {
        val intent = Intent(this, PaymentCompleteActivity::class.java)
        intent.putExtra("paymentResult", paymentResult.toString())
        startActivity(intent)
        finish()
    }

    private fun toast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}

Google Pay

Google payment form examples
  1. Declare the Google Pay form layout
package com.paykka.kotlincheckoutdemo.views

import android.view.ViewGroup
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.paykka.android.checkout.ui.component.googlepay.GooglePaymentForm


@Composable
fun GooglePayConfirmOrderScreen(
    googlePaymentForm: GooglePaymentForm
) {
    Scaffold(
        bottomBar = {
            Surface(shadowElevation = 8.dp) {
                AndroidView(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(12.dp),
                    // Import and declare the Google Pay form layout
                    factory = { googlePaymentForm },
                    update = { view ->
                        val parent = view.parent
                        if (parent is ViewGroup) {
                            parent.removeView(view)
                        }
                    }
                )
            }
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding)
                .verticalScroll(rememberScrollState())
                .fillMaxSize()
        ) {
            OrderCardView()
        }
    }
}
  1. Initialize and integrate GooglePaymentSDK
package com.paykka.kotlincheckoutdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.paykka.android.checkout.PayKKa
import com.paykka.android.checkout.PayKKaConfiguration
import com.paykka.android.checkout.PayKKaEnv
import com.paykka.android.checkout.PaymentResult
import com.paykka.android.checkout.PaymentResultCallback
import com.paykka.android.checkout.payments.PaymentBase
import com.paykka.android.checkout.payments.googlepay.GooglePayment
import com.paykka.android.checkout.ui.component.googlepay.GooglePaymentForm
import com.paykka.android.utils.gson.JsonObject
import com.paykka.kotlincheckoutdemo.YourMerchantConf.Companion.EXAMPLE_CHECKOUT_SESSION_ID
import com.paykka.kotlincheckoutdemo.ui.theme.KotlinCheckoutDemoTheme
import com.paykka.kotlincheckoutdemo.views.GooglePayConfirmOrderScreen

class GooglePayConfirmOrderActivity : ComponentActivity() {
    val TAG = "GooglePayConfirmOrderActivity"

    private lateinit var googlePayment: GooglePayment
    private lateinit var googlePaymentForm: GooglePaymentForm
    // Initialize your merchant configuration
    private val APP_CONF = AppConf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // (1) You must initialize the PayKKa SDK in advance
        PayKKa.init(this, APP_CONF, PayKKaEnv.SANDBOX)

        // (2) Initialize Google Pay
        googlePaymentForm = GooglePaymentForm(this)
        // Payment must be initialized before Lifecycle enters STARTED
        googlePayment = GooglePayment(this, googlePaymentForm, this::requestPayment)

        setContent {
            KotlinCheckoutDemoTheme {
                GooglePayConfirmOrderScreen(googlePaymentForm = googlePaymentForm)
            }
        }
    }

    private fun requestPayment(@Suppress("UNUSED_PARAMETER") view: View) {
        googlePaymentForm.showLoadingOverlay()
        // (3) Initiate payment and launch the Google Pay popup
        googlePayment.requestPayment(EXAMPLE_CHECKOUT_SESSION_ID, object : PaymentResultCallback {
            override fun onResult(paymentResult: PaymentResult) {
                // Callback after payment is completed; triggered only after EXPIRED/ SUCCESS
                googlePaymentForm.hideLoadingOverlay()
                handlePaymentResult(paymentResult)
            }

            override fun onError(e: Throwable) {
                // You can capture errors in the payment flow here
                // By default, the SDK prints errors in the UI and shows a user-friendly message explaining the cause
                // You can focus on your own error-handling logic in this callback
                googlePaymentForm.hideLoadingOverlay()
                Log.e(TAG, "onError: ", e)
                toast(e.message ?: "Unknown error")
            }

            override fun onUserCanceled(paymentSource: PaymentBase?, extraData: JsonObject?) {
                // The user canceled the payment and closed the Google Pay popup
                googlePaymentForm.hideLoadingOverlay()
                toast("Google Payment: CANCELED")
            }
        })
    }

    private fun handlePaymentResult(paymentResult: PaymentResult) {
        val intent = Intent(this, PaymentCompleteActivity::class.java)
        intent.putExtra("paymentResult", paymentResult.toString())
        startActivity(intent)
        finish()
    }

    private fun toast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}

WeChat Pay

WeChat payment form examples
  1. Declare the WeChat Pay form layout (used to display user-friendly messages)
package com.paykka.kotlincheckoutdemo.views

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.paykka.android.checkout.ui.component.wechatpay.WeChatPaymentForm


@Composable
fun WeChatPayConfirmOrderScreen(
    isPayButtonEnabled: Boolean,
    onPayClick: () -> Unit,
    onFormCreated: (WeChatPaymentForm) -> Unit
) {
    Scaffold(
        bottomBar = {
            Surface(shadowElevation = 8.dp) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 10.dp)
                ) {
                    AndroidView(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 12.dp),
                        factory = { context ->
                            WeChatPaymentForm(context).also { onFormCreated(it) }
                        }
                    )

                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 12.dp, vertical = 10.dp),
                        contentAlignment = Alignment.Center
                    ) {
                        Button(
                            onClick = onPayClick,
                            enabled = isPayButtonEnabled,
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            Text(text = "WeChat Pay ¥1118.83", fontWeight = FontWeight.Bold)
                        }
                    }
                }
            }
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding)
                .verticalScroll(rememberScrollState())
                .fillMaxSize()
                .padding(vertical = 12.dp)
        ) {
            OrderCardView()
        }
    }
}
  1. Initialize and integrate WeChatPaymentSDK
package com.paykka.kotlincheckoutdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.paykka.android.checkout.PayKKa
import com.paykka.android.checkout.PaymentResult
import com.paykka.android.checkout.PaymentResultCallback
import com.paykka.android.checkout.payments.PaymentBase
import com.paykka.android.checkout.payments.wechatpay.WeChatPayment
import com.paykka.android.checkout.ui.component.wechatpay.WeChatPaymentForm
import com.paykka.android.utils.gson.JsonObject
import com.paykka.kotlincheckoutdemo.YourMerchantConf.Companion.EXAMPLE_CHECKOUT_SESSION_ID
import com.paykka.kotlincheckoutdemo.ui.theme.KotlinCheckoutDemoTheme
import com.paykka.kotlincheckoutdemo.views.WeChatPayConfirmOrderScreen

class WeChatPayConfirmOrderActivity : ComponentActivity() {
    val TAG = "WeChatPayConfirmOrderActivity"

    private var isPaying by mutableStateOf(false)
    private var isPayEnabled by mutableStateOf(false)

    private lateinit var weChatPaymentForm: WeChatPaymentForm
    private lateinit var weChatPayment: WeChatPayment

    // Initialize your merchant configuration
    private val APP_CONF = AppConf()

    private val payCallback = object : PaymentResultCallback {
        override fun onResult(paymentResult: PaymentResult) {
            // Callback after payment is completed; triggered only after EXPIRED/ SUCCESS
            hideLoadingOverlay()
            handlePaymentResult(paymentResult)
        }

        override fun onError(e: Throwable) {
            // You can capture errors in the payment flow here
            // By default, the SDK prints errors in the UI and shows a user-friendly message explaining the cause
            // You can focus on your own error-handling logic in this callback
            Log.e(TAG, "onError: ", e)
            hideLoadingOverlay()
            toast(e.message ?: "Unknown error")
        }

        override fun onUserCanceled(paymentSource: PaymentBase?, extraData: JsonObject?) {
            // The user canceled the payment
            hideLoadingOverlay()
            toast("WeChat Payment: CANCELED")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // (1) You must initialize the PayKKa SDK in advance
        PayKKa.init(this, APP_CONF, AppConf.ENVIRONMENT)

        setContent {
            KotlinCheckoutDemoTheme {
                Box(modifier = Modifier.fillMaxSize()) {
                    WeChatPayConfirmOrderScreen(
                        isPayButtonEnabled = isPayEnabled && !isPaying,
                        onPayClick = { onClickBtnPay() },
                        onFormCreated = { form ->
                            weChatPaymentForm = form
                            initWeChatPayment(form)
                        }
                    )

                    if (isPaying) {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .background(Color.Black.copy(alpha = 0.25f)),
                            contentAlignment = Alignment.Center
                        ) {
                            CircularProgressIndicator()
                        }
                    }
                }
            }
        }
    }

    private fun initWeChatPayment(form: WeChatPaymentForm) {
        if (this::weChatPayment.isInitialized) return

        // (2) Initialize WeChat PaymentSDK
        weChatPayment = WeChatPayment(this).init(form, EXAMPLE_CHECKOUT_SESSION_ID) {
            // Callback when initialization is complete
            isPayEnabled = true
        }
    }

    private fun onClickBtnPay() {
        if (isPaying) return
        if (!this::weChatPayment.isInitialized) return

        showLoadingOverlay()
        // (3) Initiate payment
        weChatPayment.requestPayment(payCallback)
    }

    private fun showLoadingOverlay() {
        isPaying = true
    }

    private fun hideLoadingOverlay() {
        isPaying = false
    }

    private fun handlePaymentResult(paymentResult: PaymentResult) {
        val intent = Intent(this, PaymentCompleteActivity::class.java)
        intent.putExtra("paymentResult", paymentResult.toString())
        startActivity(intent)
        finish()
    }

    private fun toast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}

Alipay+

Alipay+ payment form examples
  1. Declare the Alipay+ payment form layout (used to display user-friendly messages)
package com.paykka.kotlincheckoutdemo.views

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.paykka.android.checkout.ui.component.alipay.AlipayPlusPaymentForm

@Composable
fun AlipayPlusConfirmOrderScreen(
    isPayButtonEnabled: Boolean,
    onPayClick: () -> Unit,
    onFormCreated: (AlipayPlusPaymentForm) -> Unit
) {
    Scaffold(
        bottomBar = {
            Surface(shadowElevation = 8.dp) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 10.dp)
                ) {
                    AndroidView(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 12.dp),
                        factory = { context ->
                            AlipayPlusPaymentForm(context).also { onFormCreated(it) }
                        }
                    )

                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 12.dp, vertical = 10.dp),
                        contentAlignment = Alignment.Center
                    ) {
                        Button(
                            onClick = onPayClick,
                            enabled = isPayButtonEnabled,
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            Text(text = "Alipay+ Payment ¥1118.83", fontWeight = FontWeight.Bold)
                        }
                    }
                }
            }
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding)
                .verticalScroll(rememberScrollState())
                .fillMaxSize()
                .padding(vertical = 12.dp)
        ) {
            OrderCardView()
        }
    }
}
  1. Initialize and integrate AlipayPlusPaymentSDK
package com.paykka.kotlincheckoutdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.paykka.android.checkout.PayKKa
import com.paykka.android.checkout.PaymentResult
import com.paykka.android.checkout.PaymentResultCallback
import com.paykka.android.checkout.payments.PaymentBase
import com.paykka.android.checkout.payments.alipay.AlipayPlusPayment
import com.paykka.android.checkout.ui.component.alipay.AlipayPlusPaymentForm
import com.paykka.android.utils.gson.JsonObject
import com.paykka.kotlincheckoutdemo.YourMerchantConf.Companion.EXAMPLE_CHECKOUT_SESSION_ID
import com.paykka.kotlincheckoutdemo.ui.theme.KotlinCheckoutDemoTheme
import com.paykka.kotlincheckoutdemo.views.AlipayPlusConfirmOrderScreen

class AlipayPlusConfirmOrderActivity : ComponentActivity() {
    val TAG = "AlipayPlusConfirmOrderActivity"

    private var isPaying by mutableStateOf(false)
    private var isPayEnabled by mutableStateOf(false)

    private lateinit var alipayPlusPaymentForm: AlipayPlusPaymentForm
    private lateinit var alipayPlusPayment: AlipayPlusPayment

    // Initialize your merchant configuration
    private val APP_CONF = AppConf()

    private val payCallback = object : PaymentResultCallback {
        override fun onResult(paymentResult: PaymentResult) {
            // Callback after payment is completed; triggered only after EXPIRED/ SUCCESS
            hideLoadingOverlay()
            handlePaymentResult(paymentResult)
        }

        override fun onError(e: Throwable) {
            // You can capture errors in the payment flow here
            // By default, the SDK prints errors in the UI and shows a user-friendly message explaining the cause
            // You can focus on your own error-handling logic in this callback
            Log.e(TAG, "onError: ", e)
            hideLoadingOverlay()
            toast(e.message ?: "Unknown error")
        }

        override fun onUserCanceled(paymentSource: PaymentBase?, extraData: JsonObject?) {
            // The user canceled the payment and closed the Alipay+ payment popup
            hideLoadingOverlay()
            toast("AlipayPlus Payment: CANCELED")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // (1) You must initialize the PayKKa SDK in advance
        PayKKa.init(this, APP_CONF, AppConf.ENVIRONMENT)

        setContent {
            KotlinCheckoutDemoTheme {
                Box(modifier = Modifier.fillMaxSize()) {
                    AlipayPlusConfirmOrderScreen(
                        isPayButtonEnabled = isPayEnabled && !isPaying,
                        onPayClick = { onClickBtnPay() },
                        onFormCreated = { form ->
                            alipayPlusPaymentForm = form
                            initAlipayPlusPayment(form)
                        }
                    )

                    if (isPaying) {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .background(Color.Black.copy(alpha = 0.25f)),
                            contentAlignment = Alignment.Center
                        ) {
                            CircularProgressIndicator()
                        }
                    }
                }
            }
        }
    }

    private fun initAlipayPlusPayment(form: AlipayPlusPaymentForm) {
        if (this::alipayPlusPayment.isInitialized) return

        // (2) Initialize Alipay+ PaymentSDK
        alipayPlusPayment = AlipayPlusPayment(this).init(form, EXAMPLE_CHECKOUT_SESSION_ID) {
            isPayEnabled = true
        }
    }

    private fun onClickBtnPay() {
        if (isPaying) return
        if (!this::alipayPlusPayment.isInitialized) return

        showLoadingOverlay()
        // (3) Initiate payment
        alipayPlusPayment.requestPayment(payCallback)
    }

    private fun showLoadingOverlay() {
        isPaying = true
    }

    private fun hideLoadingOverlay() {
        isPaying = false
    }

    private fun handlePaymentResult(paymentResult: PaymentResult) {
        val intent = Intent(this, PaymentCompleteActivity::class.java)
        intent.putExtra("paymentResult", paymentResult.toString())
        startActivity(intent)
        finish()
    }

    private fun toast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}

Step 4: Test Payments

You can use test cards in the PayKKa sandbox environment (SANDBOX) to test payments, and then switch to the PROD environment when packaging the release version of your app.

// You can specify the payment environment to use in the init method
PayKKa.init(this, APP_CONF, PayKKaEnv.SANDBOX)

/// SANDBOX (default)
PayKKa.useEnv(PayKKaEnv.SANDBOX)
/// Switch to the EU production environment
PayKKa.useEnv(PayKKaEnv.PROD_EU)
/// Switch to the HK production environment
PayKKa.useEnv(PayKKaEnv.PROD_HK)

Notes

Google Pay

1. How do I use test cards for payment testing?

Please refer to: Payment Method - Google Pay.

2. Google Pay cannot accept payments in the PROD environment and shows "OR_BIBED_11"

Google Pay uses the test environment and mock data in the sandbox; however, after going live, it processes real user payment card information. To protect the funds and privacy of both users and merchants, Google requires:

  • A supported payment gateway (for example, PayKKa), or
  • A PCI DSS-compliant (Payment Card Industry Data Security Standard) environment in order to apply for production access. This ensures that you are capable of securely handling sensitive payment information, rather than operating only in development and test mode.

Therefore, you also need to complete the Google Pay - Publish your integration steps.