Skip to content

Commit

Permalink
Convert StripeIntentResult to Kotlin (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshafrir-stripe authored Oct 8, 2019
1 parent 51d6d53 commit 1fece7d
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 214 deletions.
13 changes: 0 additions & 13 deletions stripe/src/main/java/com/stripe/android/ApiResultCallback.java

This file was deleted.

11 changes: 11 additions & 0 deletions stripe/src/main/java/com/stripe/android/ApiResultCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.stripe.android

/**
* Generic interface for an API operation callback that either returns a
* result, [ResultType], or an [Exception]
*/
interface ApiResultCallback<ResultType> {
fun onSuccess(result: ResultType)

fun onError(e: Exception)
}
14 changes: 6 additions & 8 deletions stripe/src/main/java/com/stripe/android/PaymentController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ internal open class PaymentController @VisibleForTesting constructor(
object : ApiResultCallback<StripeIntent> {
override fun onSuccess(stripeIntent: StripeIntent) {
if (stripeIntent is PaymentIntent) {
callback.onSuccess(PaymentIntentResult.Builder()
.setPaymentIntent(stripeIntent)
.setOutcome(flowOutcome)
.build())
callback.onSuccess(
PaymentIntentResult(stripeIntent, flowOutcome)
)
}
}

Expand Down Expand Up @@ -181,10 +180,9 @@ internal open class PaymentController @VisibleForTesting constructor(
object : ApiResultCallback<StripeIntent> {
override fun onSuccess(stripeIntent: StripeIntent) {
if (stripeIntent is SetupIntent) {
callback.onSuccess(SetupIntentResult.Builder()
.setSetupIntent(stripeIntent)
.setOutcome(flowOutcome)
.build())
callback.onSuccess(
SetupIntentResult(stripeIntent, flowOutcome)
)
}
}

Expand Down
34 changes: 0 additions & 34 deletions stripe/src/main/java/com/stripe/android/PaymentIntentResult.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.stripe.android

import com.stripe.android.model.PaymentIntent

class PaymentIntentResult internal constructor(
paymentIntent: PaymentIntent,
@Outcome outcome: Int = 0
) : StripeIntentResult<PaymentIntent>(paymentIntent, outcome)
34 changes: 0 additions & 34 deletions stripe/src/main/java/com/stripe/android/SetupIntentResult.java

This file was deleted.

8 changes: 8 additions & 0 deletions stripe/src/main/java/com/stripe/android/SetupIntentResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.stripe.android

import com.stripe.android.model.SetupIntent

class SetupIntentResult internal constructor(
setupIntent: SetupIntent,
@Outcome outcome: Int = 0
) : StripeIntentResult<SetupIntent>(setupIntent, outcome)
115 changes: 0 additions & 115 deletions stripe/src/main/java/com/stripe/android/StripeIntentResult.java

This file was deleted.

101 changes: 101 additions & 0 deletions stripe/src/main/java/com/stripe/android/StripeIntentResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.stripe.android

import androidx.annotation.IntDef
import com.stripe.android.model.StripeIntent
import java.util.Objects

/**
* A model representing the result of a [StripeIntent] confirmation or authentication attempt
* via [Stripe.confirmPayment] or [Stripe.authenticatePayment]
*
* [intent] is a [StripeIntent] retrieved after confirmation/authentication succeeded or failed.
*/
abstract class StripeIntentResult<T : StripeIntent> internal constructor(
val intent: T,
@Outcome outcome: Int
) {
@Outcome
@get:Outcome
val outcome: Int

init {
this.outcome = determineOutcome(intent.status, outcome)
}

@StripeIntentResult.Outcome
private fun determineOutcome(
stripeIntentStatus: StripeIntent.Status?,
@StripeIntentResult.Outcome outcome: Int
): Int {
if (outcome != Outcome.UNKNOWN) {
return outcome
}

when (stripeIntentStatus) {
StripeIntent.Status.RequiresAction, StripeIntent.Status.Canceled -> {
return Outcome.CANCELED
}
StripeIntent.Status.RequiresPaymentMethod -> {
return Outcome.FAILED
}
StripeIntent.Status.Succeeded,
StripeIntent.Status.RequiresCapture,
StripeIntent.Status.RequiresConfirmation -> {
return Outcome.SUCCEEDED
}
StripeIntent.Status.Processing -> {
return Outcome.UNKNOWN
}
else -> {
return Outcome.UNKNOWN
}
}
}

override fun equals(other: Any?): Boolean {
return when {
this === other -> true
other is StripeIntentResult<*> -> typedEquals(other)
else -> false
}
}

private fun typedEquals(setupIntentResult: StripeIntentResult<*>): Boolean {
return intent == setupIntentResult.intent && outcome == setupIntentResult.outcome
}

override fun hashCode(): Int {
return Objects.hash(intent, outcome)
}

/**
* Values that indicate the outcome of confirmation and payment authentication.
*/
@Retention(AnnotationRetention.SOURCE)
@IntDef(Outcome.UNKNOWN, Outcome.SUCCEEDED, Outcome.FAILED, Outcome.CANCELED, Outcome.TIMEDOUT)
annotation class Outcome {
companion object {
const val UNKNOWN: Int = 0

/**
* Confirmation or payment authentication succeeded
*/
const val SUCCEEDED: Int = 1

/**
* Confirm or payment authentication failed
*/
const val FAILED: Int = 2

/**
* Payment authentication was canceled by the user
*/
const val CANCELED: Int = 3

/**
* Payment authentication timed-out
*/
const val TIMEDOUT: Int = 4
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ class PaymentIntentResultTest {

@Test
fun testBuilder() {
assertEquals(PaymentIntentFixtures.PI_REQUIRES_MASTERCARD_3DS2,
PaymentIntentResult.Builder()
.setPaymentIntent(PaymentIntentFixtures.PI_REQUIRES_MASTERCARD_3DS2)
.build()
.intent)
assertEquals(
PaymentIntentFixtures.PI_REQUIRES_MASTERCARD_3DS2,
PaymentIntentResult(PaymentIntentFixtures.PI_REQUIRES_MASTERCARD_3DS2).intent
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ class SetupIntentResultTest {

@Test
fun testBuilder() {
assertEquals(SetupIntentFixtures.SI_NEXT_ACTION_REDIRECT,
SetupIntentResult.Builder()
.setSetupIntent(SetupIntentFixtures.SI_NEXT_ACTION_REDIRECT)
.build()
.intent)
assertEquals(
SetupIntentFixtures.SI_NEXT_ACTION_REDIRECT,
SetupIntentResult(SetupIntentFixtures.SI_NEXT_ACTION_REDIRECT).intent
)
}
}

0 comments on commit 1fece7d

Please sign in to comment.