Skip to content

Commit

Permalink
use suspend fun in example app
Browse files Browse the repository at this point in the history
  • Loading branch information
ccen-stripe committed Apr 15, 2021
1 parent c5b86dd commit be443bd
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 87 deletions.
6 changes: 6 additions & 0 deletions example/res/layout/create_card_payment_method_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
android:layout_width="320dp"
android:text="@string/create_payment_method" />

<CheckBox
android:id="@+id/use_suspend_api"
android:layout_height="wrap_content"
android:layout_width="320dp"
android:text="@string/use_suspend_api" />

<TextView
style="@style/Header"
android:layout_marginTop="30dp"
Expand Down
7 changes: 7 additions & 0 deletions example/res/layout/create_card_source_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
android:layout_gravity="center"
android:text="@string/create_source_header"/>

<CheckBox
android:id="@+id/use_suspend_api"
android:layout_height="wrap_content"
android:layout_width="160dp"
android:layout_gravity="center"
android:text="@string/use_suspend_api" />

<TextView android:text="@string/updatedSources"
style="@style/Header" />

Expand Down
6 changes: 6 additions & 0 deletions example/res/layout/create_card_token_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
android:text="@string/create_token"
/>

<CheckBox
android:id="@+id/use_suspend_api"
android:layout_height="wrap_content"
android:layout_width="160dp"
android:text="@string/use_suspend_api" />

<TextView android:id="@+id/token_list_title"
android:text="@string/paymentMethods"
android:layout_marginTop="12dp"
Expand Down
6 changes: 6 additions & 0 deletions example/res/layout/klarna_source_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
android:text="@string/fetch_klarna_source"
/>

<CheckBox
android:id="@+id/use_suspend_api"
android:layout_height="wrap_content"
android:layout_width="320dp"
android:text="@string/use_suspend_api" />

<TextView
android:id="@+id/source_result"
android:layout_width="match_parent"
Expand Down
8 changes: 8 additions & 0 deletions example/res/layout/payment_auth_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"/>

<CheckBox
android:id="@+id/use_suspend_api"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/use_suspend_api"
android:layout_marginBottom="12dp" />

<TextView
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface="monospace"
android:layout_marginTop="40dp"/>

</LinearLayout>
</ScrollView>
1 change: 1 addition & 0 deletions example/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<string name="launch_payment_session">Payment Session</string>
<string name="launch_payment_session_from_fragment">Fragment Examples</string>
<string name="create_payment_method">Create Payment Method</string>
<string name="use_suspend_api">Use suspend API</string>
<string name="create_token">Create Token</string>
<string name="launch_confirm_pm_sepa_debit">Confirm with SEPA Debit Payment Method</string>
<string name="fpx_payment_example">FPX Payment Example</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class CreateCardPaymentMethodActivity : AppCompatActivity() {
keyboardController.hide()

viewBinding.cardMultilineWidget.paymentMethodCreateParams?.let {
createPaymentMethod(it)
createPaymentMethod(it, viewBinding.useSuspendApi.isChecked)
}
}
}

private fun createPaymentMethod(params: PaymentMethodCreateParams) {
private fun createPaymentMethod(params: PaymentMethodCreateParams, useSuspendApi: Boolean) {
onCreatePaymentMethodStart()
viewModel.createPaymentMethod(params).observe(
viewModel.createPaymentMethod(params, useSuspendApi).observe(
this,
{ result ->
onCreatePaymentMethodCompleted()
Expand All @@ -72,11 +72,13 @@ class CreateCardPaymentMethodActivity : AppCompatActivity() {
private fun onCreatePaymentMethodStart() {
viewBinding.progressBar.visibility = View.VISIBLE
viewBinding.createButton.isEnabled = false
viewBinding.useSuspendApi.isEnabled = false
}

private fun onCreatePaymentMethodCompleted() {
viewBinding.progressBar.visibility = View.INVISIBLE
viewBinding.createButton.isEnabled = true
viewBinding.useSuspendApi.isEnabled = true
}

private fun onCreatedPaymentMethod(paymentMethod: PaymentMethod?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ class CreateCardSourceActivity : AppCompatActivity() {
keyboardController.hide()

viewBinding.createButton.isEnabled = false
viewBinding.useSuspendApi.isEnabled = false
viewBinding.progressBar.visibility = View.VISIBLE

val params = SourceParams.createCardParams(cardParams)
viewModel.createSource(params).observe(
viewModel.createSource(params, viewBinding.useSuspendApi.isChecked).observe(
this,
{ result ->
viewBinding.createButton.isEnabled = true
viewBinding.useSuspendApi.isEnabled = true
viewBinding.progressBar.visibility = View.INVISIBLE

result.fold(
Expand Down Expand Up @@ -144,7 +146,7 @@ class CreateCardSourceActivity : AppCompatActivity() {
cardId = source.id.orEmpty()
)

viewModel.createSource(params).observe(
viewModel.createSource(params, viewBinding.useSuspendApi.isChecked).observe(
this,
{ result ->
viewBinding.progressBar.visibility = View.INVISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.stripe.android.ApiResultCallback
import com.stripe.android.createCardToken
import com.stripe.android.model.CardParams
import com.stripe.android.model.Token
import com.stripe.example.R
import com.stripe.example.StripeFactory
import com.stripe.example.databinding.CreateCardTokenActivityBinding
import com.stripe.example.databinding.TokenItemBinding
import kotlinx.coroutines.launch

class CreateCardTokenActivity : AppCompatActivity() {
private val viewBinding: CreateCardTokenActivityBinding by lazy {
Expand Down Expand Up @@ -48,7 +51,7 @@ class CreateCardTokenActivity : AppCompatActivity() {

viewBinding.cardInputWidget.cardParams?.let { cardParams ->
onRequestStart()
viewModel.createCardToken(cardParams).observe(
viewModel.createCardToken(cardParams, viewBinding.useSuspendApi.isChecked).observe(
this,
{
onRequestEnd()
Expand All @@ -68,11 +71,13 @@ class CreateCardTokenActivity : AppCompatActivity() {
private fun onRequestStart() {
viewBinding.progressBar.visibility = View.VISIBLE
viewBinding.createTokenButton.isEnabled = false
viewBinding.useSuspendApi.isEnabled = false
}

private fun onRequestEnd() {
viewBinding.progressBar.visibility = View.INVISIBLE
viewBinding.createTokenButton.isEnabled = true
viewBinding.useSuspendApi.isEnabled = true
}

internal class Adapter(
Expand Down Expand Up @@ -118,23 +123,35 @@ class CreateCardTokenActivity : AppCompatActivity() {
) : AndroidViewModel(application) {
private val stripe = StripeFactory(application).create()

fun createCardToken(cardParams: CardParams): LiveData<Token> {
fun createCardToken(cardParams: CardParams, useSuspendApi: Boolean): LiveData<Token> {
val data = MutableLiveData<Token>()

stripe.createCardToken(
cardParams,
callback = object : ApiResultCallback<Token> {
override fun onSuccess(result: Token) {
BackgroundTaskTracker.onStop()
data.value = result
}

override fun onError(e: Exception) {
BackgroundTaskTracker.onStop()
if (useSuspendApi) {
viewModelScope.launch {
data.value = try {
stripe.createCardToken(cardParams)
} catch (e: Exception) {
Log.e("StripeExample", "Error while creating card token", e)
null
} finally {
BackgroundTaskTracker.onStop()
}
}
)
} else {
stripe.createCardToken(
cardParams,
callback = object : ApiResultCallback<Token> {
override fun onSuccess(result: Token) {
BackgroundTaskTracker.onStop()
data.value = result
}

override fun onError(e: Exception) {
BackgroundTaskTracker.onStop()
Log.e("StripeExample", "Error while creating card token", e)
}
}
)
}

return data
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import android.widget.Button
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.lifecycleScope
import com.stripe.android.ApiResultCallback
import com.stripe.android.Stripe
import com.stripe.android.getAuthenticateSourceResult
import com.stripe.android.model.Address
import com.stripe.android.model.DateOfBirth
import com.stripe.android.model.KlarnaSourceParams
import com.stripe.android.model.Source
import com.stripe.android.model.SourceParams
import com.stripe.example.StripeFactory
import com.stripe.example.databinding.KlarnaSourceActivityBinding
import kotlinx.coroutines.launch

class KlarnaSourceActivity : AppCompatActivity() {
private val viewBinding: KlarnaSourceActivityBinding by lazy {
Expand Down Expand Up @@ -59,7 +62,10 @@ class KlarnaSourceActivity : AppCompatActivity() {

viewBinding.fetchButton.setOnClickListener {
disableUi()
viewModel.fetchSource(viewModel.source).observe(
viewModel.fetchSource(
viewModel.source,
viewBinding.useSuspendApi.isChecked
).observe(
this,
{ result ->
enableUi()
Expand All @@ -71,24 +77,41 @@ class KlarnaSourceActivity : AppCompatActivity() {

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (data != null && stripe.isAuthenticateSourceResult(requestCode, data)) {
stripe.onAuthenticateSourceResult(
data,
object : ApiResultCallback<Source> {
override fun onSuccess(result: Source) {
enableUi()
viewModel.source = result
logSource(result)
}

override fun onError(e: Exception) {
if (viewBinding.useSuspendApi.isChecked) {
data?.let {
lifecycleScope.launch {
viewModel.source = try {
stripe.getAuthenticateSourceResult(requestCode, data).let { result ->
enableUi()
result
}
} catch (e: java.lang.Exception) {
enableUi()
logException(e)
null
}
}
)
}
} else {
if (data != null && stripe.isAuthenticateSourceResult(requestCode, data)) {
stripe.onAuthenticateSourceResult(
data,
object : ApiResultCallback<Source> {
override fun onSuccess(result: Source) {
enableUi()
viewModel.source = result
logSource(result)
}

override fun onError(e: Exception) {
enableUi()
logException(e)
}
}
)
}
}

}

private fun logSource(source: Source) {
Expand All @@ -112,11 +135,13 @@ class KlarnaSourceActivity : AppCompatActivity() {
private fun enableUi() {
viewBinding.progressBar.visibility = View.INVISIBLE
buttons.forEach { it.isEnabled = true }
viewBinding.useSuspendApi.isEnabled = true
}

private fun disableUi() {
viewBinding.progressBar.visibility = View.VISIBLE
buttons.forEach { it.isEnabled = false }
viewBinding.useSuspendApi.isEnabled = false
}

private fun createKlarnaSource(): LiveData<Result<Source>> {
Expand All @@ -143,7 +168,8 @@ class KlarnaSourceActivity : AppCompatActivity() {
billingPhone = "02012267709",
billingDob = DateOfBirth(1, 1, 1990)
)
)
),
viewBinding.useSuspendApi.isChecked
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class PaymentAuthActivity : StripeIntentActivity() {
stripeAccountId = stripeAccountId
)
}

viewBinding.useSuspendApi.setOnCheckedChangeListener { _, isChecked ->
viewModel.useSuspendApi = isChecked
}

}

private fun enableUi(enable: Boolean) {
Expand All @@ -88,6 +93,7 @@ class PaymentAuthActivity : StripeIntentActivity() {
viewBinding.confirmWith3ds1Button.isEnabled = enable
viewBinding.confirmWithNewCardButton.isEnabled = enable
viewBinding.setupButton.isEnabled = enable
viewBinding.useSuspendApi.isEnabled = enable
}

private companion object {
Expand Down
Loading

0 comments on commit be443bd

Please sign in to comment.