Skip to content
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

Don't allow PaymentSheet.Configuration to be null. #7625

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion link/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ID>MaxLineLength:InjectableActivityScenario.kt$InjectableActivityScenario$delegate ?: throw IllegalStateException("Cannot move to state $newState since the activity hasn't been launched.")</ID>
<ID>MaxLineLength:InjectableActivityScenario.kt$InjectableActivityScenario$val d = delegate ?: throw IllegalStateException("Cannot run onActivity since the activity hasn't been launched.")</ID>
<ID>MaxLineLength:LinkActivityResultTest.kt$LinkActivityResultTest$"link-popup://complete?link_status=complete&amp;pm=eyJpZCI6InBtXzFOSmVFckx1NW8zUDE4WnBtWHBDdElyUiIsIm9iamVjdCI6InBheW1lbnRfbWV0aG9kIiwiYmlsbGluZ19kZXRhaWxzIjp7ImFkZHJlc3MiOnsiY2l0eSI6bnVsbCwiY291bnRyeSI6bnVsbCwibGluZTEiOm51bGwsImxpbmUyIjpudWxsLCJwb3N0YWxfY29kZSI6bnVsbCwic3RhdGUiOm51bGx9LCJlbWFpbCI6bnVsbCwibmFtZSI6bnVsbCwicGhvbmUiOm51bGx9LCJjYXJkIjp7ImJyYW5kIjoidmlzYSIsImNoZWNrcyI6eyJhZGRyZXNzX2xpbmUxX2NoZWNrIjpudWxsLCJhZGRyZXNzX3Bvc3RhbF9jb2RlX2NoZWNrIjpudWxsLCJjdmNfY2hlY2siOm51bGx9LCJjb3VudHJ5IjpudWxsLCJleHBfbW9udGgiOjEyLCJleHBfeWVhciI6MjAzNCwiZnVuZGluZyI6ImNyZWRpdCIsImdlbmVyYXRlZF9mcm9tIjpudWxsLCJsYXN0NCI6IjAwMDAiLCJuZXR3b3JrcyI6eyJhdmFpbGFibGUiOlsidmlzYSJdLCJwcmVmZXJyZWQiOm51bGx9LCJ0aHJlZV9kX3NlY3VyZV91c2FnZSI6eyJzdXBwb3J0ZWQiOnRydWV9LCJ3YWxsZXQiOnsiZHluYW1pY19sYXN0NCI6bnVsbCwibGluayI6e30sInR5cGUiOiJsaW5rIn19LCJjcmVhdGVkIjoxNjg2OTI4MDIxLCJjdXN0b21lciI6bnVsbCwibGl2ZW1vZGUiOmZhbHNlLCJ0eXBlIjoiY2FyZCJ9ICAg"</ID>
<ID>NoUnusedImports:com.stripe.android.link.theme.Theme.kt:9</ID>
<ID>SwallowedException:LinkActivityResult.kt$e: Exception</ID>
<ID>TooGenericExceptionCaught:LinkActivityResult.kt$e: Exception</ID>
<ID>TooManyFunctions:DefaultLinkEventsReporter.kt$DefaultLinkEventsReporter : LinkEventsReporter</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const val IS_PAYMENT_INTENT = "isPaymentIntent"
*/
const val IS_INSTANT_APP = "isInstantApp"

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
const val APP_NAME = "appName"

/**
* Status bar color of the host activity.
*/
Expand Down
3 changes: 1 addition & 2 deletions paymentsheet/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<ID>LongMethod:FormViewModelTest.kt$FormViewModelTest$@Test fun `Verify params are set when required address fields are complete`()</ID>
<ID>LongMethod:PaymentOptionFactory.kt$PaymentOptionFactory$fun create(selection: PaymentSelection): PaymentOption</ID>
<ID>LongMethod:PaymentSheetConfigurationKtx.kt$internal fun PaymentSheet.Appearance.parseAppearance()</ID>
<ID>LongMethod:PaymentSheetLoader.kt$DefaultPaymentSheetLoader$private suspend fun create( elementsSession: ElementsSession, config: PaymentSheet.Configuration?, isGooglePayReady: Boolean, ): PaymentSheetState.Full</ID>
<ID>LongMethod:PaymentSheetLoader.kt$DefaultPaymentSheetLoader$private suspend fun create( elementsSession: ElementsSession, config: PaymentSheet.Configuration, isGooglePayReady: Boolean, ): PaymentSheetState.Full</ID>
<ID>LongMethod:PlaceholderHelperTest.kt$PlaceholderHelperTest$@Test fun `Test correct placeholder is removed for placeholder spec`()</ID>
<ID>LongMethod:USBankAccountEmitters.kt$@Composable internal fun USBankAccountEmitters( viewModel: USBankAccountFormViewModel, usBankAccountFormArgs: USBankAccountFormArguments, )</ID>
<ID>LongMethod:USBankAccountForm.kt$@Composable internal fun BillingDetailsForm( formArgs: FormArguments, isProcessing: Boolean, isPaymentFlow: Boolean, nameController: TextFieldController, emailController: TextFieldController, phoneController: PhoneNumberController, addressController: AddressController, lastTextFieldIdentifier: IdentifierSpec?, sameAsShippingElement: SameAsShippingElement?, )</ID>
Expand All @@ -55,7 +55,6 @@
<ID>MagicNumber:StateFlows.kt$5</ID>
<ID>MagicNumber:StateFlows.kt$6</ID>
<ID>MagicNumber:USBankAccountForm.kt$0.5f</ID>
<ID>MaxLineLength:CustomerAdapterTest.kt$CustomerAdapterTest$fun</ID>
<ID>MaxLineLength:CustomerRepositoryTest.kt$CustomerRepositoryTest$fun</ID>
<ID>MaxLineLength:CustomerSheetViewModelTest.kt$CustomerSheetViewModelTest$fun</ID>
<ID>MaxLineLength:CustomerSheetViewModelTest.kt$CustomerSheetViewModelTest$publishableKey = "pk_test_51HvTI7Lu5o3livep6t5AgBSkMvWoTtA0nyA7pVYDqpfLkRtWun7qZTYCOHCReprfLM464yaBeF72UFfB7cY9WG4a00ZnDtiC2C"</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal class DefaultPaymentSheetLauncher(
) {
val args = PaymentSheetContractV2.Args(
initializationMode = mode,
config = configuration,
config = configuration ?: PaymentSheet.Configuration.default(activity),
statusBarColor = activity.window?.statusBarColor,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal class PaymentOptionsViewModel @Inject constructor(
) : BaseSheetViewModel(
application = application,
config = args.state.config,
prefsRepository = prefsRepositoryFactory(args.state.config?.customer),
prefsRepository = prefsRepositoryFactory(args.state.config.customer),
eventReporter = eventReporter,
customerRepository = customerRepository,
workContext = workContext,
Expand Down Expand Up @@ -140,7 +140,7 @@ internal class PaymentOptionsViewModel @Inject constructor(

cbcEligibility = when (args.state.isEligibleForCardBrandChoice) {
true -> CardBrandChoiceEligibility.Eligible(
preferredNetworks = args.state.config?.preferredNetworks ?: listOf()
preferredNetworks = args.state.config.preferredNetworks
)
false -> CardBrandChoiceEligibility.Ineligible
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ class PaymentSheet internal constructor(
preferredNetworks = preferredNetworks
)
}

internal companion object {
fun default(context: Context): Configuration {
val appName = context.applicationInfo.loadLabel(context.packageManager).toString()
return Configuration(appName)
}
}
}

@Parcelize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ internal class PaymentSheetActivity : BaseSheetActivity<PaymentSheetResult>() {
} else {
try {
starterArgs.initializationMode.validate()
starterArgs.config?.validate()
starterArgs.config?.appearance?.parseAppearance()
starterArgs.config.validate()
starterArgs.config.appearance.parseAppearance()
Result.success(starterArgs)
} catch (e: InvalidParameterException) {
Result.failure(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PaymentSheetContract :
): Intent {
val statusBarColor = (context as? Activity)?.window?.statusBarColor
val args = input.copy(statusBarColor = statusBarColor)
return Intent(context, PaymentSheetActivity::class.java).putExtra(EXTRA_ARGS, args.toV2())
return Intent(context, PaymentSheetActivity::class.java).putExtra(EXTRA_ARGS, args.toV2(context))
}

override fun parseResult(
Expand All @@ -54,7 +54,7 @@ class PaymentSheetContract :

val googlePayConfig: PaymentSheet.GooglePayConfiguration? get() = config?.googlePay

internal fun toV2(): PaymentSheetContractV2.Args {
internal fun toV2(context: Context): PaymentSheetContractV2.Args {
return PaymentSheetContractV2.Args(
initializationMode = when (clientSecret) {
is PaymentIntentClientSecret -> {
Expand All @@ -64,7 +64,7 @@ class PaymentSheetContract :
PaymentSheet.InitializationMode.SetupIntent(clientSecret.value)
}
},
config = config,
config = config ?: PaymentSheet.Configuration.default(context),
statusBarColor = statusBarColor,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ internal class PaymentSheetContractV2 :
@Parcelize
data class Args(
internal val initializationMode: PaymentSheet.InitializationMode,
internal val config: PaymentSheet.Configuration?,
internal val config: PaymentSheet.Configuration,
@ColorInt internal val statusBarColor: Int?,
) : ActivityStarter.Args {

val googlePayConfig: PaymentSheet.GooglePayConfiguration? get() = config?.googlePay
val googlePayConfig: PaymentSheet.GooglePayConfiguration? get() = config.googlePay

companion object {
internal fun fromIntent(intent: Intent): Args? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ internal class PaymentSheetViewModel @Inject internal constructor(
private fun handlePaymentSheetStateLoaded(state: PaymentSheetState.Full) {
cbcEligibility = when (state.isEligibleForCardBrandChoice) {
true -> CardBrandChoiceEligibility.Eligible(
preferredNetworks = state.config?.preferredNetworks ?: listOf()
preferredNetworks = state.config.preferredNetworks
)
false -> CardBrandChoiceEligibility.Ineligible
}
Expand Down Expand Up @@ -494,7 +494,7 @@ internal class PaymentSheetViewModel @Inject internal constructor(
val nextStep = intentConfirmationInterceptor.intercept(
initializationMode = args.initializationMode,
paymentSelection = paymentSelection,
shippingValues = args.config?.shippingDetails?.toConfirmPaymentIntentShipping(),
shippingValues = args.config.shippingDetails?.toConfirmPaymentIntentShipping(),
)

deferredIntentConfirmationType = nextStep.deferredIntentConfirmationType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class DefaultEventReporter @Inject internal constructor(
) : EventReporter {

override fun onInit(
configuration: PaymentSheet.Configuration?,
configuration: PaymentSheet.Configuration,
isDecoupling: Boolean,
) {
fireEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal interface EventReporter {
* PaymentSheet has been instantiated or FlowController has finished its configuration.
*/
fun onInit(
configuration: PaymentSheet.Configuration?,
configuration: PaymentSheet.Configuration,
isDecoupling: Boolean,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,55 +47,55 @@ internal sealed class PaymentSheetEvent : AnalyticsEvent {

class Init(
private val mode: EventReporter.Mode,
private val configuration: PaymentSheet.Configuration?,
private val configuration: PaymentSheet.Configuration,
override val isDecoupled: Boolean,
) : PaymentSheetEvent() {

override val eventName: String
get() {
val configValue = listOfNotNull(
FIELD_CUSTOMER.takeIf { configuration?.customer != null },
FIELD_GOOGLE_PAY.takeIf { configuration?.googlePay != null }
FIELD_CUSTOMER.takeIf { configuration.customer != null },
FIELD_GOOGLE_PAY.takeIf { configuration.googlePay != null }
).takeUnless { it.isEmpty() }?.joinToString(separator = "_") ?: "default"
return formatEventName(mode, "init_$configValue")
}

override val additionalParams: Map<String, Any?>
get() {
val primaryButtonConfig = configuration?.appearance?.primaryButton
val primaryButtonConfig = configuration.appearance.primaryButton
val primaryButtonConfigMap = mapOf(
FIELD_COLORS_LIGHT to (
primaryButtonConfig?.colorsLight
primaryButtonConfig.colorsLight
!= PaymentSheet.PrimaryButtonColors.defaultLight
),
FIELD_COLORS_DARK to (
primaryButtonConfig?.colorsDark
primaryButtonConfig.colorsDark
!= PaymentSheet.PrimaryButtonColors.defaultDark
),
FIELD_CORNER_RADIUS to (primaryButtonConfig?.shape?.cornerRadiusDp != null),
FIELD_BORDER_WIDTH to (primaryButtonConfig?.shape?.borderStrokeWidthDp != null),
FIELD_FONT to (primaryButtonConfig?.typography?.fontResId != null)
FIELD_CORNER_RADIUS to (primaryButtonConfig.shape.cornerRadiusDp != null),
FIELD_BORDER_WIDTH to (primaryButtonConfig.shape.borderStrokeWidthDp != null),
FIELD_FONT to (primaryButtonConfig.typography.fontResId != null)
)
val appearanceConfigMap = mutableMapOf(
FIELD_COLORS_LIGHT to (
configuration?.appearance?.colorsLight
configuration.appearance.colorsLight
!= PaymentSheet.Colors.defaultLight
),
FIELD_COLORS_DARK to (
configuration?.appearance?.colorsDark
configuration.appearance.colorsDark
!= PaymentSheet.Colors.defaultDark
),
FIELD_CORNER_RADIUS to (
configuration?.appearance?.shapes?.cornerRadiusDp
configuration.appearance.shapes.cornerRadiusDp
!= StripeThemeDefaults.shapes.cornerRadius
),
FIELD_BORDER_WIDTH to (
configuration?.appearance?.shapes?.borderStrokeWidthDp
configuration.appearance.shapes.borderStrokeWidthDp
!= StripeThemeDefaults.shapes.borderStrokeWidth
),
FIELD_FONT to (configuration?.appearance?.typography?.fontResId != null),
FIELD_FONT to (configuration.appearance.typography.fontResId != null),
FIELD_SIZE_SCALE_FACTOR to (
configuration?.appearance?.typography?.sizeScaleFactor
configuration.appearance.typography.sizeScaleFactor
!= StripeThemeDefaults.typography.fontSizeMultiplier
),
FIELD_PRIMARY_BUTTON to primaryButtonConfigMap
Expand All @@ -110,33 +110,33 @@ internal sealed class PaymentSheetEvent : AnalyticsEvent {

val billingDetailsCollectionConfigMap = mapOf(
FIELD_ATTACH_DEFAULTS to configuration
?.billingDetailsCollectionConfiguration
?.attachDefaultsToPaymentMethod,
.billingDetailsCollectionConfiguration
.attachDefaultsToPaymentMethod,
FIELD_COLLECT_NAME to configuration
?.billingDetailsCollectionConfiguration
?.name
?.name,
.billingDetailsCollectionConfiguration
.name
.name,
FIELD_COLLECT_EMAIL to configuration
?.billingDetailsCollectionConfiguration
?.email
?.name,
.billingDetailsCollectionConfiguration
.email
.name,
FIELD_COLLECT_PHONE to configuration
?.billingDetailsCollectionConfiguration
?.phone
?.name,
.billingDetailsCollectionConfiguration
.phone
.name,
FIELD_COLLECT_ADDRESS to configuration
?.billingDetailsCollectionConfiguration
?.address
?.name,
.billingDetailsCollectionConfiguration
.address
.name,
)

@Suppress("DEPRECATION")
val configurationMap = mapOf(
FIELD_CUSTOMER to (configuration?.customer != null),
FIELD_GOOGLE_PAY to (configuration?.googlePay != null),
FIELD_PRIMARY_BUTTON_COLOR to (configuration?.primaryButtonColor != null),
FIELD_BILLING to (configuration?.defaultBillingDetails != null),
FIELD_DELAYED_PMS to (configuration?.allowsDelayedPaymentMethods),
FIELD_CUSTOMER to (configuration.customer != null),
FIELD_GOOGLE_PAY to (configuration.googlePay != null),
FIELD_PRIMARY_BUTTON_COLOR to (configuration.primaryButtonColor != null),
FIELD_BILLING to (configuration.defaultBillingDetails != null),
FIELD_DELAYED_PMS to (configuration.allowsDelayedPaymentMethods),
FIELD_APPEARANCE to appearanceConfigMap,
FIELD_BILLING_DETAILS_COLLECTION_CONFIGURATION to
billingDetailsCollectionConfigMap,
Expand Down
Loading
Loading