Switcloud Client
Switcloud Client (switcloud-clt-kt) is the library a payment application integrates to process payments with Switcloud. It sits between the Switcloud APIs and Switcloud L2:
- it fetches the payment and its POI configuration from Switcloud, and configures the EMV L2 stack with them;
- it drives the EMV L2 stack to process the card;
- it sends the payment data back to Switcloud.
Note
To add Switcloud Client to your application, follow the instructions in deliverables.
Switcloud Client can be used in two ways:
- basic usage: the application calls a handful of functions (
initialize,configure,initiate,complete,cleanup) and Switcloud Client does the rest; - advanced usage: the application drives the EMV L2 stack itself, and Switcloud Client takes care of Switcloud: configuration before card processing, payment data after.
Both rely on the same events to interact with the cardholder: messages, tones and PIN entry.
1. Setup
Switcloud Client uses Koin for dependency injection. At application startup, declare the Switcloud L2 instance, the Switcloud Client module and a factory for the client:
import io.switstack.switcloud.switcloudclt.di.switcloudClientModule
import io.switstack.switcloud.switcloudclt.domain.SwitcloudClt
import io.switstack.switcloud.switcloudclt.internal.SwitcloudCltImpl
import io.switstack.switcloud.switcloudl2.ISwitcloudL2
import io.switstack.switcloud.switcloudl2.SwitcloudL2
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
modules(module {
single<ISwitcloudL2> { SwitcloudL2.getInstance() }
includes(switcloudClientModule)
factory<SwitcloudClt> { (serverUrl: String) -> SwitcloudCltImpl(serverUrl) }
})
}
}
}
Then, in the activity, get a client for your Switcloud server and give Switcloud L2 access to the NFC adapter:
val switcloudClt: SwitcloudClt by inject { parametersOf("https://switcloud.switstack.io") }
SwitcloudClt.setActivity(activity)
Before processing payments, authenticate against Switcloud, as a machine or as a user:
2. Basic usage
In basic usage, a payment takes a few calls. The payment itself is created beforehand, by your backend, with the Switcloud API; the terminal only needs its ID.
try {
// reader parameters: see Switcloud L2
switcloudClt.initialize(readerParams, pinInput)
// fetches the payment and its POI configuration, configures the EMV L2 stack
val sessionData = switcloudClt.configure(paymentId)
// processes the card, sends the payment data to Switcloud
val initiateResponse = switcloudClt.initiate(sessionData)
val response = if (initiateResponse.outcomeParameterSet?.status == Status.ONLINE_REQUEST) {
// authorizationResponse: the issuer's response (TLV, hexadecimal),
// or an empty string when there is none
switcloudClt.complete(sessionData, authorizationResponse, initiateResponse)
} else {
initiateResponse
}
// response.outcomeParameterSet: final status, CVM, receipt
// response.userInterfaceRequestData: final message
} finally {
switcloudClt.cleanup()
}
| Function | Role |
|---|---|
initialize |
Initializes Switcloud L2 and configures the reader. Call it before each payment. |
configure |
Fetches the payment and its POI configuration from Switcloud, and configures the EMV L2 stack: entry point, combinations, CAPKs, CRLs. |
initiate |
Processes the card and sends the payment data to Switcloud. |
complete |
Completes an online transaction with the issuer's response (second presentment, if needed) and sends the completion data to Switcloud. |
cleanup |
Releases the transaction's resources. Call it after each payment. |
initiate handles the whole card processing on its own:
- it waits for the card, and for it again whenever an outcome asks to start again: try again, or the cardholder performing an on-device CVM on their phone;
- it handles data exchange (DEK/DET) and the online request;
- it asks for the PIN when the kernel requires an online PIN (see PIN entry);
- it raises a bip event when card processing ends.
It returns only the final outcome. Everything the cardholder must be told in between reaches the application through the userInfo flow.
3. Advanced usage
In advanced usage, the application drives the EMV L2 stack itself through the GLA service, for example to add its own steps between card processing steps. Switcloud Client still configures the EMV L2 stack and sends the payment data to Switcloud.
initiate is replaced with preInitiate, the application's own card processing, then postInitiate. Likewise, complete is replaced with preComplete, the application's own processing, then postComplete.
switcloudClt.initialize(readerParams, pinInput)
val sessionData = switcloudClt.configure(paymentId)
switcloudClt.preInitiate(sessionData)
// card processing, with switcloudClt.gla (see Switcloud L2):
// preProcessing, protocolActivation, combinationSelection, kernelActivation...
// signals: the final outcome's signals
// pinBlock: switcloudClt.requestPinEntry() when the kernel requires an online PIN
val paymentData = Codec.makePaymentData(
signals,
switcloudClt.fetchIntermediateSignals(),
pinBlock,
) ?: error("failed to prepare payment data")
// sends the payment data to Switcloud
val initiateResponse = switcloudClt.postInitiate(sessionData, paymentData)
| Member | Role |
|---|---|
gla, vcard |
Switcloud L2's GLA and virtual card services. |
preInitiate, postInitiate |
Before and after card processing. postInitiate sends the payment data to Switcloud. |
preComplete, postComplete |
Before and after completion. postComplete sends the completion data to Switcloud. |
fetchIntermediateSignals |
Signals of the intermediate outcomes, to include in the payment data. |
requestPinEntry |
Asks the cardholder for the PIN (see PIN entry) and returns the PIN block. |
Codec.makePaymentData |
Builds the payment data from the signals and the PIN block. |
Warning
In advanced usage, do not call initiate nor complete.
In advanced usage, the application sees every outcome: each kernelActivation call returns the outcome it ends with, along with its user interface request data. Switcloud Client raises no bip event: the application plays the tones itself.
Tip
The Switcloud L3 template implements both usages; see its AdvancedPaymentManager for a complete advanced card processing.
4. Events
Switcloud Client exposes Kotlin SharedFlows on the SwitcloudClt companion object:
| Flow | Type | Usage | Content |
|---|---|---|---|
userInfo |
ByteArray |
Basic, advanced | Cardholder messages and LEDs requested by the EMV L2 stack |
bipEvent |
BipEvent |
Basic | Request to play a tone at the end of card processing |
pinRequest |
PinRequestEvent? |
Basic, advanced | Request to show the PIN pad (null once done) |
paymentData |
ByteArray |
Basic, advanced | Payment data sent to Switcloud |
Each flow replays its latest values to new collectors, including values of previous transactions. Collect them once, at application startup, rather than once per payment:
scope.launch {
merge(SwitcloudClt.userInfo, SwitcloudClt.bipEvent).collect { event ->
when (event) {
is ByteArray -> onUserInfo(event)
is BipEvent -> onBip(event)
}
}
}
4.1 Cardholder messages
The EMV L2 stack asks the application to show messages to the cardholder: user interface request data, or UIRD. These come from two sources, and the application must capture both:
- The responses returned by Switcloud Client.
initiate,complete,postInitiateandpostCompletereturn anInitiationDatawhoseuserInterfaceRequestDatais the UIRD of the final outcome, if any. In advanced usage, each GLA call also returns the UIRD of the outcome it ends with. - The
userInfoflow. It carries every UIRD and LED request, as it happens, including those of intermediate outcomes.
It is up to the application to decide which messages it displays, and for how long. The final message may be received both ways.
In basic usage, userInfo is the only way to know about intermediate outcomes, as initiate handles them without returning. The typical case is the on-device CVM (CDCVM): a phone or a watch asks the cardholder to authenticate on the device, then to present it again. While initiate keeps running, userInfo carries:
- "present card": waiting for the card;
- "see phone for instructions": the cardholder must authenticate on the phone;
- "present card again": waiting for the phone again;
- messages from the second presentation, such as "card read OK".
initiate then returns the final outcome and its message, e.g. "approved".
Each userInfo value is a single TLV: a UIRD (DF8116 or 9F8205) or a LED (DFA01B). See Switcloud L2 for their format and when the EMV L2 stack raises them. Switcloud Client's UserInterfaceRequestData decodes a UIRD value:
fun onUserInfo(event: ByteArray) {
// a single TLV, its length is always on 1 byte
val tag = event.copyOfRange(0, 3).toHexString().uppercase()
val value = event.copyOfRange(4, event.size)
when (tag) {
"DF8116", "9F8205" -> {
val uird = UserInterfaceRequestData(value)
showMessage(uird.messageIdentifier, uird.status)
}
"DFA01B" -> if (value[0].toInt() and 0x80 != 0) {
switchLedsOn(upTo = value[0].toInt() and 0x0F)
}
}
}
4.2 Tones
EMV contactless requires the terminal to play a tone once the card has been read, so that the cardholder knows the card can be removed. Switcloud Client has no access to the device's audio: in basic usage, initiate raises a BipEvent instead, and the application plays the tone.
initiateraises one event, when card processing ends: after any PIN entry, and before the payment data is sent to Switcloud.successistruewhen the card was read successfully (approved, declined or online request): play the success tone. It isfalseotherwise (e.g. end of application, try another interface): play the alert tone.- No event is raised when
initiatefails with an exception, nor bycomplete. bipEventreplays its last event to new collectors: usetimeStampto ignore an event of a previous payment.
fun onBip(event: BipEvent) {
if (event.timeStamp < paymentStartTime) {
return
}
if (event.success) {
tones.playSuccess()
} else {
tones.playAlert()
}
}
In advanced usage, no event is raised: the application plays the tones itself, when its card processing ends.
4.3 PIN entry
When the kernel requires an online PIN, Switcloud Client:
- emits a
PinRequestEventonpinRequest: show the PIN pad; - waits, for 60 seconds at most, for the PIN on the
pinInputchannel given toinitialize: send the PIN digits, ornullif the cardholder cancels; - computes the PIN block and emits
nullonpinRequest: hide the PIN pad.
val pinInput = Channel<String?>(Channel.BUFFERED)
scope.launch {
SwitcloudClt.pinRequest.collect { request ->
if (request != null) showPinPad() else hidePinPad()
}
}
// when the cardholder validates, or cancels
pinInput.trySend(pin)
In basic usage, initiate and complete ask for the PIN themselves. In advanced usage, the application calls requestPinEntry.