Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ internal class DynamicCheckoutInteractor(

init {
interactorScope.launch {
POLogger.info("Starting dynamic checkout.")
POLogger.info("Starting.", attributes = logAttributes)
dispatch(WillStart)
start()
POLogger.info("Started: waiting for user input.")
POLogger.info("Started.", attributes = logAttributes)
dispatch(DidStart)
}
}
Expand Down Expand Up @@ -143,7 +143,10 @@ internal class DynamicCheckoutInteractor(
cancelWebAuthorization()
configuration = configuration.copy(invoiceRequest = invoiceRequest)
logAttributes = logAttributes(invoiceId = invoiceRequest.invoiceId)
interactorScope.launch { start() }
interactorScope.launch {
start()
POLogger.info("Restarted with the new invoice.", attributes = logAttributes)
}
}

private fun cancelWebAuthorization() {
Expand All @@ -162,31 +165,30 @@ internal class DynamicCheckoutInteractor(
when (invoice.transaction?.status()) {
WAITING -> setStartedState(invoice)
AUTHORIZED, COMPLETED -> handleSuccess()
else -> _completion.update {
Failure(
ProcessOutResult.Failure(
code = Generic(),
message = "Unsupported invoice state. Please create new invoice and restart dynamic checkout."
)
else -> {
val failure = ProcessOutResult.Failure(
code = Generic(),
message = "Unsupported invoice state. Please create the new invoice and restart the dynamic checkout."
)
POLogger.warn("%s", failure, attributes = logAttributes)
_completion.update { Failure(failure) }
}
}
}.onFailure { failure ->
POLogger.warn("Failed to fetch the invoice: %s", failure, attributes = logAttributes)
_completion.update { Failure(failure) }
}
}

private suspend fun setStartedState(invoice: POInvoice) {
val paymentMethods = invoice.paymentMethods
if (paymentMethods.isNullOrEmpty()) {
_completion.update {
Failure(
ProcessOutResult.Failure(
code = Generic(),
message = "Missing payment methods configuration."
)
)
}
val failure = ProcessOutResult.Failure(
code = Generic(),
message = "Missing payment methods configuration."
)
POLogger.warn("%s", failure, attributes = logAttributes)
_completion.update { Failure(failure) }
return
}
val mappedPaymentMethods = paymentMethods.map(
Expand Down Expand Up @@ -438,6 +440,7 @@ internal class DynamicCheckoutInteractor(
return
}
paymentMethod(event.id)?.let { paymentMethod ->
POLogger.info("Selected payment method: %s", paymentMethod.original)
dispatch(DidSelectPaymentMethod(paymentMethod = paymentMethod.original))
resetPaymentMethods()
if (state.processingPaymentMethod != null) {
Expand All @@ -462,6 +465,7 @@ internal class DynamicCheckoutInteractor(
nativeAlternativePayment.reset()
authorizeInvoiceJob?.cancel()
authorizeInvoiceJob = null
POLogger.info("All payment methods has been reset.")
}

private fun start(paymentMethod: PaymentMethod) {
Expand Down Expand Up @@ -576,6 +580,7 @@ internal class DynamicCheckoutInteractor(
}

private fun onDialogAction(event: DialogAction) {
POLogger.debug("Dialog action: %s", event)
val paymentMethod = event.paymentMethodId?.let { paymentMethod(it) }
when (paymentMethod) {
is NativeAlternativePayment -> nativeAlternativePayment.onEvent(
Expand All @@ -589,6 +594,7 @@ internal class DynamicCheckoutInteractor(
}

private fun onSavedPaymentMethodsAction() {
POLogger.debug("Invoked saved payment methods.")
interactorScope.launch {
eventDispatcher.send(
DynamicCheckoutSavedPaymentMethodsRequest(
Expand Down Expand Up @@ -631,6 +637,7 @@ internal class DynamicCheckoutInteractor(
}
if (paymentMethod.isExpress()) {
if (dispatchEvents) {
POLogger.info("Selected payment method: %s", paymentMethod.original)
dispatch(DidSelectPaymentMethod(paymentMethod = paymentMethod.original))
}
resetPaymentMethods()
Expand All @@ -652,6 +659,7 @@ internal class DynamicCheckoutInteractor(
)
return
}
POLogger.info("Submitting payment method: %s", paymentMethod.original)
when (paymentMethod) {
is GooglePay -> submitGooglePay(paymentMethod)
is AlternativePayment -> submitAlternativePayment(paymentMethod)
Expand Down Expand Up @@ -782,6 +790,7 @@ internal class DynamicCheckoutInteractor(
}

private fun invalidateInvoice(reason: PODynamicCheckoutInvoiceInvalidationReason) {
POLogger.info("Invalidating invoice. Reason: %s", reason, attributes = logAttributes)
var errorMessage: String? = null
if (reason is PODynamicCheckoutInvoiceInvalidationReason.Failure) {
_state.value.processingPaymentMethod?.let { paymentMethod ->
Expand Down Expand Up @@ -812,6 +821,7 @@ internal class DynamicCheckoutInteractor(
latestInvoiceRequest = request
interactorScope.launch {
eventDispatcher.send(request)
POLogger.info("Requested to provide the new invoice.")
}
}
}
Expand All @@ -834,6 +844,10 @@ internal class DynamicCheckoutInteractor(
)
is PODynamicCheckoutInvoiceInvalidationReason.Failure -> reason.failure
}
POLogger.warn(
message = "New invoice has not been provided. Invalidation failure: %s", failure,
attributes = logAttributes
)
_completion.update { Failure(failure) }
}
}
Expand Down Expand Up @@ -883,6 +897,7 @@ internal class DynamicCheckoutInteractor(
eventDispatcher.subscribeForResponse<PODynamicCheckoutInvoiceAuthorizationResponse>(
coroutineScope = interactorScope
) { response ->
POLogger.info("Authorizing the invoice.", attributes = logAttributes)
val threeDSService = PODefaultProxy3DSService()
val job = invoicesService.authorizeInvoice(
request = response.request,
Expand Down Expand Up @@ -927,13 +942,25 @@ internal class DynamicCheckoutInteractor(
}
}

private fun handleSuccess() {
POLogger.info("Success: payment completed.", attributes = logAttributes)
dispatch(DidCompletePayment)
interactorScope.launch {
_sideEffects.send(DynamicCheckoutSideEffect.BeforeSuccess)
}
configuration.paymentSuccess?.let { paymentSuccess ->
_state.update { it.copy(delayedSuccess = true) }
handler.postDelayed(delayInMillis = paymentSuccess.durationSeconds * 1000L) {
_completion.update { Success }
}
} ?: _completion.update { Success }
}

private fun handleCompletions() {
interactorScope.launch {
_completion.collect { completion ->
when (completion) {
Success -> dispatch(DidCompletePayment)
is Failure -> dispatch(DidFail(completion.failure))
else -> {}
if (completion is Failure) {
dispatch(DidFail(completion.failure))
}
}
}
Expand Down Expand Up @@ -961,21 +988,10 @@ internal class DynamicCheckoutInteractor(
}
}

private fun handleSuccess() {
interactorScope.launch {
_sideEffects.send(DynamicCheckoutSideEffect.BeforeSuccess)
}
configuration.paymentSuccess?.let { paymentSuccess ->
_state.update { it.copy(delayedSuccess = true) }
handler.postDelayed(delayInMillis = paymentSuccess.durationSeconds * 1000L) {
_completion.update { Success }
}
} ?: _completion.update { Success }
}

private fun dispatch(event: PODynamicCheckoutEvent) {
interactorScope.launch {
eventDispatcher.send(event)
POLogger.debug("Event has been sent: %s", event)
}
}

Expand Down Expand Up @@ -1006,19 +1022,20 @@ internal class DynamicCheckoutInteractor(
when (sideEffect) {
is NativeAlternativePaymentSideEffect.PermissionRequest ->
activePaymentMethod()?.let { paymentMethod ->
_sideEffects.send(
DynamicCheckoutSideEffect.PermissionRequest(
paymentMethodId = paymentMethod.id,
permission = sideEffect.permission
)
val permissionRequest = DynamicCheckoutSideEffect.PermissionRequest(
paymentMethodId = paymentMethod.id,
permission = sideEffect.permission
)
_sideEffects.send(permissionRequest)
POLogger.info("System permission requested: %s", permissionRequest)
}
}
}
}
}

private fun handlePermission(result: PermissionRequestResult) {
POLogger.info("System permission result: %s", result)
when (paymentMethod(result.paymentMethodId)) {
is NativeAlternativePayment -> nativeAlternativePayment.onEvent(
NativeAlternativePaymentEvent.PermissionRequestResult(
Expand All @@ -1036,6 +1053,7 @@ internal class DynamicCheckoutInteractor(
paymentMethods = state.paymentMethods
.filterNot { it.id == tokenId })
}
POLogger.debug("Deleted local customer token: %s", tokenId)
}

private fun collectPreferredScheme() {
Expand All @@ -1059,14 +1077,12 @@ internal class DynamicCheckoutInteractor(
}

private fun cancel() {
_completion.update {
Failure(
ProcessOutResult.Failure(
code = Cancelled,
message = "Cancelled by the user with cancel action."
).also { POLogger.info("Cancelled: %s", it) }
)
}
val failure = ProcessOutResult.Failure(
code = Cancelled,
message = "Cancelled by the user with the cancel action."
)
POLogger.info("Cancelled: %s", failure)
_completion.update { Failure(failure) }
}

private fun dismiss(event: Dismiss) {
Expand Down