You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin实现UPI收款二维码及扫码支付功能求助

Android 内生成UPI收款二维码实现方案

核心原理

UPI收款二维码本质是包含UPI支付参数的URI字符串,生成二维码后,用户扫码即可唤起UPI应用(如Google Pay、PhonePe等)完成支付,资金直接转入你指定的银行账户对应的UPI ID。

实现步骤与代码

1. 添加依赖

在build.gradle(Module level)中添加二维码生成依赖:

dependencies {
    implementation 'com.google.zxing:core:3.5.2'
    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
}

2. 构建UPI支付URI

按照UPI规范拼接核心支付参数,参数需做URL编码避免解析异常:

import java.net.URLEncoder

fun buildUpiPaymentUri(upiId: String, payeeName: String, amount: String, note: String): String {
    val encodedUpiId = URLEncoder.encode(upiId, "UTF-8")
    val encodedName = URLEncoder.encode(payeeName, "UTF-8")
    val encodedAmount = URLEncoder.encode(amount, "UTF-8")
    val encodedNote = URLEncoder.encode(note, "UTF-8")
    
    return "upi://pay?pa=$encodedUpiId&pn=$encodedName&am=$encodedAmount&cu=INR&tn=$encodedNote"
}

核心参数说明:

  • pa:你的UPI ID(需绑定目标银行账户,格式如yourname@bank)
  • pn:收款人/商户名称
  • am:收款金额(需保留两位小数,如100.00)
  • cu:货币代码(默认填INR)
  • tn:交易备注

3. 生成二维码Bitmap

将UPI URI转换为可显示的二维码图片:

import android.graphics.Bitmap
import com.google.zxing.BarcodeFormat
import com.google.zxing.MultiFormatWriter
import com.google.zxing.WriterException
import com.google.zxing.common.BitMatrix
import com.journeyapps.barcodescanner.BarcodeEncoder

fun generateQrCode(upiUri: String, width: Int, height: Int): Bitmap? {
    return try {
        val bitMatrix: BitMatrix = MultiFormatWriter()
            .encode(upiUri, BarcodeFormat.QR_CODE, width, height)
        BarcodeEncoder().createBitmap(bitMatrix)
    } catch (e: WriterException) {
        e.printStackTrace()
        null
    }
}

4. 在Activity中显示二维码

调用上述方法生成并展示二维码:

import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

class UpiQrActivity : AppCompatActivity() {
    private lateinit var qrImageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_upi_qr)
        qrImageView = findViewById(R.id.iv_qr_code)

        // 替换为你的真实参数
        val upiId = "your_valid_upi_id@your_bank"
        val payeeName = "Your Full Name"
        val amount = "50.00"
        val note = "Payment for Service"

        val upiUri = buildUpiPaymentUri(upiId, payeeName, amount, note)
        val qrBitmap = generateQrCode(upiUri, 500, 500)
        
        qrBitmap?.let { qrImageView.setImageBitmap(it) }
    }
}

5. 配套布局文件(activity_upi_qr.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16dp">

    <ImageView
        android:id="@+id/iv_qr_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="UPI收款二维码"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="扫码完成支付"
        android:textSize="18sp"/>

</LinearLayout>

关键注意事项

  • 确保你的UPI ID已完成实名认证并绑定目标银行账户,否则会导致支付失败。
  • 测试时优先使用小额金额验证,确认资金准确转入指定账户。
  • 若需动态修改金额,可通过输入框让用户输入后再生成二维码。

内容的提问来源于stack exchange,提问作者Vishal Ugalmugale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 10:30:18