diff --git a/example/AndroidManifest.xml b/example/AndroidManifest.xml index b55e70319f1..f8067422145 100644 --- a/example/AndroidManifest.xml +++ b/example/AndroidManifest.xml @@ -15,6 +15,16 @@ android:supportsRtl="true" tools:ignore="AllowBackup"> + + + + launchAddPaymentMethod()); diff --git a/example/src/main/java/com/stripe/example/activity/FragmentExamplesActivity.kt b/example/src/main/java/com/stripe/example/activity/FragmentExamplesActivity.kt index 43358155087..07ec7c62ea9 100644 --- a/example/src/main/java/com/stripe/example/activity/FragmentExamplesActivity.kt +++ b/example/src/main/java/com/stripe/example/activity/FragmentExamplesActivity.kt @@ -10,6 +10,7 @@ import android.widget.ProgressBar import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment import com.stripe.android.ApiResultCallback import com.stripe.android.CustomerSession import com.stripe.android.PaymentConfiguration @@ -25,7 +26,7 @@ import com.stripe.android.model.ConfirmSetupIntentParams import com.stripe.android.model.Customer import com.stripe.android.view.ShippingInfoWidget import com.stripe.example.R -import com.stripe.example.module.RetrofitFactory +import com.stripe.example.module.BackendApiFactory import com.stripe.example.service.BackendApi import com.stripe.example.service.ExampleEphemeralKeyProvider import io.reactivex.android.schedulers.AndroidSchedulers @@ -54,7 +55,7 @@ class FragmentExamplesActivity : AppCompatActivity() { .commit() } - class LauncherFragment : androidx.fragment.app.Fragment() { + class LauncherFragment : Fragment() { private val compositeDisposable = CompositeDisposable() @@ -71,7 +72,7 @@ class FragmentExamplesActivity : AppCompatActivity() { override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - backendApi = RetrofitFactory.instance.create(BackendApi::class.java) + backendApi = BackendApiFactory(requireContext()).create() stripe = Stripe( requireContext(), PaymentConfiguration.getInstance(requireContext()).publishableKey @@ -274,7 +275,10 @@ class FragmentExamplesActivity : AppCompatActivity() { } private fun createCustomerSession(): CustomerSession { - CustomerSession.initCustomerSession(requireContext(), ExampleEphemeralKeyProvider()) + CustomerSession.initCustomerSession( + requireContext(), + ExampleEphemeralKeyProvider(requireContext()) + ) val customerSession = CustomerSession.getInstance() customerSession.retrieveCurrentCustomer( object : CustomerSession.CustomerRetrievalListener { diff --git a/example/src/main/java/com/stripe/example/activity/LauncherActivity.kt b/example/src/main/java/com/stripe/example/activity/LauncherActivity.kt index 7e294654d3f..42ee05d92e5 100644 --- a/example/src/main/java/com/stripe/example/activity/LauncherActivity.kt +++ b/example/src/main/java/com/stripe/example/activity/LauncherActivity.kt @@ -20,7 +20,7 @@ class LauncherActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_launcher) - PaymentConfiguration.init(this, Settings.PUBLISHABLE_KEY) + PaymentConfiguration.init(this, Settings(this).publishableKey) val linearLayoutManager = LinearLayoutManager(this) .apply { diff --git a/example/src/main/java/com/stripe/example/activity/PaymentAuthActivity.kt b/example/src/main/java/com/stripe/example/activity/PaymentAuthActivity.kt index 4de5301e48a..a9efb772eef 100644 --- a/example/src/main/java/com/stripe/example/activity/PaymentAuthActivity.kt +++ b/example/src/main/java/com/stripe/example/activity/PaymentAuthActivity.kt @@ -16,7 +16,7 @@ import com.stripe.android.model.ConfirmSetupIntentParams import com.stripe.android.model.PaymentMethodCreateParams import com.stripe.example.R import com.stripe.example.Settings -import com.stripe.example.module.RetrofitFactory +import com.stripe.example.module.BackendApiFactory import com.stripe.example.service.BackendApi import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable @@ -39,12 +39,12 @@ class PaymentAuthActivity : AppCompatActivity() { private lateinit var backendApi: BackendApi private lateinit var statusTextView: TextView - private val stripeAccountId: String? = Settings.STRIPE_ACCOUNT_ID - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_payment_auth) + val stripeAccountId = Settings(this).stripeAccountId + val uiCustomization = PaymentAuthConfig.Stripe3ds2UiCustomization.Builder().build() PaymentAuthConfig.init(PaymentAuthConfig.Builder() @@ -59,7 +59,7 @@ class PaymentAuthActivity : AppCompatActivity() { statusTextView.text = savedInstanceState.getString(STATE_STATUS) } - backendApi = RetrofitFactory.instance.create(BackendApi::class.java) + backendApi = BackendApiFactory(this).create() val publishableKey = PaymentConfiguration.getInstance(this).publishableKey stripe = Stripe(this, publishableKey, stripeAccountId = stripeAccountId, diff --git a/example/src/main/java/com/stripe/example/activity/PaymentSessionActivity.kt b/example/src/main/java/com/stripe/example/activity/PaymentSessionActivity.kt index 71eb2c18c10..5b28e3e853c 100644 --- a/example/src/main/java/com/stripe/example/activity/PaymentSessionActivity.kt +++ b/example/src/main/java/com/stripe/example/activity/PaymentSessionActivity.kt @@ -94,7 +94,7 @@ class PaymentSessionActivity : AppCompatActivity() { private fun createCustomerSession(): CustomerSession { CustomerSession.initCustomerSession( this, - ExampleEphemeralKeyProvider(), + ExampleEphemeralKeyProvider(this), false ) return CustomerSession.getInstance() diff --git a/example/src/main/java/com/stripe/example/module/RetrofitFactory.kt b/example/src/main/java/com/stripe/example/module/BackendApiFactory.kt similarity index 76% rename from example/src/main/java/com/stripe/example/module/RetrofitFactory.kt rename to example/src/main/java/com/stripe/example/module/BackendApiFactory.kt index 6a2c62311b7..275f7cbdcdc 100644 --- a/example/src/main/java/com/stripe/example/module/RetrofitFactory.kt +++ b/example/src/main/java/com/stripe/example/module/BackendApiFactory.kt @@ -1,8 +1,10 @@ package com.stripe.example.module +import android.content.Context import com.facebook.stetho.okhttp3.StethoInterceptor import com.google.gson.GsonBuilder import com.stripe.example.Settings +import com.stripe.example.service.BackendApi import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit @@ -12,10 +14,11 @@ import retrofit2.converter.gson.GsonConverterFactory /** * Factory to generate our Retrofit instance. */ -object RetrofitFactory { - val instance: Retrofit +class BackendApiFactory internal constructor(private val backendUrl: String) { - init { + constructor(context: Context) : this(Settings(context).backendUrl) + + fun create(): BackendApi { // Set your desired log level. Use Level.BODY for debugging errors. // Adding Rx so the calls can be Observable, and adding a Gson converter with // leniency to make parsing the results simple. @@ -31,11 +34,12 @@ object RetrofitFactory { .setLenient() .create() - instance = Retrofit.Builder() + return Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) - .baseUrl(Settings.BASE_URL) + .baseUrl(backendUrl) .client(httpClient) .build() + .create(BackendApi::class.java) } } diff --git a/example/src/main/java/com/stripe/example/service/ExampleEphemeralKeyProvider.kt b/example/src/main/java/com/stripe/example/service/ExampleEphemeralKeyProvider.kt index 88b91efceb8..55e91a8753c 100644 --- a/example/src/main/java/com/stripe/example/service/ExampleEphemeralKeyProvider.kt +++ b/example/src/main/java/com/stripe/example/service/ExampleEphemeralKeyProvider.kt @@ -1,10 +1,12 @@ package com.stripe.example.service +import android.content.Context import android.util.Log import androidx.annotation.Size import com.stripe.android.EphemeralKeyProvider import com.stripe.android.EphemeralKeyUpdateListener -import com.stripe.example.module.RetrofitFactory +import com.stripe.example.Settings +import com.stripe.example.module.BackendApiFactory import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers @@ -14,11 +16,14 @@ import java.io.IOException * An implementation of [EphemeralKeyProvider] that can be used to generate * ephemeral keys on the backend. */ -class ExampleEphemeralKeyProvider : EphemeralKeyProvider { +internal class ExampleEphemeralKeyProvider constructor( + backendUrl: String +) : EphemeralKeyProvider { + + constructor(context: Context) : this(Settings(context).backendUrl) private val compositeDisposable: CompositeDisposable = CompositeDisposable() - private val backendApi: BackendApi = - RetrofitFactory.instance.create(BackendApi::class.java) + private val backendApi: BackendApi = BackendApiFactory(backendUrl).create() override fun createEphemeralKey( @Size(min = 4) apiVersion: String,