-
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 initial
CustomerSheet
data sources & use in CustomerSheet
- Loading branch information
1 parent
c8bcb35
commit d31e470
Showing
9 changed files
with
205 additions
and
7 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
23 changes: 23 additions & 0 deletions
23
...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,23 @@ | ||
package com.stripe.android.customersheet.data | ||
|
||
import com.stripe.android.customersheet.CustomerAdapter | ||
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi | ||
import com.stripe.android.customersheet.map | ||
import com.stripe.android.model.PaymentMethod | ||
import com.stripe.android.paymentsheet.model.SavedSelection | ||
import javax.inject.Inject | ||
|
||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
internal class CustomerAdapterDataSource @Inject constructor( | ||
private val customerAdapter: CustomerAdapter, | ||
) : CustomerSheetCombinedDataSource { | ||
override suspend fun retrievePaymentMethods(): CustomerSheetDataResult<List<PaymentMethod>> { | ||
return customerAdapter.retrievePaymentMethods().toCustomerSheetDataResult() | ||
} | ||
|
||
override suspend fun retrieveSavedSelection(): CustomerSheetDataResult<SavedSelection?> { | ||
return customerAdapter.retrieveSelectedPaymentOption().map { result -> | ||
result?.toSavedSelection() | ||
}.toCustomerSheetDataResult() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...et/src/main/java/com/stripe/android/customersheet/data/CustomerSheetCombinedDataSource.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,5 @@ | ||
package com.stripe.android.customersheet.data | ||
|
||
internal interface CustomerSheetCombinedDataSource : | ||
CustomerSheetPaymentMethodDataSource, | ||
CustomerSheetSavedSelectionDataSource |
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
...c/main/java/com/stripe/android/customersheet/data/CustomerSheetPaymentMethodDataSource.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 | ||
|
||
/** | ||
* [CustomerSheetPaymentMethodDataSource] defines a set of operations for managing saved payment methods within a | ||
* [com.stripe.android.customersheet.CustomerSheet] context. | ||
*/ | ||
internal interface CustomerSheetPaymentMethodDataSource { | ||
/** | ||
* Retrieves a list of payment methods | ||
*/ | ||
suspend fun retrievePaymentMethods(): CustomerSheetDataResult<List<PaymentMethod>> | ||
} |
8 changes: 8 additions & 0 deletions
8
.../main/java/com/stripe/android/customersheet/data/CustomerSheetSavedSelectionDataSource.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,8 @@ | ||
package com.stripe.android.customersheet.data | ||
|
||
import com.stripe.android.paymentsheet.model.SavedSelection | ||
|
||
internal interface CustomerSheetSavedSelectionDataSource { | ||
|
||
suspend fun retrieveSavedSelection(): CustomerSheetDataResult<SavedSelection?> | ||
} |
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
95 changes: 95 additions & 0 deletions
95
...heet/src/test/java/com/stripe/android/customersheet/data/CustomerAdapterDataSourceTest.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,95 @@ | ||
package com.stripe.android.customersheet.data | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import com.stripe.android.customersheet.CustomerAdapter | ||
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi | ||
import com.stripe.android.customersheet.FakeCustomerAdapter | ||
import com.stripe.android.paymentsheet.model.SavedSelection | ||
import com.stripe.android.testing.PaymentMethodFactory | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
|
||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
class CustomerAdapterDataSourceTest { | ||
@Test | ||
fun `on retrieve payment methods, should completely successfully from adapter`() = runTest { | ||
val paymentMethods = PaymentMethodFactory.cards(size = 6) | ||
val dataSource = createCustomerAdapterDataSource( | ||
adapter = FakeCustomerAdapter( | ||
paymentMethods = CustomerAdapter.Result.success(paymentMethods), | ||
), | ||
) | ||
|
||
val successResult = dataSource.retrievePaymentMethods().asSuccess() | ||
|
||
assertThat(successResult.value).containsExactlyElementsIn(paymentMethods) | ||
} | ||
|
||
@Test | ||
fun `on retrieve payment methods, should fail from adapter`() = runTest { | ||
val dataSource = createCustomerAdapterDataSource( | ||
adapter = FakeCustomerAdapter( | ||
paymentMethods = CustomerAdapter.Result.failure( | ||
cause = IllegalStateException("Failed to retrieve!"), | ||
displayMessage = "Something went wrong!", | ||
), | ||
), | ||
) | ||
|
||
val failedResult = dataSource.retrievePaymentMethods().asFailure() | ||
|
||
assertThat(failedResult.cause).isInstanceOf(IllegalStateException::class.java) | ||
assertThat(failedResult.cause.message).isEqualTo("Failed to retrieve!") | ||
assertThat(failedResult.displayMessage).isEqualTo("Something went wrong!") | ||
} | ||
|
||
@Test | ||
fun `on retrieve payment option, should completely successfully from adapter`() = runTest { | ||
val paymentOptionId = "pm_1" | ||
val dataSource = createCustomerAdapterDataSource( | ||
adapter = FakeCustomerAdapter( | ||
selectedPaymentOption = CustomerAdapter.Result.success( | ||
value = CustomerAdapter.PaymentOption.fromId(paymentOptionId), | ||
), | ||
), | ||
) | ||
|
||
val successResult = dataSource.retrieveSavedSelection().asSuccess() | ||
|
||
assertThat(successResult.value).isEqualTo(SavedSelection.PaymentMethod(paymentOptionId)) | ||
} | ||
|
||
@Test | ||
fun `on retrieve payment option, should fail from adapter`() = runTest { | ||
val dataSource = createCustomerAdapterDataSource( | ||
adapter = FakeCustomerAdapter( | ||
selectedPaymentOption = CustomerAdapter.Result.failure( | ||
cause = IllegalStateException("Failed to retrieve!"), | ||
displayMessage = "Something went wrong!", | ||
) | ||
) | ||
) | ||
|
||
val failedResult = dataSource.retrieveSavedSelection().asFailure() | ||
|
||
assertThat(failedResult.cause).isInstanceOf(IllegalStateException::class.java) | ||
assertThat(failedResult.cause.message).isEqualTo("Failed to retrieve!") | ||
assertThat(failedResult.displayMessage).isEqualTo("Something went wrong!") | ||
} | ||
|
||
private fun createCustomerAdapterDataSource( | ||
adapter: CustomerAdapter = FakeCustomerAdapter(), | ||
): CustomerAdapterDataSource { | ||
return CustomerAdapterDataSource( | ||
customerAdapter = adapter, | ||
) | ||
} | ||
|
||
private fun <T> CustomerSheetDataResult<T>.asSuccess(): CustomerSheetDataResult.Success<T> { | ||
return this as CustomerSheetDataResult.Success<T> | ||
} | ||
|
||
private fun <T> CustomerSheetDataResult<T>.asFailure(): CustomerSheetDataResult.Failure<T> { | ||
return this as CustomerSheetDataResult.Failure<T> | ||
} | ||
} |