Skip to content

Commit

Permalink
Handle deferred flow with thin types
Browse files Browse the repository at this point in the history
  • Loading branch information
djoksimo committed May 8, 2024
1 parent 404f1fd commit 0b5672b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ internal class CapturePaymentRepository(
is ForageApiResponse.Success -> EncryptionKeys.ModelMapper.from(response.data)
else -> return response
}
val payment = when (val response = paymentService.getPayment(paymentRef)) {
is ForageApiResponse.Success -> Payment(response.data)
val paymentMethodRef = when (val response = paymentService.getPayment(paymentRef)) {
is ForageApiResponse.Success -> Payment.getPaymentMethodRef(response.data)
else -> return response
}
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethodRef)) {
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(paymentMethodRef)) {
is ForageApiResponse.Success -> PaymentMethod(response.data)
else -> return response
}
Expand All @@ -60,7 +60,7 @@ internal class CapturePaymentRepository(

val pollingResponse = pollingService.execute(
contentId = vaultResponse.contentId,
operationDescription = "payment capture of Payment $payment"
operationDescription = "payment capture of Payment $paymentRef"
)
if (pollingResponse is ForageApiResponse.Failure) {
return pollingResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ internal class DeferPaymentCaptureRepository(
is ForageApiResponse.Success -> EncryptionKeys.ModelMapper.from(response.data)
else -> return response
}
val payment = when (val response = paymentService.getPayment(paymentRef)) {
is ForageApiResponse.Success -> Payment(response.data)
val paymentMethodRef = when (val response = paymentService.getPayment(paymentRef)) {
is ForageApiResponse.Success -> Payment.getPaymentMethodRef(response.data)
else -> return response
}
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(payment.paymentMethodRef)) {
val paymentMethod = when (val response = paymentMethodService.getPaymentMethod(paymentMethodRef)) {
is ForageApiResponse.Success -> PaymentMethod(response.data)
else -> return response
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ data class Payment(
successDate = jsonObject.getStringOrNull("success_date"),
updated = jsonObject.getString("updated")
)

internal companion object {
/**
* @djoksimo (2024-05-08) The deferred flows return a "thin" Payment object
* where most fields are null or empty.
* This utility helps us safely grab the paymentMethodRef
* for VaultSubmitter requests without unpacking the "full" Payment object.
*/
internal fun getPaymentMethodRef(jsonString: String): String {
return JSONObject(jsonString).getString("payment_method")
}
}
}

internal fun JSONArray.toListOfStrings(): List<String> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ class PaymentModelTests {
"balance": null
}
""".trimIndent()

val thinPaymentJsonString = """
{
"metadata": {},
"funding_type": "ebt_snap",
"description": "a description",
"payment_method": "$paymentMethodRef"
}
""".trimIndent()
}

@Test
Expand Down Expand Up @@ -179,4 +188,10 @@ class PaymentModelTests {

assertEquals(listOf("refun123", "abcde456", "defg890"), payment.refunds)
}

@Test
fun `getPaymentMethodRef succeeds`() {
val actualPaymentMethodRef = Payment.getPaymentMethodRef(thinPaymentJsonString)
assertEquals(paymentMethodRef, actualPaymentMethodRef)
}
}

0 comments on commit 0b5672b

Please sign in to comment.