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

Move PaymentSheet example into its own app. #3712

Merged
merged 2 commits into from
May 11, 2021
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
3 changes: 0 additions & 3 deletions example/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@

<activity android:name=".activity.SimpleConfirmationActivity" />
<activity android:name=".activity.ConnectExampleActivity" />

<activity android:name=".activity.LaunchPaymentSheetCompleteActivity" />
<activity android:name=".activity.LaunchPaymentSheetCustomActivity" />
</application>

</manifest>
3 changes: 0 additions & 3 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ plugins {
id 'kotlin-android'
id 'checkstyle'
id 'org.jetbrains.kotlin.plugin.parcelize'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.0'
}

assemble.dependsOn('lint')
Expand Down Expand Up @@ -70,15 +69,13 @@ 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.2.0"

ktlint "com.pinterest:ktlint:$ktlintVersion"

Expand Down
9 changes: 1 addition & 8 deletions 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 }
}

internal companion object {
private companion object {
/**
* Note: only necessary if not configured via `gradle.properties`.
*
Expand All @@ -62,13 +62,6 @@ 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 = ""

// Publishable key for example payment sheet backend
internal const val PAYMENT_SHEET_PUBLISHABLE_KEY = ""

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
Expand Up @@ -36,14 +36,6 @@ class LauncherActivity : AppCompatActivity() {
private val activity: Activity
) : RecyclerView.Adapter<ExamplesAdapter.ExamplesViewHolder>() {
private val items = listOf(
Item(
"Launch PaymentSheet Complete",
LaunchPaymentSheetCompleteActivity::class.java
),
Item(
"Launch PaymentSheet Custom",
LaunchPaymentSheetCustomActivity::class.java
),
Item(
activity.getString(R.string.payment_auth_example),
PaymentAuthActivity::class.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ package com.stripe.example.module

import android.content.Context
import com.google.gson.GsonBuilder
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.stripe.example.Settings
import com.stripe.example.service.BackendApi
import com.stripe.example.service.CheckoutBackendApi
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
Expand Down Expand Up @@ -47,34 +42,6 @@ internal class BackendApiFactory internal constructor(private val backendUrl: St
.create(BackendApi::class.java)
}

@OptIn(ExperimentalSerializationApi::class)
fun createCheckout(): CheckoutBackendApi {
if (Settings.PAYMENT_SHEET_BASE_URL.isNullOrEmpty() ||
Settings.PAYMENT_SHEET_PUBLISHABLE_KEY.isNullOrEmpty()
) {
error(
"Settings.PAYMENT_SHEET_BASE_URL and Settings.PAYMENT_SHEET_PUBLISHABLE_KEY " +
"must be set for PaymentSheet example"
)
}

val logging = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)

val httpClient = OkHttpClient.Builder()
.connectTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
.readTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
.addInterceptor(logging)
.build()

return Retrofit.Builder()
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.baseUrl(Settings.PAYMENT_SHEET_BASE_URL)
.client(httpClient)
.build()
.create(CheckoutBackendApi::class.java)
}

private companion object {
private const val TIMEOUT_SECONDS = 15L
}
Expand Down
46 changes: 44 additions & 2 deletions paymentsheet-example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.0'
}

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

def getPublishableKey() {
return getValue('STRIPE_PAYMENTSHEET_EXAMPLE_PUBLISHABLE_KEY')
}
brnunes-stripe marked this conversation as resolved.
Show resolved Hide resolved

private def getValue(key) {
// first try to get the value from Gradle properties
// see https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties
final String propValue
if (hasProperty(key)) {
propValue = property(key)
} else {
propValue = null
}

if (propValue?.trim()) {
return propValue
} else {
// Otherwise, get the value from environment variables
// see https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_environment_variables
final String envValue = System.getenv(key)
return envValue?.trim() ? envValue : ""
}
}

dependencies {
implementation project(':stripe')

implementation "androidx.lifecycle:lifecycle-livedata-ktx:$androidLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$androidLifecycleVersion"
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation "androidx.activity:activity-ktx:1.2.3"
implementation 'com.google.android.material:material:1.3.0'

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0"

implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand All @@ -41,6 +78,11 @@ android {
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

manifestPlaceholders = [
BACKEND_URL: getBackendUrl(),
PUBLISHABLE_KEY: getPublishableKey()
]
}

buildTypes {
Expand Down
12 changes: 12 additions & 0 deletions paymentsheet-example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
package="com.stripe.android.paymentsheet.example">

<application
android:name=".PaymentSheetExampleApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">

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

<activity
android:name=".MainActivity"
android:label="@string/app_name"
Expand All @@ -20,6 +29,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".activity.LaunchPaymentSheetCompleteActivity" />
<activity android:name=".activity.LaunchPaymentSheetCustomActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package com.stripe.android.paymentsheet.example

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.stripe.android.paymentsheet.example.activity.LaunchPaymentSheetCompleteActivity
import com.stripe.android.paymentsheet.example.activity.LaunchPaymentSheetCustomActivity
import com.stripe.android.paymentsheet.example.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private val viewBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setContentView(viewBinding.root)
setSupportActionBar(findViewById(R.id.toolbar))

viewBinding.launchCompleteButton.setOnClickListener {
startActivity(Intent(this, LaunchPaymentSheetCompleteActivity::class.java))
}

viewBinding.launchCustomButton.setOnClickListener {
startActivity(Intent(this, LaunchPaymentSheetCustomActivity::class.java))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.stripe.android.paymentsheet.example

import android.app.Application
import com.stripe.android.PaymentConfiguration

class PaymentSheetExampleApplication : Application() {

override fun onCreate() {
super.onCreate()
PaymentConfiguration.init(this, Settings(this).publishableKey)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.stripe.android.paymentsheet.example

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

/**
* See [Configure the app](https://github.com/stripe/stripe-android/tree/master/example#configure-the-app)
* for instructions on how to configure the example app before running it.
*/
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)

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

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

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

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

/**
* Note: only necessary if not configured via `gradle.properties`.
*
* Set to publishable key from https://dashboard.stripe.com/test/apikeys
*/
private const val PUBLISHABLE_KEY = "pk_test_your_key_goes_here"

private const val METADATA_KEY_BACKEND_URL_KEY =
"com.stripe.android.paymentsheet.example.metadata.backend_url"
private const val METADATA_KEY_PUBLISHABLE_KEY =
"com.stripe.android.paymentsheet.example.metadata.publishable_key"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.stripe.example.activity
package com.stripe.android.paymentsheet.example.activity

import android.content.SharedPreferences
import android.view.Menu
Expand All @@ -9,8 +9,9 @@ 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
import com.stripe.example.paymentsheet.PaymentSheetViewModel
import com.stripe.android.paymentsheet.example.R
import com.stripe.android.paymentsheet.example.config.ConfigBottomSheet
import com.stripe.android.paymentsheet.example.viewmodel.PaymentSheetViewModel

internal abstract class BasePaymentSheetActivity : AppCompatActivity() {
protected val viewModel: PaymentSheetViewModel by viewModels {
Expand Down Expand Up @@ -70,9 +71,9 @@ internal abstract class BasePaymentSheetActivity : AppCompatActivity() {

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.config) {
PaymentSheetConfigBottomSheet().show(
ConfigBottomSheet().show(
supportFragmentManager,
PaymentSheetConfigBottomSheet.TAG
ConfigBottomSheet.TAG
)
return true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.stripe.example.activity
package com.stripe.android.paymentsheet.example.activity

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

internal class LaunchPaymentSheetCompleteActivity : BasePaymentSheetActivity() {
private val viewBinding by lazy {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.stripe.example.activity
package com.stripe.android.paymentsheet.example.activity

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.core.view.isInvisible
import com.stripe.android.PaymentConfiguration
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.PaymentSheetResult
import com.stripe.android.paymentsheet.example.R
import com.stripe.android.paymentsheet.example.databinding.ActivityPaymentSheetCustomBinding
import com.stripe.android.paymentsheet.model.PaymentOption
import com.stripe.example.R
import com.stripe.example.Settings
import com.stripe.example.databinding.ActivityPaymentSheetCustomBinding

internal class LaunchPaymentSheetCustomActivity : BasePaymentSheetActivity() {
private val viewBinding by lazy {
Expand All @@ -23,9 +21,6 @@ internal class LaunchPaymentSheetCustomActivity : BasePaymentSheetActivity() {
super.onCreate(savedInstanceState)
setContentView(viewBinding.root)

// TODO(brnunes-stripe): Remove this once FlowController initialization is refactored.
PaymentConfiguration.init(this, Settings.PAYMENT_SHEET_PUBLISHABLE_KEY)

flowController = PaymentSheet.FlowController.create(
this,
::onPaymentOption,
Expand Down
Loading