diff --git a/forage-android/src/main/java/com/joinforage/forage/android/ecom/services/vault/bt/BTResponseParser.kt b/forage-android/src/main/java/com/joinforage/forage/android/ecom/services/vault/bt/BTResponseParser.kt index 21ffc2d06..09990e659 100644 --- a/forage-android/src/main/java/com/joinforage/forage/android/ecom/services/vault/bt/BTResponseParser.kt +++ b/forage-android/src/main/java/com/joinforage/forage/android/ecom/services/vault/bt/BTResponseParser.kt @@ -6,6 +6,7 @@ import com.joinforage.forage.android.core.services.forageapi.network.ForageApiRe import com.joinforage.forage.android.core.services.forageapi.network.ForageError import com.joinforage.forage.android.core.services.forageapi.network.UnknownErrorApiResponse import com.joinforage.forage.android.core.services.vault.VaultResponseParser +import org.json.JSONObject internal class BTResponseParser(btRes: Result) : VaultResponseParser { override val isNullResponse: Boolean = false @@ -63,9 +64,21 @@ internal class BTResponseParser(btRes: Result) : VaultResponseParser { return if (!isSuccessful) { null } else { - // the BT response get's parsed as a Map instead of String - val jsonResponse = Gson().toJson(vaultResponse) - ForageApiResponse.Success(jsonResponse.toString()) + // BT uses the Gson package under the hood to work with JSON + // Unfortunately, they are returning Gson parsed JSON instead + // of standard JSON which forces us to use Gson to get back + // the original JSON (we return a JSON string in our + // ForageApiResponse.Success) + val gsonParsedJson = Gson().toJson(vaultResponse) + + // for some really really strange reason, Gson wraps the + // actual JSON we care about into top-level "value" JSON + // field like so: { "value": } + // So, annoyingly, we need to unwrap it. Since deferred + // payments have an empty response, we need to consider + // the case where there is no top-level "value" field + val unwrapped = JSONObject(gsonParsedJson).optJSONObject("value") ?: "" + ForageApiResponse.Success(unwrapped.toString()) } } }