-
Notifications
You must be signed in to change notification settings - Fork 629
/
PaymentOptionsActivity.kt
260 lines (235 loc) · 10.5 KB
/
PaymentOptionsActivity.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
package com.stripe.android.paymentsheet
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import android.widget.TextView
import androidx.activity.viewModels
import androidx.annotation.IdRes
import androidx.annotation.RestrictTo
import androidx.annotation.VisibleForTesting
import androidx.compose.ui.platform.ComposeView
import androidx.core.os.bundleOf
import androidx.core.view.doOnNextLayout
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.MaterialToolbar
import com.stripe.android.paymentsheet.databinding.ActivityPaymentOptionsBinding
import com.stripe.android.utils.AnimationConstants
import com.stripe.android.paymentsheet.ui.BaseSheetActivity
import com.stripe.android.paymentsheet.ui.PrimaryButton
import com.stripe.android.paymentsheet.viewmodels.BaseSheetViewModel
import java.security.InvalidParameterException
/**
* An `Activity` for selecting a payment option.
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
internal class PaymentOptionsActivity : BaseSheetActivity<PaymentOptionResult>() {
@VisibleForTesting
internal val viewBinding by lazy {
ActivityPaymentOptionsBinding.inflate(layoutInflater)
}
@VisibleForTesting
internal var viewModelFactory: ViewModelProvider.Factory =
PaymentOptionsViewModel.Factory(
{ application },
{ requireNotNull(starterArgs) },
this,
intent?.extras
)
override val viewModel: PaymentOptionsViewModel by viewModels { viewModelFactory }
private val starterArgs: PaymentOptionContract.Args? by lazy {
PaymentOptionContract.Args.fromIntent(intent)
}
private val fragmentContainerId: Int
@IdRes
get() = viewBinding.fragmentContainer.id
override val rootView: ViewGroup by lazy { viewBinding.root }
override val bottomSheet: ViewGroup by lazy { viewBinding.bottomSheet }
override val appbar: AppBarLayout by lazy { viewBinding.appbar }
override val linkAuthView: ComposeView by lazy { viewBinding.linkAuth }
override val toolbar: MaterialToolbar by lazy { viewBinding.toolbar }
override val testModeIndicator: TextView by lazy { viewBinding.testmode }
override val scrollView: ScrollView by lazy { viewBinding.scrollView }
override val header: ComposeView by lazy { viewBinding.header }
override val fragmentContainerParent: ViewGroup by lazy { viewBinding.fragmentContainerParent }
override val messageView: TextView by lazy { viewBinding.message }
override val notesView: ComposeView by lazy { viewBinding.notes }
override val primaryButton: PrimaryButton by lazy { viewBinding.continueButton }
override val bottomSpacer: View by lazy { viewBinding.bottomSpacer }
override fun onCreate(savedInstanceState: Bundle?) {
val starterArgs = this.starterArgs
if (starterArgs == null) {
finish()
return
}
try {
starterArgs.config?.validate()
starterArgs.config?.appearance?.parseAppearance()
} catch (e: InvalidParameterException) {
finish()
return
}
super.onCreate(savedInstanceState)
starterArgs.statusBarColor?.let {
window.statusBarColor = it
}
setContentView(viewBinding.root)
viewModel.paymentOptionResult.observe(this) {
closeSheet(it)
}
viewModel.error.observe(this) {
updateErrorMessage(
messageView,
BaseSheetViewModel.UserErrorMessage(it)
)
}
viewModel.transition.observe(this) { event ->
clearErrorMessages()
event?.getContentIfNotHandled()?.let { transitionTarget ->
onTransitionTarget(
transitionTarget,
bundleOf(
PaymentSheetActivity.EXTRA_STARTER_ARGS to starterArgs,
PaymentSheetActivity.EXTRA_FRAGMENT_CONFIG to
transitionTarget.fragmentConfig
)
)
}
}
// if we are recovering from process kill or activity died, we should leave the fragment
// in it's current state.
if (!isSelectOrAddFragment()) {
viewModel.fragmentConfigEvent.observe(this) { event ->
val config = event.getContentIfNotHandled()
if (config != null) {
if (viewModel.isResourceRepositoryReady.value == true) {
viewModel.transitionTo(
// It would be nice to see this condition move into the PaymentOptionsListFragment
// where we also jump to a new unsaved card. However this move require
// the transition target to specify when to and when not to add things to the
// backstack.
if (
starterArgs.paymentMethods.isEmpty() &&
!config.isGooglePayReady &&
viewModel.isLinkEnabled.value != true // WHy not use config.isLinkEnabled?
) {
PaymentOptionsViewModel.TransitionTarget.AddPaymentMethodSheet(
config
)
} else {
PaymentOptionsViewModel.TransitionTarget.SelectSavedPaymentMethod(
config
)
}
)
}
}
}
}
viewModel.selection.observe(this) {
clearErrorMessages()
resetPrimaryButtonState()
}
supportFragmentManager.registerFragmentLifecycleCallbacks(
object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentStarted(fm: FragmentManager, fragment: Fragment) {
val visible =
fragment is PaymentOptionsAddPaymentMethodFragment ||
viewModel.primaryButtonUIState.value?.visible == true
viewBinding.continueButton.isVisible = visible
viewBinding.bottomSpacer.isVisible = visible
}
},
false
)
}
private fun isSelectOrAddFragment() = supportFragmentManager.fragments.firstOrNull()?.let {
it.tag == ADD_FULL_FRAGMENT_TAG ||
it.tag == ADD_PAYMENT_METHOD_SHEET_TAG ||
it.tag == SELECT_SAVED_PAYMENT_METHOD_TAG
} ?: false
override fun resetPrimaryButtonState() {
viewBinding.continueButton.lockVisible = false
viewBinding.continueButton.updateState(PrimaryButton.State.Ready)
viewBinding.continueButton.setLabel(
getString(R.string.stripe_continue_button_label)
)
viewBinding.continueButton.setOnClickListener {
clearErrorMessages()
viewModel.onUserSelection()
}
}
private fun onTransitionTarget(
transitionTarget: PaymentOptionsViewModel.TransitionTarget,
fragmentArgs: Bundle
) {
supportFragmentManager.commit {
when (transitionTarget) {
is PaymentOptionsViewModel.TransitionTarget.AddPaymentMethodFull -> {
// Once the add fragment has been opened there is never a scenario that
// we should back to the add fragment from the select list view.
viewModel.hasTransitionToUnsavedLpm = true
setCustomAnimations(
AnimationConstants.FADE_IN,
AnimationConstants.FADE_OUT,
AnimationConstants.FADE_IN,
AnimationConstants.FADE_OUT
)
addToBackStack(null)
replace(
fragmentContainerId,
PaymentOptionsAddPaymentMethodFragment::class.java,
fragmentArgs,
ADD_FULL_FRAGMENT_TAG
)
}
is PaymentOptionsViewModel.TransitionTarget.SelectSavedPaymentMethod -> {
replace(
fragmentContainerId,
PaymentOptionsListFragment::class.java,
fragmentArgs,
SELECT_SAVED_PAYMENT_METHOD_TAG
)
}
is PaymentOptionsViewModel.TransitionTarget.AddPaymentMethodSheet -> {
// Once the add fragment has been opened there is never a scenario that
// we should back to the add fragment from the select list view.
viewModel.hasTransitionToUnsavedLpm = true
replace(
fragmentContainerId,
PaymentOptionsAddPaymentMethodFragment::class.java,
fragmentArgs,
ADD_PAYMENT_METHOD_SHEET_TAG
)
}
}
}
// Ensure the bottom sheet is expanded only after the fragment transaction is completed
supportFragmentManager.executePendingTransactions()
rootView.doOnNextLayout {
// Expand sheet only after the first fragment is attached so that it animates in.
// Further calls to expand() are no-op if the sheet is already expanded.
bottomSheetController.expand()
}
}
override fun setActivityResult(result: PaymentOptionResult) {
setResult(
result.resultCode,
Intent()
.putExtras(result.toBundle())
)
}
internal companion object {
internal const val EXTRA_FRAGMENT_CONFIG = BaseSheetActivity.EXTRA_FRAGMENT_CONFIG
internal const val EXTRA_STARTER_ARGS = BaseSheetActivity.EXTRA_STARTER_ARGS
const val ADD_FULL_FRAGMENT_TAG = "AddFullFragment"
const val ADD_PAYMENT_METHOD_SHEET_TAG = "AddPaymentMethodSheet"
const val SELECT_SAVED_PAYMENT_METHOD_TAG = "SelectSavedPaymentMethod"
}
}