diff --git a/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmViewModel.kt b/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmViewModel.kt index af2903fa..dcfae1a7 100644 --- a/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmViewModel.kt +++ b/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmViewModel.kt @@ -98,7 +98,10 @@ class NativeApmViewModel( customerTokens.createCustomerToken( POCreateCustomerTokenRequest( customerId = customerId, - body = POCreateCustomerTokenRequestBody() + body = POCreateCustomerTokenRequestBody( + verify = true, + returnUrl = Constants.RETURN_URL_DEEP_LINK + ) ) ).getOrNull() diff --git a/sdk/src/main/kotlin/com/processout/sdk/api/model/request/POCreateCustomerTokenRequest.kt b/sdk/src/main/kotlin/com/processout/sdk/api/model/request/POCreateCustomerTokenRequest.kt index 82712855..a86e6d14 100644 --- a/sdk/src/main/kotlin/com/processout/sdk/api/model/request/POCreateCustomerTokenRequest.kt +++ b/sdk/src/main/kotlin/com/processout/sdk/api/model/request/POCreateCustomerTokenRequest.kt @@ -11,11 +11,34 @@ data class POCreateCustomerTokenRequest( val body: POCreateCustomerTokenRequestBody ) -/** @suppress */ +/** + * @suppress + * + * @param verify Whether to verify the customer token. Required when [invoiceId] or [manualInvoiceCancellation] is set. + * @param returnUrl URL to return to the app after the customer completes the redirect. + * Also used as the return URL of the invoice created for verification. + * @param invoiceId Invoice to associate with the token (buy and tokenize). + * @param manualInvoiceCancellation Whether you cancel the associated invoice yourself. + */ @ProcessOutInternalApi -@JsonClass(generateAdapter = true) data class POCreateCustomerTokenRequestBody( val verify: Boolean = false, + val returnUrl: String? = null, + val invoiceId: String? = null, + val manualInvoiceCancellation: Boolean? = null +) + +@JsonClass(generateAdapter = true) +internal data class CreateCustomerTokenRequestWithDeviceData( + val verify: Boolean, + @Json(name = "return_url") + val returnUrl: String?, @Json(name = "invoice_return_url") - val returnUrl: String? = null + val invoiceReturnUrl: String?, + @Json(name = "invoice_id") + val invoiceId: String?, + @Json(name = "manual_invoice_cancellation") + val manualInvoiceCancellation: Boolean?, + @Json(name = "device") + val deviceData: DeviceData ) diff --git a/sdk/src/main/kotlin/com/processout/sdk/api/model/request/napm/v2/PONativeAlternativePaymentRedirectConfirmation.kt b/sdk/src/main/kotlin/com/processout/sdk/api/model/request/napm/v2/PONativeAlternativePaymentRedirectConfirmation.kt index 32084346..947fe43e 100644 --- a/sdk/src/main/kotlin/com/processout/sdk/api/model/request/napm/v2/PONativeAlternativePaymentRedirectConfirmation.kt +++ b/sdk/src/main/kotlin/com/processout/sdk/api/model/request/napm/v2/PONativeAlternativePaymentRedirectConfirmation.kt @@ -1,5 +1,6 @@ package com.processout.sdk.api.model.request.napm.v2 +import com.processout.sdk.core.annotation.ProcessOutInternalApi import com.squareup.moshi.JsonClass /** @@ -9,5 +10,21 @@ import com.squareup.moshi.JsonClass */ @JsonClass(generateAdapter = true) data class PONativeAlternativePaymentRedirectConfirmation( - val success: Boolean -) + val success: Boolean, + @property:ProcessOutInternalApi + val result: Result? +) { + + constructor(success: Boolean) : this(success = success, result = null) + + /** + * Redirect result. + * + * @param[url] Return URL that customer was redirected to. + */ + @ProcessOutInternalApi + @JsonClass(generateAdapter = true) + data class Result( + val url: String + ) +} diff --git a/sdk/src/main/kotlin/com/processout/sdk/api/network/CustomerTokensApi.kt b/sdk/src/main/kotlin/com/processout/sdk/api/network/CustomerTokensApi.kt index 058683ca..a07ce851 100644 --- a/sdk/src/main/kotlin/com/processout/sdk/api/network/CustomerTokensApi.kt +++ b/sdk/src/main/kotlin/com/processout/sdk/api/network/CustomerTokensApi.kt @@ -1,8 +1,8 @@ package com.processout.sdk.api.network import com.processout.sdk.api.model.request.AssignCustomerTokenRequestWithDeviceData +import com.processout.sdk.api.model.request.CreateCustomerTokenRequestWithDeviceData import com.processout.sdk.api.model.request.POCreateCustomerRequest -import com.processout.sdk.api.model.request.POCreateCustomerTokenRequestBody import com.processout.sdk.api.model.request.napm.v2.NativeAlternativePaymentRequestBody import com.processout.sdk.api.model.response.CustomerResponse import com.processout.sdk.api.model.response.CustomerTokenResponse @@ -37,7 +37,7 @@ internal interface CustomerTokensApi { @POST("/customers/{customer_id}/tokens") suspend fun createCustomerToken( @Path("customer_id") customerId: String, - @Body request: POCreateCustomerTokenRequestBody + @Body request: CreateCustomerTokenRequestWithDeviceData ): Response @POST("/customers") diff --git a/sdk/src/main/kotlin/com/processout/sdk/api/repository/DefaultCustomerTokensRepository.kt b/sdk/src/main/kotlin/com/processout/sdk/api/repository/DefaultCustomerTokensRepository.kt index 753fbe97..1b3b1e6b 100644 --- a/sdk/src/main/kotlin/com/processout/sdk/api/repository/DefaultCustomerTokensRepository.kt +++ b/sdk/src/main/kotlin/com/processout/sdk/api/repository/DefaultCustomerTokensRepository.kt @@ -54,7 +54,12 @@ internal class DefaultCustomerTokensRepository( } override suspend fun createCustomerToken(request: POCreateCustomerTokenRequest) = - apiCall { api.createCustomerToken(request.customerId, request.body) }.let { result -> + apiCall { + api.createCustomerToken( + customerId = request.customerId, + request = request.body.withDeviceData() + ) + }.let { result -> when (result) { is ProcessOutResult.Success -> result.value.token?.let { token -> ProcessOutResult.Success(token) @@ -66,6 +71,16 @@ internal class DefaultCustomerTokensRepository( override suspend fun createCustomer(request: POCreateCustomerRequest) = apiCall { api.createCustomer(request) }.map { it.customer } + private fun POCreateCustomerTokenRequestBody.withDeviceData() = + CreateCustomerTokenRequestWithDeviceData( + verify = verify, + returnUrl = returnUrl, + invoiceReturnUrl = returnUrl, + invoiceId = invoiceId, + manualInvoiceCancellation = manualInvoiceCancellation, + deviceData = contextGraph.deviceData + ) + private fun POAssignCustomerTokenRequest.withDeviceData() = AssignCustomerTokenRequestWithDeviceData( source = source, diff --git a/sdk/src/main/kotlin/com/processout/sdk/core/POFailure.kt b/sdk/src/main/kotlin/com/processout/sdk/core/POFailure.kt index 0651d78f..26fd3aca 100644 --- a/sdk/src/main/kotlin/com/processout/sdk/core/POFailure.kt +++ b/sdk/src/main/kotlin/com/processout/sdk/core/POFailure.kt @@ -149,6 +149,8 @@ class POFailure private constructor() { invalidUrl("request.validation.invalid-url"), invalidUser("request.validation.invalid-user"), invalidRedirectResult("request.validation.redirect-result-invalid"), + unexpectedRedirectResult("request.validation.redirect-result-unexpected"), + unexpectedRedirectResultForStep("request.validation.redirect-result-unexpected-for-step"), missingCurrency("request.validation.missing-currency"), missingCustomerInput("gateway.missing-customer-input"), missingDescription("request.validation.missing-description"), diff --git a/ui/src/main/kotlin/com/processout/sdk/ui/napm/NativeAlternativePaymentInteractor.kt b/ui/src/main/kotlin/com/processout/sdk/ui/napm/NativeAlternativePaymentInteractor.kt index c73f744d..f97d9635 100644 --- a/ui/src/main/kotlin/com/processout/sdk/ui/napm/NativeAlternativePaymentInteractor.kt +++ b/ui/src/main/kotlin/com/processout/sdk/ui/napm/NativeAlternativePaymentInteractor.kt @@ -592,6 +592,13 @@ internal class NativeAlternativePaymentInteractor( } private fun handleDeepLink(uri: Uri) { + when (val flow = configuration.flow) { + is Authorization -> handleAuthorizationDeepLink(uri) + is Tokenization -> handleTokenizationDeepLink(flow, uri) + } + } + + private fun handleAuthorizationDeepLink(uri: Uri) { interactorScope.launch { val request = PONativeAlternativePaymentUrlResolutionRequest( redirect = PONativeAlternativePaymentUrlResolutionRequest.Redirect( @@ -615,18 +622,70 @@ internal class NativeAlternativePaymentInteractor( redirect = response.redirect ) }.onFailure { failure -> - val isInvalidRedirectResult = when (val code = failure.code) { - is Validation -> code.validationCode == ValidationCode.invalidRedirectResult - else -> false - } - if (isInvalidRedirectResult) { - return@onFailure + handleDeepLinkFailure(failure) + } + } + } + + private fun handleTokenizationDeepLink(flow: Tokenization, uri: Uri) { + interactorScope.launch { + val request = PONativeAlternativePaymentTokenizationRequest( + customerId = flow.customerId, + customerTokenId = flow.customerTokenId, + gatewayConfigurationId = flow.gatewayConfigurationId, + configuration = flow.configuration, + redirectConfirmation = PONativeAlternativePaymentRedirectConfirmation( + success = true, + result = PONativeAlternativePaymentRedirectConfirmation.Result( + url = uri.toString() + ) + ) + ) + customerTokensService.tokenize(request) + .onSuccess { response -> + if (response.state == PENDING && _state.value is Pending) { + return@onSuccess } - _completion.update { Failure(failure) } + handlePaymentState( + stateValue = initNextStepStateValue( + paymentMethod = response.paymentMethod, + invoice = null + ), + paymentState = response.state, + elements = response.elements, + redirect = response.redirect + ) + }.onFailure { failure -> + handleDeepLinkFailure(failure) } } } + private fun handleDeepLinkFailure(failure: ProcessOutResult.Failure) { + // Redirect result is either unrelated to this payment or no longer needed, so payment continues as is. + // Note that capture polling only stops on success, so if payment has advanced to another step + // it is not shown until polling times out. + val isIgnoredRedirectResult = when (val code = failure.code) { + is Validation -> code.validationCode in listOf( + ValidationCode.invalidRedirectResult, + ValidationCode.unexpectedRedirectResult, + ValidationCode.unexpectedRedirectResultForStep + ) + else -> false + } + if (isIgnoredRedirectResult) { + if (_state.value is NextStep) { + // Nothing else advances the next step after the redirect, so payment may not progress. + POLogger.warn( + message = "Ignored redirect result while waiting for next step: %s", failure, + attributes = configuration.logAttributes + ) + } + return + } + _completion.update { Failure(failure) } + } + private fun PONativeAlternativePaymentInvoice.map() = Invoice( id = id, amount = amount,