Skip to content

Commit f3f2752

Browse files
authored
Merge pull request #101 from code-payments/fix/micropayment-deeplink-redirect
fix(micropayments): redirect after successful payment for deeplinks
2 parents d1547d0 + 9b1b95d commit f3f2752

2 files changed

Lines changed: 37 additions & 37 deletions

File tree

app/src/main/java/com/getcode/view/main/home/HomeScan.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import kotlinx.coroutines.flow.map
8989
import kotlinx.coroutines.flow.onEach
9090
import kotlinx.coroutines.launch
9191
import timber.log.Timber
92+
import kotlin.time.Duration.Companion.seconds
9293

9394

9495
enum class HomeBottomSheet {
@@ -175,6 +176,7 @@ private fun HomeScan(
175176
}
176177

177178
if (!requestPayload.isNullOrBlank()) {
179+
delay(500)
178180
homeViewModel.handlePaymentRequest(requestPayload)
179181
}
180182
}

app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class HomeViewModel @Inject constructor(
561561
val presentationStyle = if (isReceived) PresentationStyle.Pop else PresentationStyle.Slide
562562
withContext(Dispatchers.Main) {
563563
uiFlow.update {
564-
var billState= it.billState.copy(
564+
var billState = it.billState.copy(
565565
bill = Bill.Payment(amount, code, request),
566566
)
567567

@@ -587,9 +587,11 @@ class HomeViewModel @Inject constructor(
587587
analytics.requestShown(amount = amount)
588588

589589
if (DEBUG_SCAN_TIMES) {
590-
Timber.tag("codescan")
591-
.d("scan processing took ${System.currentTimeMillis() - scanProcessingTime}")
592-
scanProcessingTime = 0
590+
if (scanProcessingTime > 0) {
591+
Timber.tag("codescan")
592+
.d("scan processing took ${System.currentTimeMillis() - scanProcessingTime}")
593+
scanProcessingTime = 0
594+
}
593595
}
594596

595597

@@ -634,39 +636,27 @@ class HomeViewModel @Inject constructor(
634636
}
635637
}
636638

637-
delay(500)
638-
uiFlow.update {
639-
it.copy(
639+
cancelPayment(false)
640+
}.onFailure { error ->
641+
error.printStackTrace()
642+
TopBarManager.showMessage(
643+
"Payment Failed",
644+
"This payment request could not be paid at this time. Please try again later."
645+
)
646+
uiFlow.update { uiModel ->
647+
uiModel.copy(
640648
presentationStyle = PresentationStyle.Hidden,
641-
billState = it.billState.copy(
649+
billState = uiModel.billState.copy(
642650
bill = null,
651+
showToast = false,
643652
paymentConfirmation = null,
653+
toast = null,
644654
valuation = null,
645655
hideBillButtons = false,
646656
)
647657
)
648658
}
649659
}
650-
.onFailure { error ->
651-
error.printStackTrace()
652-
TopBarManager.showMessage(
653-
"Payment Failed",
654-
"This payment request could not be paid at this time. Please try again later."
655-
)
656-
uiFlow.update { uiModel ->
657-
uiModel.copy(
658-
presentationStyle = PresentationStyle.Hidden,
659-
billState = uiModel.billState.copy(
660-
bill = null,
661-
showToast = false,
662-
paymentConfirmation = null,
663-
toast = null,
664-
valuation = null,
665-
hideBillButtons = false,
666-
)
667-
)
668-
}
669-
}
670660
}
671661

672662
fun cancelPayment(rejected: Boolean, ignoreRedirect: Boolean = false) {
@@ -979,15 +969,16 @@ class HomeViewModel @Inject constructor(
979969
)
980970
}
981971

982-
private fun cancelRemoteSend(giftCard: GiftCardAccount, amount: KinAmount) = viewModelScope.launch {
983-
val organizer = SessionManager.getOrganizer() ?: return@launch
984-
client.cancelRemoteSend(giftCard, amount.kin, organizer)
985-
analytics.remoteSendIncoming(
986-
kin = amount.kin,
987-
currencyCode = amount.rate.currency,
988-
isVoiding = true
989-
)
990-
}
972+
private fun cancelRemoteSend(giftCard: GiftCardAccount, amount: KinAmount) =
973+
viewModelScope.launch {
974+
val organizer = SessionManager.getOrganizer() ?: return@launch
975+
client.cancelRemoteSend(giftCard, amount.kin, organizer)
976+
analytics.remoteSendIncoming(
977+
kin = amount.kin,
978+
currencyCode = amount.rate.currency,
979+
isVoiding = true
980+
)
981+
}
991982

992983
private fun showRemoteSendDialog(
993984
context: Context,
@@ -1046,6 +1037,13 @@ class HomeViewModel @Inject constructor(
10461037
value = request.fiat,
10471038
nonce = request.clientSecret
10481039
)
1040+
1041+
if (scannedRendezvous.contains(payload.rendezvous.publicKey)) {
1042+
Timber.d("Nonce previously received: ${payload.nonce.hexEncodedString()}")
1043+
return
1044+
}
1045+
1046+
scannedRendezvous.add(payload.rendezvous.publicKey)
10491047
attemptPayment(payload, request)
10501048
}
10511049
}

0 commit comments

Comments
 (0)