Skip to content

Commit 5584551

Browse files
committed
Update PaymentManager to use single-call processPaymentIntent API
🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 33bd653 commit 5584551

File tree

3 files changed

+53
-137
lines changed

3 files changed

+53
-137
lines changed

libs/cardreader/src/main/java/com/woocommerce/android/cardreader/CardReaderManagerFactory.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import com.woocommerce.android.cardreader.internal.payments.PaymentManager
1818
import com.woocommerce.android.cardreader.internal.payments.PaymentUtils
1919
import com.woocommerce.android.cardreader.internal.payments.RefundErrorMapper
2020
import com.woocommerce.android.cardreader.internal.payments.actions.CancelPaymentAction
21-
import com.woocommerce.android.cardreader.internal.payments.actions.CollectPaymentAction
2221
import com.woocommerce.android.cardreader.internal.payments.actions.CreatePaymentAction
23-
import com.woocommerce.android.cardreader.internal.payments.actions.ProcessPaymentAction
22+
import com.woocommerce.android.cardreader.internal.payments.actions.ProcessPaymentIntentAction
2423
import com.woocommerce.android.cardreader.internal.payments.actions.ProcessRefundAction
2524
import com.woocommerce.android.cardreader.internal.wrappers.PaymentIntentParametersFactory
2625
import com.woocommerce.android.cardreader.internal.wrappers.PaymentMethodTypeMapper
@@ -60,8 +59,7 @@ object CardReaderManagerFactory {
6059
cardReaderConfigFactory,
6160
paymentUtils,
6261
),
63-
CollectPaymentAction(terminal, logWrapper),
64-
ProcessPaymentAction(terminal, logWrapper),
62+
ProcessPaymentIntentAction(terminal, logWrapper),
6563
CancelPaymentAction(terminal),
6664
paymentUtils,
6765
PaymentErrorMapper(),

libs/cardreader/src/main/java/com/woocommerce/android/cardreader/internal/payments/PaymentManager.kt

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ import com.woocommerce.android.cardreader.CardReaderStore.CapturePaymentResponse
88
import com.woocommerce.android.cardreader.config.CardReaderConfigFactory
99
import com.woocommerce.android.cardreader.config.CardReaderConfigForSupportedCountry
1010
import com.woocommerce.android.cardreader.internal.payments.actions.CancelPaymentAction
11-
import com.woocommerce.android.cardreader.internal.payments.actions.CollectPaymentAction
12-
import com.woocommerce.android.cardreader.internal.payments.actions.CollectPaymentAction.CollectPaymentStatus
1311
import com.woocommerce.android.cardreader.internal.payments.actions.CreatePaymentAction
1412
import com.woocommerce.android.cardreader.internal.payments.actions.CreatePaymentAction.CreatePaymentStatus.Failure
1513
import com.woocommerce.android.cardreader.internal.payments.actions.CreatePaymentAction.CreatePaymentStatus.Success
16-
import com.woocommerce.android.cardreader.internal.payments.actions.ProcessPaymentAction
17-
import com.woocommerce.android.cardreader.internal.payments.actions.ProcessPaymentAction.ProcessPaymentStatus
14+
import com.woocommerce.android.cardreader.internal.payments.actions.ProcessPaymentIntentAction
15+
import com.woocommerce.android.cardreader.internal.payments.actions.ProcessPaymentIntentAction.ProcessPaymentIntentStatus
1816
import com.woocommerce.android.cardreader.internal.wrappers.TerminalWrapper
1917
import com.woocommerce.android.cardreader.payments.CardPaymentStatus
20-
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.*
18+
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.CapturingPayment
2119
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.CardPaymentStatusErrorType.Generic
20+
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.InitializingPayment
21+
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.PaymentCompleted
22+
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.PaymentFailed
23+
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.PaymentMethodType
24+
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.ProcessingPayment
25+
import com.woocommerce.android.cardreader.payments.CardPaymentStatus.ProcessingPaymentCompleted
2226
import com.woocommerce.android.cardreader.payments.PaymentData
2327
import com.woocommerce.android.cardreader.payments.PaymentInfo
2428
import kotlinx.coroutines.flow.Flow
@@ -30,8 +34,7 @@ internal class PaymentManager(
3034
private val terminalWrapper: TerminalWrapper,
3135
private val cardReaderStore: CardReaderStore,
3236
private val createPaymentAction: CreatePaymentAction,
33-
private val collectPaymentAction: CollectPaymentAction,
34-
private val processPaymentAction: ProcessPaymentAction,
37+
private val processPaymentIntentAction: ProcessPaymentIntentAction,
3538
private val cancelPaymentAction: CancelPaymentAction,
3639
private val paymentUtils: PaymentUtils,
3740
private val errorMapper: PaymentErrorMapper,
@@ -68,10 +71,9 @@ internal class PaymentManager(
6871
return@flow
6972
}
7073

71-
if (paymentIntent.status == PaymentIntentStatus.REQUIRES_PAYMENT_METHOD) {
72-
paymentIntent = collectPayment(paymentIntent)
73-
}
74-
if (paymentIntent.status == PaymentIntentStatus.REQUIRES_CONFIRMATION) {
74+
if (paymentIntent.status == PaymentIntentStatus.REQUIRES_PAYMENT_METHOD ||
75+
paymentIntent.status == PaymentIntentStatus.REQUIRES_CONFIRMATION
76+
) {
7577
paymentIntent = processPayment(paymentIntent)
7678
}
7779

@@ -125,29 +127,15 @@ internal class PaymentManager(
125127
return paymentIntent
126128
}
127129

128-
private suspend fun FlowCollector<CardPaymentStatus>.collectPayment(
129-
paymentIntent: PaymentIntent
130-
): PaymentIntent {
131-
var result = paymentIntent
132-
emit(CollectingPayment)
133-
collectPaymentAction.collectPayment(paymentIntent).collect {
134-
when (it) {
135-
is CollectPaymentStatus.Failure -> emit(errorMapper.mapTerminalError(paymentIntent, it.exception))
136-
is CollectPaymentStatus.Success -> result = it.paymentIntent
137-
}
138-
}
139-
return result
140-
}
141-
142130
private suspend fun FlowCollector<CardPaymentStatus>.processPayment(
143131
paymentIntent: PaymentIntent
144132
): PaymentIntent {
145133
var result = paymentIntent
146134
emit(ProcessingPayment)
147-
processPaymentAction.processPayment(paymentIntent).collect {
135+
processPaymentIntentAction.processPaymentIntent(paymentIntent).collect {
148136
when (it) {
149-
is ProcessPaymentStatus.Failure -> emit(errorMapper.mapTerminalError(paymentIntent, it.exception))
150-
is ProcessPaymentStatus.Success -> {
137+
is ProcessPaymentIntentStatus.Failure -> emit(errorMapper.mapTerminalError(paymentIntent, it.exception))
138+
is ProcessPaymentIntentStatus.Success -> {
151139
val paymentMethodType = determinePaymentMethodType(it)
152140
emit(ProcessingPaymentCompleted(paymentMethodType))
153141
result = it.paymentIntent
@@ -188,7 +176,7 @@ internal class PaymentManager(
188176
}
189177
}
190178

191-
private fun determinePaymentMethodType(status: ProcessPaymentStatus.Success): PaymentMethodType {
179+
private fun determinePaymentMethodType(status: ProcessPaymentIntentStatus.Success): PaymentMethodType {
192180
val charge = status.paymentIntent.getCharges().firstOrNull()
193181
return when {
194182
charge?.paymentMethodDetails?.interacPresentDetails != null -> PaymentMethodType.INTERAC_PRESENT

0 commit comments

Comments
 (0)