diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b4a794..5550067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ All notable changes to this project are documented in this file. ### Added +- **ISO 3166-2 region codes in Google Pay addresses.** `CNPostalAddress` adds + `isoAdministrativeArea`, the region as an ISO 3166-2 code such as `'US-CA'`, for tax and shipping + systems that need a code rather than the free-text `state`. Android now requests full billing + addresses and shipping addresses in Google Pay's `FULL-ISO3166` format, which is `FULL` plus that + code. A `MIN` billing address, requested for a phone number alone, is unchanged. Apple Pay has no + such code, so it is always omitted on iOS + ([#90](https://github.com/gmi-software/react-native-pay/issues/90)). - **`pix` and `ewallet` Google Pay buttons.** `GooglePayButtonType` adds `'pix'` and `'ewallet'`, Google's `PIX` and `EWALLET` button types. `'pix'` shows "Direct Pix payment"; Google Play services draws `'ewallet'` like `'buy'` so far. They change only the button: the sheet still diff --git a/README.md b/README.md index 69943d2..1072074 100644 --- a/README.md +++ b/README.md @@ -1007,6 +1007,7 @@ interface CNPostalAddress { postalCode?: string; country?: string; isoCountryCode?: string; + isoAdministrativeArea?: string; // Android only, ISO 3166-2, e.g. "US-CA" } ``` diff --git a/docs/docs/api/types.md b/docs/docs/api/types.md index 4b0c4d2..bc67044 100644 --- a/docs/docs/api/types.md +++ b/docs/docs/api/types.md @@ -658,7 +658,7 @@ interface PaymentContact { } ``` -In `postalAddress`, `street` joins the address lines with `'\n'`, `isoCountryCode` is uppercase ISO 3166-1 alpha-2, and `country` (the country name) is iOS only. +In `postalAddress`, `street` joins the address lines with `'\n'`, `isoCountryCode` is uppercase ISO 3166-1 alpha-2, and `country` (the country name) is iOS only. `isoAdministrativeArea` is Android only: the region as an ISO 3166-2 code with the country prefix, such as `'US-CA'`, next to the free-text `state`. --- @@ -680,6 +680,7 @@ interface CNPostalAddress { postalCode?: string country?: string isoCountryCode?: string + isoAdministrativeArea?: string // Android only, ISO 3166-2, e.g. 'US-CA' } interface CNLabeledPhoneNumber { diff --git a/docs/docs/guides/contact-details.md b/docs/docs/guides/contact-details.md index 48d28d3..552c164 100644 --- a/docs/docs/guides/contact-details.md +++ b/docs/docs/guides/contact-details.md @@ -64,7 +64,7 @@ With `HybridPaymentHandler`, set the same flags on the `PaymentRequest`; the det - **name**: the full name, for example `'Jane Appleseed'`. - **givenName**, **familyName**: iOS only. Google Pay returns the name as one string. -- **postalAddress**: `street` (address lines joined with `'\n'`), `city`, `state`, `postalCode`, `country` (iOS only) and `isoCountryCode` (uppercase ISO 3166-1 alpha-2, such as `'US'`). +- **postalAddress**: `street` (address lines joined with `'\n'`), `city`, `state`, `postalCode`, `country` (iOS only), `isoCountryCode` (uppercase ISO 3166-1 alpha-2, such as `'US'`) and `isoAdministrativeArea` (Android only: the ISO 3166-2 region code, such as `'US-CA'`). Use `isoAdministrativeArea` where a tax or shipping system needs a region code; on iOS, `state` is the only region you get. `emailAddress` and `phoneNumber` are plain strings. The phone number is in the format the payer entered. Parts the payer left empty are omitted, not empty strings. diff --git a/package/android/src/main/java/com/margelo/nitro/pay/Constants.kt b/package/android/src/main/java/com/margelo/nitro/pay/Constants.kt index 8df9c96..478c097 100644 --- a/package/android/src/main/java/com/margelo/nitro/pay/Constants.kt +++ b/package/android/src/main/java/com/margelo/nitro/pay/Constants.kt @@ -18,9 +18,10 @@ object PaymentConstants { const val AUTH_PAN_ONLY = "PAN_ONLY" const val AUTH_CRYPTOGRAM_3DS = "CRYPTOGRAM_3DS" - // Billing address formats + // Address formats. MIN is for billing only. const val BILLING_ADDRESS_FORMAT_MIN = "MIN" - const val BILLING_ADDRESS_FORMAT_FULL = "FULL" + // FULL plus `iso3166AdministrativeArea`, the region as an ISO 3166-2 code + const val ADDRESS_FORMAT_FULL_ISO3166 = "FULL-ISO3166" // Tokenization const val TOKENIZATION_PAYMENT_GATEWAY = "PAYMENT_GATEWAY" diff --git a/package/android/src/main/java/com/margelo/nitro/pay/GooglePayRequestBuilder.kt b/package/android/src/main/java/com/margelo/nitro/pay/GooglePayRequestBuilder.kt index 34d1c35..6bba666 100644 --- a/package/android/src/main/java/com/margelo/nitro/pay/GooglePayRequestBuilder.kt +++ b/package/android/src/main/java/com/margelo/nitro/pay/GooglePayRequestBuilder.kt @@ -78,6 +78,7 @@ object GooglePayRequestBuilder { if (requested.shippingContact) { put("shippingAddressRequired", true) put("shippingAddressParameters", JSONObject().apply { + put("format", PaymentConstants.ADDRESS_FORMAT_FULL_ISO3166) put("phoneNumberRequired", requested.phoneNumberOnShippingAddress) }) } @@ -190,7 +191,7 @@ object GooglePayRequestBuilder { put( "format", if (requested.billingContact) - PaymentConstants.BILLING_ADDRESS_FORMAT_FULL + PaymentConstants.ADDRESS_FORMAT_FULL_ISO3166 else PaymentConstants.BILLING_ADDRESS_FORMAT_MIN ) diff --git a/package/android/src/main/java/com/margelo/nitro/pay/PaymentMapper.kt b/package/android/src/main/java/com/margelo/nitro/pay/PaymentMapper.kt index af371c7..1411b03 100644 --- a/package/android/src/main/java/com/margelo/nitro/pay/PaymentMapper.kt +++ b/package/android/src/main/java/com/margelo/nitro/pay/PaymentMapper.kt @@ -118,7 +118,9 @@ object PaymentMapper { state = address.optNonEmpty("administrativeArea"), postalCode = address.optNonEmpty("postalCode"), country = null, - isoCountryCode = address.optNonEmpty("countryCode")?.uppercase() + isoCountryCode = address.optNonEmpty("countryCode")?.uppercase(), + // Only with the FULL-ISO3166 format, which MIN billing addresses don't use + isoAdministrativeArea = address.optNonEmpty("iso3166AdministrativeArea")?.uppercase() ) ) } diff --git a/package/android/src/test/java/com/margelo/nitro/pay/GooglePayRequestBuilderTest.kt b/package/android/src/test/java/com/margelo/nitro/pay/GooglePayRequestBuilderTest.kt index c9ee5ed..1e3b197 100644 --- a/package/android/src/test/java/com/margelo/nitro/pay/GooglePayRequestBuilderTest.kt +++ b/package/android/src/test/java/com/margelo/nitro/pay/GooglePayRequestBuilderTest.kt @@ -357,12 +357,12 @@ class GooglePayRequestBuilderTest { } @Test - fun billingContact_requestsAFullBillingAddress() { + fun billingContact_requestsAFullBillingAddressWithTheIsoRegion() { val json = build(paymentRequest(coffee, billingContactRequired = true)) val parameters = json.cardParameters() assertTrue(parameters.getBoolean("billingAddressRequired")) - assertEquals("FULL", parameters.getJSONObject("billingAddressParameters").getString("format")) + assertEquals("FULL-ISO3166", parameters.getJSONObject("billingAddressParameters").getString("format")) assertFalse(parameters.getJSONObject("billingAddressParameters").getBoolean("phoneNumberRequired")) assertFalse(json.has("shippingAddressRequired")) } @@ -380,7 +380,9 @@ class GooglePayRequestBuilderTest { assertTrue(json.getBoolean("emailRequired")) assertTrue(json.getBoolean("shippingAddressRequired")) - assertTrue(json.getJSONObject("shippingAddressParameters").getBoolean("phoneNumberRequired")) + val shippingParameters = json.getJSONObject("shippingAddressParameters") + assertEquals("FULL-ISO3166", shippingParameters.getString("format")) + assertTrue(shippingParameters.getBoolean("phoneNumberRequired")) assertFalse(json.cardParameters().has("billingAddressRequired")) } @@ -401,7 +403,7 @@ class GooglePayRequestBuilderTest { val json = build(paymentRequest(coffee, billingContactRequired = true, phoneNumberRequired = true)) val billingParameters = json.cardParameters().getJSONObject("billingAddressParameters") - assertEquals("FULL", billingParameters.getString("format")) + assertEquals("FULL-ISO3166", billingParameters.getString("format")) assertTrue(billingParameters.getBoolean("phoneNumberRequired")) } diff --git a/package/android/src/test/java/com/margelo/nitro/pay/PaymentMapperTest.kt b/package/android/src/test/java/com/margelo/nitro/pay/PaymentMapperTest.kt index ee04509..e588c52 100644 --- a/package/android/src/test/java/com/margelo/nitro/pay/PaymentMapperTest.kt +++ b/package/android/src/test/java/com/margelo/nitro/pay/PaymentMapperTest.kt @@ -42,6 +42,7 @@ class PaymentMapperTest { "address3": "", "locality": "Springfield", "administrativeArea": "IL", + "iso3166AdministrativeArea": "US-IL", "countryCode": "us", "postalCode": "62701", "sortingCode": "", @@ -57,6 +58,7 @@ class PaymentMapperTest { "address1": "1 Billing St", "locality": "Cupertino", "administrativeArea": "CA", + "iso3166AdministrativeArea": " us-ca ", "countryCode": "US", "postalCode": "95014", "phoneNumber": "+1 555 0100" @@ -176,7 +178,8 @@ class PaymentMapperTest { state = "CA", postalCode = "95014", country = null, - isoCountryCode = "US" + isoCountryCode = "US", + isoAdministrativeArea = "US-CA" ) ), result.billingContact @@ -192,7 +195,8 @@ class PaymentMapperTest { state = "IL", postalCode = "62701", country = null, - isoCountryCode = "US" + isoCountryCode = "US", + isoAdministrativeArea = "US-IL" ) ), result.shippingContact @@ -253,7 +257,9 @@ class PaymentMapperTest { state = null, postalCode = null, country = null, - isoCountryCode = "GB" + isoCountryCode = "GB", + // A MIN billing address has no ISO 3166-2 code. + isoAdministrativeArea = null ) ), result.billingContact diff --git a/package/ios/PassKitTypeMapper.swift b/package/ios/PassKitTypeMapper.swift index cb961fa..3928f04 100644 --- a/package/ios/PassKitTypeMapper.swift +++ b/package/ios/PassKitTypeMapper.swift @@ -316,7 +316,9 @@ enum PassKitTypeMapper { state: nonEmpty(address.state), postalCode: nonEmpty(address.postalCode), country: nonEmpty(address.country), - isoCountryCode: nonEmpty(address.isoCountryCode)?.uppercased() + isoCountryCode: nonEmpty(address.isoCountryCode)?.uppercased(), + // Apple Pay has no ISO 3166-2 code, and `state` is free text. + isoAdministrativeArea: nil ) } @@ -366,7 +368,8 @@ enum PassKitTypeMapper { state: contactsAddress.state, postalCode: contactsAddress.postalCode, country: contactsAddress.country, - isoCountryCode: contactsAddress.isoCountryCode + isoCountryCode: contactsAddress.isoCountryCode, + isoAdministrativeArea: nil ) return CNLabeledPostalAddress.init( diff --git a/package/nitrogen/generated/android/c++/JCNPostalAddress.hpp b/package/nitrogen/generated/android/c++/JCNPostalAddress.hpp index 3b90d73..83362e2 100644 --- a/package/nitrogen/generated/android/c++/JCNPostalAddress.hpp +++ b/package/nitrogen/generated/android/c++/JCNPostalAddress.hpp @@ -44,13 +44,16 @@ namespace margelo::nitro::pay { jni::local_ref country = this->getFieldValue(fieldCountry); static const auto fieldIsoCountryCode = clazz->getField("isoCountryCode"); jni::local_ref isoCountryCode = this->getFieldValue(fieldIsoCountryCode); + static const auto fieldIsoAdministrativeArea = clazz->getField("isoAdministrativeArea"); + jni::local_ref isoAdministrativeArea = this->getFieldValue(fieldIsoAdministrativeArea); return CNPostalAddress( street != nullptr ? std::make_optional(street->toStdString()) : std::nullopt, city != nullptr ? std::make_optional(city->toStdString()) : std::nullopt, state != nullptr ? std::make_optional(state->toStdString()) : std::nullopt, postalCode != nullptr ? std::make_optional(postalCode->toStdString()) : std::nullopt, country != nullptr ? std::make_optional(country->toStdString()) : std::nullopt, - isoCountryCode != nullptr ? std::make_optional(isoCountryCode->toStdString()) : std::nullopt + isoCountryCode != nullptr ? std::make_optional(isoCountryCode->toStdString()) : std::nullopt, + isoAdministrativeArea != nullptr ? std::make_optional(isoAdministrativeArea->toStdString()) : std::nullopt ); } @@ -60,7 +63,7 @@ namespace margelo::nitro::pay { */ [[maybe_unused]] static jni::local_ref fromCpp(const CNPostalAddress& value) { - using JSignature = JCNPostalAddress(jni::alias_ref, jni::alias_ref, jni::alias_ref, jni::alias_ref, jni::alias_ref, jni::alias_ref); + using JSignature = JCNPostalAddress(jni::alias_ref, jni::alias_ref, jni::alias_ref, jni::alias_ref, jni::alias_ref, jni::alias_ref, jni::alias_ref); static const auto clazz = javaClassStatic(); static const auto create = clazz->getStaticMethod("fromCpp"); return create( @@ -70,7 +73,8 @@ namespace margelo::nitro::pay { value.state.has_value() ? jni::make_jstring(value.state.value()) : nullptr, value.postalCode.has_value() ? jni::make_jstring(value.postalCode.value()) : nullptr, value.country.has_value() ? jni::make_jstring(value.country.value()) : nullptr, - value.isoCountryCode.has_value() ? jni::make_jstring(value.isoCountryCode.value()) : nullptr + value.isoCountryCode.has_value() ? jni::make_jstring(value.isoCountryCode.value()) : nullptr, + value.isoAdministrativeArea.has_value() ? jni::make_jstring(value.isoAdministrativeArea.value()) : nullptr ); } }; diff --git a/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/CNPostalAddress.kt b/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/CNPostalAddress.kt index b0a5811..14bc7d8 100644 --- a/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/CNPostalAddress.kt +++ b/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/CNPostalAddress.kt @@ -35,7 +35,10 @@ data class CNPostalAddress( val country: String?, @DoNotStrip @Keep - val isoCountryCode: String? + val isoCountryCode: String?, + @DoNotStrip + @Keep + val isoAdministrativeArea: String? ) { /* primary constructor */ @@ -48,6 +51,7 @@ data class CNPostalAddress( && Objects.deepEquals(this.postalCode, other.postalCode) && Objects.deepEquals(this.country, other.country) && Objects.deepEquals(this.isoCountryCode, other.isoCountryCode) + && Objects.deepEquals(this.isoAdministrativeArea, other.isoAdministrativeArea) } override fun hashCode(): Int { @@ -57,7 +61,8 @@ data class CNPostalAddress( state, postalCode, country, - isoCountryCode + isoCountryCode, + isoAdministrativeArea ).contentDeepHashCode() } @@ -69,8 +74,8 @@ data class CNPostalAddress( @Keep @Suppress("unused") @JvmStatic - private fun fromCpp(street: String?, city: String?, state: String?, postalCode: String?, country: String?, isoCountryCode: String?): CNPostalAddress { - return CNPostalAddress(street, city, state, postalCode, country, isoCountryCode) + private fun fromCpp(street: String?, city: String?, state: String?, postalCode: String?, country: String?, isoCountryCode: String?, isoAdministrativeArea: String?): CNPostalAddress { + return CNPostalAddress(street, city, state, postalCode, country, isoCountryCode, isoAdministrativeArea) } } } diff --git a/package/nitrogen/generated/ios/swift/CNPostalAddress.swift b/package/nitrogen/generated/ios/swift/CNPostalAddress.swift index c59e85f..ced29fd 100644 --- a/package/nitrogen/generated/ios/swift/CNPostalAddress.swift +++ b/package/nitrogen/generated/ios/swift/CNPostalAddress.swift @@ -18,7 +18,7 @@ public extension CNPostalAddress { /** * Create a new instance of `CNPostalAddress`. */ - init(street: String?, city: String?, state: String?, postalCode: String?, country: String?, isoCountryCode: String?) { + init(street: String?, city: String?, state: String?, postalCode: String?, country: String?, isoCountryCode: String?, isoAdministrativeArea: String?) { self.init({ () -> bridge.std__optional_std__string_ in if let __unwrappedValue = street { return bridge.create_std__optional_std__string_(std.string(__unwrappedValue)) @@ -55,6 +55,12 @@ public extension CNPostalAddress { } else { return .init() } + }(), { () -> bridge.std__optional_std__string_ in + if let __unwrappedValue = isoAdministrativeArea { + return bridge.create_std__optional_std__string_(std.string(__unwrappedValue)) + } else { + return .init() + } }()) } @@ -129,4 +135,16 @@ public extension CNPostalAddress { } }() } + + @inline(__always) + var isoAdministrativeArea: String? { + return { () -> String? in + if bridge.has_value_std__optional_std__string_(self.__isoAdministrativeArea) { + let __unwrapped = bridge.get_std__optional_std__string_(self.__isoAdministrativeArea) + return String(__unwrapped) + } else { + return nil + } + }() + } } diff --git a/package/nitrogen/generated/shared/c++/CNPostalAddress.hpp b/package/nitrogen/generated/shared/c++/CNPostalAddress.hpp index 482de13..6926f1c 100644 --- a/package/nitrogen/generated/shared/c++/CNPostalAddress.hpp +++ b/package/nitrogen/generated/shared/c++/CNPostalAddress.hpp @@ -46,10 +46,11 @@ namespace margelo::nitro::pay { std::optional postalCode SWIFT_PRIVATE; std::optional country SWIFT_PRIVATE; std::optional isoCountryCode SWIFT_PRIVATE; + std::optional isoAdministrativeArea SWIFT_PRIVATE; public: CNPostalAddress() = default; - explicit CNPostalAddress(std::optional street, std::optional city, std::optional state, std::optional postalCode, std::optional country, std::optional isoCountryCode): street(street), city(city), state(state), postalCode(postalCode), country(country), isoCountryCode(isoCountryCode) {} + explicit CNPostalAddress(std::optional street, std::optional city, std::optional state, std::optional postalCode, std::optional country, std::optional isoCountryCode, std::optional isoAdministrativeArea): street(street), city(city), state(state), postalCode(postalCode), country(country), isoCountryCode(isoCountryCode), isoAdministrativeArea(isoAdministrativeArea) {} public: friend bool operator==(const CNPostalAddress& lhs, const CNPostalAddress& rhs) = default; @@ -70,7 +71,8 @@ namespace margelo::nitro { JSIConverter>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "state"))), JSIConverter>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "postalCode"))), JSIConverter>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "country"))), - JSIConverter>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "isoCountryCode"))) + JSIConverter>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "isoCountryCode"))), + JSIConverter>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "isoAdministrativeArea"))) ); } static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::pay::CNPostalAddress& arg) { @@ -81,6 +83,7 @@ namespace margelo::nitro { obj.setProperty(runtime, PropNameIDCache::get(runtime, "postalCode"), JSIConverter>::toJSI(runtime, arg.postalCode)); obj.setProperty(runtime, PropNameIDCache::get(runtime, "country"), JSIConverter>::toJSI(runtime, arg.country)); obj.setProperty(runtime, PropNameIDCache::get(runtime, "isoCountryCode"), JSIConverter>::toJSI(runtime, arg.isoCountryCode)); + obj.setProperty(runtime, PropNameIDCache::get(runtime, "isoAdministrativeArea"), JSIConverter>::toJSI(runtime, arg.isoAdministrativeArea)); return obj; } static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) { @@ -97,6 +100,7 @@ namespace margelo::nitro { if (!JSIConverter>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "postalCode")))) return false; if (!JSIConverter>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "country")))) return false; if (!JSIConverter>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "isoCountryCode")))) return false; + if (!JSIConverter>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "isoAdministrativeArea")))) return false; return true; } }; diff --git a/package/src/types/Contact.ts b/package/src/types/Contact.ts index e2a4aeb..5206715 100644 --- a/package/src/types/Contact.ts +++ b/package/src/types/Contact.ts @@ -16,6 +16,12 @@ export interface CNPostalAddress { postalCode?: string country?: string isoCountryCode?: string + /** + * Android only: the region as an ISO 3166-2 code, such as `'US-CA'`, with + * the country prefix. Google Pay can return it for full billing and shipping + * addresses, and omits it when it has no code. On iOS it is always omitted. + */ + isoAdministrativeArea?: string } export interface CNLabeledPhoneNumber {