-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Boolti-309 fix: 토스 페이먼츠 OrderName 인코딩 버그 패치
Percent Encoding https://docs.tosspayments.com/reference/using-api/req-res#url-인코딩
- Loading branch information
Showing
2 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
tosspayments/src/main/java/com/nexters/boolti/tosspayments/extension/String.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |