-
Notifications
You must be signed in to change notification settings - Fork 629
/
GooglePayLauncherActivity.kt
190 lines (172 loc) · 6.18 KB
/
GooglePayLauncherActivity.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
package com.stripe.android.googlepaylauncher
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.tasks.Task
import com.google.android.gms.wallet.AutoResolveHelper
import com.google.android.gms.wallet.PaymentData
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.StripeIntent
import com.stripe.android.view.AuthActivityStarterHost
import kotlinx.coroutines.launch
import org.json.JSONObject
/**
* [GooglePayLauncherActivity] is used to return the result of a Google Pay operation.
* The activity's result will be a [GooglePayLauncher.Result].
*
* [GooglePayLauncherActivity] will:
* 1. Fetch the [StripeIntent]
* 2. Verify that Google Pay is available
* 3. Create a [PaymentDataRequest](https://developers.google.com/pay/api/web/reference/request-objects#PaymentDataRequest)
* 4. Confirm the [StripeIntent] using the Google Pay token
*
* Use [GooglePayLauncherContract] to start [GooglePayLauncherActivity].
*
* See [Troubleshooting](https://developers.google.com/pay/api/android/support/troubleshooting)
* for a guide to troubleshooting Google Pay issues.
*/
internal class GooglePayLauncherActivity : AppCompatActivity() {
private val viewModel: GooglePayLauncherViewModel by viewModels {
GooglePayLauncherViewModel.Factory(
application,
args,
this
)
}
private lateinit var args: GooglePayLauncherContract.Args
@SuppressLint("SourceLockedOrientationActivity")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
disableAnimations()
args = runCatching {
requireNotNull(GooglePayLauncherContract.Args.fromIntent(intent)) {
"GooglePayLauncherActivity was started without arguments."
}
}.getOrElse {
finishWithResult(
GooglePayLauncher.Result.Failed(it)
)
return
}
val statusColor = intent.getIntExtra(GooglePayLauncherContract.EXTRA_STATUS_BAR_COLOR, -1)
if (statusColor != -1) {
window.statusBarColor = statusColor
}
viewModel.googlePayResult.observe(this) { googlePayResult ->
googlePayResult?.let(::finishWithResult)
}
if (!viewModel.hasLaunched) {
lifecycleScope.launch {
runCatching {
viewModel.createLoadPaymentDataTask()
}.fold(
onSuccess = {
payWithGoogle(it)
viewModel.hasLaunched = true
},
onFailure = {
viewModel.updateResult(
GooglePayLauncher.Result.Failed(it)
)
}
)
}
}
}
override fun finish() {
super.finish()
disableAnimations()
}
private fun payWithGoogle(task: Task<PaymentData>) {
AutoResolveHelper.resolveTask(
task,
this,
LOAD_PAYMENT_DATA_REQUEST_CODE
)
}
public override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == LOAD_PAYMENT_DATA_REQUEST_CODE) {
when (resultCode) {
RESULT_OK -> {
onGooglePayResult(data)
}
RESULT_CANCELED -> {
viewModel.updateResult(
GooglePayLauncher.Result.Canceled
)
}
AutoResolveHelper.RESULT_ERROR -> {
val status = AutoResolveHelper.getStatusFromIntent(data)
val statusMessage = status?.statusMessage.orEmpty()
viewModel.updateResult(
GooglePayLauncher.Result.Failed(
RuntimeException(
"Google Pay failed with error: $statusMessage"
)
)
)
}
else -> {
viewModel.updateResult(
GooglePayLauncher.Result.Failed(
RuntimeException(
"Google Pay returned an expected result code."
)
)
)
}
}
} else {
lifecycleScope.launch {
viewModel.onConfirmResult(
requestCode,
data ?: Intent()
)
}
}
}
private fun onGooglePayResult(data: Intent?) {
val paymentData = data?.let { PaymentData.getFromIntent(it) }
if (paymentData == null) {
viewModel.updateResult(
GooglePayLauncher.Result.Failed(
IllegalArgumentException("Google Pay data was not available")
)
)
return
}
val paymentDataJson = JSONObject(paymentData.toJson())
val params = PaymentMethodCreateParams.createFromGooglePay(paymentDataJson)
val host = AuthActivityStarterHost.create(this)
lifecycleScope.launch {
viewModel.confirmStripeIntent(host, params)
}
}
private fun finishWithResult(result: GooglePayLauncher.Result) {
setResult(
RESULT_OK,
Intent()
.putExtras(
bundleOf(GooglePayLauncherContract.EXTRA_RESULT to result)
)
)
finish()
}
private fun disableAnimations() {
// this is a transparent Activity so we want to disable animations
overridePendingTransition(0, 0)
}
private companion object {
// the value isn't meaningful / is arbitrary
private const val LOAD_PAYMENT_DATA_REQUEST_CODE = 4444
}
}