Skip to content

Commit 6c62f8c

Browse files
committed
[In Person Payments] Capture payment with min amount error handling
Backport of wordpress-mobile/WordPress-FluxC-Android#3128
1 parent bc699f8 commit 6c62f8c

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/payments/inperson/WCCapturePaymentResponsePayload.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class WCCapturePaymentResponsePayload(
2323

2424
class WCCapturePaymentError(
2525
val type: WCCapturePaymentErrorType = GENERIC_ERROR,
26-
val message: String = ""
26+
val message: String = "",
27+
val extraData: Map<String, Any>? = null
2728
) : OnChangedError
2829

2930
enum class WCCapturePaymentErrorType {

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/payments/inperson/InPersonPaymentsRestClient.kt

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.wordpress.android.fluxc.network.rest.wpcom.wc.payments.inperson
22

3+
import com.android.volley.VolleyError
4+
import com.google.gson.Gson
5+
import com.google.gson.reflect.TypeToken
36
import org.wordpress.android.fluxc.generated.endpoint.WOOCOMMERCE
47
import org.wordpress.android.fluxc.model.SiteModel
58
import org.wordpress.android.fluxc.model.payments.inperson.WCCapturePaymentError
@@ -27,8 +30,12 @@ import org.wordpress.android.fluxc.store.WCInPersonPaymentsStore.InPersonPayment
2730
import org.wordpress.android.fluxc.store.WCInPersonPaymentsStore.InPersonPaymentsPluginType.WOOCOMMERCE_PAYMENTS
2831
import org.wordpress.android.fluxc.utils.toWooPayload
2932
import javax.inject.Inject
33+
import org.wordpress.android.util.AppLog
3034

31-
class InPersonPaymentsRestClient @Inject constructor(private val wooNetwork: WooNetwork) {
35+
class InPersonPaymentsRestClient @Inject constructor(
36+
private val wooNetwork: WooNetwork,
37+
private val gson: Gson,
38+
) {
3239
suspend fun fetchConnectionToken(
3340
activePlugin: InPersonPaymentsPluginType,
3441
site: SiteModel
@@ -71,15 +78,21 @@ class InPersonPaymentsRestClient @Inject constructor(private val wooNetwork: Woo
7178
response.data?.let { data ->
7279
WCCapturePaymentResponsePayload(site, paymentId, orderId, data.status)
7380
} ?: WCCapturePaymentResponsePayload(
74-
mapToCapturePaymentError(error = null, message = "status field is null, but isError == false"),
81+
mapToCapturePaymentError(
82+
error = null,
83+
message = "status field is null, but isError == false"
84+
),
7585
site,
7686
paymentId,
7787
orderId
7888
)
7989
}
8090
is WPAPIResponse.Error -> {
8191
WCCapturePaymentResponsePayload(
82-
mapToCapturePaymentError(response.error, response.error.message ?: "Unexpected error"),
92+
mapToCapturePaymentError(
93+
response.error,
94+
response.error.message ?: "Unexpected error"
95+
),
8396
site,
8497
paymentId,
8598
orderId
@@ -148,7 +161,10 @@ class InPersonPaymentsRestClient @Inject constructor(private val wooNetwork: Woo
148161
}
149162
is WPAPIResponse.Error -> {
150163
WCTerminalStoreLocationResult(
151-
mapToStoreLocationForSiteError(response.error, response.error.message ?: "Unexpected error")
164+
mapToStoreLocationForSiteError(
165+
response.error,
166+
response.error.message ?: "Unexpected error"
167+
)
152168
)
153169
}
154170
}
@@ -194,7 +210,10 @@ class InPersonPaymentsRestClient @Inject constructor(private val wooNetwork: Woo
194210
return response.toWooPayload()
195211
}
196212

197-
private fun mapToCapturePaymentError(error: WPAPINetworkError?, message: String): WCCapturePaymentError {
213+
private fun mapToCapturePaymentError(
214+
error: WPAPINetworkError?,
215+
message: String
216+
): WCCapturePaymentError {
198217
val type = when {
199218
error == null -> GENERIC_ERROR
200219
error.errorCode == "wcpay_missing_order" -> MISSING_ORDER
@@ -206,7 +225,23 @@ class InPersonPaymentsRestClient @Inject constructor(private val wooNetwork: Woo
206225
error.type == GenericErrorType.NETWORK_ERROR -> NETWORK_ERROR
207226
else -> GENERIC_ERROR
208227
}
209-
return WCCapturePaymentError(type, message)
228+
return WCCapturePaymentError(
229+
type,
230+
message,
231+
extraData = error?.volleyError?.getExtraData()
232+
)
233+
}
234+
235+
@Suppress("TooGenericExceptionCaught")
236+
private fun VolleyError.getExtraData(): Map<String, Any>? {
237+
val jsonString = this.networkResponse?.data?.toString(Charsets.UTF_8)
238+
return try {
239+
val mapType = object : TypeToken<Map<String, Any>>() {}.type
240+
return gson.fromJson<Map<String, Any>>(jsonString, mapType)["data"] as Map<String, Any>?
241+
} catch (e: Throwable) {
242+
AppLog.e(AppLog.T.API, "Error parsing volley error $jsonString", e)
243+
null
244+
}
210245
}
211246

212247
private fun mapToStoreLocationForSiteError(error: WPAPINetworkError?, message: String):

0 commit comments

Comments
 (0)