11package 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
36import org.wordpress.android.fluxc.generated.endpoint.WOOCOMMERCE
47import org.wordpress.android.fluxc.model.SiteModel
58import org.wordpress.android.fluxc.model.payments.inperson.WCCapturePaymentError
@@ -27,8 +30,12 @@ import org.wordpress.android.fluxc.store.WCInPersonPaymentsStore.InPersonPayment
2730import org.wordpress.android.fluxc.store.WCInPersonPaymentsStore.InPersonPaymentsPluginType.WOOCOMMERCE_PAYMENTS
2831import org.wordpress.android.fluxc.utils.toWooPayload
2932import 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