Skip to content

Commit

Permalink
Define a PaymentSheet-specific backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
brnunes-stripe committed Apr 29, 2021
1 parent 0c39bfa commit 7b3608e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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 = ""

// 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 @@ -4,10 +4,12 @@ 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.model.PaymentOption
import com.stripe.example.R
import com.stripe.example.Settings
import com.stripe.example.databinding.ActivityPaymentSheetCustomBinding

internal class LaunchPaymentSheetCustomActivity : BasePaymentSheetActivity() {
Expand All @@ -21,6 +23,9 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ internal class BackendApiFactory internal constructor(private val backendUrl: St

@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)

Expand All @@ -60,7 +69,7 @@ internal class BackendApiFactory internal constructor(private val backendUrl: St

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

0 comments on commit 7b3608e

Please sign in to comment.