-
Notifications
You must be signed in to change notification settings - Fork 629
/
StripeGooglePayActivity.kt
230 lines (208 loc) · 7.47 KB
/
StripeGooglePayActivity.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
package com.stripe.android.googlepaylauncher
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.wallet.AutoResolveHelper
import com.google.android.gms.wallet.PaymentData
import com.google.android.gms.wallet.PaymentDataRequest
import com.google.android.gms.wallet.PaymentsClient
import com.stripe.android.PaymentConfiguration
import com.stripe.android.model.GooglePayResult
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.ShippingInformation
import com.stripe.android.utils.AnimationConstants
import org.json.JSONObject
/**
* [StripeGooglePayActivity] is used to return the result of a Google Pay operation.
* The activity will return payment data via [GooglePayLauncherResult.PaymentData].
*
* Use [StripeGooglePayContract] to start [StripeGooglePayActivity].
*
* See [Troubleshooting](https://developers.google.com/pay/api/android/support/troubleshooting)
* for a guide to troubleshooting Google Pay issues.
*/
internal class StripeGooglePayActivity : AppCompatActivity() {
private val paymentsClient: PaymentsClient by lazy {
PaymentsClientFactory(this).create(args.config.environment)
}
private val publishableKey: String by lazy {
PaymentConfiguration.getInstance(this).publishableKey
}
private val stripeAccountId: String? by lazy {
PaymentConfiguration.getInstance(this).stripeAccountId
}
private val viewModel: StripeGooglePayViewModel by viewModels {
StripeGooglePayViewModel.Factory(
application,
publishableKey,
stripeAccountId,
args
)
}
private lateinit var args: StripeGooglePayContract.Args
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setFadeAnimations()
setResult(
RESULT_OK,
Intent().putExtras(
GooglePayLauncherResult.Canceled.toBundle()
)
)
val nullableArgs = StripeGooglePayContract.Args.create(intent)
if (nullableArgs == null) {
finishWithResult(
GooglePayLauncherResult.Error(
RuntimeException(
"StripeGooglePayActivity was started without arguments."
)
)
)
return
}
args = nullableArgs
args.statusBarColor?.let {
window.statusBarColor = it
}
viewModel.googlePayResult.observe(this) { googlePayResult ->
googlePayResult?.let(::finishWithResult)
}
if (!viewModel.hasLaunched) {
viewModel.hasLaunched = true
isReadyToPay(
viewModel.createPaymentDataRequestForPaymentIntentArgs()
)
}
}
override fun finish() {
super.finish()
setFadeAnimations()
}
private fun setFadeAnimations() {
overridePendingTransition(AnimationConstants.FADE_IN, AnimationConstants.FADE_OUT)
}
/**
* Check that Google Pay is available and ready
*/
private fun isReadyToPay(paymentDataRequest: JSONObject) {
paymentsClient.isReadyToPay(
viewModel.createIsReadyToPayRequest()
).addOnCompleteListener { task ->
runCatching {
if (task.isSuccessful) {
payWithGoogle(paymentDataRequest)
} else {
viewModel.updateGooglePayResult(
GooglePayLauncherResult.Unavailable
)
}
}.onFailure {
viewModel.updateGooglePayResult(
GooglePayLauncherResult.Error(it)
)
}
}
}
private fun payWithGoogle(paymentDataRequest: JSONObject) {
AutoResolveHelper.resolveTask(
paymentsClient.loadPaymentData(
PaymentDataRequest.fromJson(paymentDataRequest.toString())
),
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 -> {
viewModel.updateGooglePayResult(
GooglePayLauncherResult.Canceled
)
}
AutoResolveHelper.RESULT_ERROR -> {
val status = AutoResolveHelper.getStatusFromIntent(data)
viewModel.updateGooglePayResult(
GooglePayLauncherResult.Error(
RuntimeException(
"Google Pay returned an error. See googlePayStatus property for more information."
),
googlePayStatus = status
)
)
}
else -> {
viewModel.updateGooglePayResult(
GooglePayLauncherResult.Error(
RuntimeException(
"Google Pay returned an expected result code."
)
)
)
}
}
}
}
private fun onGooglePayResult(data: Intent?) {
val paymentData = data?.let { PaymentData.getFromIntent(it) }
if (paymentData == null) {
viewModel.updateGooglePayResult(
GooglePayLauncherResult.Error(
IllegalArgumentException("Google Pay data was not available")
)
)
return
}
val paymentDataJson = JSONObject(paymentData.toJson())
val shippingInformation =
GooglePayResult.fromJson(paymentDataJson).shippingInformation
val params = PaymentMethodCreateParams.createFromGooglePay(paymentDataJson)
viewModel.createPaymentMethod(params).observe(this) { result ->
result?.fold(
onSuccess = {
onPaymentMethodCreated(
it,
shippingInformation
)
},
onFailure = {
viewModel.paymentMethod = null
viewModel.updateGooglePayResult(
GooglePayLauncherResult.Error(it)
)
}
)
}
}
private fun onPaymentMethodCreated(
paymentMethod: PaymentMethod,
shippingInformation: ShippingInformation?
) {
viewModel.paymentMethod = paymentMethod
viewModel.updateGooglePayResult(
GooglePayLauncherResult.PaymentData(
paymentMethod,
shippingInformation
)
)
}
private fun finishWithResult(result: GooglePayLauncherResult) {
setResult(
RESULT_OK,
Intent().putExtras(
result.toBundle()
)
)
finish()
}
private companion object {
private const val LOAD_PAYMENT_DATA_REQUEST_CODE = 4444
}
}