-
Notifications
You must be signed in to change notification settings - Fork 664
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
[Connect SDK] Integrate with backend calls for client secret #9287
Merged
simond-stripe
merged 11 commits into
master
from
simond/connect-sdk-example-app-backend-integration
Oct 18, 2024
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1422da5
Integrate with backend calls for client secret
simond-stripe 1dab786
Fix lint
simond-stripe 3db4ef5
add dependencies file
simond-stripe 9372621
Move from timber to Logger
simond-stripe 1ee1c3f
Remove unnecessary import
simond-stripe c910fb7
fix lint for deps
simond-stripe 7e43b2b
Merge branch 'master' into simond/connect-sdk-example-app-backend-int…
simond-stripe f674b46
Merge branch 'master' into simond/connect-sdk-example-app-backend-int…
simond-stripe 53911b5
Fix deps
simond-stripe 3c2d62b
Fix colors, fix merge confligt
simond-stripe b347fb1
Fix icon lint
simond-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
package com.stripe.android.connectsdk.example | ||
|
||
import android.app.Application | ||
import android.os.StrictMode | ||
|
||
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() | ||
) | ||
} | ||
} |
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
86 changes: 80 additions & 6 deletions
86
...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,15 +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(ARTIFICIAL_DELAY_MS) | ||
return null | ||
private val fuel = FuelManager.instance | ||
.apply { | ||
// add logging | ||
addRequestInterceptor(RequestLogger(tag = "EmbeddedComponentService")) | ||
addResponseInterceptor(ResponseLogger(tag = "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 ARTIFICIAL_DELAY_MS = 3000L | ||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove? |
||
val body = response.body().asString("application/json") | ||
return Json.decodeFromString(serializer, body) | ||
} | ||
} | ||
|
||
return awaitResult(deserializer) | ||
} |
62 changes: 62 additions & 0 deletions
62
.../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,62 @@ | ||
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.connectsdk.example.BuildConfig | ||
import com.stripe.android.core.Logger | ||
import com.stripe.android.core.version.StripeSdkVersion | ||
|
||
object ApplicationJsonHeaderInterceptor : FoldableRequestInterceptor { | ||
override fun invoke(next: RequestTransformer): RequestTransformer { | ||
return { request -> | ||
next(request.header("content-type", "application/json")) | ||
} | ||
} | ||
} | ||
|
||
object UserAgentHeader : FoldableRequestInterceptor { | ||
private fun getUserAgent(): String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could be a val |
||
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 RequestLogger( | ||
private val tag: String, | ||
private val logger: Logger = Logger.getInstance(enableLogging = BuildConfig.DEBUG), | ||
) : FoldableRequestInterceptor { | ||
override fun invoke(next: RequestTransformer): RequestTransformer { | ||
return { request -> | ||
logger.info("($tag) Request: ${request.cUrlString()}") | ||
next(request) | ||
} | ||
} | ||
} | ||
|
||
class ResponseLogger( | ||
private val tag: String, | ||
private val logger: Logger = Logger.getInstance(enableLogging = BuildConfig.DEBUG), | ||
) : FoldableResponseInterceptor { | ||
override fun invoke(next: ResponseTransformer): ResponseTransformer { | ||
return { request, response -> | ||
logger.info("($tag) Response: $response") | ||
next(request, response) | ||
} | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
...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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: IMO we should prefer having only one public class per file. It makes finding things a lot easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I'll refactor in a follow-up