Skip to content

Commit

Permalink
refactor: update PayPalEnvironment to use sealed class
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinpaypal committed Jun 22, 2024
1 parent 4a9d720 commit ee7813f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 53 deletions.
16 changes: 0 additions & 16 deletions demo/src/main/java/com/paypal/messagesdemo/EnvVars.kt

This file was deleted.

1 change: 0 additions & 1 deletion demo/src/main/java/com/paypal/messagesdemo/XmlActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,5 @@ class XmlActivity : AppCompatActivity() {
message.onLoading = {}
message.onSuccess = {}
message.onError = {}
EnvVars.getClientId(PayPalEnvironment.STAGE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,34 @@ package com.paypal.messages.config
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl

enum class PayPalEnvironment {
LOCAL,
STAGE,
SANDBOX,
LIVE,
;

val isProduction: Boolean
get() = this == LIVE || this == SANDBOX

private var host: String? = null
private var port: Int? = null
sealed class PayPalEnvironment {
abstract val presentmentUrl: String
abstract val loggerBaseUrl: String
abstract val isProduction: Boolean

class LocalEnvironment(port: Int) : PayPalEnvironment() {
override val presentmentUrl = "http://localhost:$port"
override val loggerBaseUrl = presentmentUrl
override val isProduction = false
}

companion object {
fun local(port: Int = 8443): PayPalEnvironment {
LOCAL.port = port
return LOCAL
}
fun stage(host: String): PayPalEnvironment {
STAGE.host = host
return STAGE
}
class StageEnvironment(host: String) : PayPalEnvironment() {
override val presentmentUrl = "https://www.$host"
override val loggerBaseUrl = "https://api.$host"
override val isProduction = false
}

private val presentmentUrl: String
get() = when (this) {
LOCAL -> "http://localhost:$port"
STAGE -> "https://www.$host"
SANDBOX -> "https://www.sandbox.paypal.com"
LIVE -> "https://www.paypal.com"
}
class SandboxEnvironment : PayPalEnvironment() {
override val presentmentUrl = "https://www.sandbox.paypal.com"
override val loggerBaseUrl = "https://api.sandbox.paypal.com"
override val isProduction = true
}

private val loggerBaseUrl: String
get() = when (this) {
LOCAL -> presentmentUrl
STAGE -> "https://api.$host"
SANDBOX -> "https://api.sandbox.paypal.com"
LIVE -> "https://api.paypal.com"
}
class LiveEnvironment : PayPalEnvironment() {
override val presentmentUrl = "https://www.paypal.com"
override val loggerBaseUrl = "https://api.paypal.com"
override val isProduction = true
}

enum class Endpoints(val path: String) {
MESSAGE_DATA("credit-presentment/native/message"),
Expand All @@ -54,4 +43,18 @@ enum class PayPalEnvironment {
val baseUrl = if (endpoint === Endpoints.LOGGER) loggerBaseUrl else presentmentUrl
return "$baseUrl/${endpoint.path}".toHttpUrl()
}

@Suppress("FunctionName")
companion object {
fun LOCAL(port: Int = 8443): LocalEnvironment {
return LocalEnvironment(port)
}

fun STAGE(host: String): StageEnvironment {
return StageEnvironment(host)
}

val SANDBOX = SandboxEnvironment()
val LIVE = LiveEnvironment()
}
}
2 changes: 1 addition & 1 deletion library/src/main/java/com/paypal/messages/io/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object Api {
// if connecting to a test environment, use ApiClient.insecureClient
private val client = ApiClient.secureClient
private val gson = GsonBuilder().setPrettyPrinting().create()
var env = Env.SANDBOX
var env: Env = Env.SANDBOX
var devTouchpoint: Boolean = false
var ignoreCache: Boolean = false
var stageTag: String? = null
Expand Down

0 comments on commit ee7813f

Please sign in to comment.