@@ -11,6 +11,7 @@ import to.bitkit.paykit.PaykitIntegrationHelper
1111import to.bitkit.paykit.PaykitManager
1212import to.bitkit.repositories.LightningRepo
1313import to.bitkit.utils.Logger
14+ import uniffi.paykit_mobile.PaymentCandidate
1415import uniffi.paykit_mobile.SelectionPreferences
1516import uniffi.paykit_mobile.SelectionStrategy
1617import 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