-
Notifications
You must be signed in to change notification settings - Fork 66
/
DropInViewModel.kt
458 lines (407 loc) · 17.9 KB
/
DropInViewModel.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright (c) 2019 Adyen N.V.
*
* This file is open source and available under the MIT license. See the LICENSE file for more info.
*
* Created by arman on 2/7/2019.
*/
package com.adyen.checkout.dropin.ui.viewmodel
import android.content.ComponentName
import android.content.Intent
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.adyen.checkout.components.PaymentComponentState
import com.adyen.checkout.components.model.PaymentMethodsApiResponse
import com.adyen.checkout.components.model.paymentmethods.PaymentMethod
import com.adyen.checkout.components.model.paymentmethods.StoredPaymentMethod
import com.adyen.checkout.components.model.payments.Amount
import com.adyen.checkout.components.model.payments.request.OrderRequest
import com.adyen.checkout.components.model.payments.request.PaymentMethodDetails
import com.adyen.checkout.components.model.payments.response.BalanceResult
import com.adyen.checkout.components.model.payments.response.OrderResponse
import com.adyen.checkout.components.repository.OrderStatusRepository
import com.adyen.checkout.components.util.PaymentMethodTypes
import com.adyen.checkout.core.exception.CheckoutException
import com.adyen.checkout.core.log.LogUtil
import com.adyen.checkout.core.log.Logger
import com.adyen.checkout.dropin.DropInConfiguration
import com.adyen.checkout.dropin.R
import com.adyen.checkout.dropin.ui.giftcard.GiftCardBalanceResult
import com.adyen.checkout.dropin.ui.giftcard.GiftCardPaymentConfirmationData
import com.adyen.checkout.dropin.ui.order.OrderModel
import com.adyen.checkout.giftcard.GiftCardComponentState
import com.adyen.checkout.giftcard.util.GiftCardBalanceStatus
import com.adyen.checkout.giftcard.util.GiftCardBalanceUtils
import com.adyen.checkout.googlepay.GooglePayComponent
import com.adyen.checkout.sessions.model.Session
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
@Suppress("TooManyFunctions")
class DropInViewModel(
private val savedStateHandle: SavedStateHandle,
private val orderStatusRepository: OrderStatusRepository = OrderStatusRepository()
) : ViewModel() {
private val eventChannel = Channel<DropInActivityEvent>(Channel.BUFFERED)
internal val eventsFlow = eventChannel.receiveAsFlow()
val dropInConfiguration: DropInConfiguration = getStateValueOrFail(DROP_IN_CONFIGURATION_KEY)
val serviceComponentName: ComponentName = getStateValueOrFail(DROP_IN_SERVICE_KEY)
var amount: Amount
get() {
return getStateValueOrFail(AMOUNT)
}
private set(value) {
savedStateHandle[AMOUNT] = value
}
private var session: Session?
get() = savedStateHandle[SESSION_KEY]
private set(value) {
savedStateHandle[SESSION_KEY] = value
}
private var paymentMethodsApiResponse: PaymentMethodsApiResponse?
get() {
return savedStateHandle[PAYMENT_METHODS_RESPONSE_KEY]
}
private set(value) {
savedStateHandle[PAYMENT_METHODS_RESPONSE_KEY] = value
}
var isWaitingResult: Boolean
get() {
return savedStateHandle[IS_WAITING_FOR_RESULT_KEY] ?: false
}
set(value) {
savedStateHandle[IS_WAITING_FOR_RESULT_KEY] = value
}
private var cachedGiftCardComponentState: GiftCardComponentState?
get() {
return savedStateHandle[CACHED_GIFT_CARD]
}
private set(value) {
savedStateHandle[CACHED_GIFT_CARD] = value
}
private var cachedPartialPaymentAmount: Amount?
get() {
return savedStateHandle[PARTIAL_PAYMENT_AMOUNT]
}
private set(value) {
savedStateHandle[PARTIAL_PAYMENT_AMOUNT] = value
}
var currentOrder: OrderModel?
get() {
return savedStateHandle[CURRENT_ORDER]
}
private set(value) {
savedStateHandle[CURRENT_ORDER] = value
}
private fun <T> getStateValueOrFail(key: String): T {
val value: T? = savedStateHandle[key]
if (value == null) {
Logger.e(TAG, "Failed to initialize bundle from SavedStateHandle")
throw CheckoutException("Failed to initialize Drop-in, did you manually launch DropInActivity?")
}
return value
}
init {
amount = dropInConfiguration.amount
}
fun getPaymentMethods(): List<PaymentMethod> {
return paymentMethodsApiResponse?.paymentMethods.orEmpty()
}
fun getStoredPaymentMethods(): List<StoredPaymentMethod> {
return paymentMethodsApiResponse?.storedPaymentMethods.orEmpty()
}
fun shouldShowPreselectedStored(): Boolean {
return getStoredPaymentMethods().any { it.isEcommerce } &&
dropInConfiguration.showPreselectedStoredPaymentMethod
}
fun getPreselectedStoredPaymentMethod(): StoredPaymentMethod {
return getStoredPaymentMethods().firstOrNull {
it.isEcommerce && PaymentMethodTypes.SUPPORTED_PAYMENT_METHODS.contains(it.type)
} ?: StoredPaymentMethod()
}
fun getStoredPaymentMethod(id: String): StoredPaymentMethod {
return getStoredPaymentMethods().firstOrNull { it.id == id } ?: StoredPaymentMethod()
}
fun shouldSkipToSinglePaymentMethod(): Boolean {
val noStored = getStoredPaymentMethods().isEmpty()
val singlePm = getPaymentMethods().size == 1
val firstPaymentMethod = getPaymentMethods().firstOrNull()
val paymentMethodHasComponent =
PaymentMethodTypes.SUPPORTED_PAYMENT_METHODS.contains(firstPaymentMethod?.type) &&
!GooglePayComponent.PAYMENT_METHOD_TYPES.contains(firstPaymentMethod?.type) &&
!PaymentMethodTypes.SUPPORTED_ACTION_ONLY_PAYMENT_METHODS.contains(firstPaymentMethod?.type)
return noStored && singlePm && paymentMethodHasComponent && dropInConfiguration.skipListWhenSinglePaymentMethod
}
fun onCreated() {
if (isInitializedWithSession() && paymentMethodsApiResponse == null) {
// we just need to wait for the service to fetch the payment methods and send them to us
// TODO add loading state?
return
} else {
navigateToInitialDestination()
}
}
fun onDropInServiceConnected() {
session?.let { session ->
val event = DropInActivityEvent.SessionServiceConnected(
session = session,
clientKey = dropInConfiguration.clientKey,
baseUrl = dropInConfiguration.environment.baseUrl,
shouldFetchPaymentMethods = paymentMethodsApiResponse == null,
)
sendEvent(event)
}
}
private fun isInitializedWithSession(): Boolean {
return session != null
}
fun onSessionSetupSuccessful(paymentMethods: PaymentMethodsApiResponse?) {
paymentMethodsApiResponse = paymentMethods
navigateToInitialDestination()
}
fun onSessionDataChanged(sessionData: String) {
session = session?.copy(sessionData = sessionData)
}
private fun navigateToInitialDestination() {
val destination = when {
shouldSkipToSinglePaymentMethod() -> {
val firstPaymentMethod = getPaymentMethods().firstOrNull()
if (firstPaymentMethod != null) {
DropInDestination.PaymentComponent(firstPaymentMethod)
} else {
throw CheckoutException("First payment method is null")
}
}
shouldShowPreselectedStored() -> DropInDestination.PreselectedStored
else -> DropInDestination.PaymentMethods
}
sendEvent(DropInActivityEvent.NavigateTo(destination))
}
/**
* @return the payment method details required to request the balance, or null if invalid
*/
fun onBalanceCallRequested(giftCardComponentState: GiftCardComponentState): PaymentMethodDetails? {
val paymentMethod = giftCardComponentState.data.paymentMethod
if (paymentMethod == null) {
Logger.e(TAG, "onBalanceCallRequested - paymentMethod is null")
return null
}
cachedGiftCardComponentState = giftCardComponentState
return paymentMethod
}
fun handleBalanceResult(balanceResult: BalanceResult): GiftCardBalanceResult {
Logger.d(
TAG,
"handleBalanceResult - balance: ${balanceResult.balance} - " +
"transactionLimit: ${balanceResult.transactionLimit}"
)
val giftCardBalanceResult = GiftCardBalanceUtils.checkBalance(
balance = balanceResult.balance,
transactionLimit = balanceResult.transactionLimit,
amountToBePaid = amount
)
val cachedGiftCardComponentState =
cachedGiftCardComponentState ?: throw CheckoutException("Failed to retrieved cached gift card object")
return when (giftCardBalanceResult) {
is GiftCardBalanceStatus.ZeroBalance -> {
Logger.i(TAG, "handleBalanceResult - Gift Card has zero balance")
GiftCardBalanceResult.Error(
R.string.checkout_giftcard_error_zero_balance,
"Gift Card has zero balance",
false
)
}
is GiftCardBalanceStatus.NonMatchingCurrencies -> {
Logger.e(TAG, "handleBalanceResult - Gift Card currency mismatch")
GiftCardBalanceResult.Error(
R.string.checkout_giftcard_error_currency,
"Gift Card currency mismatch",
false
)
}
is GiftCardBalanceStatus.ZeroAmountToBePaid -> {
Logger.e(
TAG,
"handleBalanceResult - You must set an amount in DropInConfiguration.Builder to enable gift " +
"card payments"
)
GiftCardBalanceResult.Error(R.string.payment_failed, "Drop-in amount is not set", true)
}
is GiftCardBalanceStatus.FullPayment -> {
cachedPartialPaymentAmount = giftCardBalanceResult.amountPaid
GiftCardBalanceResult.FullPayment(
createGiftCardPaymentConfirmationData(giftCardBalanceResult, cachedGiftCardComponentState)
)
}
is GiftCardBalanceStatus.PartialPayment -> {
cachedPartialPaymentAmount = giftCardBalanceResult.amountPaid
if (currentOrder == null) GiftCardBalanceResult.RequestOrderCreation
else GiftCardBalanceResult.RequestPartialPayment
}
}
}
private fun createGiftCardPaymentConfirmationData(
giftCardBalanceStatus: GiftCardBalanceStatus.FullPayment,
giftCardComponentState: GiftCardComponentState
): GiftCardPaymentConfirmationData {
return GiftCardPaymentConfirmationData(
amountPaid = giftCardBalanceStatus.amountPaid,
remainingBalance = giftCardBalanceStatus.remainingBalance,
shopperLocale = dropInConfiguration.shopperLocale,
brand = giftCardComponentState.data.paymentMethod?.brand.orEmpty(),
lastFourDigits = giftCardComponentState.lastFourDigits.orEmpty()
)
}
fun handleOrderCreated(orderResponse: OrderResponse) {
viewModelScope.launch(Dispatchers.IO) {
handleOrderResponse(orderResponse)
if (currentOrder != null) {
partialPaymentRequested()
}
}
}
private suspend fun handleOrderResponse(orderResponse: OrderResponse?) {
val orderModel = getOrderDetails(orderResponse)
if (orderModel == null) {
currentOrder = null
amount = dropInConfiguration.amount
Logger.d(TAG, "handleOrderResponse - Amount reverted: $amount")
Logger.d(TAG, "handleOrderResponse - Order cancelled")
} else {
currentOrder = orderModel
amount = orderModel.remainingAmount
Logger.d(TAG, "handleOrderResponse - New amount set: $amount")
Logger.d(TAG, "handleOrderResponse - Order cached")
}
}
fun updatePaymentComponentStateForPaymentsCall(paymentComponentState: PaymentComponentState<*>) {
// include amount value if merchant passed it to the DropIn
val existingAmount = paymentComponentState.data.amount
when {
existingAmount != null && !existingAmount.isEmpty -> {
Logger.d(TAG, "Payment amount already set: $existingAmount")
}
!amount.isEmpty -> {
paymentComponentState.data.amount = amount
Logger.d(TAG, "Payment amount set: $amount")
}
else -> {
Logger.d(TAG, "Payment amount not set")
}
}
currentOrder?.let {
paymentComponentState.data.order = createOrder(it)
Logger.d(TAG, "Order appended to payment")
}
}
private fun createOrder(order: OrderModel): OrderRequest {
return OrderRequest(
pspReference = order.pspReference,
orderData = order.orderData
)
}
fun handlePaymentMethodsUpdate(paymentMethodsApiResponse: PaymentMethodsApiResponse, order: OrderResponse?) {
viewModelScope.launch(Dispatchers.IO) {
handleOrderResponse(order)
this@DropInViewModel.paymentMethodsApiResponse = paymentMethodsApiResponse
sendEvent(DropInActivityEvent.ShowPaymentMethods)
}
}
fun removeStoredPaymentMethodWithId(id: String) {
val positionToRemove = getStoredPaymentMethods().indexOfFirst { it.id == id }
val updatedStoredPaymentMethods = getStoredPaymentMethods().toMutableList()
if (positionToRemove != -1) {
updatedStoredPaymentMethods.removeAt(positionToRemove)
paymentMethodsApiResponse?.storedPaymentMethods = updatedStoredPaymentMethods
}
}
private suspend fun getOrderDetails(orderResponse: OrderResponse?): OrderModel? {
if (orderResponse == null) return null
return orderStatusRepository.getOrderStatus(dropInConfiguration, orderResponse.orderData)
.fold(
onSuccess = { statusResponse ->
OrderModel(
orderData = orderResponse.orderData,
pspReference = orderResponse.pspReference,
remainingAmount = statusResponse.remainingAmount,
paymentMethods = statusResponse.paymentMethods
)
},
onFailure = { e ->
Logger.e(PaymentMethodsListViewModel.TAG, "Unable to fetch order details", e)
null
}
)
}
fun partialPaymentRequested() {
val paymentComponentState = cachedGiftCardComponentState
?: throw CheckoutException("Lost reference to cached GiftCardComponentState")
val partialPaymentAmount = cachedPartialPaymentAmount
?: throw CheckoutException("Lost reference to cached partial payment amount")
paymentComponentState.data.amount = partialPaymentAmount
Logger.d(TAG, "Partial payment amount set: $partialPaymentAmount")
cachedGiftCardComponentState = null
cachedPartialPaymentAmount = null
sendEvent(DropInActivityEvent.MakePartialPayment(paymentComponentState))
}
fun orderCancellationRequested() {
val order = currentOrder
?: throw CheckoutException("No order in progress")
sendCancelOrderEvent(order, false)
}
fun cancelDropIn() {
currentOrder?.let { sendCancelOrderEvent(it, true) }
sendEvent(DropInActivityEvent.CancelDropIn)
}
private fun sendCancelOrderEvent(order: OrderModel, isDropInCancelledByUser: Boolean) {
val orderRequest = OrderRequest(
pspReference = order.pspReference,
orderData = order.orderData
)
sendEvent(DropInActivityEvent.CancelOrder(orderRequest, isDropInCancelledByUser))
}
private fun sendEvent(event: DropInActivityEvent) {
viewModelScope.launch {
Logger.d(TAG, "sendEvent - ${event::class.simpleName}")
eventChannel.send(event)
}
}
companion object {
private val TAG = LogUtil.getTag()
private const val PAYMENT_METHODS_RESPONSE_KEY = "PAYMENT_METHODS_RESPONSE_KEY"
private const val SESSION_KEY = "SESSION_KEY"
private const val DROP_IN_CONFIGURATION_KEY = "DROP_IN_CONFIGURATION_KEY"
private const val DROP_IN_SERVICE_KEY = "DROP_IN_SERVICE_KEY"
private const val IS_WAITING_FOR_RESULT_KEY = "IS_WAITING_FOR_RESULT_KEY"
private const val CACHED_GIFT_CARD = "CACHED_GIFT_CARD"
private const val CURRENT_ORDER = "CURRENT_ORDER"
private const val PARTIAL_PAYMENT_AMOUNT = "PARTIAL_PAYMENT_AMOUNT"
private const val AMOUNT = "AMOUNT"
fun putIntentExtras(
intent: Intent,
dropInConfiguration: DropInConfiguration,
paymentMethodsApiResponse: PaymentMethodsApiResponse,
service: ComponentName,
) {
intent.apply {
putExtra(PAYMENT_METHODS_RESPONSE_KEY, paymentMethodsApiResponse)
putExtra(DROP_IN_CONFIGURATION_KEY, dropInConfiguration)
putExtra(DROP_IN_SERVICE_KEY, service)
}
}
fun putIntentExtras(
intent: Intent,
dropInConfiguration: DropInConfiguration,
session: Session,
service: ComponentName,
) {
intent.apply {
putExtra(SESSION_KEY, session)
putExtra(DROP_IN_CONFIGURATION_KEY, dropInConfiguration)
putExtra(DROP_IN_SERVICE_KEY, service)
}
}
}
}