Register-SDK for Android
Register SDK Andorid为POS应用提供了通过WLAN快速连接到CodePay Register的功能,并为POS应用程序提供了支付、退款、查询等接口。
开始使用我们的 示例项目。
特征
- 快速连接: 注册SDK使用mDNS,启动SDK,在CodePay注册APP中可以快速找到所有POS应用设备,点击配对,实现自动连接。
- 支付: 该SDK提供了包括扫码银行卡在内的多种支付方式,还提供了消费、撤销、退款、预授权、预授权完成等多种媒介支付功能。
- 客户端管理: 在LAN模式下,WiseCashier充当服务器,SDK能够连接到多个服务器,并且SDK提供了管理服务器的方法。
安装
必要条件
- Android Studio Giraffe | 2022.3.1 Patch 3
- kotlin showLineNumbers1.3.41
- gradle4.2.2
- Java11
- Android 8.0 (API level 26) and above
配置
- 将“codepay-register-sdk-android”添加到您的build.gradle依赖项中。
dependencies {
implementation 'com.github.codepay-us:codepay-register-sdk-android:1.0.0'
}
- 您可以使用GitHub中的ecr_sdk模块作为应用程序的库模块。
函数列表
1 设备发现和配对
备注
只有WLAN连接模式需要先配对,USB连接模式不需要配对。
1.1 启动/停止设备发现服务
只有在设备发现服务启动时,终端才能发现您的POS应用程序
- 在完成配对操作后,终端和POS应用程序将记录彼此的网络信息
- 当需要配对和连接到网络时,需要启用设备发现服务
import com.codepay.register.sdk.device.ECRHubWebSocketDiscoveryService
private var mService: ECRHubWebSocketDiscoveryService? = null
mService = ECRHubWebSocketDiscoveryService(this)
//start device discover service
mService?.start(object :ECRHubPairListener {
override fun onDevicePair(data: ECRHubMessageData?, ip: String?) {
//When a pairing message is received from CodePayRegister, call back this method
}
})
//stop device discover service
service.stop();
1.2 获取配对终端的列表
- 使用POS应用程序推送订单时,您可以从配对设备列表中选择一个设备来推送订单;
- 当POS应用程序需要显示配对的POS终端时,此功能可用于获得配对设备的列表以供显示。
import com.codepay.register.sdk.device.ECRHubWebSocketDiscoveryService
private var mService: ECRHubWebSocketDiscoveryService? = null
private var mPairedList = mutableListOf<ECRHubDevice>()
mService = ECRHubWebSocketDiscoveryService(this)
mPairedList = mService!!.pairedDeviceList
for(device in mPairedList) {
Log.e(TAG,device.terminal_sn)
Log.e(TAG,device.ws_address)
}
1.3 移除配对的终端
当POS终端不再使用时,可以手动将其从POS应用程序的配对列表中删除。
import com.codepay.register.sdk.device.ECRHubWebSocketDiscoveryService
private var mService: ECRHubWebSocketDiscoveryService? = null
private var mPairedList = mutableListOf<ECRHubDevice>()
mService?.unPair(mPairedList[0],object :ECRHubResponseCallBack{
override fun onError(errorCode: String?, errorMsg: String?) {
//upPair error
}
override fun onSuccess(data: PaymentResponseParams?) {
// unPair Success
}
})
2 连接
选择一个配对的终端来启动网络连接,一旦建立了连接,就可以发送交易指令。
2.1 调用流程
2.2 创建客户端实例
WLAN连接模式 当POS应用程序使用WLAN连接到POS终端时,请使用以下方法创建客户端
import com.codepay.register.sdk.client.ECRHubClient
import com.codepay.register.sdk.client.ECRHubConfig
val config = ECRHubConfig()
mClient = ECRHubClient.getInstance()
mClient.init(config, this)
2.3 连接
在POS应用程序和POS终端之间建立连接。
/// Connecting to the POS Terminal
mClient .connect("ws://xxxxxx")
2.4 断开连接
断开POS应用程序与POS终端的连接。
// This will try disconnect from POS Terminal
mClient .disconnect();
3 交易
3.1 消费
- 请求/响应参数
- 示例:
import com.codepay.register.sdk.client.payment.PaymentRequestParams
import com.codepay.register.sdk.client.payment.PaymentResponseParams
import com.codepay.register.sdk.listener.ECRHubResponseCallBack
// Build sale request
val params = PaymentRequestParams()
params.app_id = "your payment app_id"
params.merchant_order_no = "12345644"
params.order_amount = "1.1"
params.pay_scenario = "SWIPE_CARD"
params.confirm_on_terminal = false
mClient.payment.sale(params, object :
ECRHubResponseCallBack {
override fun onError(errorCode: String?, errorMsg: String?) {
//sale fail
}
override fun onSuccess(data: PaymentResponseParams?) {
//sale success
}
})
3.2 返现
- 请求/响应参数
- 示例:
import com.codepay.register.sdk.client.payment.PaymentRequestParams
import com.codepay.register.sdk.client.payment.PaymentResponseParams
import com.codepay.register.sdk.listener.ECRHubResponseCallBack
// Build cash back request
val params = PaymentRequestParams()
params.app_id = "your payment app_id"
params.merchant_order_no = "12345644"
params.order_amount = "1.1"
params.cashback_amount = "1.1"
params.pay_scenario = "SWIPE_CARD"
params.confirm_on_terminal = false
mClient.payment.sale(params, object :
ECRHubResponseCallBack {
override fun onError(errorCode: String?, errorMsg: String?) {
//sale fail
}
override fun onSuccess(data: PaymentResponseParams?) {
//sale success
}
})
3.3 预授权
- 请求/响应参数
- 示例:
import com.codepay.register.sdk.client.payment.PaymentRequestParams
import com.codepay.register.sdk.client.payment.PaymentResponseParams
import com.codepay.register.sdk.listener.ECRHubResponseCallBack
// Build auth request
val params = PaymentRequestParams()
params.app_id = "your payment app_id"
params.order_amount = "1.1"
params.merchant_order_no = "12345644"
params.pay_scenario = "SWIPE_CARD"
params.confirm_on_terminal = false
mClient.payment.auth(params, object :
ECRHubResponseCallBack {
override fun onError(errorCode: String?, errorMsg: String?) {
//auth fail
}
override fun onSuccess(data: PaymentResponseParams?) {
//auth success
}
})
3.4 预授权完成
- 请求/响应参数
- 示例:
import com.codepay.register.sdk.client.payment.PaymentRequestParams
import com.codepay.register.sdk.client.payment.PaymentResponseParams
import com.codepay.register.sdk.listener.ECRHubResponseCallBack
// Build completion request
val params = PaymentRequestParams()
params.app_id = "your payment app_id"
params.order_amount = "1.1"
params.merchant_order_no = "12345644"
params.orig_merchant_order_no = "123322222"
params.confirm_on_terminal = false
mClient.payment.completion(params, object :
ECRHubResponseCallBack {
override fun onError(errorCode: String?, errorMsg: String?) {
//completion fail
}
override fun onSuccess(data: PaymentResponseParams?) {
//completion success
}
})
3.5 撤销
- 请求/响应参数
- 示例:
import com.codepay.register.sdk.client.payment.PaymentRequestParams
import com.codepay.register.sdk.client.payment.PaymentResponseParams
import com.codepay.register.sdk.listener.ECRHubResponseCallBack
// Build void request
val params = PaymentRequestParams()
params.app_id = "your payment app_id"
params.orig_merchant_order_no = "123322222"
params.merchant_order_no = "12345644"
params.confirm_on_terminal = false
mClient.payment.cancel(params, object :
ECRHubResponseCallBack {
override fun onError(errorCode: String?, errorMsg: String?) {
//void fail
}
override fun onSuccess(data: PaymentResponseParams?) {
//void success
}
})
3.6 退款
- 请求/响应参数
- 示例:
import com.codepay.register.sdk.client.payment.PaymentRequestParams
import com.codepay.register.sdk.client.payment.PaymentResponseParams
import com.codepay.register.sdk.listener.ECRHubResponseCallBack
// Build refund request
val params = PaymentRequestParams()
params.app_id = "your payment app_id"
params.orig_merchant_order_no = "123322222"
params.order_amount = "1.1"
params.merchant_order_no = "12345644"
params.confirm_on_terminal = false
mClient.payment.refund(params, object :
ECRHubResponseCallBack {
override fun onError(errorCode: String?, errorMsg: String?) {
//refund fail
}
override fun onSuccess(data: PaymentResponseParams?) {
//refund success
}
})
3.7 查询
- 请求/响应参数
- 示例:
import com.codepay.register.sdk.client.payment.PaymentRequestParams
import com.codepay.register.sdk.client.payment.PaymentResponseParams
import com.codepay.register.sdk.listener.ECRHubResponseCallBack
// Build query request
val params = PaymentRequestParams()
params.app_id = "your payment app_id"
params.merchant_order_no = "12345644"
mClient.payment.query(params, object :
ECRHubResponseCallBack {
override fun onError(errorCode: String?, errorMsg: String?) {
//query fail
}
override fun onSuccess(data: PaymentResponseParams?) {
//query success
}
})
3.8 关闭
- 请求/响应参数
- 示例:
// Build close request
val params = PaymentRequestParams()
params.app_id = "your payment app_id"
params.merchant_order_no = "12345644"
mClient.payment.close(params, object :
ECRHubResponseCallBack {
override fun onError(errorCode: String?, errorMsg: String?) {
//close fail
}
override fun onSuccess(data: PaymentResponseParams?) {
//close success
}
})