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

Update configuration of example app #1871

Merged
merged 2 commits into from
Nov 25, 2019
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
10 changes: 10 additions & 0 deletions example/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
android:supportsRtl="true"
tools:ignore="AllowBackup">

<meta-data
android:name="com.stripe.example.metadata.backend_url"
android:value="${BACKEND_URL}" />
<meta-data
android:name="com.stripe.example.metadata.publishable_key"
android:value="${PUBLISHABLE_KEY}" />
<meta-data
android:name="com.stripe.example.metadata.stripe_account_id"
android:value="${STRIPE_ACCOUNT_ID}" />

<!-- Enables the Google Payment API -->
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
Expand Down
30 changes: 30 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ configurations {
ktlint
}

// Read values from gradle.properties or system environment variable
def getBackendUrl() {
return readProperty('STRIPE_EXAMPLE_BACKEND_URL')
}

def getPublishableKey() {
return readProperty('STRIPE_EXAMPLE_PUBLISHABLE_KEY')
}

def getAccountId() {
return readProperty('STRIPE_ACCOUNT_ID')
}

private def readProperty(name) {
final String propValue
if (hasProperty(name)) {
propValue = property(name)
} else {
propValue = System.getenv(name)
}

return propValue?.trim() ? propValue : ""
}

dependencies {
implementation project(':stripe')
implementation 'androidx.multidex:multidex:2.0.1'
Expand Down Expand Up @@ -62,6 +86,12 @@ android {
minSdkVersion 19
targetSdkVersion rootProject.ext.compileSdkVersion
multiDexEnabled true

manifestPlaceholders = [
BACKEND_URL: getBackendUrl(),
PUBLISHABLE_KEY: getPublishableKey(),
STRIPE_ACCOUNT_ID: getAccountId()
]
}
packagingOptions {
exclude 'LICENSE.txt'
Expand Down
78 changes: 59 additions & 19 deletions example/src/main/java/com/stripe/example/Settings.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
package com.stripe.example

import android.content.Context
import android.content.pm.PackageManager

/**
* See [Configuring the example app](https://github.com/stripe/stripe-android#building-the-example-project)
* for instructions on how to configure the app before running it.
*/
internal object Settings {
/**
* Set to the base URL of your test backend. If you are using
* [example-ios-backend](https://github.com/stripe/example-ios-backend),
* the URL will be something like `https://hidden-beach-12345.herokuapp.com/`.
*/
const val BASE_URL = "put your base url here"

/**
* Set to publishable key from https://dashboard.stripe.com/test/apikeys
*/
const val PUBLISHABLE_KEY = "pk_test_your_key_goes_here"

/**
* Optionally, set to a Connect Account id to use for API requests to test Connect
*
* See https://dashboard.stripe.com/test/connect/accounts/overview
*/
val STRIPE_ACCOUNT_ID: String? = null
class Settings(context: Context) {
private val appContext = context.applicationContext
private val backendMetadata = getMetadata(METADATA_KEY_BACKEND_URL_KEY)
private val publishableKeyMetadata = getMetadata(METADATA_KEY_PUBLISHABLE_KEY)
private val stripeAccountIdMetadata = getMetadata(METADATA_KEY_STRIPE_ACCOUNT_ID)

val backendUrl: String
get() {
return backendMetadata ?: BASE_URL
}

val publishableKey: String
get() {
return publishableKeyMetadata ?: PUBLISHABLE_KEY
}

val stripeAccountId: String?
get() {
return stripeAccountIdMetadata ?: STRIPE_ACCOUNT_ID
}

private fun getMetadata(key: String): String? {
return appContext.packageManager
.getApplicationInfo(appContext.packageName, PackageManager.GET_META_DATA)
.metaData
.getString(key)
.takeIf { it?.isNotBlank() == true }
}

private companion object {
/**
* Set to the base URL of your test backend. If you are using
* [example-ios-backend](https://github.com/stripe/example-ios-backend),
* the URL will be something like `https://hidden-beach-12345.herokuapp.com/`.
*/
private const val BASE_URL = "put your base url here"

/**
* Set to publishable key from https://dashboard.stripe.com/test/apikeys
*/
private const val PUBLISHABLE_KEY = "pk_test_your_key_goes_here"

/**
* Optionally, set to a Connect Account id to use for API requests to test Connect
*
* See https://dashboard.stripe.com/test/connect/accounts/overview
*/
private val STRIPE_ACCOUNT_ID: String? = null

private const val METADATA_KEY_BACKEND_URL_KEY =
"com.stripe.example.metadata.backend_url"
private const val METADATA_KEY_PUBLISHABLE_KEY =
"com.stripe.example.metadata.publishable_key"
private const val METADATA_KEY_STRIPE_ACCOUNT_ID =
"com.stripe.example.metadata.stripe_account_id"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CustomerSessionActivity : AppCompatActivity() {
errorDialogHandler = ErrorDialogHandler(this)
CustomerSession.initCustomerSession(
this,
ExampleEphemeralKeyProvider(),
ExampleEphemeralKeyProvider(this),
false
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_fpx_payment);
setTitle(R.string.fpx_payment_example);

PaymentConfiguration.init(this, Settings.PUBLISHABLE_KEY);
PaymentConfiguration.init(this, new Settings(this).getPublishableKey());

findViewById(R.id.btn_select_payment_method)
.setOnClickListener(view -> launchAddPaymentMethod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -54,7 +55,7 @@ class FragmentExamplesActivity : AppCompatActivity() {
.commit()
}

class LauncherFragment : androidx.fragment.app.Fragment() {
class LauncherFragment : Fragment() {

private val compositeDisposable = CompositeDisposable()

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class PaymentSessionActivity : AppCompatActivity() {
private fun createCustomerSession(): CustomerSession {
CustomerSession.initCustomerSession(
this,
ExampleEphemeralKeyProvider(),
ExampleEphemeralKeyProvider(this),
false
)
return CustomerSession.getInstance()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down