diff --git a/README.md b/README.md index 401518c..a1bb693 100644 --- a/README.md +++ b/README.md @@ -567,10 +567,12 @@ import { HybridPaymentHandler } from "@gmisoftware/react-native-pay"; const isBusy = HybridPaymentHandler.isPaymentInProgress(); ``` -##### `openPaymentSetup(): void` +##### `openPaymentSetup(options?: OpenPaymentSetupOptions): void` iOS only: opens Wallet so the user can add a card. Call it from an `ApplePayButton` with `buttonType="setUp"`. It does nothing on Android, where the Google Pay sheet lets the user add a card. +On iOS 26.4 and later it passes your merchant ID to Wallet, so Apple can attribute a card added there to your app. It uses the first ID in `ReactNativePayApplePayMerchantIdentifiers`, or `options.applePayMerchantIdentifier` when given. + --- ### Button Components diff --git a/docs/docs/api/hybrid-payment-handler.md b/docs/docs/api/hybrid-payment-handler.md index 22e54c8..ed3c267 100644 --- a/docs/docs/api/hybrid-payment-handler.md +++ b/docs/docs/api/hybrid-payment-handler.md @@ -109,7 +109,7 @@ const isBusy = HybridPaymentHandler.isPaymentInProgress() --- -### openPaymentSetup() +### openPaymentSetup(options?) iOS only. Opens Wallet (Settings on iPad) so the user can add a card. Call it from a **Set up Apple Pay** button (`ApplePayButton` with `buttonType="setUp"`) when `isSupported` is `true` and `hasActiveCard` is `false`. It does nothing on devices without Apple Pay, and nothing on Android, where the Google Pay sheet lets the user add a card. @@ -119,6 +119,18 @@ import { HybridPaymentHandler } from '@gmisoftware/react-native-pay' HybridPaymentHandler.openPaymentSetup() ``` +On iOS 26.4 and later it passes your merchant ID to Wallet, which Apple uses to attribute a card added there to your app. The ID is the first one in the Info.plist key `ReactNativePayApplePayMerchantIdentifiers`, which the Expo plugin writes. Pass `applePayMerchantIdentifier` ([OpenPaymentSetupOptions](/docs/api/types#openpaymentsetupoptions)) to use another, as in `PaymentRequest`: + +```ts +import { HybridPaymentHandler } from '@gmisoftware/react-native-pay' + +HybridPaymentHandler.openPaymentSetup({ + applePayMerchantIdentifier: 'merchant.com.example.store', +}) +``` + +Earlier iOS versions, apps built with Xcode older than 26.4, and apps with no merchant ID configured open the setup without one. + --- ### Raw native handler diff --git a/docs/docs/api/types.md b/docs/docs/api/types.md index 1e440bc..c954242 100644 --- a/docs/docs/api/types.md +++ b/docs/docs/api/types.md @@ -425,6 +425,20 @@ The values are checked as in `startPayment`, and the Promise rejects with the sa --- +## OpenPaymentSetupOptions + +What `openPaymentSetup` passes to Wallet (iOS only). + +```ts +interface OpenPaymentSetupOptions { + applePayMerchantIdentifier?: string // default: the first ID in ReactNativePayApplePayMerchantIdentifiers +} +``` + +On iOS 26.4 and later, Apple attributes a card the user adds in the setup to this merchant ID. A blank value falls back to the default. + +--- + ## GooglePayEnvironment Used in config and `PaymentRequest` for Android. diff --git a/docs/docs/guides/availability.md b/docs/docs/guides/availability.md index d957b2c..fea4c61 100644 --- a/docs/docs/guides/availability.md +++ b/docs/docs/guides/availability.md @@ -21,7 +21,7 @@ On Android, the check goes to the Google Pay environment you pass as `googlePayE ## Show the right button - **Android**: show the Google Pay button when `isSupported` is `true`. A user without a card adds one in the Google Pay sheet. Google offers no separate setup screen, so `openPaymentSetup()` does nothing on Android. -- **iOS**: show the Apple Pay buy button when `hasActiveCard` is `true`. When only `isSupported` is `true`, show the **Set up Apple Pay** button (`buttonType="setUp"`) and call `HybridPaymentHandler.openPaymentSetup()` on tap. It opens Wallet (Settings on iPad) to add a card. +- **iOS**: show the Apple Pay buy button when `hasActiveCard` is `true`. When only `isSupported` is `true`, show the **Set up Apple Pay** button (`buttonType="setUp"`) and call `HybridPaymentHandler.openPaymentSetup()` on tap. It opens Wallet (Settings on iPad) to add a card. On iOS 26.4 and later it also passes your merchant ID, so Apple can attribute the new card to your app. - **Neither platform**: when `isSupported` is `false`, hide the wallet buttons and offer another way to pay. In the example below, the checkout screen owns the hook and the cart, and `WalletButton` picks the button from the status. The user comes back from Wallet with a card the status does not know about yet, so the screen checks again when the app returns to the foreground: diff --git a/package/android/src/main/java/com/margelo/nitro/pay/HybridPaymentHandler.kt b/package/android/src/main/java/com/margelo/nitro/pay/HybridPaymentHandler.kt index b771202..9f423b8 100644 --- a/package/android/src/main/java/com/margelo/nitro/pay/HybridPaymentHandler.kt +++ b/package/android/src/main/java/com/margelo/nitro/pay/HybridPaymentHandler.kt @@ -106,7 +106,7 @@ class HybridPaymentHandler : HybridPaymentHandlerSpec() { return pendingPayment.isInProgress } - override fun openPaymentSetup() { + override fun openPaymentSetup(options: OpenPaymentSetupOptions?) { // Google Pay has no setup screen to open: its sheet lets the user add a card. PayLog.d(PaymentConstants.TAG_PAYMENT_HANDLER, "openPaymentSetup does nothing on Android") } diff --git a/package/ios/HybridPaymentHandler.swift b/package/ios/HybridPaymentHandler.swift index 026ab68..97d49ee 100644 --- a/package/ios/HybridPaymentHandler.swift +++ b/package/ios/HybridPaymentHandler.swift @@ -155,22 +155,32 @@ struct PaymentRequestBuilder { } private static func resolveMerchantIdentifier(from request: PaymentRequest) -> String? { - if let overrideIdentifier = request.applePayMerchantIdentifier?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines), + resolveMerchantIdentifier(override: request.applePayMerchantIdentifier) + } + + /// The override when it isn't blank, else the first ID in the Info.plist key. + /// Internal so `openPaymentSetup` can use it. + static func resolveMerchantIdentifier(override: String?) -> String? { + resolveMerchantIdentifier( + override: override, + configured: Bundle.main.object(forInfoDictionaryKey: BundleKey.applePayMerchantIdentifiers) + ) + } + + /// `configured` is the Info.plist value. Internal so the NitroPay unit tests can pass one. + static func resolveMerchantIdentifier(override: String?, configured: Any?) -> String? { + if let overrideIdentifier = override?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines), !overrideIdentifier.isEmpty { return overrideIdentifier } - if let merchantIdentifiers = Bundle.main.object( - forInfoDictionaryKey: BundleKey.applePayMerchantIdentifiers - ) as? [String] { + if let merchantIdentifiers = configured as? [String] { return merchantIdentifiers.first { !$0.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty } } - if let merchantIdentifier = Bundle.main.object( - forInfoDictionaryKey: BundleKey.applePayMerchantIdentifiers - ) as? String { + if let merchantIdentifier = configured as? String { let trimmedIdentifier = merchantIdentifier.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) return trimmedIdentifier.isEmpty ? nil : trimmedIdentifier } @@ -837,10 +847,22 @@ class HybridPaymentHandler: HybridPaymentHandlerSpec { } } - func openPaymentSetup() throws { + func openPaymentSetup(options: OpenPaymentSetupOptions?) throws { + let merchantIdentifier = PaymentRequestBuilder.resolveMerchantIdentifier( + override: options?.applePayMerchantIdentifier + ) // Hands over to Wallet (Settings on iPad); does nothing without Apple Pay support. DispatchQueue.main.async { - PKPassLibrary().openPaymentSetup() + let library = PKPassLibrary() + // iOS 26.4 attributes a card added in the setup to the merchant ID. + // Needs the iOS 26.4 SDK, which ships with Swift 6.3. + #if compiler(>=6.3) + if #available(iOS 26.4, *), let merchantIdentifier { + library.openPaymentSetup(merchantIdentifier: merchantIdentifier) + return + } + #endif + library.openPaymentSetup() } } diff --git a/package/ios/Tests/PaymentRequestBuilderTests.swift b/package/ios/Tests/PaymentRequestBuilderTests.swift index 2e99c34..5782e71 100644 --- a/package/ios/Tests/PaymentRequestBuilderTests.swift +++ b/package/ios/Tests/PaymentRequestBuilderTests.swift @@ -702,4 +702,22 @@ final class PaymentRequestBuilderTests: XCTestCase { with: "No Apple Pay merchant identifier configured. Add one to the Info.plist key ReactNativePayApplePayMerchantIdentifiers (the Expo plugin does this), or pass applePayMerchantIdentifier." ) } + + func testResolvesMerchantIdentifierFromOverrideThenInfoPlist() { + let configured = [" ", "merchant.com.example.first", "merchant.com.example.second"] + XCTAssertEqual( + PaymentRequestBuilder.resolveMerchantIdentifier(override: " merchant.com.example.override\n", configured: configured), + "merchant.com.example.override" + ) + XCTAssertEqual( + PaymentRequestBuilder.resolveMerchantIdentifier(override: " ", configured: configured), + "merchant.com.example.first" + ) + XCTAssertEqual( + PaymentRequestBuilder.resolveMerchantIdentifier(override: nil, configured: " merchant.com.example "), + "merchant.com.example" + ) + XCTAssertNil(PaymentRequestBuilder.resolveMerchantIdentifier(override: nil, configured: [" "])) + XCTAssertNil(PaymentRequestBuilder.resolveMerchantIdentifier(override: "", configured: nil)) + } } diff --git a/package/nitrogen/generated/android/c++/JHybridPaymentHandlerSpec.cpp b/package/nitrogen/generated/android/c++/JHybridPaymentHandlerSpec.cpp index 44780d2..125d606 100644 --- a/package/nitrogen/generated/android/c++/JHybridPaymentHandlerSpec.cpp +++ b/package/nitrogen/generated/android/c++/JHybridPaymentHandlerSpec.cpp @@ -103,6 +103,8 @@ namespace margelo::nitro::pay { struct PaymentSheetError; } namespace margelo::nitro::pay { enum class PaymentSheetErrorCode; } // Forward declaration of `PaymentSheetCard` to properly resolve imports. namespace margelo::nitro::pay { struct PaymentSheetCard; } +// Forward declaration of `OpenPaymentSetupOptions` to properly resolve imports. +namespace margelo::nitro::pay { struct OpenPaymentSetupOptions; } #include "PayServiceStatus.hpp" #include @@ -216,6 +218,8 @@ namespace margelo::nitro::pay { struct PaymentSheetCard; } #include "JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_PaymentSheetUpdate_____ShippingMethod_PaymentSheetSelection.hpp" #include "JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_PaymentSheetUpdate_____PaymentSheetCard_PaymentSheetSelection.hpp" #include "JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_PaymentSheetUpdate_____std__string_PaymentSheetSelection.hpp" +#include "OpenPaymentSetupOptions.hpp" +#include "JOpenPaymentSetupOptions.hpp" namespace margelo::nitro::pay { @@ -287,9 +291,9 @@ namespace margelo::nitro::pay { auto __result = method(_javaPart); return static_cast(__result); } - void JHybridPaymentHandlerSpec::openPaymentSetup() { - static const auto method = _javaPart->javaClassStatic()->getMethod("openPaymentSetup"); - method(_javaPart); + void JHybridPaymentHandlerSpec::openPaymentSetup(const std::optional& options) { + static const auto method = _javaPart->javaClassStatic()->getMethod /* options */)>("openPaymentSetup"); + method(_javaPart, options.has_value() ? JOpenPaymentSetupOptions::fromCpp(options.value()) : nullptr); } } // namespace margelo::nitro::pay diff --git a/package/nitrogen/generated/android/c++/JHybridPaymentHandlerSpec.hpp b/package/nitrogen/generated/android/c++/JHybridPaymentHandlerSpec.hpp index 5ad05ee..943c5df 100644 --- a/package/nitrogen/generated/android/c++/JHybridPaymentHandlerSpec.hpp +++ b/package/nitrogen/generated/android/c++/JHybridPaymentHandlerSpec.hpp @@ -57,7 +57,7 @@ namespace margelo::nitro::pay { std::shared_ptr> payServiceStatus(const std::optional& options) override; std::shared_ptr> startPayment(const PaymentRequest& request, const std::optional& options) override; bool isPaymentInProgress() override; - void openPaymentSetup() override; + void openPaymentSetup(const std::optional& options) override; private: jni::global_ref _javaPart; diff --git a/package/nitrogen/generated/android/c++/JOpenPaymentSetupOptions.hpp b/package/nitrogen/generated/android/c++/JOpenPaymentSetupOptions.hpp new file mode 100644 index 0000000..fd52172 --- /dev/null +++ b/package/nitrogen/generated/android/c++/JOpenPaymentSetupOptions.hpp @@ -0,0 +1,58 @@ +/// +/// JOpenPaymentSetupOptions.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +#include +#include "OpenPaymentSetupOptions.hpp" + +#include +#include + +namespace margelo::nitro::pay { + + using namespace facebook; + + /** + * The C++ JNI bridge between the C++ struct "OpenPaymentSetupOptions" and the Kotlin data class "OpenPaymentSetupOptions". + */ + struct JOpenPaymentSetupOptions final: public jni::JavaClass { + public: + static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/pay/OpenPaymentSetupOptions;"; + + public: + /** + * Convert this Java/Kotlin-based struct to the C++ struct OpenPaymentSetupOptions by copying all values to C++. + */ + [[maybe_unused]] + [[nodiscard]] + OpenPaymentSetupOptions toCpp() const { + static const auto clazz = javaClassStatic(); + static const auto fieldApplePayMerchantIdentifier = clazz->getField("applePayMerchantIdentifier"); + jni::local_ref applePayMerchantIdentifier = this->getFieldValue(fieldApplePayMerchantIdentifier); + return OpenPaymentSetupOptions( + applePayMerchantIdentifier != nullptr ? std::make_optional(applePayMerchantIdentifier->toStdString()) : std::nullopt + ); + } + + public: + /** + * Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java. + */ + [[maybe_unused]] + static jni::local_ref fromCpp(const OpenPaymentSetupOptions& value) { + using JSignature = JOpenPaymentSetupOptions(jni::alias_ref); + static const auto clazz = javaClassStatic(); + static const auto create = clazz->getStaticMethod("fromCpp"); + return create( + clazz, + value.applePayMerchantIdentifier.has_value() ? jni::make_jstring(value.applePayMerchantIdentifier.value()) : nullptr + ); + } + }; + +} // namespace margelo::nitro::pay diff --git a/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/HybridPaymentHandlerSpec.kt b/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/HybridPaymentHandlerSpec.kt index a8b7e29..b266c43 100644 --- a/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/HybridPaymentHandlerSpec.kt +++ b/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/HybridPaymentHandlerSpec.kt @@ -43,7 +43,7 @@ abstract class HybridPaymentHandlerSpec: HybridObject() { @DoNotStrip @Keep - abstract fun openPaymentSetup(): Unit + abstract fun openPaymentSetup(options: OpenPaymentSetupOptions?): Unit // Default implementation of `HybridObject.toString()` override fun toString(): String { diff --git a/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/OpenPaymentSetupOptions.kt b/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/OpenPaymentSetupOptions.kt new file mode 100644 index 0000000..b81ca99 --- /dev/null +++ b/package/nitrogen/generated/android/kotlin/com/margelo/nitro/pay/OpenPaymentSetupOptions.kt @@ -0,0 +1,51 @@ +/// +/// OpenPaymentSetupOptions.kt +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +package com.margelo.nitro.pay + +import androidx.annotation.Keep +import com.facebook.proguard.annotations.DoNotStrip +import java.util.Objects + + +/** + * Represents the JavaScript object/struct "OpenPaymentSetupOptions". + */ +@DoNotStrip +@Keep +data class OpenPaymentSetupOptions( + @DoNotStrip + @Keep + val applePayMerchantIdentifier: String? +) { + /* primary constructor */ + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is OpenPaymentSetupOptions) return false + return Objects.deepEquals(this.applePayMerchantIdentifier, other.applePayMerchantIdentifier) + } + + override fun hashCode(): Int { + return arrayOf( + applePayMerchantIdentifier + ).contentDeepHashCode() + } + + companion object { + /** + * Constructor called from C++ + */ + @DoNotStrip + @Keep + @Suppress("unused") + @JvmStatic + private fun fromCpp(applePayMerchantIdentifier: String?): OpenPaymentSetupOptions { + return OpenPaymentSetupOptions(applePayMerchantIdentifier) + } + } +} diff --git a/package/nitrogen/generated/ios/NitroPay-Swift-Cxx-Bridge.hpp b/package/nitrogen/generated/ios/NitroPay-Swift-Cxx-Bridge.hpp index e94da0e..a79e9b0 100644 --- a/package/nitrogen/generated/ios/NitroPay-Swift-Cxx-Bridge.hpp +++ b/package/nitrogen/generated/ios/NitroPay-Swift-Cxx-Bridge.hpp @@ -46,6 +46,8 @@ namespace margelo::nitro::pay { struct GooglePayTokenization; } namespace margelo::nitro::pay { class HybridApplePayButtonSpec; } // Forward declaration of `HybridPaymentHandlerSpec` to properly resolve imports. namespace margelo::nitro::pay { class HybridPaymentHandlerSpec; } +// Forward declaration of `OpenPaymentSetupOptions` to properly resolve imports. +namespace margelo::nitro::pay { struct OpenPaymentSetupOptions; } // Forward declaration of `PKSecureElementPass` to properly resolve imports. namespace margelo::nitro::pay { struct PKSecureElementPass; } // Forward declaration of `PassActivationState` to properly resolve imports. @@ -133,6 +135,7 @@ namespace NitroPay { class HybridPaymentHandlerSpec_cxx; } #include "GooglePayTokenizationType.hpp" #include "HybridApplePayButtonSpec.hpp" #include "HybridPaymentHandlerSpec.hpp" +#include "OpenPaymentSetupOptions.hpp" #include "PKSecureElementPass.hpp" #include "PassActivationState.hpp" #include "PayServiceStatus.hpp" @@ -1217,6 +1220,21 @@ namespace margelo::nitro::pay::bridge::swift { return optional.value(); } + // pragma MARK: std::optional + /** + * Specialized version of `std::optional`. + */ + using std__optional_OpenPaymentSetupOptions_ = std::optional; + inline std::optional create_std__optional_OpenPaymentSetupOptions_(const OpenPaymentSetupOptions& value) noexcept { + return std::optional(value); + } + inline bool has_value_std__optional_OpenPaymentSetupOptions_(const std::optional& optional) noexcept { + return optional.has_value(); + } + inline OpenPaymentSetupOptions get_std__optional_OpenPaymentSetupOptions_(const std::optional& optional) noexcept { + return optional.value(); + } + // pragma MARK: std::shared_ptr /** * Specialized version of `std::shared_ptr`. diff --git a/package/nitrogen/generated/ios/NitroPay-Swift-Cxx-Umbrella.hpp b/package/nitrogen/generated/ios/NitroPay-Swift-Cxx-Umbrella.hpp index a4cd95d..a75d0e9 100644 --- a/package/nitrogen/generated/ios/NitroPay-Swift-Cxx-Umbrella.hpp +++ b/package/nitrogen/generated/ios/NitroPay-Swift-Cxx-Umbrella.hpp @@ -50,6 +50,8 @@ namespace margelo::nitro::pay { struct GooglePayTokenization; } namespace margelo::nitro::pay { class HybridApplePayButtonSpec; } // Forward declaration of `HybridPaymentHandlerSpec` to properly resolve imports. namespace margelo::nitro::pay { class HybridPaymentHandlerSpec; } +// Forward declaration of `OpenPaymentSetupOptions` to properly resolve imports. +namespace margelo::nitro::pay { struct OpenPaymentSetupOptions; } // Forward declaration of `PKSecureElementPass` to properly resolve imports. namespace margelo::nitro::pay { struct PKSecureElementPass; } // Forward declaration of `PassActivationState` to properly resolve imports. @@ -135,6 +137,7 @@ namespace margelo::nitro::pay { struct StartPaymentOptions; } #include "GooglePayTokenizationType.hpp" #include "HybridApplePayButtonSpec.hpp" #include "HybridPaymentHandlerSpec.hpp" +#include "OpenPaymentSetupOptions.hpp" #include "PKSecureElementPass.hpp" #include "PassActivationState.hpp" #include "PayServiceStatus.hpp" diff --git a/package/nitrogen/generated/ios/c++/HybridPaymentHandlerSpecSwift.hpp b/package/nitrogen/generated/ios/c++/HybridPaymentHandlerSpecSwift.hpp index a04db5f..c0ad9d2 100644 --- a/package/nitrogen/generated/ios/c++/HybridPaymentHandlerSpecSwift.hpp +++ b/package/nitrogen/generated/ios/c++/HybridPaymentHandlerSpecSwift.hpp @@ -108,6 +108,8 @@ namespace margelo::nitro::pay { struct PaymentSheetError; } namespace margelo::nitro::pay { enum class PaymentSheetErrorCode; } // Forward declaration of `PaymentSheetCard` to properly resolve imports. namespace margelo::nitro::pay { struct PaymentSheetCard; } +// Forward declaration of `OpenPaymentSetupOptions` to properly resolve imports. +namespace margelo::nitro::pay { struct OpenPaymentSetupOptions; } #include "PayServiceStatus.hpp" #include @@ -164,6 +166,7 @@ namespace margelo::nitro::pay { struct PaymentSheetCard; } #include "PaymentSheetError.hpp" #include "PaymentSheetErrorCode.hpp" #include "PaymentSheetCard.hpp" +#include "OpenPaymentSetupOptions.hpp" #include "NitroPay-Swift-Cxx-Umbrella.hpp" @@ -239,8 +242,8 @@ namespace margelo::nitro::pay { auto __value = std::move(__result.value()); return __value; } - inline void openPaymentSetup() override { - auto __result = _swiftPart.openPaymentSetup(); + inline void openPaymentSetup(const std::optional& options) override { + auto __result = _swiftPart.openPaymentSetup(options); if (__result.hasError()) [[unlikely]] { std::rethrow_exception(__result.error()); } diff --git a/package/nitrogen/generated/ios/swift/HybridPaymentHandlerSpec.swift b/package/nitrogen/generated/ios/swift/HybridPaymentHandlerSpec.swift index 0a57a93..07e4d47 100644 --- a/package/nitrogen/generated/ios/swift/HybridPaymentHandlerSpec.swift +++ b/package/nitrogen/generated/ios/swift/HybridPaymentHandlerSpec.swift @@ -16,7 +16,7 @@ public protocol HybridPaymentHandlerSpec_protocol: HybridObject { func payServiceStatus(options: PayServiceStatusOptions?) throws -> Promise func startPayment(request: PaymentRequest, options: StartPaymentOptions?) throws -> Promise func isPaymentInProgress() throws -> Bool - func openPaymentSetup() throws -> Void + func openPaymentSetup(options: OpenPaymentSetupOptions?) throws -> Void } public extension HybridPaymentHandlerSpec_protocol { diff --git a/package/nitrogen/generated/ios/swift/HybridPaymentHandlerSpec_cxx.swift b/package/nitrogen/generated/ios/swift/HybridPaymentHandlerSpec_cxx.swift index 40a131a..7b589ad 100644 --- a/package/nitrogen/generated/ios/swift/HybridPaymentHandlerSpec_cxx.swift +++ b/package/nitrogen/generated/ios/swift/HybridPaymentHandlerSpec_cxx.swift @@ -175,9 +175,9 @@ open class HybridPaymentHandlerSpec_cxx { } @inline(__always) - public final func openPaymentSetup() -> bridge.Result_void_ { + public final func openPaymentSetup(options: bridge.std__optional_OpenPaymentSetupOptions_) -> bridge.Result_void_ { do { - try self.__implementation.openPaymentSetup() + try self.__implementation.openPaymentSetup(options: options.value) return bridge.create_Result_void_() } catch (let __error) { let __exceptionPtr = __error.toCpp() diff --git a/package/nitrogen/generated/ios/swift/OpenPaymentSetupOptions.swift b/package/nitrogen/generated/ios/swift/OpenPaymentSetupOptions.swift new file mode 100644 index 0000000..544ed98 --- /dev/null +++ b/package/nitrogen/generated/ios/swift/OpenPaymentSetupOptions.swift @@ -0,0 +1,42 @@ +/// +/// OpenPaymentSetupOptions.swift +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +import NitroModules + +/** + * Represents an instance of `OpenPaymentSetupOptions`, backed by a C++ struct. + */ +public typealias OpenPaymentSetupOptions = margelo.nitro.pay.OpenPaymentSetupOptions + +public extension OpenPaymentSetupOptions { + private typealias bridge = margelo.nitro.pay.bridge.swift + + /** + * Create a new instance of `OpenPaymentSetupOptions`. + */ + init(applePayMerchantIdentifier: String?) { + self.init({ () -> bridge.std__optional_std__string_ in + if let __unwrappedValue = applePayMerchantIdentifier { + return bridge.create_std__optional_std__string_(std.string(__unwrappedValue)) + } else { + return .init() + } + }()) + } + + @inline(__always) + var applePayMerchantIdentifier: String? { + return { () -> String? in + if bridge.has_value_std__optional_std__string_(self.__applePayMerchantIdentifier) { + let __unwrapped = bridge.get_std__optional_std__string_(self.__applePayMerchantIdentifier) + return String(__unwrapped) + } else { + return nil + } + }() + } +} diff --git a/package/nitrogen/generated/shared/c++/HybridPaymentHandlerSpec.hpp b/package/nitrogen/generated/shared/c++/HybridPaymentHandlerSpec.hpp index 251e157..01ae1da 100644 --- a/package/nitrogen/generated/shared/c++/HybridPaymentHandlerSpec.hpp +++ b/package/nitrogen/generated/shared/c++/HybridPaymentHandlerSpec.hpp @@ -23,6 +23,8 @@ namespace margelo::nitro::pay { struct PaymentResult; } namespace margelo::nitro::pay { struct PaymentRequest; } // Forward declaration of `StartPaymentOptions` to properly resolve imports. namespace margelo::nitro::pay { struct StartPaymentOptions; } +// Forward declaration of `OpenPaymentSetupOptions` to properly resolve imports. +namespace margelo::nitro::pay { struct OpenPaymentSetupOptions; } #include "PayServiceStatus.hpp" #include @@ -31,6 +33,7 @@ namespace margelo::nitro::pay { struct StartPaymentOptions; } #include "PaymentResult.hpp" #include "PaymentRequest.hpp" #include "StartPaymentOptions.hpp" +#include "OpenPaymentSetupOptions.hpp" namespace margelo::nitro::pay { @@ -66,7 +69,7 @@ namespace margelo::nitro::pay { virtual std::shared_ptr> payServiceStatus(const std::optional& options) = 0; virtual std::shared_ptr> startPayment(const PaymentRequest& request, const std::optional& options) = 0; virtual bool isPaymentInProgress() = 0; - virtual void openPaymentSetup() = 0; + virtual void openPaymentSetup(const std::optional& options) = 0; protected: // Hybrid Setup diff --git a/package/nitrogen/generated/shared/c++/OpenPaymentSetupOptions.hpp b/package/nitrogen/generated/shared/c++/OpenPaymentSetupOptions.hpp new file mode 100644 index 0000000..42e4f3d --- /dev/null +++ b/package/nitrogen/generated/shared/c++/OpenPaymentSetupOptions.hpp @@ -0,0 +1,84 @@ +/// +/// OpenPaymentSetupOptions.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif + + + +#include +#include + +namespace margelo::nitro::pay { + + /** + * A struct which can be represented as a JavaScript object (OpenPaymentSetupOptions). + */ + struct OpenPaymentSetupOptions final { + public: + std::optional applePayMerchantIdentifier SWIFT_PRIVATE; + + public: + OpenPaymentSetupOptions() = default; + explicit OpenPaymentSetupOptions(std::optional applePayMerchantIdentifier): applePayMerchantIdentifier(applePayMerchantIdentifier) {} + + public: + friend bool operator==(const OpenPaymentSetupOptions& lhs, const OpenPaymentSetupOptions& rhs) = default; + }; + +} // namespace margelo::nitro::pay + +namespace margelo::nitro { + + // C++ OpenPaymentSetupOptions <> JS OpenPaymentSetupOptions (object) + template <> + struct JSIConverter final { + static inline margelo::nitro::pay::OpenPaymentSetupOptions fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { + jsi::Object obj = arg.asObject(runtime); + return margelo::nitro::pay::OpenPaymentSetupOptions( + JSIConverter>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "applePayMerchantIdentifier"))) + ); + } + static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::pay::OpenPaymentSetupOptions& arg) { + jsi::Object obj(runtime); + obj.setProperty(runtime, PropNameIDCache::get(runtime, "applePayMerchantIdentifier"), JSIConverter>::toJSI(runtime, arg.applePayMerchantIdentifier)); + return obj; + } + static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) { + if (!value.isObject()) { + return false; + } + jsi::Object obj = value.getObject(runtime); + if (!nitro::isPlainObject(runtime, obj)) { + return false; + } + if (!JSIConverter>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "applePayMerchantIdentifier")))) return false; + return true; + } + }; + +} // namespace margelo::nitro diff --git a/package/src/__tests__/paymentHandler.test.ts b/package/src/__tests__/paymentHandler.test.ts index d9a993e..2d34b73 100644 --- a/package/src/__tests__/paymentHandler.test.ts +++ b/package/src/__tests__/paymentHandler.test.ts @@ -9,7 +9,10 @@ const DECLINED_RESULT = { error: 'Declined', } -const mockInstances: Array<{ startPayment: jest.Mock }> = [] +const mockInstances: Array<{ + startPayment: jest.Mock + openPaymentSetup: jest.Mock +}> = [] const mockPresentations: Array<(result: any) => void> = [] jest.mock('react-native-nitro-modules', () => ({ @@ -21,6 +24,7 @@ jest.mock('react-native-nitro-modules', () => ({ payServiceStatus: () => Promise.resolve({ isSupported: true, hasActiveCard: true }), isPaymentInProgress: () => inFlight, + openPaymentSetup: jest.fn(), startPayment: jest.fn(() => { if (inFlight) { return Promise.resolve({ @@ -375,3 +379,28 @@ describe('request validation', () => { expect(nativeStartPayment().mock.calls[0]).toHaveLength(1) }) }) + +describe('openPaymentSetup', () => { + const nativeOpenPaymentSetup = () => { + getPaymentHandler() + return mockInstances[0]!.openPaymentSetup + } + + beforeEach(() => nativeOpenPaymentSetup().mockClear()) + + it('passes the merchant identifier to native', () => { + HybridPaymentHandler.openPaymentSetup({ + applePayMerchantIdentifier: 'merchant.com.example', + }) + + expect(nativeOpenPaymentSetup()).toHaveBeenCalledWith({ + applePayMerchantIdentifier: 'merchant.com.example', + }) + }) + + it('calls native without options when none are given', () => { + HybridPaymentHandler.openPaymentSetup() + + expect(nativeOpenPaymentSetup().mock.calls[0]).toHaveLength(0) + }) +}) diff --git a/package/src/paymentHandler.ts b/package/src/paymentHandler.ts index 5a53c2e..15ca4a0 100644 --- a/package/src/paymentHandler.ts +++ b/package/src/paymentHandler.ts @@ -187,5 +187,8 @@ export const validatingPaymentHandler: PaymentHandlerMethods = { : getPaymentHandler().startPayment(validatedRequest, preparedOptions) }, isPaymentInProgress: () => getPaymentHandler().isPaymentInProgress(), - openPaymentSetup: () => getPaymentHandler().openPaymentSetup(), + openPaymentSetup: (options) => + options === undefined + ? getPaymentHandler().openPaymentSetup() + : getPaymentHandler().openPaymentSetup(options), } diff --git a/package/src/specs/PaymentHandler.nitro.ts b/package/src/specs/PaymentHandler.nitro.ts index 4c456fe..c313006 100644 --- a/package/src/specs/PaymentHandler.nitro.ts +++ b/package/src/specs/PaymentHandler.nitro.ts @@ -1,5 +1,6 @@ import type { HybridObject } from 'react-native-nitro-modules' import type { + OpenPaymentSetupOptions, PayServiceStatus, PayServiceStatusOptions, PaymentRequest, @@ -16,5 +17,5 @@ export interface PaymentHandler ): Promise isPaymentInProgress(): boolean // iOS only: opens Wallet to add a card. Does nothing on Android. - openPaymentSetup(): void + openPaymentSetup(options?: OpenPaymentSetupOptions): void } diff --git a/package/src/types/Payment.ts b/package/src/types/Payment.ts index 2b9b9ec..bb98ae2 100644 --- a/package/src/types/Payment.ts +++ b/package/src/types/Payment.ts @@ -828,5 +828,15 @@ export interface PayServiceStatusOptions { googlePayEnvironment?: GooglePayEnvironment } +/** + * What `openPaymentSetup` passes to Wallet. iOS only. + */ +export interface OpenPaymentSetupOptions { + // The merchant ID that Apple attributes a card added in this setup to, on + // iOS 26.4 and later. Defaults to the first ID in the Info.plist key + // `ReactNativePayApplePayMerchantIdentifiers`, as `PaymentRequest`. + applePayMerchantIdentifier?: string +} + // Backward compatibility alias export type ApplePayStatus = PayServiceStatus