Skip to content

Commit 297430a

Browse files
committed
fix(onramp): dismiss Google Pay sheet on commit_error or cancel
When a Coinbase commit_error arrives while the Google Pay GMS Activity is on top, nothing dismissed it — the user had to close it manually. Extract the CLEAR_TOP intent trick into dismissPaymentSheet() and call it from wrappedOnPaymentFailure and wrappedOnCancel whenever the payment sheet was presented.
1 parent a3e9358 commit 297430a

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

apps/flipcash/shared/onramp/coinbase/src/main/kotlin/com/flipcash/app/onramp/CoinbaseOnRampWebview.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private fun WebView.configureForCoinbaseOnRamp(
7777

7878
val autoClickTriggered = AtomicBoolean(false)
7979
val terminalEventReceived = AtomicBoolean(false)
80+
val paymentSheetPresented = AtomicBoolean(false)
8081
var messageListenerInstalled = false
8182

8283
var initialTimeoutRunnable: Runnable? = null
@@ -89,6 +90,15 @@ private fun WebView.configureForCoinbaseOnRamp(
8990
paymentSheetTimeoutRunnable?.let { removeCallbacks(it) }
9091
}
9192

93+
fun dismissPaymentSheet() {
94+
(context as? Activity)?.let { activity ->
95+
val intent = Intent(activity, activity::class.java).apply {
96+
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
97+
}
98+
activity.startActivity(intent)
99+
}
100+
}
101+
92102
val timeoutAction = Runnable {
93103
if (terminalEventReceived.compareAndSet(false, true)) {
94104
trace(
@@ -118,12 +128,18 @@ private fun WebView.configureForCoinbaseOnRamp(
118128
val wrappedOnPaymentFailure: (CoinbaseOnRampWebError) -> Unit = { error ->
119129
terminalEventReceived.set(true)
120130
post { cancelAllTimeouts() }
131+
if (paymentSheetPresented.get()) {
132+
post { dismissPaymentSheet() }
133+
}
121134
onPaymentFailure(error)
122135
}
123136

124137
val wrappedOnCancel: () -> Unit = {
125138
terminalEventReceived.set(true)
126139
post { cancelAllTimeouts() }
140+
if (paymentSheetPresented.get()) {
141+
post { dismissPaymentSheet() }
142+
}
127143
onCancel()
128144
}
129145

@@ -135,20 +151,13 @@ private fun WebView.configureForCoinbaseOnRamp(
135151
if (terminalEventReceived.compareAndSet(false, true)) {
136152
trace(tag = "CoinbaseOnRamp", message = "Payment sheet timeout fired (60s)")
137153
cancelAllTimeouts()
138-
// The Google Pay sheet is a GMS Activity sitting on top of ours
139-
// in the task stack. Relaunching our Activity with CLEAR_TOP
140-
// finishes everything above it, dismissing the sheet.
141-
(context as? Activity)?.let { activity ->
142-
val intent = Intent(activity, activity::class.java).apply {
143-
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
144-
}
145-
activity.startActivity(intent)
146-
}
154+
dismissPaymentSheet()
147155
onPaymentFailure(CoinbaseOnRampWebError.PaymentSheetTimeout())
148156
}
149157
}
150158

151159
val pauseWatchdog: () -> Unit = {
160+
paymentSheetPresented.set(true)
152161
post {
153162
cancelAllTimeouts()
154163
val runnable = Runnable { paymentSheetTimeoutAction.run() }

0 commit comments

Comments
 (0)