Skip to content

Commit

Permalink
Get __shared__ for onReady
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinpaypal committed May 13, 2024
1 parent 3464d64 commit 57acf20
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
15 changes: 14 additions & 1 deletion library/src/main/java/com/paypal/messages/ModalFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
import com.paypal.messages.analytics.AnalyticsComponent
import com.paypal.messages.analytics.AnalyticsEvent
Expand All @@ -53,6 +54,7 @@ internal class ModalFragment(
) : BottomSheetDialogFragment() {
private val TAG = "PayPalMessageModal"
private val offsetTop = 50.dp
private val gson = GsonBuilder().setPrettyPrinting().create()

private var modalUrl: String? = null

Expand Down Expand Up @@ -371,7 +373,7 @@ internal class ModalFragment(
val nameAndArgs = JsonParser.parseString(params).asJsonObject
val name = nameAndArgs.get("name").asString
val args = nameAndArgs.get("args").asJsonArray[0].asJsonObject
LogCat.debug(TAG, "CallbackHandler:\n name = $name\n args = $args")
LogCat.debug(TAG, "CallbackHandler:\n name = $name\n args = ${gson.toJson(args)}")

// If __shared__ does not exist, use an empty object
val sharedJson = args.get("__shared__") ?: JsonParser.parseString("{}")
Expand Down Expand Up @@ -408,6 +410,17 @@ internal class ModalFragment(
)
}

"onReady" -> {
logEvent(
AnalyticsEvent(
eventType = EventType.MESSAGE_RENDERED,
renderDuration = args.get("render_duration")?.asString ?: "",
requestDuration = args.get("request_duration")?.asString ?: "",
),
shared,
)
}

else -> {
// do something else or throw an exception
}
Expand Down
44 changes: 2 additions & 42 deletions library/src/main/java/com/paypal/messages/io/Api.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.paypal.messages.io

import android.annotation.SuppressLint
import android.content.Context
import com.google.gson.GsonBuilder
import com.paypal.messages.BuildConfig
Expand All @@ -14,59 +13,20 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okio.IOException
import org.json.JSONObject
import java.security.cert.X509Certificate
import java.util.UUID
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import com.paypal.messages.config.PayPalEnvironment as Env
import com.paypal.messages.config.PayPalMessageOfferType as OfferType
import com.paypal.messages.config.message.PayPalMessageConfig as MessageConfig

object Api {
private const val TAG = "Api"

private val trustAllCerts = arrayOf<TrustManager>(
@SuppressLint("CustomX509TrustManager")
object : X509TrustManager {
@SuppressLint("TrustAllX509TrustManager")
override fun checkClientTrusted(
chain: Array<out X509Certificate>?,
authType: String?,
) {}

@SuppressLint("TrustAllX509TrustManager")
override fun checkServerTrusted(
chain: Array<out X509Certificate>?,
authType: String?,
) {}

override fun getAcceptedIssuers() = arrayOf<X509Certificate>()
},
)

private val sslContext: SSLContext = SSLContext.getInstance("SSL")
.apply {
this.init(null, trustAllCerts, java.security.SecureRandom())
}

// Create an ssl socket factory with our all-trusting manager
private val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory

// connect to server
@Suppress("unused")
private val insecureClient = OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
.hostnameVerifier { _, _ -> true }
.build()

private val client = OkHttpClient()
// 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 devTouchpoint: Boolean = false
Expand Down
49 changes: 49 additions & 0 deletions library/src/main/java/com/paypal/messages/io/ApiClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.paypal.messages.io

import android.annotation.SuppressLint
import okhttp3.OkHttpClient
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

object ApiClient {
// trustAllCerts is only used for connecting to test environments
private val trustAllCerts = arrayOf<TrustManager>(
@SuppressLint("CustomX509TrustManager")
object : X509TrustManager {
@SuppressLint("TrustAllX509TrustManager")
override fun checkClientTrusted(
chain: Array<out X509Certificate>?,
authType: String?,
) {}

@SuppressLint("TrustAllX509TrustManager")
override fun checkServerTrusted(
chain: Array<out X509Certificate>?,
authType: String?,
) {}

override fun getAcceptedIssuers() = arrayOf<X509Certificate>()
},
)

// sslContext is only used for connecting to test environments
private val sslContext: SSLContext = SSLContext.getInstance("SSL")
.apply {
this.init(null, trustAllCerts, java.security.SecureRandom())
}

// sslSocketFactor is only used for connecting to test environments
private val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory

// insecureClient is only used for connecting to test environments
@Suppress("unused")
internal val insecureClient = OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
.hostnameVerifier { _, _ -> true }
.build()

internal val secureClient = OkHttpClient()
}

0 comments on commit 57acf20

Please sign in to comment.