jw
03/24/2019, 5:10 PMdave08
03/24/2019, 5:23 PMoverride fun fromJson(reader: JsonReader): PaymentResult? {
val peeked = reader.peekJson()
return when(peeked.readId()) {
200 -> jsonAdapter.fromJson(reader)!!
403 -> PaymentResult.PaymentSuccededWithRegistrationNotCompleted
401 -> PaymentResult.CouponOrProductNotFound
402 -> PaymentResult.PaymentNotAccepted(reader.readError())
420 -> PaymentResult.SellerIdNotValid
else -> error("Invalid response from Odoo")
}
}
fun JsonReader.readId(): Int {
beginObject()
while (hasNext()) {
if(nextName() == "id") return nextInt() else skipValue()
}
error("Response Id not found")
}
fun JsonReader.readError(): String {
beginObject()
while (hasNext()) {
if(nextName() == "error") return nextString() else skipValue()
}
error("Response error message not found")
}
?