-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate with backend calls for client secret
- Loading branch information
1 parent
23956f4
commit 132cadd
Showing
16 changed files
with
606 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
stripe-connect-example/src/main/java/com/stripe/android/connectsdk/example/App.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.stripe.android.connectsdk.example | ||
|
||
import android.app.Application | ||
import android.os.StrictMode | ||
import timber.log.Timber | ||
|
||
class App: Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
StrictMode.setThreadPolicy( | ||
StrictMode.ThreadPolicy.Builder() | ||
.detectDiskReads() | ||
.detectDiskWrites() | ||
.detectAll() | ||
.penaltyLog() | ||
.build() | ||
) | ||
|
||
StrictMode.setVmPolicy( | ||
StrictMode.VmPolicy.Builder() | ||
.detectLeakedSqlLiteObjects() | ||
.detectLeakedClosableObjects() | ||
.penaltyLog() | ||
.build() | ||
) | ||
|
||
if (BuildConfig.DEBUG) { | ||
Timber.plant(Timber.DebugTree()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 83 additions & 5 deletions
88
...rc/main/java/com/stripe/android/connectsdk/example/networking/EmbeddedComponentService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,89 @@ | ||
package com.stripe.android.connectsdk.example.networking | ||
|
||
import kotlinx.coroutines.delay | ||
import com.github.kittinunf.fuel.core.Deserializable | ||
import com.github.kittinunf.fuel.core.FuelError | ||
import com.github.kittinunf.fuel.core.FuelManager | ||
import com.github.kittinunf.fuel.core.Request | ||
import com.github.kittinunf.fuel.core.Response | ||
import com.github.kittinunf.fuel.core.awaitResult | ||
import com.github.kittinunf.result.Result | ||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
|
||
class EmbeddedComponentService { | ||
suspend fun fetchClientSecret(): String? { | ||
// TODO MXMOBILE-2511 - add backend call | ||
delay(3000) | ||
return null | ||
private val fuel = FuelManager.instance | ||
.apply { | ||
// add logging | ||
addRequestInterceptor(TimberRequestLogger("EmbeddedComponentService")) | ||
addResponseInterceptor(TimberResponseLogger("EmbeddedComponentService")) | ||
|
||
// add headers | ||
addRequestInterceptor(ApplicationJsonHeaderInterceptor) | ||
addRequestInterceptor(UserAgentHeader) | ||
} | ||
|
||
/** | ||
* Returns the publishable key for use in the Stripe Connect SDK as well as a list | ||
* of available merchants. Throws a [FuelError] exception on network issues and other errors. | ||
*/ | ||
suspend fun getAccounts(): GetAccountsResponse { | ||
return fuel.get(EXAMPLE_BACKEND_URL + "app_info") | ||
.awaitModel(GetAccountsResponse.serializer()) | ||
.get() | ||
} | ||
|
||
/** | ||
* Returns the client secret for the given merchant account to be used in the Stripe Connect SDK. | ||
* Throws a [FuelError] exception on network issues and other errors. | ||
*/ | ||
suspend fun fetchClientSecret(account: String): String { | ||
return fuel.post(EXAMPLE_BACKEND_URL + "account_session") | ||
.header("account", account) | ||
.awaitModel(FetchClientSecretResponse.serializer()) | ||
.get() | ||
.clientSecret | ||
} | ||
|
||
companion object { | ||
private const val EXAMPLE_BACKEND_URL = "https://stripe-connect-mobile-example-v1.glitch.me/" | ||
} | ||
} | ||
|
||
@Serializable | ||
data class FetchClientSecretResponse( | ||
@SerialName("client_secret") | ||
val clientSecret: String | ||
) | ||
|
||
@Serializable | ||
data class GetAccountsResponse( | ||
@SerialName("publishable_key") | ||
val publishableKey: String, | ||
@SerialName("available_merchants") | ||
val availableMerchants: List<Merchant> | ||
) | ||
|
||
@Serializable | ||
data class Merchant( | ||
@SerialName("merchant_id") | ||
val merchantId: String, | ||
@SerialName("display_name") | ||
val displayName: String | ||
) | ||
|
||
suspend fun <T : Any> Request.awaitModel( | ||
serializer: DeserializationStrategy<T> | ||
): Result<T, FuelError> { | ||
val deserializer = object : Deserializable<T> { | ||
|
||
override fun deserialize(response: Response): T { | ||
println(response.toString()) | ||
val body = response.body().asString("application/json") | ||
return Json.decodeFromString(serializer, body) | ||
} | ||
} | ||
|
||
return awaitResult(deserializer) | ||
} |
59 changes: 59 additions & 0 deletions
59
.../src/main/java/com/stripe/android/connectsdk/example/networking/NetworkingInterceptors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.stripe.android.connectsdk.example.networking | ||
|
||
import android.os.Build | ||
import com.github.kittinunf.fuel.core.FoldableRequestInterceptor | ||
import com.github.kittinunf.fuel.core.FoldableResponseInterceptor | ||
import com.github.kittinunf.fuel.core.RequestTransformer | ||
import com.github.kittinunf.fuel.core.ResponseTransformer | ||
import com.github.kittinunf.fuel.core.extensions.cUrlString | ||
import com.stripe.android.core.version.StripeSdkVersion | ||
import timber.log.Timber | ||
|
||
object ApplicationJsonHeaderInterceptor : FoldableRequestInterceptor { | ||
override fun invoke(next: RequestTransformer): RequestTransformer { | ||
return { request -> | ||
next(request.header("content-type", "application/json")) | ||
} | ||
} | ||
} | ||
|
||
object UserAgentHeader : FoldableRequestInterceptor { | ||
fun getUserAgent(): String { | ||
val androidBrand = Build.BRAND | ||
val androidDevice = Build.MODEL | ||
val osVersion = Build.VERSION.SDK_INT | ||
return buildString { | ||
append("Stripe/ConnectSDKExample") | ||
append(" (Android $androidBrand $androidDevice; (OS Version $osVersion))+") | ||
append(" Version/${StripeSdkVersion.VERSION_NAME}") | ||
} | ||
} | ||
|
||
override fun invoke(next: RequestTransformer): RequestTransformer { | ||
return { request -> | ||
next(request.header("User-Agent", getUserAgent())) | ||
} | ||
} | ||
} | ||
|
||
class TimberRequestLogger(private val tag: String) : FoldableRequestInterceptor { | ||
private val timber get() = Timber.tag(tag) | ||
|
||
override fun invoke(next: RequestTransformer): RequestTransformer { | ||
return { request -> | ||
timber.i("Request: ${request.cUrlString()}") | ||
next(request) | ||
} | ||
} | ||
} | ||
|
||
class TimberResponseLogger(private val tag: String) : FoldableResponseInterceptor { | ||
private val timber get() = Timber.tag(tag) | ||
|
||
override fun invoke(next: ResponseTransformer): ResponseTransformer { | ||
return { request, response -> | ||
timber.i("Response: $response") | ||
next(request, response) | ||
} | ||
} | ||
} |
53 changes: 0 additions & 53 deletions
53
...tripe/android/connectsdk/example/ui/accountonboarding/AccountOnboardingExampleActivity.kt
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
...ripe/android/connectsdk/example/ui/accountonboarding/AccountOnboardingExampleViewModel.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.