Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve response error object #273

Merged
merged 16 commits into from
May 31, 2021
52 changes: 49 additions & 3 deletions android/src/main/java/com/reactnativestripesdk/Errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package com.reactnativestripesdk

import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableNativeMap
import com.stripe.android.exception.APIException
import com.stripe.android.exception.AuthenticationException
import com.stripe.android.exception.CardException
import com.stripe.android.exception.InvalidRequestException
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.SetupIntent

enum class ConfirmPaymentErrorType {
Failed, Canceled, Unknown
Expand All @@ -28,10 +34,50 @@ enum class PaymentSheetErrorType {
Failed, Canceled
}

internal fun createError(errorType: String, message: String): WritableMap {
internal fun mapError(code: String, message: String?, localizedMessage: String?, declineCode: String?, type: String?, stripeErrorCode: String?): WritableMap {
val map: WritableMap = WritableNativeMap()
map.putString("message", message)
map.putString("code", errorType)
val details: WritableMap = WritableNativeMap()
details.putString("code", code)
details.putString("message", message)
details.putString("localizedMessage", localizedMessage)
details.putString("declineCode", declineCode)
details.putString("type", type)
details.putString("stripeErrorCode", stripeErrorCode)

map.putMap("error", details)
return map
}

internal fun createError(code: String, message: String?): WritableMap {
return mapError(code, message, message, null, null, null)
}

internal fun createError(code: String, error: PaymentIntent.Error?): WritableMap {
return mapError(code, error?.message, error?.message, error?.declineCode, error?.type?.code, error?.code)
}

internal fun createError(code: String, error: SetupIntent.Error?): WritableMap {
return mapError(code, error?.message, error?.message, error?.declineCode, error?.type?.code, error?.code)
}

internal fun createError(code: String, error: Exception): WritableMap {
return when (error) {
is CardException -> {
mapError(code, error.message, error.localizedMessage, error.declineCode, error.stripeError?.type, error.stripeError?.code)
}
is InvalidRequestException -> {
mapError(code, error.message, error.localizedMessage, error.stripeError?.declineCode, error.stripeError?.type, error.stripeError?.code)
}
is AuthenticationException -> {
mapError(code, error.message, error.localizedMessage, error.stripeError?.declineCode, error.stripeError?.type, error.stripeError?.code)
}
is APIException -> {
mapError(code, error.message, error.localizedMessage, error.stripeError?.declineCode, error.stripeError?.type, error.stripeError?.code)
}
else -> mapError(code, error.message, error.localizedMessage.orEmpty(), null, null, null)
}
}

internal fun createError(code: String, error: Throwable): WritableMap {
return mapError(code, error.message, error.localizedMessage, null, null, null)
}
21 changes: 13 additions & 8 deletions android/src/main/java/com/reactnativestripesdk/Mappers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import com.facebook.react.bridge.*
import com.stripe.android.PaymentAuthConfig
import com.stripe.android.model.*

internal fun createResult(key: String, value: WritableMap): WritableMap {
val map = WritableNativeMap()
map.putMap(key, value)
return map
}

internal fun mapIntentStatus(status: StripeIntent.Status?): String {
return when (status) {
Expand Down Expand Up @@ -357,14 +362,14 @@ internal fun mapFromPaymentIntentResult(paymentIntent: PaymentIntent): WritableM

internal fun mapFromPaymentIntentLastErrorType(errorType: PaymentIntent.Error.Type?): String? {
return when (errorType) {
PaymentIntent.Error.Type.ApiConnectionError -> "ApiConnection"
PaymentIntent.Error.Type.AuthenticationError -> "Authentication"
PaymentIntent.Error.Type.ApiError -> "Api"
PaymentIntent.Error.Type.CardError -> "Card"
PaymentIntent.Error.Type.IdempotencyError -> "Idempotency"
PaymentIntent.Error.Type.InvalidRequestError -> "InvalidRequest"
PaymentIntent.Error.Type.RateLimitError -> "RateLimit"
else -> "Unknown"
PaymentIntent.Error.Type.ApiConnectionError -> "api_connection_error"
PaymentIntent.Error.Type.AuthenticationError -> "authentication_error"
PaymentIntent.Error.Type.ApiError -> "api_error"
PaymentIntent.Error.Type.CardError -> "card_error"
PaymentIntent.Error.Type.IdempotencyError -> "idempotency_error"
PaymentIntent.Error.Type.InvalidRequestError -> "invalid_request_error"
PaymentIntent.Error.Type.RateLimitError -> "rate_limit_error"
else -> null
}
}

Expand Down