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

Updated to Stripe 20.2 #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ javadoc/
# Gradle files
.gradle/
build/
/.build/

# Local configuration file (sdk path, etc)
local.properties
Expand All @@ -40,4 +41,4 @@ tmp/
app/target
app/gen
app/bin
app/*.log
*.log
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ android {
}

dependencies {
implementation 'com.stripe:stripe-android:17.2.0'
implementation 'com.stripe:stripe-android:20.2.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.activity:activity-ktx:1.3.1'
implementation 'androidx.fragment:fragment-ktx:1.3.6'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.google.android.gms:play-services-wallet:18.1.3'

Expand Down
11 changes: 8 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@
android:name="com.stripe.example.metadata.stripe_account_id"
android:value="${STRIPE_ACCOUNT_ID}" />

<activity android:name=".StoreActivity"
android:theme="@style/AppTheme.NoActionBar">
<activity
android:name=".StoreActivity"
android:theme="@style/AppTheme.NoActionBar"
android:exported="true"
tools:node="merge">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity
android:name=".PaymentActivity">
android:name=".PaymentActivity"
android:exported="true"
tools:node="merge">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ package com.stripe.android.samplestore

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.activity.result.contract.ActivityResultContract
import kotlinx.parcelize.Parcelize


internal class CheckoutContract : ActivityResultContract<CheckoutContract.Args, CheckoutContract.Result>() {
override fun createIntent(context: Context, args: Args?): Intent {
return Intent(context, PaymentActivity::class.java)
.putExtra(EXTRA_ARGS, args)
override fun createIntent(context: Context, input: Args): Intent {
// WARNING: only `Bundle` seems to work with `@Parcelize data class`.
val bundle = Bundle()
bundle.putParcelable("data", input)
val intent = Intent(context, PaymentActivity::class.java)
.putExtra(EXTRA_ARGS, bundle)
return intent
}

override fun parseResult(resultCode: Int, intent: Intent?): Result? {
return intent?.getParcelableExtra(EXTRA_RESULT)
override fun parseResult(resultCode: Int, intent: Intent?): Result {
val result = intent?.getParcelableExtra<Result>(EXTRA_RESULT)
if (result == null) {
Common.logError(TAG, "parseResult: intent is missing.")
}
return result ?: Result.SetupIntent
}

@Parcelize
Expand All @@ -32,6 +42,7 @@ internal class CheckoutContract : ActivityResultContract<CheckoutContract.Args,
}

companion object {
const val TAG = "CheckoutContract"
const val EXTRA_ARGS = "extra_args"
const val EXTRA_RESULT = "result_extra"
}
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/stripe/android/samplestore/Common.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.stripe.android.samplestore;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

public class Common {
public static Handler handler = new Handler(Looper.getMainLooper());

public static String STRIPE_KEY = "pk_test_here";

public static void log(String tag, String message) {
Log.w(tag, message);
}

public static void logError(String tag, String message) {
Log.e(tag, message);
}

public static String webApiUrl() {
// 10.0.2.2 is the Android emulator's alias to localhost
return "http://10.0.2.2/stripe-store/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class PaymentActivity : AppCompatActivity() {
)
}
private val args: CheckoutContract.Args by lazy {
requireNotNull(intent?.extras?.getParcelable(CheckoutContract.EXTRA_ARGS))
val bundle: Bundle? = intent?.extras?.getBundle(CheckoutContract.EXTRA_ARGS)
requireNotNull(bundle?.getParcelable("data"))
}

private val storeCart: StoreCart by lazy { args.cart }
Expand Down Expand Up @@ -640,7 +641,12 @@ class PaymentActivity : AppCompatActivity() {
private fun getDisplayName(name: String?): String {
return (name.orEmpty())
.split("_")
.joinToString(separator = " ") { it.capitalize(Locale.ROOT) }
.joinToString(separator = " ") {
it.replaceFirstChar {
if (it.isLowerCase()) it.titlecase(Locale.ROOT)
else it.toString()
}
}
}

private fun onPaymentSessionDataChanged(data: PaymentSessionData) {
Expand Down Expand Up @@ -742,7 +748,7 @@ class PaymentActivity : AppCompatActivity() {
private class ShippingMethodsFactory : PaymentSessionConfig.ShippingMethodsFactory {
override fun create(shippingInformation: ShippingInformation): List<ShippingMethod> {
val isCourierSupported = "94110" == shippingInformation.address?.postalCode
val currency = Currency.getInstance(Settings.CURRENCY.toUpperCase(Locale.ROOT))
val currency = Currency.getInstance(Settings.CURRENCY.uppercase(Locale.ROOT))
val courierMethod = if (isCourierSupported) {
ShippingMethod(
label = "1 Hour Courier",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ internal class PaymentViewModel(
class Factory(
private val application: Application
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return PaymentViewModel(
BackendApiFactory(application).create(),
StripeFactory(application).create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ internal data class StoreCart(
) : Parcelable {

internal val totalPrice: Long
get() = lineItems.sumBy { it.totalPrice.toInt() }.toLong()
get() = lineItems.sumOf { it.totalPrice.toInt() }.toLong()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.stripe.android.CustomerSession
import com.stripe.android.StripeError
import com.stripe.android.core.StripeError
import com.stripe.android.model.Customer

internal class StoreViewModel : ViewModel() {
Expand Down
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlinVersion = '1.5.21'
ext.kotlinVersion = '1.6.20'
repositories {
gradlePluginPortal()
google()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.android.tools.build:gradle:7.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

allprojects {
buildDir = "${rootProject.rootDir}/.build/${project.name}"

repositories {
google()
gradlePluginPortal()
Expand All @@ -34,7 +36,7 @@ allprojects {
}

ext {
buildToolsVersion = "30.0.3"
compileSdkVersion = 30
buildToolsVersion = "31.0.0"
compileSdkVersion = 31
coroutineVersion = '1.5.1'
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Sep 05 11:54:36 EDT 2019
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-all.zip