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

Add prebuilt UI (PaymentSheet) functionality #3687

Merged
merged 184 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 181 commits
Commits
Show all changes
184 commits
Select commit Hold shift + click to select a range
f9db2da
Add payment sheet support to the example app
mshafrir-stripe Sep 2, 2020
4a992ba
Merge branch 'master' into mc
mshafrir-stripe Dec 16, 2020
dd5e27c
Merge branch 'master' into mc
mshafrir-stripe Dec 17, 2020
3134053
Merge branch 'master' into mc
mshafrir-stripe Dec 17, 2020
a87af7f
Merge branch 'master' into mc
mshafrir-stripe Dec 18, 2020
9a3f7ab
Merge branch 'master' into mc
mshafrir-stripe Dec 18, 2020
36eac83
Merge branch 'master' into mc
mshafrir-stripe Dec 21, 2020
0284d68
Merge branch 'master' into mc
mshafrir-stripe Dec 21, 2020
df80a63
Merge branch 'master' into mc
mshafrir-stripe Dec 21, 2020
5743fd1
Merge branch 'master' into mc
mshafrir-stripe Dec 21, 2020
7241991
Merge branch 'master' into mc
mshafrir-stripe Jan 4, 2021
cad0899
Merge branch 'master' into mc
mshafrir-stripe Jan 4, 2021
8f4a41c
Merge branch 'master' into mc
mshafrir-stripe Jan 4, 2021
e390e2f
Merge branch 'master' into mc
mshafrir-stripe Jan 5, 2021
2cedffa
Merge branch 'master' into mc
mshafrir-stripe Jan 6, 2021
71dc8ae
Merge branch 'master' into mc
mshafrir-stripe Jan 6, 2021
0eb9ba9
Create preferences screen to configure payment sheet
mshafrir-stripe Jan 6, 2021
617ba4d
Merge branch 'master' into mc
mshafrir-stripe Jan 7, 2021
529ae91
Merge branch 'master' into mc
mshafrir-stripe Jan 12, 2021
9b5084c
Merge branch 'master' into mc
mshafrir-stripe Jan 12, 2021
0d9e103
Merge branch 'master' into mc
mshafrir-stripe Jan 12, 2021
ec2f8dc
Create BasePaymentSheetActivity
mshafrir-stripe Jan 12, 2021
8d60018
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
e144bbe
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
0fb3256
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
4bcfc43
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
639c0fa
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
0cbc3ff
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
59aedeb
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
48bf91f
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
31f3252
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
6204b2e
Merge branch 'master' into mc
mshafrir-stripe Jan 13, 2021
2f33b90
Merge branch 'master' into mc
mshafrir-stripe Jan 14, 2021
c7e5329
Merge branch 'master' into mc
mshafrir-stripe Jan 14, 2021
c86968e
Merge branch 'master' into mc
mshafrir-stripe Jan 14, 2021
31bbb51
Merge branch 'master' into mc
mshafrir-stripe Jan 15, 2021
3a868ad
Merge branch 'master' into mc
mshafrir-stripe Jan 15, 2021
000f7d7
Update callback interface
mshafrir-stripe Jan 15, 2021
6fed996
Merge branch 'master' into mc
mshafrir-stripe Jan 15, 2021
6f96b11
Merge branch 'master' into mc
mshafrir-stripe Jan 15, 2021
7e99f58
Merge branch 'master' into mc
mshafrir-stripe Jan 15, 2021
e329683
Merge branch 'master' into mc
mshafrir-stripe Jan 20, 2021
e4e4e07
Merge branch 'master' into mc
mshafrir-stripe Jan 20, 2021
6b127df
Merge branch 'master' into mc
mshafrir-stripe Jan 21, 2021
90f6335
Update example app
mshafrir-stripe Jan 20, 2021
1962057
Rename
mshafrir-stripe Jan 21, 2021
af22c61
Merge branch 'master' into mc
mshafrir-stripe Jan 21, 2021
275297f
Merge branch 'master' into mc
mshafrir-stripe Jan 21, 2021
a074f12
Merge branch 'master' into mc
mshafrir-stripe Jan 21, 2021
f5946a2
Merge branch 'master' into mc
mshafrir-stripe Jan 25, 2021
a2e6cc2
Merge branch 'master' into mc
mshafrir-stripe Jan 26, 2021
0810d52
Fix ktlint
mshafrir-stripe Jan 26, 2021
3822f11
Fix crash
mshafrir-stripe Jan 26, 2021
b256cab
Merge branch 'master' into mc
mshafrir-stripe Jan 26, 2021
1a1756d
Update integration
mshafrir-stripe Jan 26, 2021
96702c6
Merge branch 'master' into mc
mshafrir-stripe Jan 27, 2021
ba99cbb
Merge branch 'master' into mc
mshafrir-stripe Jan 27, 2021
00527e6
Init -> configure
mshafrir-stripe Jan 27, 2021
47d5c2d
Merge branch 'master' into mc
mshafrir-stripe Jan 27, 2021
ace3abb
Show PaymentIntent when successful
mshafrir-stripe Jan 27, 2021
f5ce2e1
Merge branch 'master' into mc
mshafrir-stripe Jan 27, 2021
e957a7b
Merge branch 'master' into mc
mshafrir-stripe Jan 27, 2021
3adc96b
Merge branch 'master' into mc
mshafrir-stripe Jan 28, 2021
161561a
Merge branch 'master' into mc
mshafrir-stripe Jan 28, 2021
faf2e8b
Merge branch 'master' into mc
mshafrir-stripe Jan 28, 2021
42316d1
Merge branch 'master' into mc
mshafrir-stripe Jan 28, 2021
7516be4
Merge branch 'master' into mc
mshafrir-stripe Jan 28, 2021
b196db7
Merge branch 'master' into mc
mshafrir-stripe Jan 28, 2021
d4bedca
Merge branch 'master' into mc
mshafrir-stripe Jan 29, 2021
66ce30c
Merge branch 'master' into mc
mshafrir-stripe Jan 29, 2021
1cd8ac6
Merge branch 'master' into mc
mshafrir-stripe Feb 1, 2021
5b55797
Merge branch 'master' into mc
mshafrir-stripe Feb 1, 2021
6f35964
Merge branch 'master' into mc
mshafrir-stripe Feb 2, 2021
c73a20b
Merge branch 'master' into mc
mshafrir-stripe Feb 2, 2021
42ff767
Merge branch 'master' into mc
mshafrir-stripe Feb 3, 2021
3ae9ff7
Merge branch 'master' into mc
mshafrir-stripe Feb 3, 2021
c484d51
Merge branch 'master' into mc
mshafrir-stripe Feb 3, 2021
183386b
Merge branch 'master' into mc
mshafrir-stripe Feb 3, 2021
454a08a
Merge branch 'master' into mc
mshafrir-stripe Feb 3, 2021
141ac26
Merge branch 'master' into mc
mshafrir-stripe Feb 4, 2021
0947b52
Remove arrays.xml
mshafrir-stripe Feb 4, 2021
237b797
Merge branch 'master' into mc
mshafrir-stripe Feb 4, 2021
b274f7c
Merge branch 'master' into mc
mshafrir-stripe Feb 4, 2021
e7fac3f
Merge branch 'master' into mc
mshafrir-stripe Feb 4, 2021
96706d3
Merge branch 'master' into mc
mshafrir-stripe Feb 4, 2021
5424ee2
Merge branch 'master' into mc
mshafrir-stripe Feb 4, 2021
0885c7e
Update
mshafrir-stripe Feb 4, 2021
7e6d39b
Merge branch 'master' into mc
mshafrir-stripe Feb 4, 2021
f375f07
Merge branch 'master' into mc
mshafrir-stripe Feb 5, 2021
b237e89
Update
mshafrir-stripe Feb 5, 2021
5be7674
Merge branch 'master' into mc
mshafrir-stripe Feb 5, 2021
e50f2d7
Merge branch 'master' into mc
mshafrir-stripe Feb 5, 2021
cba4866
Update copy
mshafrir-stripe Feb 5, 2021
e5e18fc
Merge branch 'master' into mc
mshafrir-stripe Feb 8, 2021
f5b1737
Merge branch 'master' into mc
mshafrir-stripe Feb 8, 2021
47469be
Merge branch 'master' into mc
mshafrir-stripe Feb 8, 2021
b7fc2ef
Merge branch 'master' into mc
mshafrir-stripe Feb 8, 2021
4e40052
Merge branch 'master' into mc
mshafrir-stripe Feb 8, 2021
2f04a84
Merge branch 'master' into mc
mshafrir-stripe Feb 8, 2021
3b60ad0
Merge branch 'master' into mc
mshafrir-stripe Feb 9, 2021
897932a
Merge branch 'master' into mc
mshafrir-stripe Feb 9, 2021
8e6f630
Merge branch 'master' into mc
mshafrir-stripe Feb 9, 2021
5261659
Merge branch 'master' into mc
mshafrir-stripe Feb 10, 2021
123cfbe
Merge branch 'master' into mc
mshafrir-stripe Feb 18, 2021
d7d25f6
Merge branch 'master' into mc
mshafrir-stripe Feb 23, 2021
48745b5
Merge remote-tracking branch 'origin/master' into mc
michelleb-stripe Feb 24, 2021
6bba17e
Merge branch 'master' into mc
michelleb-stripe Feb 25, 2021
c481b42
Merge branch 'master' into mc
mshafrir-stripe Feb 26, 2021
66f2e7e
Merge branch 'master' into mc
mshafrir-stripe Mar 3, 2021
641c0c2
Merge branch 'master' into mc
mshafrir-stripe Mar 3, 2021
f956f13
Make PaymentSheet's fragment constructor public
mshafrir-stripe Mar 3, 2021
6fcce16
Merge branch 'master' into mc
mshafrir-stripe Mar 3, 2021
4fe9e62
Fix apiDump
mshafrir-stripe Mar 4, 2021
53942d1
Merge branch 'master' into mc
mshafrir-stripe Mar 4, 2021
789a75a
Merge branch 'master' into mc
mshafrir-stripe Mar 5, 2021
c37f316
Merge branch 'master' into mc
mshafrir-stripe Mar 8, 2021
983ea55
Merge branch 'master' into mc
mshafrir-stripe Mar 8, 2021
f53904c
Merge branch 'master' into mc
mshafrir-stripe Mar 8, 2021
8b01879
Merge branch 'master' into mc
mshafrir-stripe Mar 8, 2021
b28c6c4
Merge branch 'master' into mc
mshafrir-stripe Mar 9, 2021
18b29e7
Merge branch 'master' into mc
mshafrir-stripe Mar 9, 2021
8827ebe
Merge branch 'master' into mc
mshafrir-stripe Mar 9, 2021
1fb8888
Merge branch 'master' into mc
mshafrir-stripe Mar 11, 2021
167793a
Update apiDump
mshafrir-stripe Mar 11, 2021
d7429d0
Merge branch 'master' into mc
mshafrir-stripe Mar 12, 2021
8547cb6
Merge branch 'master' into mc
mshafrir-stripe Mar 12, 2021
793812d
Payment Options won't jump to add card if google pay is ready.
Mar 15, 2021
693900f
On Payment options remove google pay from add card.
Mar 15, 2021
91849f2
Add tests.
Mar 15, 2021
1a30548
Merge remote-tracking branch 'origin/master' into michelleb/custom-ad…
Mar 16, 2021
09d382e
Fix extra blank lines.
Mar 16, 2021
88bb1be
Fix failing unit test.
Mar 16, 2021
41995f2
Merge branch 'master' into michelleb/custom-add-no-googlepay
Mar 16, 2021
6eda646
Merge branch 'master' into mc
mshafrir-stripe Mar 16, 2021
7ab4502
Merge branch 'master' into mc
mshafrir-stripe Mar 17, 2021
4fb9285
Merge branch 'master' into mc
mshafrir-stripe Mar 17, 2021
eac04ce
Merge branch 'master' into mc
mshafrir-stripe Mar 18, 2021
7d3c544
Merge branch 'master' into mc
mshafrir-stripe Mar 19, 2021
7432861
Merge branch 'master' into mc
mshafrir-stripe Mar 19, 2021
3c13d96
Merge branch 'master' into mc
mshafrir-stripe Mar 22, 2021
a368e41
Merge branch 'master' into mc
michelleb-stripe Mar 22, 2021
554fbae
Merge branch 'master' into mc
mshafrir-stripe Mar 22, 2021
85f690f
Merge branch 'master' into mc
mshafrir-stripe Mar 22, 2021
f671bb8
Merge branch 'master' into mc
mshafrir-stripe Mar 23, 2021
db99755
Merge branch 'master' into mc
mshafrir-stripe Mar 23, 2021
96fd89e
Merge branch 'master' into mc
mshafrir-stripe Mar 23, 2021
751371e
Merge branch 'master' into mc
mshafrir-stripe Mar 23, 2021
b284099
Merge branch 'master' into mc
mshafrir-stripe Mar 24, 2021
af18ba1
Merge branch 'master' into mc
mshafrir-stripe Mar 26, 2021
2eedde3
Merge branch 'master' into mc
mshafrir-stripe Mar 29, 2021
94519d5
Merge branch 'master' into mc
michelleb-stripe Mar 30, 2021
8f7f5d3
Merge branch 'master' into mc
mshafrir-stripe Apr 6, 2021
a695908
Merge branch 'master' into mc
mshafrir-stripe Apr 6, 2021
d7dbef6
Merge branch 'master' into mc
mshafrir-stripe Apr 8, 2021
fee9e71
Merge branch 'master' into mc
mshafrir-stripe Apr 8, 2021
77ada10
Merge remote-tracking branch 'origin/master' into mc
michelleb-stripe Apr 8, 2021
1438ce4
Merge branch 'master' into mc
brnunes-stripe Apr 14, 2021
8cb61a5
Merge branch 'master' into mc
mshafrir-stripe Apr 14, 2021
ac012a6
Merge branch 'master' into mc
mshafrir-stripe Apr 15, 2021
1f32038
Merge branch 'master' into mc
brnunes-stripe Apr 20, 2021
909c449
Merge branch 'master' into mc
brnunes-stripe Apr 21, 2021
9bc7782
Merge branch 'master' into mc
mshafrir-stripe Apr 21, 2021
5a2a10e
Merge branch 'master' into mc
brnunes-stripe Apr 21, 2021
e91ae7b
apiDump
brnunes-stripe Apr 21, 2021
3949c76
Use updated PaymentSheet API. (#3592)
brnunes-stripe Apr 21, 2021
7ec6903
Merge branch 'master' into mc
brnunes-stripe Apr 26, 2021
06a6382
Merge branch 'master' into mc
michelleb-stripe Apr 27, 2021
e3b16bf
Merge branch 'master' into mc
mshafrir-stripe Apr 28, 2021
9db8c5b
Merge branch 'master' into mc
brnunes-stripe Apr 29, 2021
0e63978
Merge branch 'master' into mc
mshafrir-stripe May 4, 2021
15af17f
Merge branch 'master' into mc
michelleb-stripe May 4, 2021
af5da6e
Merge branch 'master' into mc
mshafrir-stripe May 4, 2021
f50f907
Merge remote-tracking branch 'origin/master' into mc
michelleb-stripe May 5, 2021
9a4f6ea
Update the changelog.
michelleb-stripe May 5, 2021
b4ad6a4
Merge remote-tracking branch 'origin/master' into mc
michelleb-stripe May 5, 2021
ed8819d
Merge branch 'mc' into michellebe/mc-update-change-log
michelleb-stripe May 5, 2021
b2288a8
Remove unused variables in PaymentSheetViewModel (#3686)
michelleb-stripe May 5, 2021
248733a
Merge branch 'master' into mc
michelleb-stripe May 6, 2021
c05c7e8
Merge with mc
michelleb-stripe May 6, 2021
1452cd4
Move PaymentSheet example to /checkout endpoint. (#3633)
brnunes-stripe May 6, 2021
31a3433
Merge remote-tracking branch 'origin/mc' into michellebe/mc-update-ch…
michelleb-stripe May 7, 2021
9bb4b00
Update the readme (#3703)
michelleb-stripe May 7, 2021
db2499c
Merge remote-tracking branch 'origin/master' into mc
michelleb-stripe May 7, 2021
1ed76df
Merge branch 'mc' into michellebe/mc-update-change-log
michelleb-stripe May 7, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [#3677](https://github.com/stripe/stripe-android/pull/3677) Upgrade Android Gradle Plugin to `4.2.0`
* [#3680](https://github.com/stripe/stripe-android/pull/3680) Deprecate `returnUrl` in some `ConfirmPaymentIntentParams` create() methods
* [#3685](https://github.com/stripe/stripe-android/pull/3685) Upgrade Kotlin to `1.5.0`
* [#3687](https://github.com/stripe/stripe-android/pull/3687) Add support for PaymentSheet pre-built UI.

## 16.7.1 - 2021-04-29
* [#3653](https://github.com/stripe/stripe-android/pull/3653) Support WeChat Pay for creating a `PaymentMethod` and confirming a `PaymentIntent`
Expand Down
3 changes: 3 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'kotlin-android'
id 'checkstyle'
id 'org.jetbrains.kotlin.plugin.parcelize'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.30'
}

assemble.dependsOn('lint')
Expand Down Expand Up @@ -69,13 +70,15 @@ dependencies {
/* Used to make Retrofit easier and GSON & Rx-compatible*/
implementation 'com.google.code.gson:gson:2.8.6'
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"

implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0"

ktlint "com.pinterest:ktlint:$ktlintVersion"

Expand Down
7 changes: 0 additions & 7 deletions example/res/menu/payment_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/refresh_key"
android:orderInCategory="300"
android:title="Refresh Ephemeral Key"
android:icon="@drawable/ic_refresh"
app:showAsAction="ifRoom" />

<item
android:id="@+id/config"
android:orderInCategory="300"
Expand Down
18 changes: 18 additions & 0 deletions example/res/menu/payment_sheet_custom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/refresh_key"
android:orderInCategory="300"
android:title="Refresh"
android:icon="@drawable/ic_refresh"
app:showAsAction="ifRoom" />

<item
android:id="@+id/config"
android:orderInCategory="300"
android:title="Configure Payment Sheet"
android:icon="@drawable/ic_settings"
app:showAsAction="ifRoom" />
</menu>
5 changes: 5 additions & 0 deletions example/res/xml/payment_sheet_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
app:title="Use Customer API"
app:defaultValue="true" />

<SwitchPreferenceCompat
app:key="returning_customer"
app:title="Returning Customer"
app:defaultValue="true" />

<SwitchPreferenceCompat
app:key="enable_googlepay"
app:title="Enable Google Pay"
Expand Down
9 changes: 8 additions & 1 deletion example/src/main/java/com/stripe/example/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Settings(context: Context) {
.takeIf { it?.isNotBlank() == true }
}

private companion object {
internal companion object {
/**
* Note: only necessary if not configured via `gradle.properties`.
*
Expand All @@ -62,6 +62,13 @@ class Settings(context: Context) {
*/
private val STRIPE_ACCOUNT_ID: String? = null

// Example payment sheet backend with a "/checkout" endpoint
// Remix from https://glitch.com/edit/#!/stripe-mobile-payment-sheet-test-playground-v3
internal const val PAYMENT_SHEET_BASE_URL = ""
michelleb-stripe marked this conversation as resolved.
Show resolved Hide resolved

// Publishable key for example payment sheet backend
internal const val PAYMENT_SHEET_PUBLISHABLE_KEY = ""
michelleb-stripe marked this conversation as resolved.
Show resolved Hide resolved

private const val METADATA_KEY_BACKEND_URL_KEY =
"com.stripe.example.metadata.backend_url"
private const val METADATA_KEY_PUBLISHABLE_KEY =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.stripe.example.activity

import android.content.Context
import android.content.SharedPreferences
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.stripe.android.PaymentConfiguration
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.PaymentSheetResult
import com.stripe.example.R
Expand All @@ -15,8 +15,7 @@ import com.stripe.example.paymentsheet.PaymentSheetViewModel
internal abstract class BasePaymentSheetActivity : AppCompatActivity() {
protected val viewModel: PaymentSheetViewModel by viewModels {
PaymentSheetViewModel.Factory(
application,
getPreferences(Context.MODE_PRIVATE)
application
)
}

Expand All @@ -30,6 +29,9 @@ internal abstract class BasePaymentSheetActivity : AppCompatActivity() {
protected val isCustomerEnabled: Boolean
get() = prefsManager.getBoolean("enable_customer", true)

protected val isReturningCustomer: Boolean
get() = prefsManager.getBoolean("returning_customer", true)

protected val isSetupIntent: Boolean
get() = prefsManager.getBoolean("setup_intent", false)

Expand All @@ -46,6 +48,20 @@ internal abstract class BasePaymentSheetActivity : AppCompatActivity() {
}
}

protected val customer: String
get() = if (isCustomerEnabled && isReturningCustomer) {
"returning"
} else if (isCustomerEnabled) {
temporaryCustomerId ?: "new"
} else {
"new"
}

protected val mode: String
get() = if (isSetupIntent) "setup" else "payment"

protected var temporaryCustomerId: String? = null

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.payment_sheet, menu)
Expand All @@ -59,44 +75,41 @@ internal abstract class BasePaymentSheetActivity : AppCompatActivity() {
PaymentSheetConfigBottomSheet.TAG
)
return true
} else if (item.itemId == R.id.refresh_key) {
viewModel.clearKeys()
onRefreshEphemeralKey()
}

return super.onOptionsItemSelected(item)
}

protected fun fetchEphemeralKey(
onSuccess: (PaymentSheet.CustomerConfiguration) -> Unit = {}
protected fun prepareCheckout(
onSuccess: (PaymentSheet.CustomerConfiguration, String) -> Unit
) {
viewModel.fetchEphemeralKey()
.observe(this) { newEphemeralKey ->
if (newEphemeralKey != null) {
viewModel.prepareCheckout(customer, mode)
.observe(this) { checkoutResponse ->
if (checkoutResponse != null) {
temporaryCustomerId = if (isCustomerEnabled && !isReturningCustomer) {
checkoutResponse.customerId
} else {
null
}

// Re-initing here because the ExampleApplication inits with the key from
// gradle properties
PaymentConfiguration.init(this, checkoutResponse.publishableKey)

onSuccess(
PaymentSheet.CustomerConfiguration(
id = newEphemeralKey.customer,
ephemeralKeySecret = newEphemeralKey.key
)
id = checkoutResponse.customerId,
ephemeralKeySecret = checkoutResponse.customerEphemeralKeySecret
),
checkoutResponse.intentClientSecret
)
}
}
}

protected fun onPaymentSheetResult(
protected open fun onPaymentSheetResult(
paymentResult: PaymentSheetResult
) {
viewModel.status.value = paymentResult.toString()
}

protected fun onError(error: Throwable) {
viewModel.status.postValue(
"""
${viewModel.status.value}

Failed: ${error.message}
""".trimIndent()
)
}

abstract fun onRefreshEphemeralKey()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.stripe.example.activity

import android.os.Bundle
import android.view.View
import androidx.core.view.isInvisible
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.example.databinding.ActivityPaymentSheetCompleteBinding

Expand All @@ -14,66 +14,39 @@ internal class LaunchPaymentSheetCompleteActivity : BasePaymentSheetActivity() {
super.onCreate(savedInstanceState)
setContentView(viewBinding.root)

val paymentSheet = PaymentSheet(this, ::onPaymentSheetResult)

viewModel.inProgress.observe(this) {
viewBinding.progressBar.visibility = if (it) View.VISIBLE else View.INVISIBLE
viewBinding.progressBar.isInvisible = !it
viewBinding.launch.isEnabled = !it
}

viewModel.status.observe(this) {
viewBinding.status.text = it
}

viewBinding.launch.setOnClickListener {
if (isCustomerEnabled) {
fetchEphemeralKey { customerConfig ->
createPaymentIntent(
customerConfig
prepareCheckout { customerConfig, clientSecret ->
if (isSetupIntent) {
paymentSheet.presentWithSetupIntent(
clientSecret,
PaymentSheet.Configuration(
merchantDisplayName = merchantName,
customer = customerConfig,
googlePay = googlePayConfig,
)
)
}
} else {
createPaymentIntent(
null
)
}
}

fetchEphemeralKey()
}

private fun createPaymentIntent(
customerConfig: PaymentSheet.CustomerConfiguration?
) {
viewModel.createPaymentIntent(
COUNTRY_CODE,
customerId = customerConfig?.id
).observe(this) { result ->
result.fold(
onSuccess = { json ->
val clientSecret = json.getString("secret")

onPaymentIntent(
} else {
paymentSheet.presentWithPaymentIntent(
clientSecret,
customerConfig
PaymentSheet.Configuration(
merchantDisplayName = merchantName,
customer = customerConfig,
googlePay = googlePayConfig,
)
)
},
onFailure = ::onError
)
}
}
}
}

private fun onPaymentIntent(
paymentIntentClientSecret: String,
customerConfig: PaymentSheet.CustomerConfiguration?
) {
viewModel.inProgress.postValue(false)

// show PaymentSheet
}

override fun onRefreshEphemeralKey() {
fetchEphemeralKey()
}

private companion object {
private const val COUNTRY_CODE = "us"
}
}
Loading