Android integrated Alipay payment

1. Download demo: Alipay payment demo download

2. Find the "alipaySdk-xxxxxxxxxxxxxxx.aar" file in the unzipped folder and place it in the app/libs folder of the Android project

3. Insert under allprojects-repositories{} in the build.gradle file of the project

flatDir {
            dirs 'libs'
        }

4. Introduce the aar file just added in the build.gradle file in the project app directory


    // 支付宝 SDK AAR 包所需的配置(注意替换name为你的实际aar文件名)
    implementation(name: 'alipaySdk-15.6.8-20191021122514', ext: 'aar')

5. Generate an order, return the pending order information in the background, and call the payment processing

Thread(Runnable {
                                            val payTask = PayTask(this@ConfirmOrderActivity)
                                            //orderInfo为提交订单后后台返回来的订单信息
                                            val result = payTask.payV2(orderInfo, true)
                                            val msg = Message()
                                            //Constact.SDK_PAY_FLAG为处理支付结果时候判断的key
                                            msg.what = Constact.SDK_PAY_FLAG
                                            msg.obj = result
                                            mHandler.sendMessage(msg)
                                        }).start()

6. Payment result processing

private val mHandler = @SuppressLint("HandlerLeak")
    object : Handler() {
        override fun handleMessage(msg: Message) {

            if (msg.what == Constact.SDK_PAY_FLAG) {

                val payResult = msg.obj as HashMap<String, String>
                when (payResult["resultStatus"]) {
                    "9000" -> ToastUtil.showShortToast(this@ConfirmOrderActivity, "支付成功")
                    "6001" -> ToastUtil.showShortToast(this@ConfirmOrderActivity, "您取消了支付")
                    else -> ToastUtil.showLongToast(this@ConfirmOrderActivity, "支付失败")
                }
                finish()
            }
        }
    }

 

Guess you like

Origin blog.csdn.net/nsacer/article/details/103855029