31 lines
1.4 KiB
Kotlin
31 lines
1.4 KiB
Kotlin
package com.yzx.kiosk.websocket
|
|
|
|
import com.google.gson.JsonObject
|
|
|
|
/** New gateway: code -> data(type=5) -> data(order). Older gateways used a flat data object. */
|
|
internal fun parsePurchaseCompletedEvent(message: JsonObject): UploadPhotoEvent.PaySuccess? = runCatching {
|
|
if (message.get("code")?.asString != "5") return null
|
|
val envelope = message.getAsJsonObject("data") ?: return null
|
|
val order = if (envelope.has("data")) {
|
|
if (envelope.get("type")?.asInt != 5) return null
|
|
envelope.getAsJsonObject("data") ?: return null
|
|
} else {
|
|
if (envelope.has("type") && envelope.get("type")?.asInt != 5) return null
|
|
envelope
|
|
}
|
|
val number = order.get("order_number")?.takeUnless { it.isJsonNull }?.asString
|
|
?.takeIf { it.isNotBlank() } ?: return null
|
|
val captureType = order.get("capture_type")?.asInt ?: return null
|
|
if (captureType !in listOf(1, 2)) return null
|
|
val imageIds = order.get("image_id")?.takeUnless { it.isJsonNull }
|
|
?.asJsonArray?.map { it.asInt }.orEmpty()
|
|
if (captureType == 2 && imageIds.isEmpty()) return null
|
|
UploadPhotoEvent.PaySuccess(
|
|
orderNumber = number,
|
|
captureType = captureType,
|
|
imageIds = imageIds,
|
|
fileMap = order.get("file_map")?.takeUnless { it.isJsonNull }?.toString().orEmpty(),
|
|
purchaseMode = order.get("purchase_mode")?.takeUnless { it.isJsonNull }?.asString,
|
|
)
|
|
}.getOrNull()
|