-
Notifications
You must be signed in to change notification settings - Fork 629
/
PaymentLauncherConfirmationActivity.kt
101 lines (87 loc) · 3.18 KB
/
PaymentLauncherConfirmationActivity.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
package com.stripe.android.payments.paymentlauncher
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.stripe.android.utils.AnimationConstants
import com.stripe.android.view.AuthActivityStarterHost
/**
* Host activity to perform actions for [PaymentLauncher].
* This activity starts activities to handle next actions, capture their result
* and convert them to [PaymentResult] and return back to client.
*/
internal class PaymentLauncherConfirmationActivity : AppCompatActivity() {
private val starterArgs: PaymentLauncherContract.Args? by lazy {
PaymentLauncherContract.Args.fromIntent(intent)
}
@VisibleForTesting
internal var viewModelFactory: ViewModelProvider.Factory =
PaymentLauncherViewModel.Factory(
{ requireNotNull(starterArgs) },
{ application },
this
)
@VisibleForTesting
internal val viewModel: PaymentLauncherViewModel by viewModels { viewModelFactory }
@SuppressLint("SourceLockedOrientationActivity")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setFadeAnimations()
val args = runCatching {
requireNotNull(starterArgs) {
EMPTY_ARG_ERROR
}
}.getOrElse {
finishWithResult(PaymentResult.Failed(it))
return
}
args.statusBarColor?.let {
window.statusBarColor = it
}
viewModel.paymentLauncherResult.observe(this, ::finishWithResult)
viewModel.register(this)
val host = AuthActivityStarterHost.create(this)
when (args) {
is PaymentLauncherContract.Args.IntentConfirmationArgs -> {
viewModel.confirmStripeIntent(args.confirmStripeIntentParams, host)
}
is PaymentLauncherContract.Args.PaymentIntentNextActionArgs -> {
viewModel.handleNextActionForStripeIntent(args.paymentIntentClientSecret, host)
}
is PaymentLauncherContract.Args.SetupIntentNextActionArgs -> {
viewModel.handleNextActionForStripeIntent(args.setupIntentClientSecret, host)
}
}
}
override fun onDestroy() {
super.onDestroy()
viewModel.cleanUp()
}
override fun finish() {
super.finish()
setFadeAnimations()
}
private fun setFadeAnimations() {
overridePendingTransition(AnimationConstants.FADE_IN, AnimationConstants.FADE_OUT)
}
/**
* After confirmation and next action is handled, finish the activity with
* corresponding [PaymentResult]
*/
private fun finishWithResult(result: PaymentResult) {
setResult(
Activity.RESULT_OK,
Intent()
.putExtras(result.toBundle())
)
finish()
}
companion object {
const val EMPTY_ARG_ERROR =
"PaymentLauncherConfirmationActivity was started without arguments"
}
}