Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ class NativeApmViewModel(
customerTokens.createCustomerToken(
POCreateCustomerTokenRequest(
customerId = customerId,
body = POCreateCustomerTokenRequestBody()
body = POCreateCustomerTokenRequestBody(
verify = true,
returnUrl = Constants.RETURN_URL_DEEP_LINK
)
)
).getOrNull()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
@@ -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

/**
Expand All @@ -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
)
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<CustomerTokenResponse>

@POST("/customers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/main/kotlin/com/processout/sdk/core/POFailure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +595 to +597

@andrii-vysotskyi-cko andrii-vysotskyi-cko Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. This gap already exists on master for both the authorization and tokenization flows: the WebView fallback returns the URI only via the activity result, which the interactor ignores on success. It touches the launcher and WebView result plumbing, so it will be addressed in a separate PR.

}
}

private fun handleAuthorizationDeepLink(uri: Uri) {
interactorScope.launch {
val request = PONativeAlternativePaymentUrlResolutionRequest(
redirect = PONativeAlternativePaymentUrlResolutionRequest.Redirect(
Expand All @@ -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

@andrii-vysotskyi-cko andrii-vysotskyi-cko Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. The capture poller only stops on SUCCESS, which predates this PR, so a payment that advanced to another step isn't shown until polling times out. The misleading comment is corrected in this PR; the poller and next-step handling will be fixed in a separate PR.

)
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,
Expand Down
Loading