-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
CustomerSheetDataSource
& CustomerAdapterDataSource
.
- Loading branch information
1 parent
76ddab6
commit d97d4a4
Showing
13 changed files
with
377 additions
and
24 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
52 changes: 52 additions & 0 deletions
52
...entsheet/src/main/java/com/stripe/android/customersheet/data/CustomerAdapterDataSource.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,52 @@ | ||
package com.stripe.android.customersheet.data | ||
|
||
import com.stripe.android.core.exception.StripeException | ||
import com.stripe.android.customersheet.CustomerAdapter | ||
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi | ||
import com.stripe.android.customersheet.map | ||
import com.stripe.android.customersheet.util.awaitAsResult | ||
import com.stripe.android.payments.core.analytics.ErrorReporter | ||
import kotlinx.coroutines.Deferred | ||
import javax.inject.Inject | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
internal class CustomerAdapterDataSource @Inject constructor( | ||
private val customerAdapterProvider: Deferred<CustomerAdapter>, | ||
private val errorReporter: ErrorReporter, | ||
) : CustomerSheetDataSource { | ||
override suspend fun retrievePaymentMethods() = withAdapter { adapter -> | ||
adapter.retrievePaymentMethods().toCustomerSheetDataResult() | ||
} | ||
|
||
override suspend fun retrieveSavedSelection() = withAdapter { adapter -> | ||
adapter.retrieveSelectedPaymentOption().map { result -> | ||
result?.toSavedSelection() | ||
}.toCustomerSheetDataResult() | ||
} | ||
|
||
private suspend fun <T> withAdapter( | ||
task: suspend (CustomerAdapter) -> CustomerSheetDataResult<T> | ||
): CustomerSheetDataResult<T> { | ||
val adapter = retrieveCustomerAdapter().getOrElse { | ||
return CustomerSheetDataResult.Failure(cause = it, displayMessage = null) | ||
} | ||
|
||
return task(adapter) | ||
} | ||
|
||
private suspend fun retrieveCustomerAdapter(): Result<CustomerAdapter> { | ||
return customerAdapterProvider.awaitAsResult( | ||
timeout = 5.seconds, | ||
error = { | ||
"Couldn't find an instance of CustomerAdapter. " + | ||
"Are you instantiating CustomerSheet unconditionally in your app?" | ||
}, | ||
).onFailure { | ||
errorReporter.report( | ||
errorEvent = ErrorReporter.ExpectedErrorEvent.CUSTOMER_SHEET_ADAPTER_NOT_FOUND, | ||
stripeException = StripeException.create(it) | ||
) | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
paymentsheet/src/main/java/com/stripe/android/customersheet/data/CustomerSheetDataResult.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.customersheet.data | ||
|
||
internal sealed interface CustomerSheetDataResult<T> { | ||
data class Success<T>(val value: T) : CustomerSheetDataResult<T> | ||
|
||
data class Failure<T>( | ||
val cause: Throwable, | ||
val displayMessage: String? = null | ||
) : CustomerSheetDataResult<T> | ||
|
||
fun toResult(): Result<T> { | ||
return when (this) { | ||
is Success -> Result.success(value) | ||
is Failure -> Result.failure(cause) | ||
} | ||
} | ||
|
||
companion object { | ||
fun <T> success(value: T): Success<T> { | ||
return Success(value) | ||
} | ||
|
||
fun <T> failure(cause: Throwable, displayMessage: String?): Failure<T> { | ||
return Failure(cause = cause, displayMessage = displayMessage) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ntsheet/src/main/java/com/stripe/android/customersheet/data/CustomerSheetDataResultKtx.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,15 @@ | ||
package com.stripe.android.customersheet.data | ||
|
||
import com.stripe.android.customersheet.CustomerAdapter | ||
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi | ||
|
||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
internal fun <T> CustomerAdapter.Result<T>.toCustomerSheetDataResult(): CustomerSheetDataResult<T> { | ||
return when (this) { | ||
is CustomerAdapter.Result.Success -> CustomerSheetDataResult.success(value) | ||
is CustomerAdapter.Result.Failure -> CustomerSheetDataResult.failure( | ||
cause = cause, | ||
displayMessage = displayMessage, | ||
) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
paymentsheet/src/main/java/com/stripe/android/customersheet/data/CustomerSheetDataSource.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,14 @@ | ||
package com.stripe.android.customersheet.data | ||
|
||
import com.stripe.android.model.PaymentMethod | ||
import com.stripe.android.paymentsheet.model.SavedSelection | ||
|
||
/** | ||
* [CustomerSheetDataSource] defines a set of operations used by [com.stripe.android.customersheet.CustomerSheet] to | ||
* retrieve the data necessary to perform its actions. | ||
*/ | ||
internal interface CustomerSheetDataSource { | ||
suspend fun retrievePaymentMethods(): CustomerSheetDataResult<List<PaymentMethod>> | ||
|
||
suspend fun retrieveSavedSelection(): CustomerSheetDataResult<SavedSelection?> | ||
} |
39 changes: 39 additions & 0 deletions
39
.../main/java/com/stripe/android/customersheet/injection/CustomerSheetDataSourceComponent.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,39 @@ | ||
package com.stripe.android.customersheet.injection | ||
|
||
import android.content.Context | ||
import com.stripe.android.core.injection.CoreCommonModule | ||
import com.stripe.android.core.injection.CoroutineContextModule | ||
import com.stripe.android.customersheet.CustomerAdapter | ||
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi | ||
import com.stripe.android.customersheet.data.CustomerSheetDataSource | ||
import com.stripe.android.payments.core.injection.StripeRepositoryModule | ||
import dagger.BindsInstance | ||
import dagger.Component | ||
import kotlinx.coroutines.Deferred | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
@Component( | ||
modules = [ | ||
CustomerDataCommonModule::class, | ||
CustomerSheetDataSourceModule::class, | ||
StripeRepositoryModule::class, | ||
CoroutineContextModule::class, | ||
CoreCommonModule::class, | ||
] | ||
) | ||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
internal interface CustomerSheetDataSourceComponent { | ||
val customerSheetDataSource: CustomerSheetDataSource | ||
|
||
@Component.Builder | ||
interface Builder { | ||
@BindsInstance | ||
fun context(context: Context): Builder | ||
|
||
@BindsInstance | ||
fun customerAdapterProvider(provider: Deferred<CustomerAdapter>): Builder | ||
|
||
fun build(): CustomerSheetDataSourceComponent | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...src/main/java/com/stripe/android/customersheet/injection/CustomerSheetDataSourceModule.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,25 @@ | ||
package com.stripe.android.customersheet.injection | ||
|
||
import com.stripe.android.customersheet.CustomerAdapter | ||
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi | ||
import com.stripe.android.customersheet.data.CustomerAdapterDataSource | ||
import com.stripe.android.customersheet.data.CustomerSheetDataSource | ||
import com.stripe.android.payments.core.analytics.ErrorReporter | ||
import dagger.Module | ||
import dagger.Provides | ||
import kotlinx.coroutines.Deferred | ||
|
||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
@Module | ||
internal class CustomerSheetDataSourceModule { | ||
@Provides | ||
fun providesCustomerSheetDataSource( | ||
customerAdapterProvider: Deferred<CustomerAdapter>, | ||
errorReporter: ErrorReporter, | ||
): CustomerSheetDataSource { | ||
return CustomerAdapterDataSource( | ||
customerAdapterProvider = customerAdapterProvider, | ||
errorReporter = errorReporter, | ||
) | ||
} | ||
} |
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
Oops, something went wrong.