通过HCE付款

本教程将介绍通过HCE付款的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

问题描述

我一直在开发这款应用程序,我需要在其中实现TAP&Pay。我能够将HCE服务与NFC终端连接。

现在我的问题是,要用它进行实际付款,下一步是什么?

我到处都找过了,但找不到合适的文件。请帮帮我。

下面是我编写的用于将HCE服务连接到NFC终端的代码。

Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.nfcemulator">

 <uses-permission android:name="android.permission.NFC" />

 <uses-feature android:name="android.hardware.nfc.hce"
  android:required="true" />

 <application
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/Theme.NFCEmulator">

  <activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
  </activity>

  <service
android:name=".service.HCEService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE">
<intent-filter>
 <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>

<meta-data
 android:name="android.nfc.cardemulation.host_apdu_service"
 android:resource="@xml/apduservice" />
  </service>

 </application>

</manifest>

HCE服务

class HCEService: HostApduService() {

 companion object {
  val TAG = "Host Card Emulator"
  val STATUS_SUCCESS = "9000"
  val STATUS_FAILED = "6F00"
  val CLA_NOT_SUPPORTED = "6E00"
  val INS_NOT_SUPPORTED = "6D00"
  val AID = "A0000002471001"
  val SELECT_INS = "A4"
  val DEFAULT_CLA = "00"
  val MIN_APDU_LENGTH = 12
 }

 override fun onDeactivated(reason: Int) {
  Log.d(TAG, "Deactivated: " + reason)
 }

 override fun processCommandApdu(commandApdu: ByteArray?, extras: Bundle?): ByteArray {

  if (commandApdu == null) {
return Utils.hexStringToByteArray(STATUS_FAILED)
  }

  val hexCommandApdu = Utils.toHex(commandApdu)
  if (hexCommandApdu.length < MIN_APDU_LENGTH) {
return Utils.hexStringToByteArray(STATUS_FAILED)
  }

  if (hexCommandApdu.substring(0, 2) != DEFAULT_CLA) {
return Utils.hexStringToByteArray(CLA_NOT_SUPPORTED)
  }

  if (hexCommandApdu.substring(2, 4) != SELECT_INS) {
return Utils.hexStringToByteArray(INS_NOT_SUPPORTED)
  }

  if (hexCommandApdu.substring(10, 24) == AID)  {
return Utils.hexStringToByteArray(STATUS_SUCCESS)
  } else {
return Utils.hexStringToByteArray(STATUS_FAILED)
  }

 }

}

apduservices.xml

<?xml version="1.0" encoding="utf-8"?>
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
 android:description="@string/hce_service"
 android:requireDeviceUnlock="false">
 <aid-group android:description="@string/aid_groups"
  android:category="other">
  <aid-filter android:name="325041592E5359532E4444463031"/>
 </aid-group>
</host-apdu-service>

推荐答案

我认为您使用此功能的级别较低,这是不必要的,因为此功能是在使用Google Pay时提供的(它支持NFC支付和在线购物)。

All you need to know about Google Pay if you’re a developer

颤动支付插件是为您的Android应用程序添加支付功能的最快方法。

Google Pay introduces a Flutter plugin for payments

Pay plugin 1.0.6

好了关于通过HCE付款的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。

0
没有账号?注册  忘记密码?