Skip to content

Commit

Permalink
Remove deprecated BroadcastReceiver logic from PaymentFlowActivity
Browse files Browse the repository at this point in the history
This is a breaking change.

Implement `PaymentSessionConfig.shippingInformationValidator` and
`PaymentSessionConfig.shippingMethodsFactory` to replace logic
previously handled via broadcast.

See migration guide for more info.
  • Loading branch information
mshafrir-stripe committed Dec 23, 2019
1 parent 9aae2a9 commit 52c6e81
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 226 deletions.
25 changes: 18 additions & 7 deletions stripe/src/main/java/com/stripe/android/PaymentSessionConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.stripe.android.model.ShippingMethod
import com.stripe.android.view.AddPaymentMethodActivity
import com.stripe.android.view.BillingAddressFields
import com.stripe.android.view.PaymentFlowActivity
import com.stripe.android.view.PaymentFlowExtras
import com.stripe.android.view.SelectShippingMethodWidget
import com.stripe.android.view.ShippingInfoWidget
import com.stripe.android.view.ShippingInfoWidget.CustomizableShippingField
Expand All @@ -37,7 +36,7 @@ data class PaymentSessionConfig internal constructor(
val billingAddressFields: BillingAddressFields = BillingAddressFields.None,

internal val shouldPrefetchCustomer: Boolean = true,
internal val shippingInformationValidator: ShippingInformationValidator? = null,
internal val shippingInformationValidator: ShippingInformationValidator = DefaultShippingInfoValidator(),
internal val shippingMethodsFactory: ShippingMethodsFactory? = null,
internal val windowFlags: Int? = null
) : Parcelable {
Expand All @@ -51,7 +50,7 @@ data class PaymentSessionConfig internal constructor(
}
}

if (shippingInformationValidator != null && isShippingMethodRequired) {
if (isShippingMethodRequired) {
requireNotNull(shippingMethodsFactory) {
"""
If isShippingMethodRequired is true and a ShippingInformationValidator is provided,
Expand Down Expand Up @@ -217,9 +216,7 @@ data class PaymentSessionConfig internal constructor(
}

/**
* @param shippingInformationValidator if specified, will be used to validate
* [ShippingInformation] in [PaymentFlowActivity] instead of sending a broadcast with
* [PaymentFlowExtras.EVENT_SHIPPING_INFO_SUBMITTED].
* @param shippingInformationValidator used to validate [ShippingInformation] in [PaymentFlowActivity]
*
* Note: this instance must be [Serializable].
*/
Expand Down Expand Up @@ -263,12 +260,26 @@ data class PaymentSessionConfig internal constructor(
paymentMethodTypes = paymentMethodTypes,
shouldShowGooglePay = shouldShowGooglePay,
allowedShippingCountryCodes = allowedShippingCountryCodes,
shippingInformationValidator = shippingInformationValidator,
shippingInformationValidator = shippingInformationValidator
?: DefaultShippingInfoValidator(),
shippingMethodsFactory = shippingMethodsFactory,
windowFlags = windowFlags,
billingAddressFields = billingAddressFields,
shouldPrefetchCustomer = shouldPrefetchCustomer
)
}
}

/**
* A [ShippingInformationValidator] that accepts any [ShippingInformation] as valid.
*/
private class DefaultShippingInfoValidator : ShippingInformationValidator {
override fun isValid(shippingInformation: ShippingInformation): Boolean {
return true
}

override fun getErrorMessage(shippingInformation: ShippingInformation): String {
return ""
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.stripe.android.view

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.viewpager.widget.ViewPager
import com.stripe.android.CustomerSession
import com.stripe.android.PaymentSession.Companion.EXTRA_PAYMENT_SESSION_DATA
Expand All @@ -32,8 +28,6 @@ class PaymentFlowActivity : StripeActivity() {
private lateinit var customerSession: CustomerSession
private lateinit var viewModel: PaymentFlowViewModel

private var shippingInfoSubmittedBroadcastReceiver: BroadcastReceiver? = null

private val keyboardController: KeyboardController by lazy {
KeyboardController(this)
}
Expand Down Expand Up @@ -84,40 +78,9 @@ class PaymentFlowActivity : StripeActivity() {
}
})

if (paymentSessionConfig.shippingInformationValidator == null) {
shippingInfoSubmittedBroadcastReceiver = createShippingInfoSubmittedBroadcastReceiver()
}

title = paymentFlowPagerAdapter.getPageTitle(shipping_flow_viewpager.currentItem)
}

private fun createShippingInfoSubmittedBroadcastReceiver(): BroadcastReceiver {
return object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val isShippingInfoValid = intent.getBooleanExtra(
PaymentFlowExtras.EXTRA_IS_SHIPPING_INFO_VALID,
false)
if (isShippingInfoValid) {
val shippingMethods: List<ShippingMethod>? =
intent.getParcelableArrayListExtra(
PaymentFlowExtras.EXTRA_VALID_SHIPPING_METHODS
)
val defaultShippingMethod: ShippingMethod? = intent
.getParcelableExtra(PaymentFlowExtras.EXTRA_DEFAULT_SHIPPING_METHOD)

onShippingInfoValidated(
shippingMethods.orEmpty(),
defaultShippingMethod
)
} else {
val errorMessage =
intent.getStringExtra(PaymentFlowExtras.EXTRA_SHIPPING_INFO_ERROR)
onShippingInfoError(errorMessage)
}
}
}
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
shipping_flow_viewpager.currentItem = savedInstanceState.getInt(STATE_CURRENT_ITEM, 0)
Expand All @@ -132,25 +95,6 @@ class PaymentFlowActivity : StripeActivity() {
}
}

override fun onPause() {
super.onPause()

shippingInfoSubmittedBroadcastReceiver?.let {
LocalBroadcastManager.getInstance(this).unregisterReceiver(it)
}
}

override fun onResume() {
super.onResume()

shippingInfoSubmittedBroadcastReceiver?.let {
LocalBroadcastManager.getInstance(this).registerReceiver(
it,
IntentFilter(PaymentFlowExtras.EVENT_SHIPPING_INFO_PROCESSED)
)
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable(STATE_SHIPPING_INFO, shippingInfo)
Expand Down Expand Up @@ -216,17 +160,11 @@ class PaymentFlowActivity : StripeActivity() {
)
setCommunicatingProgress(true)

val shippingInfoValidator =
paymentSessionConfig.shippingInformationValidator
if (shippingInfoValidator != null) {
validateShippingInformation(
shippingInfoValidator,
paymentSessionConfig.shippingMethodsFactory,
shippingInfo
)
} else {
broadcastShippingInfoSubmitted(shippingInfo)
}
validateShippingInformation(
paymentSessionConfig.shippingInformationValidator,
paymentSessionConfig.shippingMethodsFactory,
shippingInfo
)
}
}

Expand All @@ -247,14 +185,6 @@ class PaymentFlowActivity : StripeActivity() {
}
}

private fun broadcastShippingInfoSubmitted(shippingInformation: ShippingInformation) {
LocalBroadcastManager.getInstance(this)
.sendBroadcast(
Intent(PaymentFlowExtras.EVENT_SHIPPING_INFO_SUBMITTED)
.putExtra(PaymentFlowExtras.EXTRA_SHIPPING_INFO_DATA, shippingInformation)
)
}

private fun hasNextPage(): Boolean {
return shipping_flow_viewpager.currentItem + 1 < paymentFlowPagerAdapter.count
}
Expand Down
51 changes: 0 additions & 51 deletions stripe/src/main/java/com/stripe/android/view/PaymentFlowExtras.kt

This file was deleted.

23 changes: 21 additions & 2 deletions stripe/src/test/java/com/stripe/android/PaymentSessionFixtures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.stripe.android
import com.stripe.android.model.Address
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.ShippingInformation
import com.stripe.android.model.ShippingMethod
import com.stripe.android.view.BillingAddressFields
import com.stripe.android.view.ShippingInfoWidget

Expand Down Expand Up @@ -48,11 +49,29 @@ internal object PaymentSessionFixtures {
.setAllowedShippingCountryCodes(
setOf("US", "CA")
)

.setBillingAddressFields(BillingAddressFields.Full)

.setShouldPrefetchCustomer(true)

.setShippingInformationValidator(object : PaymentSessionConfig.ShippingInformationValidator {
override fun isValid(shippingInformation: ShippingInformation): Boolean {
return true
}

override fun getErrorMessage(shippingInformation: ShippingInformation): String {
return ""
}
})
.setShippingMethodsFactory(object : PaymentSessionConfig.ShippingMethodsFactory {
override fun create(shippingInformation: ShippingInformation): List<ShippingMethod> {
return listOf(
ShippingMethod("UPS Ground", "ups-ground",
0, "USD", "Arrives in 3-5 days"),
ShippingMethod("FedEx", "fedex",
599, "USD", "Arrives tomorrow")
)
}
})

.build()

internal val PAYMENT_SESSION_DATA = PaymentSessionData(PAYMENT_SESSION_CONFIG)
Expand Down
Loading

0 comments on commit 52c6e81

Please sign in to comment.