capturePayment

open suspend override fun capturePayment(params: CapturePaymentParams): ForageApiResponse<String>

Immediately captures a payment via a ForagePINEditText Element.

  • On success, the object confirms the transaction. The response includes a Forage Payment object. (Example PosPaymentResponse and Receipt class)

  • On failure, for example in the case of card_not_reusable or ebt_error_51 errors, the response includes a list of ForageError objects that you can unpack to programmatically handle the error and display the appropriate customer-facing message based on the ForageError.code.

// Example capturePayment call in a PaymentCaptureViewModel.kt
class PaymentCaptureViewModel : ViewModel() {
val snapPaymentRef = "s0alzle0fal"
val merchantId = "mid/<merchant_id>"
val sessionToken = "<session_token>"

fun capturePayment(foragePinEditText: ForagePINEditText, paymentRef: String) =
viewModelScope.launch {
val response = forageTerminalSdk.capturePayment(
CapturePaymentParams(
foragePinEditText = foragePinEditText,
paymentRef = snapPaymentRef
)
)

when (response) {
is ForageApiResponse.Success -> {
// handle successful capture
}
is ForageApiResponse.Failure -> {
val error = response.errors[0]

// handle Insufficient Funds error
if (error.code == "ebt_error_51") {
val details = error.details as ForageErrorDetails.EbtError51Details
val (snapBalance, cashBalance) = details

// do something with balances ...
}
}
}
}
}

Return

A ForageApiResponse object.

Parameters

params

A CapturePaymentParams model that passes a foragePinEditText instance and a paymentRef, returned by the Create a Payment endpoint, that Forage uses to capture a payment.

See also

Throws

If the PosForageConfig is not set for the provided foragePinEditText.