-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update PaymentSheet card input to use compose (#4358)
- Loading branch information
1 parent
fb45794
commit 397b486
Showing
178 changed files
with
3,464 additions
and
1,846 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
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
4 changes: 3 additions & 1 deletion
4
payments-core/src/main/java/com/stripe/android/cards/CardAccountRangeRepository.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
103 changes: 103 additions & 0 deletions
103
payments-core/src/main/java/com/stripe/android/cards/CardAccountRangeService.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,103 @@ | ||
package com.stripe.android.cards | ||
|
||
import androidx.annotation.RestrictTo | ||
import androidx.annotation.VisibleForTesting | ||
import com.stripe.android.model.AccountRange | ||
import com.stripe.android.model.CardBrand | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
class CardAccountRangeService constructor( | ||
private val cardAccountRangeRepository: CardAccountRangeRepository, | ||
private val workContext: CoroutineContext, | ||
val staticCardAccountRanges: StaticCardAccountRanges, | ||
private val accountRangeResultListener: AccountRangeResultListener | ||
) { | ||
|
||
val isLoading: Flow<Boolean> = cardAccountRangeRepository.loading | ||
|
||
var accountRange: AccountRange? = null | ||
private set | ||
|
||
@VisibleForTesting | ||
var accountRangeRepositoryJob: Job? = null | ||
|
||
fun onCardNumberChanged(cardNumber: CardNumber.Unvalidated) { | ||
val staticAccountRange = staticCardAccountRanges.filter(cardNumber) | ||
.let { accountRanges -> | ||
if (accountRanges.size == 1) { | ||
accountRanges.first() | ||
} else { | ||
null | ||
} | ||
} | ||
if (staticAccountRange == null || shouldQueryRepository(staticAccountRange)) { | ||
// query for AccountRange data | ||
queryAccountRangeRepository(cardNumber) | ||
} else { | ||
// use static AccountRange data | ||
updateAccountRangeResult(staticAccountRange) | ||
} | ||
} | ||
|
||
@JvmSynthetic | ||
fun queryAccountRangeRepository(cardNumber: CardNumber.Unvalidated) { | ||
if (shouldQueryAccountRange(cardNumber)) { | ||
// cancel in-flight job | ||
cancelAccountRangeRepositoryJob() | ||
|
||
// invalidate accountRange before fetching | ||
accountRange = null | ||
|
||
accountRangeRepositoryJob = CoroutineScope(workContext).launch { | ||
val bin = cardNumber.bin | ||
val accountRange = if (bin != null) { | ||
cardAccountRangeRepository.getAccountRange(cardNumber) | ||
} else { | ||
null | ||
} | ||
|
||
withContext(Dispatchers.Main) { | ||
updateAccountRangeResult(accountRange) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun cancelAccountRangeRepositoryJob() { | ||
accountRangeRepositoryJob?.cancel() | ||
accountRangeRepositoryJob = null | ||
} | ||
|
||
@JvmSynthetic | ||
fun updateAccountRangeResult( | ||
newAccountRange: AccountRange? | ||
) { | ||
accountRange = newAccountRange | ||
accountRangeResultListener.onAccountRangeResult(accountRange) | ||
} | ||
|
||
private fun shouldQueryRepository( | ||
accountRange: AccountRange | ||
) = when (accountRange.brand) { | ||
CardBrand.Unknown, | ||
CardBrand.UnionPay -> true | ||
else -> false | ||
} | ||
|
||
private fun shouldQueryAccountRange(cardNumber: CardNumber.Unvalidated): Boolean { | ||
return accountRange == null || | ||
cardNumber.bin == null || | ||
accountRange?.binRange?.matches(cardNumber) == false | ||
} | ||
|
||
interface AccountRangeResultListener { | ||
fun onAccountRangeResult(newAccountRange: AccountRange?) | ||
} | ||
} |
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
4 changes: 3 additions & 1 deletion
4
payments-core/src/main/java/com/stripe/android/cards/StaticCardAccountRangeSource.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
Oops, something went wrong.