-
Notifications
You must be signed in to change notification settings - Fork 629
/
TransformGoogleToStripeAddress.kt
261 lines (241 loc) · 9.12 KB
/
TransformGoogleToStripeAddress.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.stripe.android.ui.core.elements.autocomplete.model
import android.content.Context
import android.os.Build
import androidx.annotation.RestrictTo
import java.util.Locale
// Largely duplicated from
// https://git.corp.stripe.com/stripe-internal/stripe-js-v3/blob/1c111621/src/elements/inner/autocomplete_suggestions/utils/transformGoogleToStripeAddress.ts#L248-L325
internal data class AddressLine1(
var streetNumber: String? = null,
var route: String? = null,
var subLocalityLevel2: String? = null,
var subLocalityLevel3: String? = null,
var subLocalityLevel4: String? = null
)
internal data class Address(
var locality: String? = null,
var country: String? = null,
var addressLine1: String? = null,
var addressLine2: String? = null,
var administrativeArea: String? = null,
var dependentLocality: String? = null,
var postalCode: String? = null
)
// These countries will have address line 1 that takes the format
// of "King Street 123" instead of "123 King Street"
// Reference for country formats:
// https://docs.google.com/spreadsheets/d/1tIZO0-Iqvs_8CA9UL3S9qYvoTzjPdrHZr-hdetz6Uuo/edit#gid=696373988
internal val STREET_NAME_FIRST_COUNTRIES = listOf(
"BE",
"BR",
"CH",
"ES",
"ID",
"IT",
"MX",
"NL",
"NO",
"PL",
"RU",
"SE"
)
internal fun Place.filter(type: Place.Type): AddressComponent? {
return addressComponents?.find { it.types.contains(type.value) }
}
internal fun composeAddressLine1(
context: Context,
addressLine1: AddressLine1,
address: Address
): String {
val streetNumber = addressLine1.streetNumber
val streetName = addressLine1.route
val locality = address.locality
val countryCode = address.country
return if (countryCode == "JP") {
val premise = address.addressLine2
composeJapaneseAddressLine1(context, addressLine1, locality, premise)
} else if (streetNumber != null || streetName != null) {
// Depending on the country we'll want either
// "123 King Street" or "King Street 123"
return if (STREET_NAME_FIRST_COUNTRIES.contains(countryCode)) {
"$streetName $streetNumber".trim()
} else {
"$streetNumber $streetName".trim()
}
} else {
""
}
}
internal fun composeJapaneseAddressLine1(
context: Context,
addressLine1: AddressLine1,
localityComponent: String?,
premiseComponent: String?
): String {
val districtBlockNumberAllExist =
addressLine1.subLocalityLevel2 != null &&
addressLine1.subLocalityLevel3 != null &&
addressLine1.subLocalityLevel4 != null
val district = addressLine1.subLocalityLevel3
val block = addressLine1.subLocalityLevel4
val buildingNumber = premiseComponent ?: ""
val locality = localityComponent ?: ""
val municipality = addressLine1.subLocalityLevel2
val composite: String
val locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.resources.configuration.locales.get(0)
} else {
context.resources.configuration.locale
}
if (locale == Locale.JAPANESE) {
// In Japanese, address line 1 components should be in this order:
// `{locality}{municipality}{district}{block}-{buildingNumber}`
val districtBlockNumber = if (districtBlockNumberAllExist) {
"$district$block-$buildingNumber"
} else {
""
}
composite = "$locality$municipality$districtBlockNumber"
} else {
// In Latin script, address line 1 components should be in this order:
// `{district}-{block}-{buildingNumber} {municipality} {locality}`
val districtBlockNumber = if (districtBlockNumberAllExist) {
"$district-$block-$buildingNumber"
} else {
""
}
composite = "$districtBlockNumber $municipality $locality"
}
return composite
}
internal fun Address.combineDependentLocalityWithLine2(): Address {
val newAddress = copy()
if (dependentLocality != null) {
newAddress.addressLine2 = if (addressLine2 != null) {
"$addressLine2, $dependentLocality"
} else {
dependentLocality
}
}
return newAddress
}
internal fun Address.modifyStripeAddressByCountry(place: Place): Address {
val administrativeAreaLevel2 = place.filter(Place.Type.ADMINISTRATIVE_AREA_LEVEL_2)?.shortName
val administrativeAreaLevel1 = place.filter(Place.Type.ADMINISTRATIVE_AREA_LEVEL_1)?.longName
var newAddress = copy()
when (country) {
"IE" -> {
if (administrativeAreaLevel1 != null) {
newAddress.administrativeArea = administrativeAreaLevel1
newAddress = newAddress.combineDependentLocalityWithLine2()
}
}
// For Japanese addresses, the "premise" is the building number and not
// address line 2, so clear addressLine2 if it exists
"JP" -> {
newAddress.addressLine2 = null
}
// Turkish districts correspond to the "locality" field in
// the address form, and districts are in "administrative_area_level_2"
// Brazilian addresses have the locality in "administrative_area_level_2"
"TR", "BR" -> {
if (locality == null && administrativeAreaLevel2 != null) {
newAddress.locality = administrativeAreaLevel2
}
newAddress = newAddress.combineDependentLocalityWithLine2()
}
// Most countries contain the province in "administrative_area_level_1"
// However, Spanish and Italian provinces are in
// "administrative_area_level_2" and "administrative_area_level_1"
// contains the region (eg. the autonomous community, like Basque), which
// isn't used in mailing addresses.
"ES", "IT" -> {
if (administrativeAreaLevel2 != null) {
newAddress.administrativeArea = administrativeAreaLevel2
}
}
"MX", "MY", "PH", "ZA" -> {
newAddress = newAddress.combineDependentLocalityWithLine2()
}
}
return newAddress
}
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
fun Place.transformGoogleToStripeAddress(
context: Context
): com.stripe.android.model.Address {
var address = Address()
val addressLine1 = AddressLine1()
addressComponents?.forEach { field ->
when (field.types[0]) {
Place.Type.STREET_NUMBER.value -> {
addressLine1.streetNumber = field.longName
}
Place.Type.ROUTE.value -> {
addressLine1.route = field.longName
}
Place.Type.PREMISE.value -> {
address.addressLine2 = field.longName
}
Place.Type.LOCALITY.value,
Place.Type.SUBLOCALITY.value,
Place.Type.POSTAL_TOWN.value -> {
address.locality = field.longName
}
Place.Type.ADMINISTRATIVE_AREA_LEVEL_1.value -> {
address.administrativeArea = field.shortName
}
Place.Type.ADMINISTRATIVE_AREA_LEVEL_3.value -> {
if (address.locality == null) {
address.locality = field.longName
}
}
Place.Type.ADMINISTRATIVE_AREA_LEVEL_2.value -> {
if (address.administrativeArea == null && address.dependentLocality == null) {
address.dependentLocality = field.longName
} else {
address.administrativeArea = field.shortName
}
}
Place.Type.NEIGHBORHOOD.value -> {
if (address.locality == null) {
address.locality = field.longName
} else {
address.dependentLocality = field.longName
}
}
Place.Type.POSTAL_CODE.value -> {
address.postalCode = field.longName
}
Place.Type.COUNTRY.value -> {
address.country = field.shortName
}
Place.Type.SUBLOCALITY_LEVEL_1.value -> {
if (address.locality == null) {
address.dependentLocality = field.longName
} else {
address.locality = field.longName
}
}
Place.Type.SUBLOCALITY_LEVEL_2.value -> {
addressLine1.subLocalityLevel2 = field.longName
}
Place.Type.SUBLOCALITY_LEVEL_3.value -> {
addressLine1.subLocalityLevel3 = field.longName
}
Place.Type.SUBLOCALITY_LEVEL_4.value -> {
addressLine1.subLocalityLevel4 = field.longName
}
}
}
address.addressLine1 = composeAddressLine1(context, addressLine1, address)
address = address.modifyStripeAddressByCountry(this)
return com.stripe.android.model.Address.Builder()
.setLine1(address.addressLine1)
.setLine2(address.addressLine2)
.setCity(address.locality)
.setState(address.administrativeArea)
.setCountry(address.country)
.setPostalCode(address.postalCode)
.build()
}