Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports EU URL for tokenization, update gradle to support latest changes, add error handling for connection issues like no internet #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions AndroidSdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ plugins {
}

android {
compileSdk 31
compileSdk 33

defaultConfig {
minSdk 21
targetSdk 31
versionCode 1
versionName "2.0.0"
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand All @@ -37,6 +35,7 @@ android {
viewBinding {
enabled = true
}
namespace 'com.recurly.androidsdk'
}

dependencies {
Expand Down
3 changes: 1 addition & 2 deletions AndroidSdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.recurly.androidsdk">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.recurly.androidsdk.data.model.tokenization.TokenizationResponse
import com.recurly.androidsdk.data.network.core.RetrofitHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.HttpException
import java.io.IOException

class TokenService {

Expand All @@ -23,37 +25,71 @@ class TokenService {
*/
suspend fun getToken(request: TokenizationRequest): TokenizationResponse {
return withContext(Dispatchers.IO) {
val response =
retrofit.create(RecurlyApiClient::class.java).recurlyTokenization(
first_name = request.firstName,
last_name = request.lastName,
company = request.company,
address1 = request.addressOne,
address2 = request.addressTwo,
city = request.city,
state = request.state,
postal_code = request.postalCode,
country = request.country,
phone = request.phone,
vat_number = request.vatNumber,
tax_identifier = request.taxIdentifier,
tax_identifier_type = request.taxIdentifierType,
number = request.cardNumber,
month = request.expirationMonth,
year = request.expirationYear,
cvv = request.cvvCode,
version = request.sdkVersion,
key = request.publicKey,
deviceId = request.deviceId,
sessionId = request.sessionId
try {
val response =
retrofit.create(RecurlyApiClient::class.java).recurlyTokenization(
first_name = request.firstName,
last_name = request.lastName,
company = request.company,
address1 = request.addressOne,
address2 = request.addressTwo,
city = request.city,
state = request.state,
postal_code = request.postalCode,
country = request.country,
phone = request.phone,
vat_number = request.vatNumber,
tax_identifier = request.taxIdentifier,
tax_identifier_type = request.taxIdentifierType,
number = request.cardNumber,
month = request.expirationMonth,
year = request.expirationYear,
cvv = request.cvvCode,
version = request.sdkVersion,
key = request.publicKey,
deviceId = request.deviceId,
sessionId = request.sessionId
)

response.body() ?: TokenizationResponse(
"", "", ErrorRecurly(
errorCode = response.code().toString(),
errorMessage = response.message(),
fields = emptyList(),
details = emptyList()
)
)
} catch (exception: HttpException) {
exception.printStackTrace()
TokenizationResponse(
error = ErrorRecurly(
errorCode = exception.code().toString(),
errorMessage = exception.message(),
fields = listOf(exception.response()?.body().toString()),
details = emptyList(),
)
)
response.body() ?: TokenizationResponse(
"", "", ErrorRecurly(
"", "", emptyList(),
emptyList()
} catch (ioException: IOException) {
ioException.printStackTrace()
TokenizationResponse(
error = ErrorRecurly(
errorCode = "recurly-client-android-internet-connection",
errorMessage = "Your internet connection appears to be offline",
fields = emptyList(),
details = emptyList(),
)
)
)
} catch (exception: Exception) {
exception.printStackTrace()
TokenizationResponse(
error = ErrorRecurly(
errorCode = "recurly-client-android-networking",
errorMessage = exception.localizedMessage ?: "",
fields = emptyList(),
details = emptyList(),
)
)
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.recurly.androidsdk.data.network.core

import okhttp3.OkHttpClient
import com.recurly.androidsdk.data.model.RecurlySessionData
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

Expand All @@ -10,10 +10,16 @@ internal object RetrofitHelper {
* Builds the retrofit for its direct use
* @return Retrofit
*/
fun getRetrofit(): Retrofit{
fun getRetrofit(): Retrofit {
val defaultURL = "https://api.recurly.com/"
val defaultURLEU = "https://api.eu.recurly.com/"

val baseUrl =
if (RecurlySessionData.getPublicKey().startsWith("fra-")) defaultURLEU else defaultURL

return Retrofit.Builder()
.client(OkHttpHelper.getOkHttpClient())
.baseUrl("https://api.recurly.com/")
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
Expand Down
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {
}

android {
compileSdk 31
compileSdk 33

defaultConfig {
applicationId "com.recurly.android"
minSdk 21
targetSdk 31
targetSdk 33
versionCode 1
versionName "2.0.0"

Expand All @@ -36,6 +36,7 @@ android {
viewBinding {
enabled = true
}
namespace 'com.recurly.android'
}

dependencies {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.recurly.android"
android:versionCode="1"
android:versionName="1.0">

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
classpath 'com.android.tools.build:gradle:7.4.2'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.21.0'

// NOTE: Do not place your application dependencies here; they belong
Expand Down