Add JsonCreator annotation to data class constructors.

This commit is contained in:
Alex Hart 2022-11-15 15:14:55 -04:00
parent 09afb1be41
commit e7f1d3fc1a
4 changed files with 23 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package org.signal.donations
import android.net.Uri
import android.os.Parcelable
import androidx.annotation.WorkerThread
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException
import com.fasterxml.jackson.module.kotlin.jsonMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule
import com.fasterxml.jackson.module.kotlin.readValue
@ -130,7 +131,13 @@ class StripeApi(
fun getSetupIntent(stripeIntentAccessor: StripeIntentAccessor): StripeSetupIntent {
return when (stripeIntentAccessor.objectType) {
StripeIntentAccessor.ObjectType.SETUP_INTENT -> get("setup_intents/${stripeIntentAccessor.intentId}?client_secret=${stripeIntentAccessor.intentClientSecret}").use {
objectMapper.readValue(it.body()!!.string())
try {
objectMapper.readValue(it.body()!!.string())
} catch (e: InvalidDefinitionException) {
throw StripeError.FailedToParseSetupIntentResponseError(e)
} catch (e: Exception) {
throw StripeError.FailedToParseSetupIntentResponseError(null)
}
}
else -> error("Unsupported type")
}
@ -142,7 +149,13 @@ class StripeApi(
fun getPaymentIntent(stripeIntentAccessor: StripeIntentAccessor): StripePaymentIntent {
return when (stripeIntentAccessor.objectType) {
StripeIntentAccessor.ObjectType.PAYMENT_INTENT -> get("payment_intents/${stripeIntentAccessor.intentId}?client_secret=${stripeIntentAccessor.intentClientSecret}").use {
objectMapper.readValue(it.body()!!.string())
try {
objectMapper.readValue(it.body()!!.string())
} catch (e: InvalidDefinitionException) {
throw StripeError.FailedToParsePaymentIntentResponseError(e)
} catch (e: Exception) {
throw StripeError.FailedToParsePaymentIntentResponseError(null)
}
}
else -> error("Unsupported type")
}

View file

@ -1,6 +1,10 @@
package org.signal.donations
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException
sealed class StripeError(message: String) : Exception(message) {
class FailedToParsePaymentIntentResponseError(invalidDefCause: InvalidDefinitionException?) : StripeError("Failed to parse payment intent response: ${invalidDefCause?.type} ${invalidDefCause?.property} ${invalidDefCause?.beanDescription}")
class FailedToParseSetupIntentResponseError(invalidDefCause: InvalidDefinitionException?) : StripeError("Failed to parse setup intent response: ${invalidDefCause?.type} ${invalidDefCause?.property} ${invalidDefCause?.beanDescription}")
object FailedToParsePaymentMethodResponseError : StripeError("Failed to parse payment method response")
object FailedToCreatePaymentSourceFromCardData : StripeError("Failed to create payment source from card data")
class PostError(val statusCode: Int, val errorCode: String?, val declineCode: StripeDeclineCode?) : StripeError("postForm failed with code: $statusCode. errorCode: $errorCode. declineCode: $declineCode")

View file

@ -1,5 +1,6 @@
package org.signal.donations.json
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
@ -9,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
* See: https://stripe.com/docs/api/payment_intents/object
*/
@JsonIgnoreProperties(ignoreUnknown = true)
data class StripePaymentIntent(
data class StripePaymentIntent @JsonCreator constructor(
@JsonProperty("id") val id: String,
@JsonProperty("client_secret") val clientSecret: String,
@JsonProperty("status") val status: StripeIntentStatus,

View file

@ -1,5 +1,6 @@
package org.signal.donations.json
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
@ -9,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
* See: https://stripe.com/docs/api/setup_intents/object
*/
@JsonIgnoreProperties(ignoreUnknown = true)
data class StripeSetupIntent(
data class StripeSetupIntent @JsonCreator constructor(
@JsonProperty("id") val id: String,
@JsonProperty("client_secret") val clientSecret: String,
@JsonProperty("status") val status: StripeIntentStatus,