Skip to content

Commit bf88f55

Browse files
Convert CustomerSession to Kotlin (#1854)
ANDROID-453
1 parent c2d4b0c commit bf88f55

File tree

11 files changed

+576
-646
lines changed

11 files changed

+576
-646
lines changed

stripe/src/main/java/com/stripe/android/CustomerSession.java

Lines changed: 0 additions & 598 deletions
This file was deleted.

stripe/src/main/java/com/stripe/android/CustomerSession.kt

Lines changed: 537 additions & 0 deletions
Large diffs are not rendered by default.

stripe/src/main/java/com/stripe/android/CustomerSessionEphemeralKeyManagerListener.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.stripe.android
22

3-
import java.util.HashMap
43
import java.util.concurrent.ThreadPoolExecutor
54

65
internal class CustomerSessionEphemeralKeyManagerListener(
76
private val runnableFactory: CustomerSessionRunnableFactory,
87
private val executor: ThreadPoolExecutor,
9-
private val listeners: HashMap<String, CustomerSession.RetrievalListener>,
8+
private val listeners: MutableMap<String, CustomerSession.RetrievalListener?>,
109
private val productUsage: CustomerSessionProductUsage
1110
) : EphemeralKeyManager.KeyManagerListener {
1211
override fun onKeyUpdate(

stripe/src/main/java/com/stripe/android/PaymentSession.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ class PaymentSession @VisibleForTesting internal constructor(
288288
}
289289

290290
override fun onError(
291-
httpCode: Int,
291+
errorCode: Int,
292292
errorMessage: String,
293293
stripeError: StripeError?
294294
) {
295-
paymentSessionListener?.onError(httpCode, errorMessage)
295+
paymentSessionListener?.onError(errorCode, errorMessage)
296296
paymentSessionListener?.onCommunicatingStateChanged(false)
297297
}
298298
})

stripe/src/main/java/com/stripe/android/view/AddPaymentMethodActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ open class AddPaymentMethodActivity : StripeActivity() {
7373
startedFromPaymentSession = args.isPaymentSessionActive
7474

7575
if (shouldAttachToCustomer && args.shouldInitCustomerSessionTokens) {
76-
initCustomerSessionTokens()
76+
initCustomerSessionTokens(CustomerSession.getInstance())
7777
}
7878
}
7979

@@ -118,8 +118,8 @@ open class AddPaymentMethodActivity : StripeActivity() {
118118
}
119119
}
120120

121-
fun initCustomerSessionTokens() {
122-
val customerSession = CustomerSession.getInstance()
121+
@JvmSynthetic
122+
internal fun initCustomerSessionTokens(customerSession: CustomerSession) {
123123
customerSession.addProductUsageTokenIfValid(TOKEN_ADD_PAYMENT_METHOD_ACTIVITY)
124124
if (startedFromPaymentSession) {
125125
customerSession.addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION)

stripe/src/test/java/com/stripe/android/CustomerSessionTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ class CustomerSessionTest : BaseViewTest<PaymentFlowActivity>(PaymentFlowActivit
640640
@Test
641641
fun shippingInfoScreen_whenLaunched_logs() {
642642
val customerSession = createCustomerSession(null)
643-
CustomerSession.setInstance(customerSession)
643+
CustomerSession.instance = customerSession
644644
createActivity(PaymentFlowActivityStarter.Args.Builder()
645645
.setPaymentSessionConfig(PaymentSessionConfig.Builder()
646646
.build())
@@ -653,7 +653,7 @@ class CustomerSessionTest : BaseViewTest<PaymentFlowActivity>(PaymentFlowActivit
653653
@Test
654654
fun shippingMethodScreen_whenLaunched_logs() {
655655
val customerSession = createCustomerSession(null)
656-
CustomerSession.setInstance(customerSession)
656+
CustomerSession.instance = customerSession
657657

658658
createActivity(PaymentFlowActivityStarter.Args.Builder()
659659
.setPaymentSessionConfig(PaymentSessionConfig.Builder()

stripe/src/test/java/com/stripe/android/CustomerSessionTestHelper.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

stripe/src/test/java/com/stripe/android/PaymentSessionTest.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class PaymentSessionTest {
8989
@Test
9090
fun init_addsPaymentSessionToken_andFetchesCustomer() {
9191
val customerSession = createCustomerSession()
92-
CustomerSession.setInstance(customerSession)
92+
CustomerSession.instance = customerSession
9393

9494
val paymentSession = PaymentSession(activity)
9595
paymentSession.init(paymentSessionListener, PaymentSessionConfig.Builder().build())
@@ -104,7 +104,7 @@ class PaymentSessionTest {
104104
fun init_whenEphemeralKeyProviderContinues_fetchesCustomerAndNotifiesListener() {
105105
ephemeralKeyProvider
106106
.setNextRawEphemeralKey(CustomerFixtures.EPHEMERAL_KEY_FIRST.toString())
107-
CustomerSession.setInstance(createCustomerSession())
107+
CustomerSession.instance = createCustomerSession()
108108

109109
val paymentSession = PaymentSession(activity)
110110
paymentSession.init(paymentSessionListener, PaymentSessionConfig.Builder().build())
@@ -118,7 +118,7 @@ class PaymentSessionTest {
118118

119119
@Test
120120
fun handlePaymentData_whenPaymentMethodRequest_notifiesListenerAndFetchesCustomer() {
121-
CustomerSession.setInstance(createCustomerSession())
121+
CustomerSession.instance = createCustomerSession()
122122

123123
val paymentSession = PaymentSession(activity)
124124
paymentSession.init(paymentSessionListener, PaymentSessionConfig.Builder().build())
@@ -140,7 +140,7 @@ class PaymentSessionTest {
140140

141141
@Test
142142
fun selectPaymentMethod_launchesPaymentMethodsActivityWithLog() {
143-
CustomerSession.setInstance(createCustomerSession())
143+
CustomerSession.instance = createCustomerSession()
144144

145145
val paymentSession = PaymentSession(activity)
146146
paymentSession.init(paymentSessionListener, PaymentSessionConfig.Builder().build())
@@ -160,7 +160,7 @@ class PaymentSessionTest {
160160

161161
@Test
162162
fun presentPaymentMethodSelection_withShouldRequirePostalCode_shouldPassInIntent() {
163-
CustomerSession.setInstance(createCustomerSession())
163+
CustomerSession.instance = createCustomerSession()
164164

165165
val paymentSession = PaymentSession(activity)
166166
paymentSession.init(paymentSessionListener, PaymentSessionConfig.Builder().build())
@@ -175,7 +175,7 @@ class PaymentSessionTest {
175175
@Test
176176
fun getSelectedPaymentMethodId_whenPrefsNotSet_returnsNull() {
177177
`when`<Customer>(customerSession.cachedCustomer).thenReturn(FIRST_CUSTOMER)
178-
CustomerSession.setInstance(customerSession)
178+
CustomerSession.instance = customerSession
179179
assertNull(createPaymentSession().getSelectedPaymentMethodId(null))
180180
}
181181

@@ -199,7 +199,7 @@ class PaymentSessionTest {
199199
.thenReturn("pm_12345")
200200

201201
`when`<Customer>(customerSession.cachedCustomer).thenReturn(FIRST_CUSTOMER)
202-
CustomerSession.setInstance(customerSession)
202+
CustomerSession.instance = customerSession
203203

204204
assertEquals("pm_12345",
205205
createPaymentSession().getSelectedPaymentMethodId(null))
@@ -219,7 +219,7 @@ class PaymentSessionTest {
219219
@Test
220220
fun init_withoutSavedState_clearsLoggingTokensAndStartsWithPaymentSession() {
221221
val customerSession = createCustomerSession()
222-
CustomerSession.setInstance(customerSession)
222+
CustomerSession.instance = customerSession
223223
customerSession
224224
.addProductUsageTokenIfValid(PaymentMethodsActivity.TOKEN_PAYMENT_METHODS_ACTIVITY)
225225
assertEquals(1, customerSession.productUsageTokens.size)
@@ -237,7 +237,7 @@ class PaymentSessionTest {
237237
@Test
238238
fun init_withSavedStateBundle_doesNotClearLoggingTokens() {
239239
val customerSession = createCustomerSession()
240-
CustomerSession.setInstance(customerSession)
240+
CustomerSession.instance = customerSession
241241
customerSession
242242
.addProductUsageTokenIfValid(PaymentMethodsActivity.TOKEN_PAYMENT_METHODS_ACTIVITY)
243243
assertEquals(1, customerSession.productUsageTokens.size)
@@ -256,7 +256,7 @@ class PaymentSessionTest {
256256
@Test
257257
fun completePayment_withLoggedActions_clearsLoggingTokensAndSetsResult() {
258258
val customerSession = createCustomerSession()
259-
CustomerSession.setInstance(customerSession)
259+
CustomerSession.instance = customerSession
260260
customerSession
261261
.addProductUsageTokenIfValid(PaymentMethodsActivity.TOKEN_PAYMENT_METHODS_ACTIVITY)
262262
assertEquals(1, customerSession.productUsageTokens.size)
@@ -279,7 +279,7 @@ class PaymentSessionTest {
279279
fun init_withSavedState_setsPaymentSessionData() {
280280
ephemeralKeyProvider
281281
.setNextRawEphemeralKey(CustomerFixtures.EPHEMERAL_KEY_FIRST.toString())
282-
CustomerSession.setInstance(createCustomerSession())
282+
CustomerSession.instance = createCustomerSession()
283283

284284
val paymentSession = PaymentSession(activity)
285285
paymentSession.init(paymentSessionListener, PaymentSessionConfig.Builder().build())

stripe/src/test/java/com/stripe/android/view/AddPaymentMethodActivityTest.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import com.nhaarman.mockitokotlin2.KArgumentCaptor
1212
import com.nhaarman.mockitokotlin2.any
1313
import com.nhaarman.mockitokotlin2.argumentCaptor
1414
import com.nhaarman.mockitokotlin2.mock
15+
import com.nhaarman.mockitokotlin2.verify
1516
import com.stripe.android.AbsFakeStripeRepository
1617
import com.stripe.android.ApiKeyFixtures
1718
import com.stripe.android.ApiRequest
1819
import com.stripe.android.CustomerSession
19-
import com.stripe.android.CustomerSession.ACTION_API_EXCEPTION
20-
import com.stripe.android.CustomerSession.EXTRA_EXCEPTION
21-
import com.stripe.android.CustomerSessionTestHelper
20+
import com.stripe.android.CustomerSession.Companion.ACTION_API_EXCEPTION
21+
import com.stripe.android.CustomerSession.Companion.EXTRA_EXCEPTION
2222
import com.stripe.android.PaymentConfiguration
2323
import com.stripe.android.PaymentSession.Companion.TOKEN_PAYMENT_SESSION
2424
import com.stripe.android.R
@@ -50,7 +50,6 @@ import org.junit.runner.RunWith
5050
import org.mockito.Mock
5151
import org.mockito.Mockito.`when`
5252
import org.mockito.Mockito.never
53-
import org.mockito.Mockito.verify
5453
import org.mockito.MockitoAnnotations
5554
import org.robolectric.RobolectricTestRunner
5655
import org.robolectric.Shadows.shadowOf
@@ -95,7 +94,8 @@ class AddPaymentMethodActivityTest :
9594
context = ApplicationProvider.getApplicationContext()
9695

9796
PaymentConfiguration.init(context, ApiKeyFixtures.FAKE_PUBLISHABLE_KEY)
98-
CustomerSessionTestHelper.setInstance(customerSession)
97+
98+
CustomerSession.instance = customerSession
9999
}
100100

101101
@AfterTest
@@ -132,7 +132,7 @@ class AddPaymentMethodActivityTest :
132132
}
133133

134134
shadowActivity = shadowOf(activity)
135-
activity.initCustomerSessionTokens()
135+
activity.initCustomerSessionTokens(customerSession)
136136
}
137137

138138
@Test
@@ -157,7 +157,7 @@ class AddPaymentMethodActivityTest :
157157
setUpForLocalTest()
158158
assertEquals(View.GONE, progressBar.visibility)
159159
activity.createPaymentMethod(stripe, null)
160-
verify<StripeRepository>(mockStripeRepository, never()).createPaymentMethod(
160+
verify(mockStripeRepository, never()).createPaymentMethod(
161161
any(), any()
162162
)
163163
}
@@ -181,11 +181,11 @@ class AddPaymentMethodActivityTest :
181181

182182
val expectedPaymentMethod = PaymentMethodFixtures.FPX_PAYMENT_METHOD
183183

184-
verify<CustomerSession>(customerSession)
184+
verify(customerSession)
185185
.addProductUsageTokenIfValid(TOKEN_ADD_PAYMENT_METHOD_ACTIVITY)
186-
verify<CustomerSession>(customerSession)
186+
verify(customerSession)
187187
.addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION)
188-
verify<CustomerSession>(customerSession, never()).attachPaymentMethod(
188+
verify(customerSession, never()).attachPaymentMethod(
189189
any(), any()
190190
)
191191

@@ -213,11 +213,11 @@ class AddPaymentMethodActivityTest :
213213
val expectedPaymentMethod =
214214
requireNotNull(PaymentMethod.fromJson(PaymentMethodTest.PM_CARD_JSON))
215215

216-
verify<CustomerSession>(customerSession)
216+
verify(customerSession)
217217
.addProductUsageTokenIfValid(TOKEN_ADD_PAYMENT_METHOD_ACTIVITY)
218-
verify<CustomerSession>(customerSession)
218+
verify(customerSession)
219219
.addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION)
220-
verify<CustomerSession>(customerSession).attachPaymentMethod(
220+
verify(customerSession).attachPaymentMethod(
221221
paymentMethodIdCaptor.capture(),
222222
listenerArgumentCaptor.capture()
223223
)
@@ -276,11 +276,11 @@ class AddPaymentMethodActivityTest :
276276

277277
val expectedPaymentMethod = PaymentMethod.fromJson(PaymentMethodTest.PM_CARD_JSON)
278278

279-
verify<CustomerSession>(customerSession)
279+
verify(customerSession)
280280
.addProductUsageTokenIfValid(TOKEN_ADD_PAYMENT_METHOD_ACTIVITY)
281-
verify<CustomerSession>(customerSession)
281+
verify(customerSession)
282282
.addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION)
283-
verify<CustomerSession>(customerSession).attachPaymentMethod(
283+
verify(customerSession).attachPaymentMethod(
284284
paymentMethodIdCaptor.capture(),
285285
listenerArgumentCaptor.capture()
286286
)
@@ -303,7 +303,7 @@ class AddPaymentMethodActivityTest :
303303
assertNull(intent)
304304
assertFalse(activity.isFinishing)
305305
assertEquals(View.GONE, progressBar.visibility)
306-
verify<StripeActivity.AlertMessageListener>(alertMessageListener)
306+
verify(alertMessageListener)
307307
.onAlertMessageDisplayed(errorMessage)
308308
}
309309

stripe/src/test/java/com/stripe/android/view/PaymentFlowActivityTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import com.nhaarman.mockitokotlin2.any
1414
import com.nhaarman.mockitokotlin2.argumentCaptor
1515
import com.stripe.android.ApiKeyFixtures
1616
import com.stripe.android.CustomerSession
17-
import com.stripe.android.CustomerSession.ACTION_API_EXCEPTION
18-
import com.stripe.android.CustomerSession.EXTRA_EXCEPTION
17+
import com.stripe.android.CustomerSession.Companion.ACTION_API_EXCEPTION
18+
import com.stripe.android.CustomerSession.Companion.EXTRA_EXCEPTION
1919
import com.stripe.android.EphemeralKeyProvider
2020
import com.stripe.android.PaymentConfiguration
2121
import com.stripe.android.PaymentSession.Companion.EXTRA_PAYMENT_SESSION_DATA

0 commit comments

Comments
 (0)