Examples

You will find below code samples in different languages for the different elements that -could- compose your payment application.

1. Create a basic estate

Here is some Python code that shows you how to create a basic estate:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from switcloudapi import SwitcloudApi
from switcloudapi.models import (
    EMVConfigCreateSchema,
    EMVListCreateSchema,
    MerchantCreateSchema,
    POIConfigCreateSchema,
    POICreateSchema,
    StoreCreateSchema
)

# Create a SwitcloudAPI instance
api = SwitcloudApi("https://switcloud.switstack.io")

# Get an authentication token
token = api.oauth.token(
    grant_type="password",
    username="<user>@<organization>",
    password="<mypassword>",
)

if not token:
    raise Exception("unable to login into switcloud")

# Save the token for subsequent calls
api.update_token(token)

# Create a Merchant
merchant = MerchantCreateSchema(name="My Merchant")
_merchant = api.bom.create_merchant(merchant)

# Create a Store
store = StoreCreateSchema(name="My Store", merchant_id=_merchant.id)
_store = api.bom.create_store(store)

# Create a POI
poi = POICreateSchema(
    name="My POI",
    identifier="XYZ123",
    serial_number="123XYZ",
    store_id=_store.id,
)

 _poi = api.bom.create_poi(poi)

That's it. you are all set!

Note

This code does not catch exceptions and errors!

2. Create a basic EMV configuration

Now that your estate is prepared, you can add a basic EMV configuration to the sytem.

Here is some Python code for that:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from switcloudapi import SwitcloudApi
from switcloudapi.models import (
    EMVCreateSchema,
    EMVTechnologyType,
)

# Create a SwitcloudAPI instance
api = SwitcloudApi("https://switcloud.switstack.io")

# Get an authentication token
token = api.oauth.token(
    grant_type="password",
    username="<user>@<organization>",
    password="<mypassword>",
)

# Create an EMV
emv = EMVCreateSchema(
    name="My EMV",
    technology_type=EMVTechnologyType.CONTACTLESS,
    kernel="02",
    aid="A0000000041010",
    tlv="9F01009F090200029F150211229F16009F1A0200569F1C009F1D086CFF0000000000009F1E0801020304050607089F33009F3501229F400500000000009F4E009F7E00DF811B0120DF8120050000000000DF8121050000000000DF8122050000000000DF812306000000010000DF812406000000030000DF812506000000050000DF812606000000001000DF81170100DF81180160DF81190108DF811F0108"
)
_emv = api.config.create_emv(emv)

# Create an EMVList
emv_list = EMVListCreateSchema(name="My EMVList")
_emv_list = api.config.create_emv_list(emv_list)

# Add the EMV to the list
api.config.add_emv_to_emv_list(id=_emv_list.id, emv_id=_emv.id)

# Create an EMVConfig
emv_config = EMVConfigCreateSchema(
    name="My EMVConfig", emv_nominal_list_id=_emv_list.id
)
_emv_config = api.config.create_emv_config(emv_config)

# Create a POIConfig
poi_config = POIConfigCreateSchema(
    name="My POIConfig", emv_config_id=_emv_config.id
)
_poi_config = api.config.create_poi_config(poi_config)

Here we are. A very simple POIConfig capable of handling Mastercard contactless transaction.

3. Make a payment

Now it's time to create and make a Payment. Creating it is the backend's job, here in Python:

from switcloudapi.models import PaymentCreateSchema

# The POIConfig to use for the transaction, and the POI of the device in use
poi_config_id = "01973613-bab2-7c99-9e81-05c499c1afe5"
poi_id = "01973613-f89d-7cf1-8082-3ba96d70ded5"

payment = PaymentCreateSchema(
    poi_config_id=poi_config_id,
    poi_id=poi_id,
    # Transaction related data: amount, date, type, currency
    trd="9F02060000000015009A032611269C01005F2A020978",
)
_payment = api.payment.create_payment(payment)

# Send _payment.id to the terminal

The terminal then processes the payment with Switcloud Client, in Kotlin:

/* Switcloud Client, set up as described in the Switcloud Client page */
val switcloudClt: SwitcloudClt by inject { parametersOf("https://switcloud.switstack.io") }

/* Authentication */
switcloudClt.authenticateMachine("<client ID>", "<client secret>")

try {
    /* Initialization, with the reader parameters (see Switcloud L2) */
    switcloudClt.initialize(readerParams)

    /* Payment configuration: fetches the payment and its POIConfig */
    val sessionData = switcloudClt.configure(paymentId)

    /* Card processing */
    val response = switcloudClt.initiate(sessionData)
} finally {
    switcloudClt.cleanup()
}