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

Refactor AddPaymentMethodActivity.Result #2609

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,35 @@
```kotlin
PaymentConfiguration.init(context, "pk_test", "acct_1234")
```
- `AddPaymentMethodActivity.Result` is now a sealed class with `Success`, `Failure`, and `Canceled` subclasses
```kotlin
// before
val result = AddPaymentMethodActivityStarter.Result.fromIntent(
intent
)
when {
result != null -> result.paymentMethod
else -> {
// error happened or customer canceled
}
}

// after
val result = AddPaymentMethodActivityStarter.Result.fromIntent(
intent
)
when (result) {
is AddPaymentMethodActivityStarter.Result.Success -> {
result.paymentMethod
}
is AddPaymentMethodActivityStarter.Result.Failure -> {
result.exception
}
is AddPaymentMethodActivityStarter.Result.Canceled -> {
// customer canceled
}
}
```
- Changes to `Source`
- `Source.SourceFlow` has been renamed to `Source.Flow` and is now an enum
- `Source.SourceStatus` has been renamed to `Source.Status` and is now an enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten

final AddPaymentMethodActivityStarter.Result result =
AddPaymentMethodActivityStarter.Result.fromIntent(data);
if (result != null) {
onPaymentMethodResult(result.getPaymentMethod());
if (result instanceof AddPaymentMethodActivityStarter.Result.Success) {
final AddPaymentMethodActivityStarter.Result.Success successResult =
(AddPaymentMethodActivityStarter.Result.Success) result;
onPaymentMethodResult(successResult.getPaymentMethod());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ class AddPaymentMethodActivity : StripeActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
configureView(args)
setResult(
Activity.RESULT_OK,
Intent()
.putExtras(
AddPaymentMethodActivityStarter.Result.Canceled
.toBundle()
)
)
}

override fun onResume() {
Expand Down Expand Up @@ -207,15 +215,21 @@ class AddPaymentMethodActivity : StripeActivity() {
})
},
onFailure = {
finish()
finishWithResult(AddPaymentMethodActivityStarter.Result.Failure(it))
}
)
}

private fun finishWithPaymentMethod(paymentMethod: PaymentMethod) {
finishWithResult(AddPaymentMethodActivityStarter.Result.Success(paymentMethod))
}

private fun finishWithResult(result: AddPaymentMethodActivityStarter.Result) {
isProgressBarVisible = false
setResult(Activity.RESULT_OK, Intent()
.putExtras(AddPaymentMethodActivityStarter.Result(paymentMethod).toBundle()))
setResult(
Activity.RESULT_OK,
Intent().putExtras(result.toBundle())
)
finish()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,33 @@ class AddPaymentMethodActivityStarter : ActivityStarter<AddPaymentMethodActivity
*
* Retrieve in `#onActivityResult()` using [fromIntent].
*/
@Parcelize
data class Result internal constructor(
val paymentMethod: PaymentMethod
) : ActivityStarter.Result {
sealed class Result : ActivityStarter.Result {
override fun toBundle(): Bundle {
val bundle = Bundle()
bundle.putParcelable(ActivityStarter.Result.EXTRA, this)
return bundle
return Bundle().also {
it.putParcelable(ActivityStarter.Result.EXTRA, this)
}
}

@Parcelize
data class Success internal constructor(
val paymentMethod: PaymentMethod
) : Result()

@Parcelize
data class Failure internal constructor(
val exception: Throwable
) : Result()

@Parcelize
object Canceled : Result()

companion object {
/**
* @return the [Result] object from the given `Intent`
* @return the [Result] object from the given `Intent`. [Canceled] by default.
*/
@JvmStatic
fun fromIntent(intent: Intent?): Result? {
return intent?.getParcelableExtra(ActivityStarter.Result.EXTRA)
fun fromIntent(intent: Intent?): Result {
return intent?.getParcelableExtra(ActivityStarter.Result.EXTRA) ?: Canceled
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,17 @@ class PaymentMethodsActivity : AppCompatActivity() {
data?.let {
val result =
AddPaymentMethodActivityStarter.Result.fromIntent(data)
result?.paymentMethod?.let { onAddedPaymentMethod(it) }
when (result) {
is AddPaymentMethodActivityStarter.Result.Success -> {
onAddedPaymentMethod(result.paymentMethod)
}
is AddPaymentMethodActivityStarter.Result.Failure -> {
// TODO(mshafrir-stripe): notify user that payment method can not be added at this time
}
else -> {
// no-op
}
}
} ?: fetchCustomerPaymentMethods()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class AddPaymentMethodActivityStarterTest {

@Test
fun testResultParceling() {
val result =
AddPaymentMethodActivityStarter.Result(PaymentMethodFixtures.CARD_PAYMENT_METHOD)
val result = AddPaymentMethodActivityStarter.Result.Success(
PaymentMethodFixtures.CARD_PAYMENT_METHOD
)
assertEquals(result, ParcelUtils.create(result))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue
import org.junit.runner.RunWith
import org.mockito.Mockito.never
Expand Down Expand Up @@ -159,10 +158,9 @@ class AddPaymentMethodActivityTest {
)

assertEquals(RESULT_OK, activityScenario.result.resultCode)
val paymentMethod = getPaymentMethodFromIntent(
activityScenario.result.resultData
)
assertEquals(expectedPaymentMethod, paymentMethod)
assertThat(
getPaymentMethodFromIntent(activityScenario.result.resultData)
).isEqualTo(expectedPaymentMethod)
}
}
}
Expand Down Expand Up @@ -294,7 +292,12 @@ class AddPaymentMethodActivityTest {
viewModel, PaymentMethodCreateParamsFixtures.DEFAULT_CARD
)

assertNull(shadowOf(activity).resultIntent)
val result = AddPaymentMethodActivityStarter.Result.fromIntent(
shadowOf(activity).resultIntent
)
assertThat(result)
.isEqualTo(AddPaymentMethodActivityStarter.Result.Canceled)

assertFalse(activity.isFinishing)
assertEquals(View.GONE, progressBar.visibility)

Expand Down Expand Up @@ -328,12 +331,11 @@ class AddPaymentMethodActivityTest {
listenerArgumentCaptor.capture()
)

assertEquals(
setOf(
AddPaymentMethodActivity.PRODUCT_TOKEN,
PaymentSession.PRODUCT_TOKEN
),
assertThat(
productUsageArgumentCaptor.firstValue
).containsExactly(
AddPaymentMethodActivity.PRODUCT_TOKEN,
PaymentSession.PRODUCT_TOKEN
)

assertEquals(EXPECTED_PAYMENT_METHOD.id, paymentMethodIdCaptor.firstValue)
Expand All @@ -342,8 +344,12 @@ class AddPaymentMethodActivityTest {
whenever(error.localizedMessage).thenReturn(ERROR_MESSAGE)
listenerArgumentCaptor.firstValue.onError(400, ERROR_MESSAGE, null)

val intent = shadowOf(activity).resultIntent
assertNull(intent)
val result = AddPaymentMethodActivityStarter.Result.fromIntent(
shadowOf(activity).resultIntent
)
assertThat(result)
.isEqualTo(AddPaymentMethodActivityStarter.Result.Canceled)

assertFalse(activity.isFinishing)
assertEquals(View.GONE, progressBar.visibility)

Expand All @@ -367,6 +373,13 @@ class AddPaymentMethodActivityTest {
viewModel,
PaymentMethodCreateParamsFixtures.DEFAULT_CARD
)

val result = AddPaymentMethodActivityStarter.Result.fromIntent(
shadowOf(activity).resultIntent
) as? AddPaymentMethodActivityStarter.Result.Failure
assertThat(result?.exception?.message)
.isEqualTo("Attempted to get instance of CustomerSession without initialization.")

assertThat(activity.isFinishing)
.isTrue()
}
Expand All @@ -384,14 +397,17 @@ class AddPaymentMethodActivityTest {

private fun verifyFinishesWithResult(activityResult: Instrumentation.ActivityResult) {
assertEquals(RESULT_OK, activityResult.resultCode)
val paymentMethod = getPaymentMethodFromIntent(activityResult.resultData)
assertEquals(EXPECTED_PAYMENT_METHOD, paymentMethod)
assertThat(getPaymentMethodFromIntent(activityResult.resultData))
.isEqualTo(EXPECTED_PAYMENT_METHOD)
}

private fun getPaymentMethodFromIntent(intent: Intent): PaymentMethod {
private fun getPaymentMethodFromIntent(intent: Intent): PaymentMethod? {
val result =
requireNotNull(AddPaymentMethodActivityStarter.Result.fromIntent(intent))
return result.paymentMethod
AddPaymentMethodActivityStarter.Result.fromIntent(intent)
return when (result) {
is AddPaymentMethodActivityStarter.Result.Success -> result.paymentMethod
else -> null
}
}

private fun createArgs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ class PaymentMethodsActivityTest {
assertNotNull(paymentMethod)

val resultIntent = Intent()
.putExtras(AddPaymentMethodActivityStarter.Result(paymentMethod).toBundle())
.putExtras(
AddPaymentMethodActivityStarter.Result.Success(paymentMethod)
.toBundle()
)
activity.onActivityResult(
AddPaymentMethodActivityStarter.REQUEST_CODE, RESULT_OK, resultIntent
)
Expand Down