-
Notifications
You must be signed in to change notification settings - Fork 629
/
BaseAddPaymentMethodFragment.kt
276 lines (250 loc) · 11.6 KB
/
BaseAddPaymentMethodFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.stripe.android.paymentsheet
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.appcompat.view.ContextThemeWrapper
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.asFlow
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.stripe.android.core.injection.InjectorKey
import com.stripe.android.link.model.AccountStatus
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.StripeIntent
import com.stripe.android.paymentsheet.databinding.FragmentPaymentsheetAddPaymentMethodBinding
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.model.getPMAddForm
import com.stripe.android.paymentsheet.paymentdatacollection.ComposeFormDataCollectionFragment
import com.stripe.android.paymentsheet.paymentdatacollection.FormFragmentArguments
import com.stripe.android.paymentsheet.paymentdatacollection.ach.USBankAccountFormFragment
import com.stripe.android.utils.AnimationConstants
import com.stripe.android.paymentsheet.ui.PrimaryButton
import com.stripe.android.paymentsheet.viewmodels.BaseSheetViewModel
import com.stripe.android.ui.core.Amount
import com.stripe.android.ui.core.forms.resources.LpmRepository.SupportedPaymentMethod
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
internal abstract class BaseAddPaymentMethodFragment : Fragment() {
abstract val viewModelFactory: ViewModelProvider.Factory
abstract val sheetViewModel: BaseSheetViewModel<*>
private lateinit var viewBinding: FragmentPaymentsheetAddPaymentMethodBinding
private var showLinkInlineSignup = false
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val themedInflater = inflater.cloneInContext(
ContextThemeWrapper(requireActivity(), R.style.StripePaymentSheetAddPaymentMethodTheme)
)
return themedInflater.inflate(
R.layout.fragment_paymentsheet_add_payment_method,
container,
false
)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewBinding = FragmentPaymentsheetAddPaymentMethodBinding.bind(view)
val paymentMethods = sheetViewModel.supportedPaymentMethods
sheetViewModel.headerText.value =
getString(R.string.stripe_paymentsheet_add_payment_method_title)
sheetViewModel.processing.observe(viewLifecycleOwner) { isProcessing ->
viewBinding.linkInlineSignup.isEnabled = !isProcessing
}
viewBinding.linkInlineSignup.apply {
linkLauncher = sheetViewModel.linkLauncher
}
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
combine(
viewBinding.linkInlineSignup.isSelected,
viewBinding.linkInlineSignup.userInput,
sheetViewModel.selection.asFlow()
) { isSelected, userInput, selection ->
if (isSelected) {
if (userInput != null && selection != null) {
PrimaryButton.UIState(
label = null,
onClick = { sheetViewModel.payWithLinkInline(userInput) },
enabled = true,
visible = true
)
} else {
PrimaryButton.UIState(
label = null,
onClick = null,
enabled = false,
visible = true
)
}
} else {
null
}
}.collect {
if (showLinkInlineSignup) {
sheetViewModel.updatePrimaryButtonUIState(it)
}
}
}
}
sheetViewModel.isResourceRepositoryReady.observe(viewLifecycleOwner) { isReady ->
if (isReady == true) {
val selectedPaymentMethodIndex = paymentMethods.indexOf(
sheetViewModel.addFragmentSelectedLPM
).takeUnless { it == -1 } ?: 0
// In order to be able to read sheetViewModel.addFragmentSelectedLPM
// the repository needs to be set. This might not be set if recovery
// from a killed process or don't keep activities event
if (paymentMethods.size > 1) {
setupRecyclerView(
viewBinding,
paymentMethods,
sheetViewModel.addFragmentSelectedLPM
)
}
if (paymentMethods.isNotEmpty()) {
updateLinkInlineSignupVisibility(paymentMethods[selectedPaymentMethodIndex])
// If the activity is destroyed and recreated, then the fragment is already present
// and doesn't need to be replaced, only the selected payment method needs to be set
if (savedInstanceState == null) {
replacePaymentMethodFragment(paymentMethods[selectedPaymentMethodIndex])
}
}
}
}
sheetViewModel.eventReporter.onShowNewPaymentOptionForm(
linkEnabled = sheetViewModel.isLinkEnabled.value ?: false,
activeLinkSession = sheetViewModel.activeLinkSession.value ?: false
)
}
private fun setupRecyclerView(
viewBinding: FragmentPaymentsheetAddPaymentMethodBinding,
paymentMethods: List<SupportedPaymentMethod>,
initialSelectedItem: SupportedPaymentMethod
) {
viewBinding.paymentMethodsRecycler.isVisible = true
viewBinding.paymentMethodsRecycler.apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
val processing by sheetViewModel.processing
.asFlow()
.collectAsState(initial = false)
val selectedItem by sheetViewModel.getAddFragmentSelectedLpm()
.asFlow()
.collectAsState(initial = initialSelectedItem)
PaymentMethodsUI(
selectedIndex = paymentMethods.indexOf(selectedItem),
isEnabled = !processing,
paymentMethods = paymentMethods,
onItemSelectedListener = { selectedLpm ->
if (sheetViewModel.addFragmentSelectedLPM != selectedLpm) {
onPaymentMethodSelected(selectedLpm)
}
}
)
}
}
}
@VisibleForTesting
internal fun onPaymentMethodSelected(paymentMethod: SupportedPaymentMethod) {
// hide the soft keyboard.
ViewCompat.getWindowInsetsController(requireView())
?.hide(WindowInsetsCompat.Type.ime())
sheetViewModel.updatePrimaryButtonUIState(null)
updateLinkInlineSignupVisibility(paymentMethod)
replacePaymentMethodFragment(paymentMethod)
}
private fun replacePaymentMethodFragment(paymentMethod: SupportedPaymentMethod) {
sheetViewModel.addFragmentSelectedLPM = paymentMethod
val args = requireArguments()
args.putParcelable(
ComposeFormDataCollectionFragment.EXTRA_CONFIG,
getFormArguments(
stripeIntent = requireNotNull(sheetViewModel.stripeIntent.value),
config = sheetViewModel.config,
showPaymentMethod = paymentMethod,
merchantName = sheetViewModel.merchantName,
amount = sheetViewModel.amount.value,
injectorKey = sheetViewModel.injectorKey,
newLpm = sheetViewModel.newPaymentSelection,
isShowingLinkInlineSignup = showLinkInlineSignup
)
)
childFragmentManager.commit {
setCustomAnimations(
AnimationConstants.FADE_IN,
AnimationConstants.FADE_OUT,
AnimationConstants.FADE_IN,
AnimationConstants.FADE_OUT
)
replace(
R.id.payment_method_fragment_container,
fragmentForPaymentMethod(paymentMethod),
args
)
}
}
private fun updateLinkInlineSignupVisibility(selectedPaymentMethod: SupportedPaymentMethod) {
showLinkInlineSignup = sheetViewModel.isLinkEnabled.value == true &&
selectedPaymentMethod.code == PaymentMethod.Type.Card.code &&
sheetViewModel.linkLauncher.accountStatus.value == AccountStatus.SignedOut
viewBinding.linkInlineSignup.isVisible = showLinkInlineSignup
}
private fun fragmentForPaymentMethod(paymentMethod: SupportedPaymentMethod) =
when (paymentMethod.code) {
PaymentMethod.Type.USBankAccount.code -> USBankAccountFormFragment::class.java
else -> ComposeFormDataCollectionFragment::class.java
}
companion object {
@VisibleForTesting
fun getFormArguments(
showPaymentMethod: SupportedPaymentMethod,
stripeIntent: StripeIntent,
config: PaymentSheet.Configuration?,
merchantName: String,
amount: Amount? = null,
@InjectorKey injectorKey: String,
newLpm: PaymentSelection.New?,
isShowingLinkInlineSignup: Boolean = false
): FormFragmentArguments {
val layoutFormDescriptor = showPaymentMethod.getPMAddForm(stripeIntent, config)
return FormFragmentArguments(
paymentMethodCode = showPaymentMethod.code,
showCheckbox = layoutFormDescriptor.showCheckbox && !isShowingLinkInlineSignup,
showCheckboxControlledFields = newLpm?.let {
newLpm.customerRequestedSave ==
PaymentSelection.CustomerRequestedSave.RequestReuse
} ?: layoutFormDescriptor.showCheckboxControlledFields,
merchantName = merchantName,
amount = amount,
billingDetails = config?.defaultBillingDetails,
shippingDetails = config?.shippingDetails,
injectorKey = injectorKey,
initialPaymentMethodCreateParams =
newLpm?.paymentMethodCreateParams?.typeCode?.takeIf {
it == showPaymentMethod.code
}?.let {
when (newLpm) {
is PaymentSelection.New.GenericPaymentMethod ->
newLpm.paymentMethodCreateParams
is PaymentSelection.New.Card ->
newLpm.paymentMethodCreateParams
else -> null
}
}
)
}
}
}