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

Address Element API review changes #5403

Merged
merged 1 commit into from Aug 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions paymentsheet/api/paymentsheet.api
Expand Up @@ -791,19 +791,19 @@ public final class com/stripe/android/paymentsheet/addresselement/AddressLaunche
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Canceled$Creator : android/os/Parcelable$Creator {
public final class com/stripe/android/paymentsheet/addresselement/AddressLauncher$DefaultAddressDetails$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Canceled;
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/paymentsheet/addresselement/AddressLauncher$DefaultAddressDetails;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Canceled;
public final fun newArray (I)[Lcom/stripe/android/paymentsheet/addresselement/AddressLauncher$DefaultAddressDetails;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Failed$Creator : android/os/Parcelable$Creator {
public final class com/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Canceled$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Failed;
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Canceled;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Failed;
public final fun newArray (I)[Lcom/stripe/android/paymentsheet/addresselement/AddressLauncherResult$Canceled;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

Expand Down
@@ -1,22 +1,41 @@
package com.stripe.android.paymentsheet.addresselement

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
import com.stripe.android.paymentsheet.PaymentSheet
import kotlinx.parcelize.Parcelize

@Parcelize
internal data class AddressDetails(
/**
* The customer's full name
*/
val name: String? = null,
val company: String? = null,
val city: String? = null,
val country: String? = null,
val line1: String? = null,
val line2: String? = null,
val postalCode: String? = null,
val state: String? = null,

/**
* The customer's address
*/
val address: PaymentSheet.Address? = null,

/**
* The customer's phone number, without formatting e.g. "5551234567"
*/
val phoneNumber: String? = null,
val checkboxChecked: Boolean? = null

/**
* Whether or not your custom checkbox is selected.
* Note: The checkbox is displayed below the other fields when AdditionalFieldsConfiguration.checkboxLabel is set.
*/
val isCheckboxSelected: Boolean? = null
) : Parcelable {
companion object {
const val KEY = "ShippingAddress"
const val KEY = "AddressDetails"
}
}

internal fun AddressLauncher.DefaultAddressDetails.toAddressDetails(): AddressDetails =
AddressDetails(
name = this.name,
address = this.address,
phoneNumber = this.phoneNumber,
isCheckboxSelected = this.isCheckboxSelected
)
Expand Up @@ -78,7 +78,7 @@ internal class AddressLauncher internal constructor(
/**
* The values to pre-populate shipping address fields with.
*/
val defaultValues: AddressDetails? = null,
val defaultValues: DefaultAddressDetails? = null,

/**
* A list of two-letter country codes representing countries the customers can select.
Expand Down Expand Up @@ -108,10 +108,10 @@ internal class AddressLauncher internal constructor(
* Google Places api key used to provide autocomplete suggestions
* When null, autocomplete is disabled.
*/
val googlePlacesApiKey: String? = "",
val googlePlacesApiKey: String? = null,

/**
* The label of a checkbox displayed below other fields. If nil, the checkbox is not displayed.
* The label of a checkbox displayed below other fields. If null, the checkbox is not displayed.
* Defaults to null
*/
val checkboxLabel: String? = null
Expand All @@ -121,7 +121,7 @@ internal class AddressLauncher internal constructor(
*/
class Builder {
var appearance: PaymentSheet.Appearance = PaymentSheet.Appearance()
var defaultValues: AddressDetails? = null
var defaultValues: DefaultAddressDetails? = null
var allowedCountries: Set<String> = emptySet()
var buttonTitle: String? = null
var phone: AdditionalFieldsConfiguration = AdditionalFieldsConfiguration.OPTIONAL
Expand All @@ -132,7 +132,7 @@ internal class AddressLauncher internal constructor(
fun appearance(appearance: PaymentSheet.Appearance) =
apply { this.appearance = appearance }

fun defaultValues(defaultValues: AddressDetails?) =
fun defaultValues(defaultValues: DefaultAddressDetails?) =
apply { this.defaultValues = defaultValues }

fun allowedCountries(allowedCountries: Set<String>) =
Expand Down Expand Up @@ -183,4 +183,28 @@ internal class AddressLauncher internal constructor(
*/
REQUIRED
}

@Parcelize
data class DefaultAddressDetails(
/**
* The customer's full name
*/
val name: String? = null,

/**
* The customer's address
*/
val address: PaymentSheet.Address? = null,

/**
* The customer's phone number, without formatting e.g. "5551234567"
*/
val phoneNumber: String? = null,

/**
* Whether or not your custom checkbox is intially selected.
* Note: The checkbox is displayed below the other fields when AdditionalFieldsConfiguration.checkboxLabel is set.
*/
val isCheckboxSelected: Boolean? = null
) : Parcelable
}
Expand Up @@ -12,15 +12,9 @@ internal sealed class AddressLauncherResult(
) : Parcelable {
@Parcelize
data class Succeeded(
// TODO add the real return type here once we iron it out.
val address: AddressDetails
) : AddressLauncherResult(Activity.RESULT_OK)

@Parcelize
data class Failed(
val error: Throwable
) : AddressLauncherResult(Activity.RESULT_CANCELED)

@Parcelize
object Canceled : AddressLauncherResult(Activity.RESULT_CANCELED)

Expand Down
Expand Up @@ -38,11 +38,12 @@ fun CharSequence.levenshtein(other: CharSequence): Int {

internal fun AddressDetails.editDistance(otherAddress: AddressDetails?): Int {
var editDistance = 0
editDistance += (city ?: "").levenshtein(otherAddress?.city ?: "")
editDistance += (country ?: "").levenshtein(otherAddress?.country ?: "")
editDistance += (line1 ?: "").levenshtein(otherAddress?.line1 ?: "")
editDistance += (line2 ?: "").levenshtein(otherAddress?.line2 ?: "")
editDistance += (postalCode ?: "").levenshtein(otherAddress?.postalCode ?: "")
editDistance += (state ?: "").levenshtein(otherAddress?.state ?: "")
val comparedAddress = otherAddress?.address
editDistance += (address?.city ?: "").levenshtein(comparedAddress?.city ?: "")
editDistance += (address?.country ?: "").levenshtein(comparedAddress?.country ?: "")
editDistance += (address?.line1 ?: "").levenshtein(comparedAddress?.line1 ?: "")
editDistance += (address?.line2 ?: "").levenshtein(comparedAddress?.line2 ?: "")
editDistance += (address?.postalCode ?: "").levenshtein(comparedAddress?.postalCode ?: "")
editDistance += (address?.state ?: "").levenshtein(comparedAddress?.state ?: "")
return editDistance
}
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.R
import com.stripe.android.paymentsheet.addresselement.analytics.AddressLauncherEventReporter
import com.stripe.android.ui.core.injection.NonFallbackInjectable
Expand Down Expand Up @@ -121,12 +122,14 @@ internal class AutocompleteViewModel @Inject constructor(
val address = it.place.transformGoogleToStripeAddress(getApplication())
addressResult.value = Result.success(
AddressDetails(
city = address.city,
country = address.country,
line1 = address.line1,
line2 = address.line2,
postalCode = address.postalCode,
state = address.state
address = PaymentSheet.Address(
city = address.city,
country = address.country,
line1 = address.line1,
line2 = address.line2,
postalCode = address.postalCode,
state = address.state
)
)
)
setResultAndGoBack()
Expand All @@ -143,7 +146,9 @@ internal class AutocompleteViewModel @Inject constructor(
fun onBackPressed() {
val result = if (queryFlow.value.isNotBlank()) {
AddressDetails(
line1 = queryFlow.value
address = PaymentSheet.Address(
line1 = queryFlow.value
)
)
} else {
null
Expand All @@ -154,7 +159,9 @@ internal class AutocompleteViewModel @Inject constructor(
fun onEnterAddressManually() {
setResultAndGoBack(
AddressDetails(
line1 = queryFlow.value
address = PaymentSheet.Address(
line1 = queryFlow.value
)
)
)
}
Expand Down