Skip to content

Commit edd87ed

Browse files
JOHNJOHN
authored andcommitted
feat: use paykit fallback execution
1 parent a0164e9 commit edd87ed

File tree

7 files changed

+5135
-5497
lines changed

7 files changed

+5135
-5497
lines changed

app/src/main/java/to/bitkit/paykit/services/PaykitPaymentService.kt

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import to.bitkit.paykit.PaykitIntegrationHelper
1111
import to.bitkit.paykit.PaykitManager
1212
import to.bitkit.repositories.LightningRepo
1313
import to.bitkit.utils.Logger
14+
import uniffi.paykit_mobile.PaymentCandidate
1415
import uniffi.paykit_mobile.SelectionPreferences
1516
import uniffi.paykit_mobile.SelectionStrategy
1617
import java.util.Date
@@ -297,52 +298,31 @@ class PaykitPaymentService @Inject constructor(
297298

298299
// Build ordered methods: primary first, then fallbacks
299300
val orderedMethods = buildOrderedPaymentMethods(pubkey, selectedMethod)
300-
val attemptedMethods = mutableListOf<String>()
301-
var lastError: Exception? = null
302-
var successResult: uniffi.paykit_mobile.PaymentExecutionResult? = null
303-
304-
// Execute with fallback loop
305-
for (method in orderedMethods) {
306-
attemptedMethods.add(method.methodId)
307-
Logger.debug("Attempting payment via ${method.methodId}", context = TAG)
308-
309-
val result = runCatching {
310-
client.executePayment(
311-
methodId = method.methodId,
312-
endpoint = method.endpoint,
313-
amountSats = amount,
314-
metadataJson = null,
315-
)
316-
}
317-
318-
if (result.isSuccess && result.getOrNull()?.success == true) {
319-
successResult = result.getOrNull()
320-
break
321-
}
322-
323-
// Check if error is retryable
324-
val error = (result.exceptionOrNull() as? Exception)
325-
?: result.getOrNull()?.error?.let { Exception(it) }
326-
lastError = error
301+
val candidates = orderedMethods.map { method ->
302+
PaymentCandidate(
303+
methodId = method.methodId,
304+
endpoint = method.endpoint,
305+
)
306+
}
327307

328-
val isRetryable = isRetryableError(error?.message ?: result.getOrNull()?.error)
329-
if (!isRetryable) {
330-
Logger.warn("Non-retryable error on ${method.methodId}: ${error?.message}", context = TAG)
331-
break
332-
}
308+
val execution = client.executeWithFallbacks(
309+
candidates = candidates,
310+
amountSats = amount,
311+
metadataJson = null,
312+
)
333313

334-
Logger.debug("Retryable error on ${method.methodId}: ${error?.message}, trying next", context = TAG)
335-
}
314+
val attemptedMethods = execution.attempts.map { it.methodId }
336315

337-
if (successResult == null) {
338-
val summary = "All ${attemptedMethods.size} methods failed: ${attemptedMethods.joinToString(", ")}"
339-
Logger.warn(summary, context = TAG)
316+
val successResult = execution.successfulExecution
317+
if (!execution.success || successResult == null || !successResult.success) {
318+
Logger.warn(execution.summary, context = TAG)
340319
val receipt = createFailedReceipt(uri, amount, PaykitReceiptType.LIGHTNING)
341-
_paymentState.value = PaykitPaymentState.Failed(mapError(lastError ?: Exception(summary)))
320+
val errorMsg = execution.attempts.lastOrNull()?.error ?: execution.summary
321+
_paymentState.value = PaykitPaymentState.Failed(mapError(Exception(errorMsg)))
342322
return PaykitPaymentResult(
343323
success = false,
344324
receipt = receipt,
345-
error = mapError(lastError ?: Exception(summary)),
325+
error = mapError(Exception(errorMsg)),
346326
)
347327
}
348328

0 commit comments

Comments
 (0)