Usage of Global-E SDK
All API requests in GlobalEMobileSDK are async. To correctly display data from the result of an API call, use the progress bar, activity indicator, etc.
iOS Initialization and Setup
Import GlobalEMobileSDK
GlobalESDK.setup(token: "YnMTYTk04do=", domain: "https://stgepi4.bglobale.com/externalapi/", cartTokenURL: "https://www2.bglobale.com/checkoutv2/fullredirect/")
Examples:
● getCountriesLis
t method:
GlobalESDK.instance.getCountriesList { (result: Result<Array<Country>, GlobalEError>) in switch result { case .success(let countries): print(countries) case .failure(let error): print(error.errorDescription) print(error.errorCode) } }
● getCurrenciesList
method:
GlobalESDK.instance.getCurrenciesList { (result: Result<Array<Currency>, GlobalEError>) in switch result { case.success(let result): print(result) case.failure(let error): print(error.errorDescription) } }
● Price calculation method:
DefaultCountry
object with selected country info. Get this object from updateCurrentCountryInfo or getCurrentCountryInfo methods.
GlobalESDK.instance.convertPrice(countryCode: "IL", currencyCode: "ILS", merchantInfo: DefaultCountry, basePrice: 100.0, quantity: 1, isGrossPrices: false, localVatRate: nil, productCountryVatRate: nil, applyMarketingRounding: true) { (result: Result<Float, GlobalEError>) in switch result { case .success(let value): print(value) case .failure(let error): print(error.errorDescription) print(error.errorCode) } } }
Implement a calculation method:
class API { static func fetchData( @escaping cb: (String) -> (String)) { cb("price") } } class PriceCell: UITableViewCell { @IBOutlet var priveLabel: UILabel! func setData(price: String) { self.priveLabel.text = price } } class TableScreen: UITableView { var dataSource: [String]! func dataSourceInit() { dataSource = [String]() } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "PriceCell", for: indexPath) as! PriceCell let price = dataSource[indexPath.row] if (price == nil) { // call API API.fetchData { (result) -> (String) in dataSource[indexPath.row] = result cell.setData(price: dataSource[indexPath.row]) } } else { cell.setData(price: price) } return cell } }
Android Initialization and Setup
Initialize SDK in your Application class
GlobalESDK.init(Application, token, domain, cartTokenUrl)
Example:
class App : Application() { override fun onCreate() { super.onCreate() GlobalESDK.init(this, "YnMTYTk04do=", "https://stgepi4.bglobale.com/externalapi/", "https://www2.bglobale.com/checkoutv2/fullredirect/") } }
Examples of different methods:
getCountriesList
method (Coroutines):
val payload = GlobalESDK.getInstance().getCountriesList() payload.data?.let { //it - get countries } payload.error?.let { it.forEach { error -> Log.d("TAG", "${error.errorCode}") Log.d("TAG", error.errorDescription) } }
getCurrenciesList
method:
val payload = GlobalESDK.getInstance().getCurrenciesList() payload.data?.let { //it - get currencies list } payload.error?.let { it.forEach { error -> Log.d("TAG", "${error.errorCode}") Log.d("TAG", error.errorDescription) } }
DefaultCountry
object with selected country info.
Get this object from updateCurrentCountryInfo or getCurrentCountryInfo methods.
getCurrentCountryInfo
method:
val payload = GlobalESDK.getInstance().getCurrentCountryInfo() payload.data?.let { //it - get default country } payload.error?.let { it.forEach { error -> Log.d("TAG", "${error.errorCode}") Log.d("TAG", error.errorDescription) } }
calculatePrice
method (Coroutines):
val payload = GlobalESDK.getInstance().convertPrice( countryCode = “IL”, currencyCode = “ILS”, merchantInfo: DefaultCountry, applyMarketingRounding = true, quantity = 1, basePrice = 100.0, productLocalVatRate = null, productCountryVatRate = null, isGrossPrices = true ) payload.data?.let { //it - get calculation price } payload.error?.let { it.forEach { error -> Log.d("TAG", "${error.errorCode}") Log.d("TAG", error.errorDescription) } }
To implement the calculation method in the list:
val totalPrice = 0.0 listProduct.forEach { product -> val payload = GlobalESDK.getInstance().convertPrice( countryCode = countryCode, currencyCode = currencyCode, merchantInfo: DefaultCountry, applyMarketingRounding = true, quantity = 1, basePrice = product.price, productLocalVatRate = 10.0, productCountryVatRate = 12.0, isGrossPrices = true ) payload.data?.let { price -> totalPrice += price.toDouble() } payload.error?.let { errors -> errors.forEach { error -> Log.d("TAG", "${error.errorCode}") Log.d("TAG", error.errorDescription) } } }
An example of how to correctly calculate the total price:
val totalPrice = 0 val calculatedPrice1 = convertPrice(basePrice1) val calculatedPrice2 = convertPrice(basePrice2) val calculatedPrice3 = convertPrice(basePrice3) totalPrice = calculatedPrice1 + calculatedPrice2 + calculatedPrice3
The incorrect way to calculate the price:
val baseTolalPrice = basePrice1 + basePrice2 + basePrice3 totalPrice = convertPrice(baseTolalPrice)
Additional information for RX users: https://github.com/Kotlin/kotlinx.coroutines/tree/master/reactive/kotlinx-coroutines-rx2