diff --git a/sdk/src/main/kotlin/com/processout/sdk/api/service/DefaultAlternativePaymentMethodsService.kt b/sdk/src/main/kotlin/com/processout/sdk/api/service/DefaultAlternativePaymentMethodsService.kt index dddd968b6..bbf1d37b1 100644 --- a/sdk/src/main/kotlin/com/processout/sdk/api/service/DefaultAlternativePaymentMethodsService.kt +++ b/sdk/src/main/kotlin/com/processout/sdk/api/service/DefaultAlternativePaymentMethodsService.kt @@ -1,12 +1,14 @@ package com.processout.sdk.api.service import android.net.Uri +import androidx.core.net.toUri import com.processout.sdk.api.model.request.POAlternativePaymentMethodRequest import com.processout.sdk.api.model.response.POAlternativePaymentMethodResponse import com.processout.sdk.api.model.response.POAlternativePaymentMethodResponse.APMReturnType import com.processout.sdk.core.POFailure.* import com.processout.sdk.core.POFailure.Code.* import com.processout.sdk.core.ProcessOutResult +import com.processout.sdk.core.logger.POLogger import com.processout.sdk.core.util.findBy import com.processout.sdk.di.ContextGraph @@ -39,7 +41,7 @@ internal class DefaultAlternativePaymentMethodsService( } arrayList } - val uriBuilder = Uri.parse(baseUrl).buildUpon() + val uriBuilder = baseUrl.toUri().buildUpon() pathComponents.forEach { uriBuilder.appendPath(it) } request.additionalData?.forEach { uriBuilder.appendQueryParameter("additional_data[${it.key}]", it.value) @@ -54,24 +56,33 @@ internal class DefaultAlternativePaymentMethodsService( message = "Invalid or malformed alternative payment method URI: $uri" ) } - + uri.getQueryParameter("token")?.let { gatewayToken -> + if (gatewayToken.isEmpty()) { + POLogger.debug("Gateway 'token' is empty in the URI: %s", uri) + } + val customerId = uri.getQueryParameter("customer_id") + val tokenId = uri.getQueryParameter("token_id") + val returnType = if (customerId != null && tokenId != null) + APMReturnType.CREATE_TOKEN else APMReturnType.AUTHORIZATION + return ProcessOutResult.Success( + POAlternativePaymentMethodResponse( + gatewayToken = gatewayToken, + customerId = customerId, + tokenId = tokenId, + returnType = returnType + ) + ) + } uri.getQueryParameter("error_code")?.let { errorCode -> return ProcessOutResult.Failure(failureCode(errorCode)) } - - val gatewayToken = uri.getQueryParameter("token") ?: String() - - val customerId = uri.getQueryParameter("customer_id") - val tokenId = uri.getQueryParameter("token_id") - val returnType = if (customerId != null && tokenId != null) - APMReturnType.CREATE_TOKEN else APMReturnType.AUTHORIZATION - + POLogger.warn("Neither the gateway 'token' nor 'error_code' is set in the URI: %s", uri) return ProcessOutResult.Success( POAlternativePaymentMethodResponse( - gatewayToken = gatewayToken, - customerId = customerId, - tokenId = tokenId, - returnType = returnType + gatewayToken = String(), + customerId = null, + tokenId = null, + returnType = APMReturnType.AUTHORIZATION ) ) }