Examples
You can find here code samples for basic operations in different languages.
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!
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.
Make a payment
Now it's time to create and make a Payment. Here a sample Kotlin code:
import io.switstack.switcloud.switcloudapi.SwitcloudApi
import io.switstack.switcloud.switcloudapi.model.PaymentCreateSchema
import io.switstack.switcloud.switcloudapi.model.PaymentReadSchema
import io.switstack.switcloud.switcloudclt.SwitcloudClt
// This code could be in the backend side
/* SwitcloudApi object to target switcloud APIs */
private val switcloudApi = SwitcloudApi("https://switcloud.switstack.io")
/* The POIConfig ID to use for the transaction */
private val poiConfigID = "01973613-bab2-7c99-9e81-05c499c1afe5"
/* The POI ID corresponding to the device in use *.
private val poiID = "01973613-f89d-7cf1-8082-3ba96d70ded5"
/* Create the Payment oject */
val payment = PaymentCreateSchema(poiConfigId = poiConfigID, poiId = poiID)
/* Auth */
switcloudApi.updateToken(
switcloudApi.oauth.tokenApiOauth2TokenPost(
grantType = "client_credentials",
clientId = "<login>",
clientSecret = "<passord>").accessToken)
/* Actual creation into switcloud */
val paymentID = switcloudApi.payment.createPayment(payment).id
// This code should be in the terminal side
/* Switcloudclt object */
private val switcloudClt = SwitcloudClt()
/* Transaction Related Data (amount, currency, etc...) */
private val trd = = byteArrayOf(
0x9F.toByte(), 0x02, 0x06, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00,
0x9A.toByte(), 0x03, 0x14, 0x08, 0x01)
/* Setup */
switcloudClt.setup(activity, "https://switcloud.switstack.io")
/* Authentication */
switcloudClt.initialize("<login>", "<password>", null)
/* Payment configuration */
switcloudClt.configure(poiID, paymentID!!, trd)
/* Card processing */
val response = switcloudClt.initiate(paymentID!!)