Skip to content

Commit

Permalink
Add deferred capture + refund to sample app (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
djoksimo authored Apr 1, 2024
1 parent a941c9c commit c57a4bc
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import com.joinforage.android.example.ui.pos.data.RefundUIState
import com.joinforage.android.example.ui.pos.screens.ActionSelectionScreen
import com.joinforage.android.example.ui.pos.screens.MerchantSetupScreen
import com.joinforage.android.example.ui.pos.screens.balance.BalanceResultScreen
import com.joinforage.android.example.ui.pos.screens.deferred.DeferredPaymentCaptureResultScreen
import com.joinforage.android.example.ui.pos.screens.deferred.DeferredPaymentRefundResultScreen
import com.joinforage.android.example.ui.pos.screens.payment.EBTCashPurchaseScreen
import com.joinforage.android.example.ui.pos.screens.payment.EBTCashPurchaseWithCashBackScreen
import com.joinforage.android.example.ui.pos.screens.payment.EBTCashWithdrawalScreen
Expand Down Expand Up @@ -91,7 +93,9 @@ enum class POSScreen(@StringRes val title: Int) {
VOIDPaymentScreen(title = R.string.title_pos_void_payment),
VOIDRefundScreen(title = R.string.title_pos_void_refund),
VOIDPaymentResultScreen(title = R.string.title_pos_void_payment_result),
VOIDRefundResultScreen(title = R.string.title_pos_void_refund_result)
VOIDRefundResultScreen(title = R.string.title_pos_void_refund_result),
DEFERPaymentCaptureResultScreen(title = R.string.title_pos_defer_payment_result),
DEFERPaymentRefundResultScreen(title = R.string.title_pos_defer_refund_result)
}

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -467,11 +471,44 @@ fun POSComposeApp(
)
}
},
onDeferButtonClicked = {
if (pinElement != null && uiState.createPaymentResponse?.ref != null) {
pinElement!!.clearFocus()
viewModel.deferPaymentCapture(
context = context,
foragePinEditText = pinElement as ForagePINEditText,
terminalId = k9SDK.terminalId,
paymentRef = uiState.createPaymentResponse!!.ref!!,
onSuccess = {
navController.navigate(POSScreen.DEFERPaymentCaptureResultScreen.name)
},
onFailure = {
navController.navigate(POSScreen.PAYErrorResultScreen.name)
}
)
}
},
onBackButtonClicked = { navController.popBackStack(POSScreen.PAYTransactionTypeSelectionScreen.name, inclusive = false) },
withPinElementReference = { pinElement = it },
errorText = uiState.capturePaymentError
)
}
composable(route = POSScreen.DEFERPaymentCaptureResultScreen.name) {
DeferredPaymentCaptureResultScreen(
terminalId = k9SDK.terminalId,
paymentRef = uiState.createPaymentResponse!!.ref!!,
onBackButtonClicked = { navController.popBackStack(POSScreen.PAYPINEntryScreen.name, inclusive = false) },
onDoneButtonClicked = { navController.popBackStack(POSScreen.ActionSelectionScreen.name, inclusive = false) }
)
}
composable(route = POSScreen.DEFERPaymentRefundResultScreen.name) {
DeferredPaymentRefundResultScreen(
terminalId = k9SDK.terminalId,
paymentRef = uiState.localRefundState!!.paymentRef,
onBackButtonClicked = { navController.popBackStack(POSScreen.REFUNDPINEntryScreen.name, inclusive = false) },
onDoneButtonClicked = { navController.popBackStack(POSScreen.ActionSelectionScreen.name, inclusive = false) }
)
}
composable(route = POSScreen.PAYErrorResultScreen.name) {
val errorReceipt = uiState.capturePaymentResponse?.receipt ?: Receipt(
refNumber = uiState.createPaymentResponse!!.ref!!,
Expand Down Expand Up @@ -566,6 +603,23 @@ fun POSComposeApp(
)
}
},
onDeferButtonClicked = {
if (pinElement != null && uiState.localRefundState != null) {
pinElement!!.clearFocus()
viewModel.deferPaymentRefund(
context = context,
foragePinEditText = pinElement as ForagePINEditText,
terminalId = k9SDK.terminalId,
paymentRef = uiState.localRefundState!!.paymentRef,
onSuccess = {
navController.navigate(POSScreen.DEFERPaymentRefundResultScreen.name)
},
onFailure = {
navController.navigate(POSScreen.REFUNDErrorResultScreen.name)
}
)
}
},
onBackButtonClicked = { navController.popBackStack(POSScreen.REFUNDDetailsScreen.name, inclusive = false) },
withPinElementReference = { pinElement = it },
errorText = uiState.refundPaymentError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import com.joinforage.android.example.ui.pos.data.tokenize.PosPaymentMethodJsonA
import com.joinforage.android.example.ui.pos.network.PosApiService
import com.joinforage.forage.android.CapturePaymentParams
import com.joinforage.forage.android.CheckBalanceParams
import com.joinforage.forage.android.DeferPaymentCaptureParams
import com.joinforage.forage.android.network.model.ForageApiResponse
import com.joinforage.forage.android.pos.ForageTerminalSDK
import com.joinforage.forage.android.pos.PosDeferPaymentRefundParams
import com.joinforage.forage.android.pos.PosForageConfig
import com.joinforage.forage.android.pos.PosRefundPaymentParams
import com.joinforage.forage.android.pos.PosTokenizeCardParams
Expand Down Expand Up @@ -226,6 +228,78 @@ class POSViewModel : ViewModel() {
}
}

fun deferPaymentCapture(
context: Context,
foragePinEditText: ForagePINEditText,
terminalId: String,
paymentRef: String,
onSuccess: () -> Unit,
onFailure: () -> Unit
) {
viewModelScope.launch {
val forageTerminalSdk = initForageTerminalSDK(context, terminalId)
val response = forageTerminalSdk.deferPaymentCapture(
DeferPaymentCaptureParams(
foragePinEditText = foragePinEditText,
paymentRef = paymentRef
)
)

when (response) {
is ForageApiResponse.Success -> {
onSuccess()
}
is ForageApiResponse.Failure -> {
Log.e("POSViewModel", response.errors[0].message)
var payment: PosPaymentResponse? = null
try {
payment = api.getPayment(paymentRef)
} catch (e: HttpException) {
Log.e("POSViewModel", "Failed to re-fetch payment $paymentRef after failed deferred capture")
}
_uiState.update { it.copy(capturePaymentError = response.errors[0].message, capturePaymentResponse = payment) }
onFailure()
}
}
}
}

fun deferPaymentRefund(
context: Context,
foragePinEditText: ForagePINEditText,
terminalId: String,
paymentRef: String,
onSuccess: () -> Unit,
onFailure: () -> Unit
) {
viewModelScope.launch {
val forageTerminalSdk = initForageTerminalSDK(context, terminalId)
val response = forageTerminalSdk.deferPaymentRefund(
PosDeferPaymentRefundParams(
foragePinEditText = foragePinEditText,
paymentRef = paymentRef
)
)

when (response) {
is ForageApiResponse.Success -> {
onSuccess()
}
is ForageApiResponse.Failure -> {
Log.e("POSViewModel", response.errors[0].message)
var payment: PosPaymentResponse? = null
try {
payment = api.getPayment(paymentRef)
} catch (e: HttpException) {
Log.e("POSViewModel", "Failed to re-fetch payment after failed deferred refund attempt. PaymentRef: $paymentRef")
}
_uiState.update { it.copy(refundPaymentError = response.errors[0].message, capturePaymentResponse = payment) }
onFailure()
}
}
}
}

fun capturePayment(context: Context, foragePinEditText: ForagePINEditText, terminalId: String, paymentRef: String, onSuccess: () -> Unit, onFailure: (sequenceNumber: String?) -> Unit) {
viewModelScope.launch {
val forageTerminalSdk = initForageTerminalSDK(context, terminalId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.joinforage.android.example.ui.pos.screens.deferred

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Composable
internal fun DeferredPaymentCaptureResultScreen(
terminalId: String,
paymentRef: String,
onBackButtonClicked: () -> Unit,
onDoneButtonClicked: () -> Unit
) {
val clipboardManager = LocalClipboardManager.current

val postRequestPrompt = """
Send a POST request to
/api/payments/$paymentRef/capture_payment/
to complete the payment
""".trimIndent()

val docsLink = "https://docs.joinforage.app/docs/capture-ebt-payments-server-side#step-4-capture-the-payment-server-side"

Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxSize().padding(16.dp)
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceBetween
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Text("Terminal ID: $terminalId")
Button(onClick = {
clipboardManager.setText(AnnotatedString(terminalId))
}, colors = ButtonDefaults.elevatedButtonColors()) {
Text("Copy")
}
}
Spacer(modifier = Modifier.height(8.dp))
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Text("Payment Ref: $paymentRef")
Button(onClick = {
clipboardManager.setText(AnnotatedString(paymentRef))
}, colors = ButtonDefaults.elevatedButtonColors()) {
Text("Copy")
}
}
Spacer(modifier = Modifier.height(48.dp))

Text(postRequestPrompt, fontFamily = FontFamily.Monospace)

Spacer(modifier = Modifier.height(48.dp))

Button(onClick = {
clipboardManager.setText(AnnotatedString(docsLink))
}, colors = ButtonDefaults.elevatedButtonColors()) {
Text("Copy Documentation Link")
}
}
Row {
Column {
Button(onClick = onBackButtonClicked) {
Text("Try Again")
}
}
Spacer(modifier = Modifier.width(8.dp))
Column {
ElevatedButton(onClick = onDoneButtonClicked) {
Text("Done")
}
}
}
}
}

@Preview
@Composable
fun DeferredPaymentCaptureResultScreenPreview() {
DeferredPaymentCaptureResultScreen(
terminalId = "",
paymentRef = "",
onBackButtonClicked = {},
onDoneButtonClicked = {}
)
}
Loading

0 comments on commit c57a4bc

Please sign in to comment.