Skip to content

Commit

Permalink
Make code improvements and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Mar 28, 2023
1 parent 47afd87 commit 1c7bde1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ constructor(
* SetupFutureUsage is considered to be set if it is on or off session.
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
private fun isTopLevelSetupFutureUsageSet() =
fun isTopLevelSetupFutureUsageSet() =
when (setupFutureUsage) {
StripeIntent.Usage.OnSession -> true
StripeIntent.Usage.OffSession -> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,36 @@ class PaymentIntentTest {
assertThat(PaymentIntent.ClientSecret("pi_a1b2c3_secret_x7y8z9").value)
.isEqualTo("pi_a1b2c3_secret_x7y8z9")
}

@Test
fun `Determines LPM-level SFU correctly if setup_future_usage exists`() {
val paymentIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy(
paymentMethodOptionsJsonString = """
{
"card": {
"setup_future_usage": ""
}
}
""".trimIndent()
)

val result = paymentIntent.isLpmLevelSetupFutureUsageSet("card")
assertThat(result).isTrue()
}

@Test
fun `Determines LPM-level SFU correctly if setup_future_usage does not exist`() {
val paymentIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy(
paymentMethodOptionsJsonString = """
{
"card": {
"some_other_key_that_has_nothing_to_do_with_sfu": ""
}
}
""".trimIndent()
)

val result = paymentIntent.isLpmLevelSetupFutureUsageSet("card")
assertThat(result).isFalse()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ internal fun SupportedPaymentMethod.getSpecWithFullfilledRequirements(

return when (stripeIntent) {
is PaymentIntent -> {
if (stripeIntent.isLpmLevelSetupFutureUsageSet(code)) {
val isSetupFutureUsageSet = stripeIntent.isTopLevelSetupFutureUsageSet() ||
stripeIntent.isLpmLevelSetupFutureUsageSet(code)

if (isSetupFutureUsageSet) {
if (supportsPaymentIntentSfuSet(stripeIntent, config)) {
merchantRequestedSave
} else {
Expand Down

0 comments on commit 1c7bde1

Please sign in to comment.