-
Notifications
You must be signed in to change notification settings - Fork 629
/
GooglePayPaymentMethodLauncherActivity.kt
194 lines (176 loc) · 6.93 KB
/
GooglePayPaymentMethodLauncherActivity.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
package com.stripe.android.googlepaylauncher
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.common.api.CommonStatusCodes
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.PaymentMethod
import com.stripe.android.utils.AnimationConstants
import kotlinx.coroutines.launch
/**
* [GooglePayPaymentMethodLauncherActivity] is used to return the result of a Google Pay operation.
* The activity's result will be a [GooglePayPaymentMethodLauncher.Result].
*
* [GooglePayPaymentMethodLauncherActivity] will:
* 1. Verify that Google Pay is available
* 2. Create a [PaymentDataRequest](https://developers.google.com/pay/api/web/reference/request-objects#PaymentDataRequest)
* 3. Create a [PaymentMethod] using the Google Pay token
*
* Use [GooglePayPaymentMethodLauncherContract] to start [GooglePayPaymentMethodLauncherActivity].
*
* See [Troubleshooting](https://developers.google.com/pay/api/android/support/troubleshooting)
* for a guide to troubleshooting Google Pay issues.
*/
internal class GooglePayPaymentMethodLauncherActivity : AppCompatActivity() {
private val viewModel: GooglePayPaymentMethodLauncherViewModel by viewModels {
GooglePayPaymentMethodLauncherViewModel.Factory(
application,
args,
this
)
}
private lateinit var args: GooglePayPaymentMethodLauncherContract.Args
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val statusColor = intent.getIntExtra(GooglePayPaymentMethodLauncherContract.EXTRA_STATUS_BAR_COLOR, -1)
if (statusColor != -1) {
window.statusBarColor = statusColor
}
setFadeAnimations()
val nullableArgs = GooglePayPaymentMethodLauncherContract.Args.fromIntent(intent)
if (nullableArgs == null) {
finishWithResult(
GooglePayPaymentMethodLauncher.Result.Failed(
RuntimeException(
"GooglePayPaymentMethodLauncherActivity was started without arguments."
),
GooglePayPaymentMethodLauncher.DEVELOPER_ERROR
)
)
return
}
args = nullableArgs
viewModel.googlePayResult.observe(this) { googlePayResult ->
googlePayResult?.let(::finishWithResult)
}
if (!viewModel.hasLaunched) {
lifecycleScope.launch {
runCatching {
viewModel.createLoadPaymentDataTask()
}.fold(
onSuccess = {
launchGooglePay(it)
viewModel.hasLaunched = true
},
onFailure = {
updateResult(
GooglePayPaymentMethodLauncher.Result.Failed(
it,
GooglePayPaymentMethodLauncher.INTERNAL_ERROR
)
)
}
)
}
}
}
override fun finish() {
super.finish()
setFadeAnimations()
}
private fun launchGooglePay(task: Task<PaymentData>) {
AutoResolveHelper.resolveTask(
task,
this,
LOAD_PAYMENT_DATA_REQUEST_CODE
)
}
@Deprecated("Deprecated in Java")
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 -> {
updateResult(
GooglePayPaymentMethodLauncher.Result.Canceled
)
}
AutoResolveHelper.RESULT_ERROR -> {
val status = AutoResolveHelper.getStatusFromIntent(data)
val statusMessage = status?.statusMessage.orEmpty()
updateResult(
GooglePayPaymentMethodLauncher.Result.Failed(
RuntimeException(
"Google Pay failed with error ${status?.statusCode}: $statusMessage"
),
status?.statusCode?.let {
googlePayStatusCodeToErrorCode(it)
} ?: GooglePayPaymentMethodLauncher.INTERNAL_ERROR
)
)
}
else -> {
updateResult(
GooglePayPaymentMethodLauncher.Result.Failed(
RuntimeException("Google Pay returned an expected result code."),
GooglePayPaymentMethodLauncher.INTERNAL_ERROR
)
)
}
}
}
}
private fun onGooglePayResult(data: Intent?) {
data?.let { PaymentData.getFromIntent(it) }?.let { paymentData ->
lifecycleScope.launch {
finishWithResult(
viewModel.createPaymentMethod(paymentData)
)
}
} ?: updateResult(
GooglePayPaymentMethodLauncher.Result.Failed(
IllegalArgumentException("Google Pay data was not available"),
GooglePayPaymentMethodLauncher.INTERNAL_ERROR
)
)
}
private fun updateResult(result: GooglePayPaymentMethodLauncher.Result) {
viewModel.updateResult(result)
}
private fun finishWithResult(result: GooglePayPaymentMethodLauncher.Result) {
setResult(
RESULT_OK,
Intent()
.putExtras(
bundleOf(GooglePayPaymentMethodLauncherContract.EXTRA_RESULT to result)
)
)
finish()
}
private fun setFadeAnimations() {
overridePendingTransition(AnimationConstants.FADE_IN, AnimationConstants.FADE_OUT)
}
private fun googlePayStatusCodeToErrorCode(googlePayStatusCode: Int):
@GooglePayPaymentMethodLauncher.ErrorCode Int {
return when (googlePayStatusCode) {
CommonStatusCodes.NETWORK_ERROR -> GooglePayPaymentMethodLauncher.NETWORK_ERROR
CommonStatusCodes.DEVELOPER_ERROR -> GooglePayPaymentMethodLauncher.DEVELOPER_ERROR
else -> GooglePayPaymentMethodLauncher.INTERNAL_ERROR
}
}
private companion object {
private const val LOAD_PAYMENT_DATA_REQUEST_CODE = 4444
}
}