-
Notifications
You must be signed in to change notification settings - Fork 640
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
Add initial CustomerSheet
data sources & use in CustomerSheet
#9278
Open
samer-stripe
wants to merge
4
commits into
master
Choose a base branch
from
samer/customer-sheet-data-source
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−8
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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, | ||
) : CustomerSheetSavedSelectionDataSource, CustomerSheetPaymentMethodDataSource { | ||
samer-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() | ||
} | ||
} |
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> { | ||
samer-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return when (this) { | ||
is CustomerAdapter.Result.Success -> CustomerSheetDataResult.success(value) | ||
is CustomerAdapter.Result.Failure -> CustomerSheetDataResult.failure( | ||
cause = cause, | ||
displayMessage = displayMessage, | ||
) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...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,16 @@ | ||
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 | ||
* | ||
* @return a result containing the list of payment methods if operation was successful | ||
*/ | ||
suspend fun retrievePaymentMethods(): CustomerSheetDataResult<List<PaymentMethod>> | ||
} |
16 changes: 16 additions & 0 deletions
16
.../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,16 @@ | ||
package com.stripe.android.customersheet.data | ||
|
||
import com.stripe.android.paymentsheet.model.SavedSelection | ||
|
||
/** | ||
* [CustomerSheetSavedSelectionDataSource] defines a set of operations for managing saved payment selections within a | ||
* [com.stripe.android.customersheet.CustomerSheet] context. | ||
*/ | ||
internal interface CustomerSheetSavedSelectionDataSource { | ||
/** | ||
* Retrieves a saved selection | ||
* | ||
* @return a result containing the saved selection if operation was successful | ||
*/ | ||
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
113 changes: 113 additions & 0 deletions
113
...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,113 @@ | ||
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.isInstanceOf | ||
import com.stripe.android.model.PaymentMethod | ||
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 complete successfully from adapter`() = runTest { | ||
val paymentMethods = PaymentMethodFactory.cards(size = 6) | ||
val dataSource = createCustomerAdapterDataSource( | ||
adapter = FakeCustomerAdapter( | ||
paymentMethods = CustomerAdapter.Result.success(paymentMethods), | ||
), | ||
) | ||
|
||
val result = dataSource.retrievePaymentMethods() | ||
|
||
assertThat(result).isInstanceOf<CustomerSheetDataResult.Success<List<PaymentMethod>>>() | ||
|
||
val successResult = result.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 result = dataSource.retrievePaymentMethods() | ||
|
||
assertThat(result).isInstanceOf<CustomerSheetDataResult.Failure<List<PaymentMethod>>>() | ||
|
||
val failedResult = result.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 complete successfully from adapter`() = runTest { | ||
val paymentOptionId = "pm_1" | ||
val dataSource = createCustomerAdapterDataSource( | ||
adapter = FakeCustomerAdapter( | ||
selectedPaymentOption = CustomerAdapter.Result.success( | ||
value = CustomerAdapter.PaymentOption.fromId(paymentOptionId), | ||
), | ||
), | ||
) | ||
|
||
val result = dataSource.retrieveSavedSelection() | ||
|
||
assertThat(result).isInstanceOf<CustomerSheetDataResult.Success<SavedSelection?>>() | ||
|
||
val successResult = result.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 result = dataSource.retrieveSavedSelection() | ||
|
||
assertThat(result).isInstanceOf<CustomerSheetDataResult.Failure<SavedSelection?>>() | ||
|
||
val failedResult = result.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> | ||
} | ||
} |
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.
Changes here are covered by the pre-existing
CustomerSheet
tests we added recently.