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

Create PaymentSessionConfig.shouldPrefetchCustomer #1974

Merged
merged 1 commit into from
Dec 19, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class PaymentSessionActivity : AppCompatActivity() {
.setShippingMethodsFactory(ShippingMethodsFactory())
.setWindowFlags(WindowManager.LayoutParams.FLAG_SECURE)
.setBillingAddressFields(BillingAddressFields.Full)
.setShouldPrefetchCustomer(shouldPrefetchCustomer)
.build(),
savedInstanceState = savedInstanceState,
shouldPrefetchCustomer = shouldPrefetchCustomer
savedInstanceState = savedInstanceState
)
if (paymentSessionInitialized) {
paymentSession.setCartTotal(2000L)
Expand Down
29 changes: 26 additions & 3 deletions stripe/src/main/java/com/stripe/android/PaymentSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ class PaymentSession @VisibleForTesting internal constructor(
paymentSessionData = paymentSessionData.copy(paymentMethod = paymentMethod)
}

/**
* Initialize the PaymentSession with a [PaymentSessionListener] to be notified of
* data changes.
*
* @param listener a [PaymentSessionListener] that will receive notifications of changes
* in payment session status, including networking status
* @param paymentSessionConfig a [PaymentSessionConfig] used to decide which items are
* necessary in the PaymentSession.
* @param savedInstanceState a `Bundle` containing the saved state of a
* PaymentSession that was stored in [savePaymentSessionInstanceState]
*
* @return `true` if the PaymentSession is initialized, `false` if a state error
* occurs. Failure can only occur if there is no initialized [CustomerSession].
*/
@JvmOverloads
fun init(
listener: PaymentSessionListener,
paymentSessionConfig: PaymentSessionConfig,
savedInstanceState: Bundle? = null
): Boolean {
return init(listener, paymentSessionConfig, savedInstanceState, true)
}

/**
* Initialize the PaymentSession with a [PaymentSessionListener] to be notified of
* data changes.
Expand All @@ -152,14 +175,14 @@ class PaymentSession @VisibleForTesting internal constructor(
* @return `true` if the PaymentSession is initialized, `false` if a state error
* occurs. Failure can only occur if there is no initialized [CustomerSession].
*/
@Deprecated("Use PaymentSessionConfig.Builder.setShouldPrefetchCustomer() to specify shouldPrefetchCustomer.")
@JvmOverloads
fun init(
listener: PaymentSessionListener,
paymentSessionConfig: PaymentSessionConfig,
savedInstanceState: Bundle? = null,
shouldPrefetchCustomer: Boolean = true
shouldPrefetchCustomer: Boolean
): Boolean {

// Checking to make sure that there is a valid CustomerSession -- the getInstance() call
// will throw a runtime exception if none is ready.
try {
Expand All @@ -178,7 +201,7 @@ class PaymentSession @VisibleForTesting internal constructor(
paymentSessionData = savedInstanceState?.getParcelable(STATE_PAYMENT_SESSION_DATA)
?: PaymentSessionData(paymentSessionConfig)

if (shouldPrefetchCustomer) {
if (shouldPrefetchCustomer || paymentSessionConfig.shouldPrefetchCustomer) {
fetchCustomer()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.stripe.android
import android.os.Parcelable
import androidx.annotation.LayoutRes
import androidx.annotation.WorkerThread
import com.stripe.android.model.Customer
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.ShippingInformation
import com.stripe.android.model.ShippingMethod
Expand Down Expand Up @@ -34,6 +35,7 @@ data class PaymentSessionConfig internal constructor(
val allowedShippingCountryCodes: Set<String> = emptySet(),
val billingAddressFields: BillingAddressFields = BillingAddressFields.None,

internal val shouldPrefetchCustomer: Boolean = true,
internal val shippingInformationValidator: ShippingInformationValidator? = null,
internal val shippingMethodsFactory: ShippingMethodsFactory? = null,
internal val windowFlags: Int? = null
Expand Down Expand Up @@ -97,6 +99,7 @@ data class PaymentSessionConfig internal constructor(
private var shippingInformationValidator: ShippingInformationValidator? = null
private var shippingMethodsFactory: ShippingMethodsFactory? = null
private var windowFlags: Int? = null
private var shouldPrefetchCustomer: Boolean = true

@LayoutRes
private var addPaymentMethodFooterLayoutId: Int = 0
Expand Down Expand Up @@ -228,6 +231,16 @@ data class PaymentSessionConfig internal constructor(
this.shippingMethodsFactory = shippingMethodsFactory
}

/**
* @param shouldPrefetchCustomer If true, will immediately fetch the [Customer] associated
* with this session. Otherwise, will only fetch when needed.
*
* Defaults to true.
*/
fun setShouldPrefetchCustomer(shouldPrefetchCustomer: Boolean): Builder = apply {
this.shouldPrefetchCustomer = shouldPrefetchCustomer
}

override fun build(): PaymentSessionConfig {
return PaymentSessionConfig(
hiddenShippingInfoFields = hiddenShippingInfoFields.orEmpty(),
Expand All @@ -241,7 +254,8 @@ data class PaymentSessionConfig internal constructor(
shippingInformationValidator = shippingInformationValidator,
shippingMethodsFactory = shippingMethodsFactory,
windowFlags = windowFlags,
billingAddressFields = billingAddressFields
billingAddressFields = billingAddressFields,
shouldPrefetchCustomer = shouldPrefetchCustomer
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ internal object PaymentSessionFixtures {

.setBillingAddressFields(BillingAddressFields.Full)

.setShouldPrefetchCustomer(true)

.build()

internal val PAYMENT_SESSION_DATA = PaymentSessionData(PAYMENT_SESSION_CONFIG)
Expand Down