From ff0765cb93d7ddce2982c21113676f5db9eab5bc Mon Sep 17 00:00:00 2001 From: mangbaam Date: Mon, 14 Oct 2024 20:33:21 +0900 Subject: [PATCH] =?UTF-8?q?Boolti-309=20fix:=20=ED=86=A0=EC=8A=A4=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EB=A8=BC=EC=B8=A0=20OrderName=20=EC=9D=B8?= =?UTF-8?q?=EC=BD=94=EB=94=A9=20=EB=B2=84=EA=B7=B8=20=ED=8C=A8=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Percent Encoding https://docs.tosspayments.com/reference/using-api/req-res#url-인코딩 --- .../tosspayments/TossPaymentWidgetActivity.kt | 5 +- .../boolti/tosspayments/extension/String.kt | 47 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tosspayments/src/main/java/com/nexters/boolti/tosspayments/extension/String.kt diff --git a/tosspayments/src/main/java/com/nexters/boolti/tosspayments/TossPaymentWidgetActivity.kt b/tosspayments/src/main/java/com/nexters/boolti/tosspayments/TossPaymentWidgetActivity.kt index bd938282..03098d77 100644 --- a/tosspayments/src/main/java/com/nexters/boolti/tosspayments/TossPaymentWidgetActivity.kt +++ b/tosspayments/src/main/java/com/nexters/boolti/tosspayments/TossPaymentWidgetActivity.kt @@ -12,6 +12,7 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import com.nexters.boolti.tosspayments.databinding.ActivityTossPaymentWidgetBinding +import com.nexters.boolti.tosspayments.extension.percentEncode import com.nexters.boolti.tosspayments.extension.toCurrency import com.tosspayments.paymentsdk.PaymentWidget import com.tosspayments.paymentsdk.model.AgreementStatus @@ -259,7 +260,7 @@ class TossPaymentWidgetActivity : AppCompatActivity() { .putExtra(EXTRA_KEY_CLIENT_KEY, clientKey) .putExtra(EXTRA_KEY_CUSTOMER_KEY, customerKey) .putExtra(EXTRA_KEY_ORDER_ID, orderId) - .putExtra(EXTRA_KEY_ORDER_NAME, orderName) + .putExtra(EXTRA_KEY_ORDER_NAME, orderName.percentEncode()) .putExtra(EXTRA_KEY_CURRENCY, currency.toCurrency()) .putExtra(EXTRA_KEY_COUNTRY_CODE, countryCode) .putExtra(EXTRA_KEY_VARIANT_KEY, variantKey) @@ -298,7 +299,7 @@ class TossPaymentWidgetActivity : AppCompatActivity() { .putExtra(EXTRA_KEY_CLIENT_KEY, clientKey) .putExtra(EXTRA_KEY_CUSTOMER_KEY, customerKey) .putExtra(EXTRA_KEY_ORDER_ID, orderId) - .putExtra(EXTRA_KEY_ORDER_NAME, orderName) + .putExtra(EXTRA_KEY_ORDER_NAME, orderName.percentEncode()) .putExtra(EXTRA_KEY_CURRENCY, currency.toCurrency()) .putExtra(EXTRA_KEY_COUNTRY_CODE, countryCode) .putExtra(EXTRA_KEY_SHOW_ID, showId) diff --git a/tosspayments/src/main/java/com/nexters/boolti/tosspayments/extension/String.kt b/tosspayments/src/main/java/com/nexters/boolti/tosspayments/extension/String.kt new file mode 100644 index 00000000..bb1db3c0 --- /dev/null +++ b/tosspayments/src/main/java/com/nexters/boolti/tosspayments/extension/String.kt @@ -0,0 +1,47 @@ +package com.nexters.boolti.tosspayments.extension + +private val encodingMap = mapOf( + ':' to "%3A", + '/' to "%2F", + '?' to "%3F", + '#' to "%23", + '[' to "%5B", + ']' to "%5D", + '@' to "%40", + '!' to "%21", + '$' to "%24", + '&' to "%26", + '\'' to "%27", + '(' to "%28", + ')' to "%29", + '*' to "%2A", + '+' to "%2B", + ',' to "%2C", + ';' to "%3B", + '=' to "%3D", + '%' to "%25", + ' ' to "+" +) + +private val decodingMap = encodingMap.map { it.value to it.key }.toMap() + +fun String.percentEncode(): String = fold(StringBuilder()) { sb, c -> + sb.append(encodingMap.getOrDefault(c, c)) +}.toString() + +fun String.percentDecode(): String { + val stringBuilder = StringBuilder() + var i = 0 + while (i < this.length) { + if (this[i] == '%' && i + 2 < this.length) { + val encodedValue = this.substring(i, i + 3) + val decodedChar = decodingMap.getOrDefault(encodedValue, encodedValue) + stringBuilder.append(decodedChar) + i += 3 + } else { + stringBuilder.append(this[i]) + i++ + } + } + return stringBuilder.toString() +}