Skip to content

Commit

Permalink
Refactor existing code to support new types
Browse files Browse the repository at this point in the history
  • Loading branch information
djoksimo committed May 3, 2024
1 parent 2f959d0 commit 4e6ba8f
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 26 deletions.
1 change: 1 addition & 0 deletions forage-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import com.joinforage.ShadowAarDependenciesPlugin
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'kotlin-parcelize'
id("com.google.devtools.ksp").version("1.8.10-1.0.9")
id("org.jetbrains.kotlinx.kover") version "0.6.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.joinforage.forage.android

import android.content.res.TypedArray
import okhttp3.HttpUrl
import org.json.JSONObject
import kotlin.random.Random

/**
Expand Down Expand Up @@ -34,3 +35,11 @@ internal fun HttpUrl.Builder.addPathSegmentsSafe(path: String): HttpUrl.Builder
}
return this
}

internal fun JSONObject.getStringOrNull(fieldName: String): String? {
return if (has(fieldName)) {
getString(fieldName)
} else {
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,3 @@ sealed class Balance : ForageModel {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ data class Address(
val line1: String,
val line2: String,
val state: String,
val zipcode: String,
): ForageModel
val zipcode: String
) : ForageModel

/**
* @property created A UTC-8 timestamp of when the Receipt was created, represented as an ISO 8601 date-time string.
Expand All @@ -43,8 +43,8 @@ data class Receipt(
val otherAmount: String,
val salesTaxApplied: String,
val snapAmount: String,
val balance: Balance?,
): ForageModel
val balance: Balance?
) : ForageModel

/**
* @property amount A positive decimal number that represents how much the PaymentMethod
Expand Down Expand Up @@ -87,8 +87,8 @@ data class Payment(
val refunds: List<String>,
val status: String,
val successDate: String?,
val updated: String,
): ForageModel {
val updated: String
) : ForageModel {
internal object ModelMapper {
fun from(jsonString: String): Payment {
val jsonObject = JSONObject(jsonString)
Expand Down Expand Up @@ -155,11 +155,11 @@ internal fun toReceipt(jsonObject: JSONObject): Receipt {
message = jsonObject.getString("message"),
otherAmount = jsonObject.getString("other_amount"),
salesTaxApplied = jsonObject.getString("sales_tax_applied"),
snapAmount = jsonObject.getString("snap_amount"),
snapAmount = jsonObject.getString("snap_amount")
)
}

internal fun JSONArray.toListOfStrings(): List<String> =
List(this.length()) { index -> this.getString(index) }

internal fun JSONObject.toMap(): Map<String, String> = keys().asSequence().associateWith { get(it).toString() }
internal fun JSONObject.toMap(): Map<String, String> = keys().asSequence().associateWith { get(it).toString() }
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class CapturePaymentRepository(
is ForageApiResponse.Success -> Payment.ModelMapper.from(response.data)
else -> return response
}
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethod)) {
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethodRef)) {
is ForageApiResponse.Success -> PaymentMethod.ModelMapper.from(response.data)
else -> return response
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class DeferPaymentCaptureRepository(
is ForageApiResponse.Success -> Payment.ModelMapper.from(response.data)
else -> return response
}
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethod)) {
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethodRef)) {
is ForageApiResponse.Success -> PaymentMethod.ModelMapper.from(response.data)
else -> return response
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class DeferPaymentRefundRepository(
is ForageApiResponse.Success -> Payment.ModelMapper.from(response.data)
else -> return response
}
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethod)) {
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethodRef)) {
is ForageApiResponse.Success -> PaymentMethod.ModelMapper.from(response.data)
else -> return response
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.joinforage.forage.android.network.model

import com.joinforage.forage.android.model.Balance
import com.joinforage.forage.android.model.Payment
import com.joinforage.forage.android.model.PaymentMethod
import org.json.JSONException
import org.json.JSONObject

Expand All @@ -22,7 +25,22 @@ sealed class ForageApiResponse<out T> {
* }
* ```
*/
data class Success<out T>(val data: T) : ForageApiResponse<T>()
data class Success<out T>(val data: T) : ForageApiResponse<T>() {
fun toPaymentMethod(): PaymentMethod {
return PaymentMethod.ModelMapper.from(data as String)
}

fun toBalance(): Balance {
// The ForageApiResponse.data string is already formatted to
// { snap: ..., cash: ... }
// so we use fromSdkResponse() instead of fromApiResponse()
return Balance.EbtBalance.ModelMapper.fromSdkResponse(data as String)
}

fun toPayment(): Payment {
return Payment.ModelMapper.from(data as String)
}
}

/**
* A model that represents a failure response from the API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ internal class PosRefundPaymentRepository(
else -> return response
}
val payment = when (val response = paymentService.getPayment(paymentRef)) {
is ForageApiResponse.Success -> Payment.ModelMapper.from(response.data)
is ForageApiResponse.Success ->
Payment.ModelMapper.from(response.data)
else -> return response
}
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethod)) {
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethodRef)) {
is ForageApiResponse.Success -> PaymentMethod.ModelMapper.from(response.data)
else -> return response
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.joinforage.forage.android.mock.MockServiceFactory
import com.joinforage.forage.android.mock.MockVaultSubmitter
import com.joinforage.forage.android.model.Card
import com.joinforage.forage.android.model.PaymentMethod
import com.joinforage.forage.android.model.USState
import com.joinforage.forage.android.network.model.ForageApiResponse
import com.joinforage.forage.android.network.model.ForageError
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -80,9 +81,10 @@ class TokenizeCardServiceTest : MockServerSuite() {
ref = "1f148fe399",
type = "ebt",
balance = null,
card = Card(
card = Card.EbtCard(
last4 = "7845",
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7"
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7",
usState = USState.PENNSYLVANIA
),
customerId = "test-android-customer-id",
reusable = true
Expand All @@ -104,9 +106,10 @@ class TokenizeCardServiceTest : MockServerSuite() {
ref = "2f148fe399",
type = "ebt",
balance = null,
card = Card(
card = Card.EbtCard(
last4 = "7845",
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7"
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7",
usState = USState.PENNSYLVANIA
),
reusable = true,
customerId = null
Expand All @@ -130,9 +133,10 @@ class TokenizeCardServiceTest : MockServerSuite() {
ref = "1f148fe399",
type = "ebt",
balance = null,
card = Card(
card = Card.EbtCard(
last4 = "7845",
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7"
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7",
usState = USState.PENNSYLVANIA
),
customerId = "test-android-customer-id",
reusable = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class CapturePaymentRepositoryTest : MockServerSuite() {
assertThat(response).isExactlyInstanceOf(ForageApiResponse.Success::class.java)
when (response) {
is ForageApiResponse.Success -> {
val paymentMethod = Payment.ModelMapper.from(response.data).paymentMethod
val paymentMethod = Payment.ModelMapper.from(response.data).paymentMethodRef
assertThat(paymentMethod).isEqualTo(expectedData.paymentMethodRef)
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ class ForageTerminalSDKTest : MockServerSuite() {
ref = "2f148fe399",
type = "ebt",
balance = null,
card = Card(
card = Card.EbtCard(
last4 = "7845",
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7"
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7",
usState = (response.card as Card.EbtCard).usState
),
reusable = true,
customerId = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AbstractVaultSubmitterTest : MockServerSuite() {
ref = "1f148fe399",
type = "ebt",
balance = null,
card = Card(
card = Card.EbtCard(
last4 = "7845",
token = "tok_sandbox_sYiPe9Q249qQ5wQyUPP5f7,basis-theory-token"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
"funding_type": "ebt_snap",
"amount": "5.00",
"description": "Testing EBT Payments from Postman",
"merchant": "9876420",
"metadata": {},
"is_delivery": false,
"delivery_address": {
"city": "Los Angeles",
"country": "US",
"line1": "4121 Santa Monica Blvd",
"line2": "Unit 513",
"state": "CA",
"zipcode": "90029"
},
"payment_method": "1f148fe399",
"created": "2023-04-17T12:14:26.741690-07:00",
"updated": "2023-04-17T12:14:26.741706-07:00",
Expand Down

0 comments on commit 4e6ba8f

Please sign in to comment.