package com.example.checkout import org.json.JSONObject import java.text.ParsePosition import java.text.SimpleDateFormat import java.util.Locale // Reject malformed/expired instructions before an SDK launch. No provider signing here. fun requireUsableInvocation(invocation: JSONObject, kind: String) { require(invocation.getString("kind") == kind) { "Unexpected invocation kind" } val timestamp = invocation.getString("expires_at") val pattern = Regex("^(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})(?:\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})$") val parts = pattern.matchEntire(timestamp) ?: error("Invalid invocation expiry") // Dropping subsecond precision makes this check conservative by less than one second. val offset = if (parts.groupValues[2] == "Z") "+0000" else parts.groupValues[2].replace(":", "") val normalized = parts.groupValues[1] + offset val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US).apply { isLenient = false } val position = ParsePosition(0) val expiry = formatter.parse(normalized, position) require(expiry != null && position.index == normalized.length && expiry.time > System.currentTimeMillis()) { "Invalid or expired payment instructions; check the original order" } }