Skip to content

Commit

Permalink
Remove ApproveOrderListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
sshropshire committed Dec 5, 2024
1 parent 1362c12 commit e396f74
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 107 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.braintreepayments.api.BrowserSwitchFinalResult
import com.braintreepayments.api.BrowserSwitchOptions
import com.braintreepayments.api.BrowserSwitchStartResult
import com.paypal.android.corepayments.BrowserSwitchRequestCodes
import com.paypal.android.corepayments.PayPalSDKError
import org.json.JSONObject

internal class CardAuthLauncher(
Expand Down Expand Up @@ -60,6 +61,36 @@ internal class CardAuthLauncher(
}
}

fun completeApproveOrderAuthRequest(
intent: Intent,
authState: String
): CardResult.FinishApproveOrder =
when (val finalResult = browserSwitchClient.completeRequest(intent, authState)) {
is BrowserSwitchFinalResult.Success -> {
if (finalResult.requestCode == BrowserSwitchRequestCodes.CARD_APPROVE_ORDER) {
val orderId = finalResult.requestMetadata?.optString(METADATA_KEY_ORDER_ID)
if (orderId == null) {
CardResult.FinishApproveOrder.Failure(CardError.unknownError)
} else {
CardResult.FinishApproveOrder.Success(
orderId = orderId,
didAttemptThreeDSecureAuthentication = true
)
}
} else {
CardResult.FinishApproveOrder.NoResult
}
}

is BrowserSwitchFinalResult.Failure -> {
val message = "Browser switch failed"
val browserSwitchError = PayPalSDKError(0, message, reason = finalResult.error)
CardResult.FinishApproveOrder.Failure(browserSwitchError)
}

BrowserSwitchFinalResult.NoResult -> CardResult.FinishApproveOrder.NoResult
}

fun completeAuthRequest(intent: Intent, authState: String): CardStatus =
when (val finalResult = browserSwitchClient.completeRequest(intent, authState)) {
is BrowserSwitchFinalResult.Success -> parseBrowserSwitchSuccessResult(finalResult)
Expand All @@ -69,7 +100,6 @@ internal class CardAuthLauncher(

private fun parseBrowserSwitchSuccessResult(result: BrowserSwitchFinalResult.Success): CardStatus =
when (result.requestCode) {
BrowserSwitchRequestCodes.CARD_APPROVE_ORDER -> parseApproveOrderSuccessResult(result)
BrowserSwitchRequestCodes.CARD_VAULT -> parseVaultSuccessResult(result)
else -> CardStatus.NoResult
}
Expand All @@ -86,16 +116,4 @@ internal class CardAuthLauncher(
CardStatus.VaultSuccess(result)
}
}

private fun parseApproveOrderSuccessResult(
finalResult: BrowserSwitchFinalResult.Success,
): CardStatus {
val orderId = finalResult.requestMetadata?.optString(METADATA_KEY_ORDER_ID)
return if (orderId == null) {
CardStatus.ApproveOrderError(CardError.unknownError, null)
} else {
val result = LegacyCardResult(orderId = orderId, didAttemptThreeDSecureAuthentication = true)
CardStatus.ApproveOrderSuccess(result)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,22 @@ class CardClient internal constructor(
}

fun finishApproveOrder(intent: Intent, authState: String): CardResult.FinishApproveOrder {
return when (val result = authChallengeLauncher.completeAuthRequest(intent, authState)) {
is CardStatus.ApproveOrderSuccess -> {
analytics.notifyApproveOrderAuthChallengeSucceeded(result.result.orderId)
result.result.run { CardResult.FinishApproveOrder.Success(orderId, status) }
}
val result = authChallengeLauncher.completeApproveOrderAuthRequest(intent, authState)
when (result) {
is CardResult.FinishApproveOrder.Success ->
analytics.notifyApproveOrderAuthChallengeSucceeded(result.orderId)

is CardStatus.ApproveOrderError -> {
analytics.notifyApproveOrderAuthChallengeFailed(result.orderId)
CardResult.FinishApproveOrder.Failure(result.error)
}
is CardResult.FinishApproveOrder.Failure ->
analytics.notifyApproveOrderAuthChallengeFailed(null)

is CardStatus.ApproveOrderCanceled -> {
analytics.notifyApproveOrderAuthChallengeCanceled(result.orderId)
CardResult.FinishApproveOrder.Canceled
}
CardResult.FinishApproveOrder.Canceled ->
analytics.notifyApproveOrderAuthChallengeCanceled(null)

is CardStatus.UnknownError -> {
val description = "An unknown error occurred: ${result.error.message}"
val error = PayPalSDKError(0, description, reason = result.error)
CardResult.FinishApproveOrder.Failure(error)
else -> {
// no analytics tracking required at the moment
}

else -> CardResult.FinishApproveOrder.NoResult
}
return result
}

fun legacyCompleteAuthChallenge(intent: Intent, authState: String): CardStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import com.paypal.android.corepayments.PayPalSDKError

sealed class CardStatus {

class ApproveOrderError(val error: PayPalSDKError, val orderId: String?) : CardStatus()
class ApproveOrderSuccess(val result: LegacyCardResult) : CardStatus()
class ApproveOrderCanceled(val orderId: String?) : CardStatus()

class VaultError(val error: PayPalSDKError) : CardStatus()
class VaultSuccess(val result: CardVaultResult) : CardStatus()
class VaultCanceled(val setupTokenId: String?) : CardStatus()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.paypal.android.ui.approveorder
import androidx.compose.runtime.Immutable
import com.paypal.android.api.model.Order
import com.paypal.android.api.model.OrderIntent
import com.paypal.android.cardpayments.CardResult
import com.paypal.android.cardpayments.LegacyCardResult
import com.paypal.android.cardpayments.threedsecure.SCA
import com.paypal.android.uishared.enums.StoreInVaultOption
import com.paypal.android.uishared.state.ActionState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,21 @@ class ApproveOrderViewModel @Inject constructor(
cardClient?.removeObservers()
}

private fun checkIfApproveOrderFinished(intent: Intent): CardResult.FinishApproveOrder?
= authState?.let { cardClient?.finishApproveOrder(intent, it) }

fun completeAuthChallenge(intent: Intent) {
authState?.let { cardClient?.finishApproveOrder(intent, it) }?.let { result ->
when (result) {
checkIfApproveOrderFinished(intent)?.let { approveOrderResult ->
when (approveOrderResult) {
is CardResult.FinishApproveOrder.Success -> {
val orderInfo = result.run {
val orderInfo = approveOrderResult.run {
OrderInfo(orderId, status, didAttemptThreeDSecureAuthentication)
}
approveOrderState = ActionState.Success(orderInfo)
}

is CardResult.FinishApproveOrder.Failure -> {
approveOrderState = ActionState.Failure(result.error)
approveOrderState = ActionState.Failure(approveOrderResult.error)
}

CardResult.FinishApproveOrder.Canceled -> {
Expand Down

0 comments on commit e396f74

Please sign in to comment.