Skip to content

Commit

Permalink
fix: correct urls (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinpaypal committed Apr 8, 2024
1 parent b7b746f commit f72ffda
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 29 deletions.
5 changes: 3 additions & 2 deletions demo/src/main/java/com/paypal/messagesdemo/JetpackActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fun toSentenceCase(input: String): String {
}

class JetpackActivity : ComponentActivity() {
private val environment = PayPalEnvironment.SANDBOX

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -59,7 +60,7 @@ class JetpackActivity : ComponentActivity() {
BasicTheme {
val context = LocalContext.current

var clientId: String by remember { mutableStateOf("") }
var clientId: String by remember { mutableStateOf(getString(R.string.client_id)) }

// Style Color
var backgroundColor by remember { mutableStateOf(Color.White) }
Expand Down Expand Up @@ -109,7 +110,7 @@ class JetpackActivity : ComponentActivity() {
val messageView = PayPalMessageView(
context,
config = PayPalMessageConfig(
data = PayPalMessageData(clientID = clientId, environment = PayPalEnvironment.SANDBOX),
data = PayPalMessageData(clientID = clientId, environment = environment),
viewStateCallbacks = PayPalMessageViewStateCallbacks(
onLoading = {
progressBar = true
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/java/com/paypal/messagesdemo/XmlActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class XmlActivity : AppCompatActivity() {
val amountString = amountEdit.text.toString()
val amount = if (amountString.isNotBlank()) amountString.toDouble() else null

val buyerCountry = buyerCountryEdit.text.toString().ifBlank { "US" }
val buyerCountry = buyerCountryEdit.text.toString().ifBlank { "" }

val backgroundColor = if (color === PayPalMessageColor.WHITE) Color.Black else Color.White
payPalMessage.setBackgroundColor(backgroundColor.hashCode())
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ tasks.register('addPreCommitGitHookOnBuild') {

preBuild.doFirst { addPreCommitGitHookOnBuild }

tasks.withType(Test) {
tasks.withType(Test).configureEach {
useJUnitPlatform()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,19 @@ class ApiTest {
val expectedQueryParts = arrayOf(
"client_id=test_client_id",
"instance_id",
"session_id",
)

assertTrue(url.contains(expectedPath))
assertTrue(url.contains("client_id=test_client_id"))
assertFalse(url.contains("devTouchpoint=false"))
assertFalse(url.contains("ignore_cache=false"))
assertTrue(url.contains("instance_id"))
assertTrue(url.contains("session_id"))
expectedQueryParts.forEach { assertTrue(url.contains(it)) }
}

@Test
fun testCreateMessageDataRequestWithAllData() {
Api.sessionId = UUID.randomUUID()
val config = MessageConfig(
data = PayPalMessageData(
clientID = "test_client_id",
Expand All @@ -112,7 +111,7 @@ class ApiTest {
assertTrue(url.contains(expectedPath))
assertTrue(url.contains("client_id=test_client_id"))
assertTrue(url.contains("instance_id"))
assertTrue(url.contains("session_id"))
assertTrue(url.contains("session_id")) //
assertTrue(url.contains("amount=1.0"))
assertTrue(url.contains("buyer_country=US"))
assertTrue(url.contains("offer=PAY_LATER_PAY_IN_1"))
Expand Down
15 changes: 7 additions & 8 deletions library/src/main/java/com/paypal/messages/ModalFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ internal class ModalFragment(
private var stageTag: String? = null

private var inErrorState: Boolean = false
private var currentUrl: String? = null
private var webView: WebView? = null
private var rootView: View? = null
private var dialog: BottomSheetDialog? = null
Expand Down Expand Up @@ -158,9 +157,9 @@ internal class ModalFragment(
view: WebView?,
request: WebResourceRequest?,
): Boolean {
val currentUri = URI.create(currentUrl)
val currentHost = currentUri.host
val currentPath = currentUri.path.split("/").getOrNull(1)
val currentUri = modalUrl?.let { URI.create(it) }
val currentHost = currentUri?.host
val currentPath = currentUri?.path?.split("/")?.getOrNull(1)
val requestUri = request?.url ?: return false
val requestHost = requestUri.host ?: return false
val requestPath = requestUri.pathSegments.getOrNull(0) ?: return false
Expand Down Expand Up @@ -190,7 +189,7 @@ internal class ModalFragment(
LogCat.debug(TAG, "Error Code ${e.errorCode}: ${e.description}")
}

val mainPageFailedToLoad = request?.url?.toString().equals(currentUrl)
val mainPageFailedToLoad = request?.url?.toString().equals(modalUrl)
val urlType = if (mainPageFailedToLoad) "Main" else "Non-main"
LogCat.debug(TAG, "$urlType URL failed: ${request?.url}")

Expand All @@ -210,7 +209,7 @@ internal class ModalFragment(
LogCat.debug(TAG, "HTTP Error Code ${e.statusCode}: ${e.reasonPhrase}\nData: ${e.data}")
}

val mainPageFailedToLoad = request?.url?.toString().equals(currentUrl)
val mainPageFailedToLoad = request?.url?.toString().equals(modalUrl)
val urlType = if (mainPageFailedToLoad) "Main" else "Non-main"
LogCat.debug(TAG, "$urlType URL failed: ${request?.url}")

Expand Down Expand Up @@ -312,7 +311,7 @@ internal class ModalFragment(
}

private fun openModalInBrowser() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(currentUrl))
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(modalUrl))
requireActivity().startActivity(intent)
}

Expand Down Expand Up @@ -341,7 +340,7 @@ internal class ModalFragment(
modalUrl = url
}
else {
LogCat.debug(TAG, "Modal reopened with original URL: $currentUrl")
LogCat.debug(TAG, "Modal reopened with original URL: $modalUrl")
}
}
this.webView?.loadUrl(url)
Expand Down
12 changes: 9 additions & 3 deletions library/src/main/java/com/paypal/messages/PayPalMessageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PayPalMessageView @JvmOverloads constructor(
buyerCountry = this.buyerCountry,
offerType = this.offerType,
pageType = this.pageType,
environment = this.environment ?: PayPalEnvironment.SANDBOX,
),
style = MessageStyle(this.color, this.logoType, this.textAlign),
viewStateCallbacks = ViewStateCallbacks(this.onLoading, this.onSuccess, this.onError),
Expand All @@ -91,7 +92,7 @@ class PayPalMessageView @JvmOverloads constructor(
merchantID = config.data.merchantID
partnerAttributionID = config.data.partnerAttributionID
amount = config.data.amount
buyerCountry = config.data.clientID
buyerCountry = config.data.buyerCountry
offerType = config.data.offerType
pageType = config.data.pageType
color = config.style.color
Expand Down Expand Up @@ -184,6 +185,7 @@ class PayPalMessageView @JvmOverloads constructor(
set(arg) {
if (field != arg) {
field = arg
Api.env = arg ?: PayPalEnvironment.SANDBOX
debounceUpdateContent(Unit)
}
}
Expand Down Expand Up @@ -289,7 +291,7 @@ class PayPalMessageView @JvmOverloads constructor(
val modalConfig = ModalConfig(
amount = this.amount,
buyerCountry = this.buyerCountry,
offer = this.offerType,
offer = response.meta?.offerType,
ignoreCache = false,
devTouchpoint = false,
stageTag = null,
Expand All @@ -312,7 +314,10 @@ class PayPalMessageView @JvmOverloads constructor(
// modal.show() above will display the modal on initial view, but if the user closes the modal
// it will become visually hidden and this method will re-display the modal without
// attempting to reattach it
modal.expand()
// the delay prevents noticeable shift when the offer type is changed
handler.postDelayed({
modal.expand()
}, 250)
}

override fun onDetachedFromWindow() {
Expand Down Expand Up @@ -489,6 +494,7 @@ class PayPalMessageView @JvmOverloads constructor(
* @param response the response obtained from the message content fetch process
*/
private fun updateContentValues(response: ApiMessageData.Response) {
modal?.offerType = response.meta?.offerType
messageContent = formatMessageContent(response, logoType)
messageLogoTag = response.meta?.variables?.logoPlaceholder
messageDisclaimer = response.content?.default?.disclaimer
Expand Down
23 changes: 13 additions & 10 deletions library/src/main/java/com/paypal/messages/io/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@ object Api {
instanceId: UUID,
) {
addQueryParameter("client_id", config.data.clientID)
devTouchpoint.toString()?.takeIf { it != "false" }?.let { addQueryParameter("devTouchpoint", it) }
ignoreCache.toString()?.takeIf { it != "false" }?.let { addQueryParameter("ignore_cache", it) }
if (devTouchpoint) addQueryParameter("dev_touchpoint", "true")
if (ignoreCache) addQueryParameter("ignore_cache", "true")
addQueryParameter("instance_id", instanceId.toString())
addQueryParameter("session_id", sessionId.toString())
sessionId?.let { addQueryParameter("session_id", it.toString()) }

if (!stageTag.isNullOrBlank()) { addQueryParameter("stage_tag", stageTag) }
config.style.logoType.let { addQueryParameter("logo_type", it.name.lowercase()) }
config.data.amount?.let { addQueryParameter("amount", it.toString()) }
config.data.buyerCountry?.let { addQueryParameter("buyer_country", it) }
config.data.offerType?.let { addQueryParameter("offer", it.name) }

hash?.let { addQueryParameter("merchant_config", it) }
addQueryParameter("logo_type", config.style.logoType.name.lowercase())
config.data.run {
amount?.let { addQueryParameter("amount", it.toString()) }
if (!buyerCountry.isNullOrBlank()) addQueryParameter("buyer_country", buyerCountry)
offerType?.let { addQueryParameter("offer", it.name) }
}

if (!hash.isNullOrBlank()) addQueryParameter("merchant_config", hash)
}

internal fun createMessageDataRequest(
Expand Down Expand Up @@ -209,7 +212,7 @@ object Api {
addQueryParameter("integration_type", BuildConfig.INTEGRATION_TYPE)
addQueryParameter("features", "native-modal")
amount?.let { addQueryParameter("amount", amount.toString()) }
buyerCountry?.let { addQueryParameter("buyer_country", buyerCountry) }
if (!buyerCountry.isNullOrBlank()) addQueryParameter("buyer_country", buyerCountry)
offer?.let { addQueryParameter("offer", it.name) }
}.build().toString()

Expand Down Expand Up @@ -243,7 +246,7 @@ object Api {
}

val integrationVersion = obj.get("integration_version")
if (checkIfEmpty(integrationName.toString())) {
if (checkIfEmpty(integrationVersion.toString())) {
obj.remove("integration_version")
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ echo git diff --staged --name-only | grep -E '(library|demo)'
echo ""
echo "Running ktFormat to fix any auto-fixable issues"

git stash --quiet --keep-index
git stash --keep-index --quiet

./gradlew ktFormat

Expand Down

0 comments on commit f72ffda

Please sign in to comment.