Skip to content

Commit

Permalink
Add requirements data to PaymentMethodDefinition. (#7867)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Feb 2, 2024
1 parent c0098ea commit 4ca235c
Show file tree
Hide file tree
Showing 36 changed files with 270 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.stripe.android.utils
import android.content.Context
import com.stripe.android.lpmfoundations.luxe.LpmRepository
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.Address
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.PaymentMethod
import com.stripe.android.testing.PaymentIntentFactory
import com.stripe.android.ui.core.BillingDetailsCollectionConfiguration
Expand All @@ -24,7 +26,12 @@ internal fun initializedLpmRepository(context: Context): LpmRepository {
"verification_method": "automatic"
}
}
""".trimIndent()
""".trimIndent(),
).copy(
shipping = PaymentIntent.Shipping(
name = "Example buyer",
address = Address(line1 = "123 Main st.", country = "US", postalCode = "12345"),
)
),
billingDetailsCollectionConfiguration = BillingDetailsCollectionConfiguration(),
allowsDelayedPaymentMethods = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.annotation.RestrictTo
import androidx.annotation.VisibleForTesting
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodRegistry
import com.stripe.android.lpmfoundations.paymentmethod.isSupported
import com.stripe.android.model.LuxePostConfirmActionRepository
import com.stripe.android.model.PaymentMethodCode
import com.stripe.android.model.StripeIntent
Expand Down Expand Up @@ -152,8 +153,13 @@ class LpmRepository(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec,
): SupportedPaymentMethod? {
return PaymentMethodRegistry.definitionsByCode[sharedDataSpec.type]
?.supportedPaymentMethod(metadata, sharedDataSpec)
val paymentMethodDefinition = PaymentMethodRegistry.definitionsByCode[sharedDataSpec.type]

if (paymentMethodDefinition?.isSupported(metadata) == true) {
return paymentMethodDefinition.supportedPaymentMethod(metadata, sharedDataSpec)
}

return null
}

@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@ internal interface PaymentMethodDefinition {
*/
val type: PaymentMethod.Type

val supportedAsSavedPaymentMethod: Boolean

/**
* The requirements that need to be met in order for this Payment Method to be available for selection by the buyer.
* For example emptySet() if no requirements exist.
* Or setOf(AddPaymentMethodRequirement.MerchantSupportsDelayedPaymentMethods) if the payment method requires the
* merchant to provide a PaymentSheet.Configuration with delayed payment methods enabled.
*/
// TODO(jaynewstrom-stripe): Will be added back in a follow up.
// fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement>
fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement>

fun supportedPaymentMethod(metadata: PaymentMethodMetadata, sharedDataSpec: SharedDataSpec): SupportedPaymentMethod
}

fun supportedPaymentMethod(metadata: PaymentMethodMetadata, sharedDataSpec: SharedDataSpec): SupportedPaymentMethod?
internal fun PaymentMethodDefinition.isSupported(metadata: PaymentMethodMetadata): Boolean {
val requirements = addRequirement(metadata.hasIntentToSetup())
for (requirement in requirements) {
if (!requirement.meetsRequirements(metadata)) {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.AffirmRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,13 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object AffirmDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Affirm

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.ShippingAddress,
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.AfterpayClearpayRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -13,6 +14,13 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object AfterpayClearpayDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.AfterpayClearpay

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.ShippingAddress,
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.AlipayRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object AlipayDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Alipay

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.AlmaRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object AlmaDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Alma

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.AmazonPayRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object AmazonPayDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.AmazonPay

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.AuBecsDebitRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object AuBecsDebitDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.AuBecsDebit

override val supportedAsSavedPaymentMethod: Boolean = true

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.MerchantSupportsDelayedPaymentMethods,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.BacsDebitRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -16,6 +17,13 @@ import com.stripe.android.uicore.elements.IdentifierSpec
internal object BacsDebitDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.BacsDebit

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.MerchantSupportsDelayedPaymentMethods,
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.BancontactRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -13,6 +14,12 @@ import com.stripe.android.uicore.elements.IdentifierSpec
internal object BancontactDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Bancontact

override val supportedAsSavedPaymentMethod: Boolean = true

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOfNotNull(
AddPaymentMethodRequirement.MerchantSupportsDelayedPaymentMethods.takeIf { hasIntentToSetup },
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.BlikRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object BlikDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Blik

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.BoletoRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object BoletoDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Boleto

override val supportedAsSavedPaymentMethod: Boolean = true

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.MerchantSupportsDelayedPaymentMethods,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.CardRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -17,6 +18,10 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object CardDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Card

override val supportedAsSavedPaymentMethod: Boolean = true

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf()

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.CashAppPayRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -13,6 +14,10 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object CashAppPayDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.CashAppPay

override val supportedAsSavedPaymentMethod: Boolean = true

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf()

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.EpsRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object EpsDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Eps

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.FpxRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object FpxDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Fpx

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.lpmfoundations.paymentmethod.definitions

import com.stripe.android.lpmfoundations.luxe.GiropayRequirement
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.lpmfoundations.paymentmethod.AddPaymentMethodRequirement
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodDefinition
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.model.PaymentMethod
Expand All @@ -12,6 +13,12 @@ import com.stripe.android.ui.core.elements.SharedDataSpec
internal object GiroPayDefinition : PaymentMethodDefinition {
override val type: PaymentMethod.Type = PaymentMethod.Type.Giropay

override val supportedAsSavedPaymentMethod: Boolean = false

override fun addRequirement(hasIntentToSetup: Boolean): Set<AddPaymentMethodRequirement> = setOf(
AddPaymentMethodRequirement.UnsupportedForSetup,
)

override fun supportedPaymentMethod(
metadata: PaymentMethodMetadata,
sharedDataSpec: SharedDataSpec
Expand Down
Loading

0 comments on commit 4ca235c

Please sign in to comment.