From 12fc3d273ccccbdfbaee42adb96a52f6e012313e Mon Sep 17 00:00:00 2001 From: Charlie Cruzan Date: Wed, 13 Sep 2023 11:05:01 -1000 Subject: [PATCH 1/2] breaking: remove Constants.API_VERSION.ISSUING in favor of getIssuingApiVersion --- CHANGELOG.md | 4 ++++ .../com/reactnativestripesdk/StripeSdkModule.kt | 8 +++++++- example/src/screens/ApplePayScreen.tsx | 5 +++-- example/src/screens/GooglePayScreen.tsx | 5 +++-- ios/StripeSdk.m | 4 ++++ ios/StripeSdk.swift | 6 +++++- jest/mock.js | 1 + src/NativeStripeSdk.tsx | 3 ++- src/functions.ts | 9 +++++++++ src/hooks/usePlatformPay.tsx | 16 ++++++++++++++++ src/hooks/useStripe.tsx | 6 ++++++ 11 files changed, 60 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aca68b25d..a54494ae5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +**Breaking changes** + +- `Constants.API_VERSIONS.ISSUING` has been removed, please use the asynchronous `getIssuingApiVersion` method instead. This should improve app startup time. + **Features** - `createPlatformPayPaymentMethod` and `createPlatformPayToken` now also include an optional `shippingContact` field in their results. [#1500](https://github.com/stripe/stripe-react-native/pull/1500) diff --git a/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt b/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt index 1a85a6cf2..7c66a70e0 100644 --- a/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt +++ b/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt @@ -126,10 +126,16 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ hashMapOf( "API_VERSIONS" to hashMapOf( "CORE" to ApiVersion.API_VERSION_CODE, - "ISSUING" to PushProvisioningProxy.getApiVersion(), ) ) + @ReactMethod + fun getIssuingApiVersion(promise: Promise) { + promise.resolve( + PushProvisioningProxy.getApiVersion() + ) + } + @ReactMethod fun initialise(params: ReadableMap, promise: Promise) { val publishableKey = getValOr(params, "publishableKey", null) as String diff --git a/example/src/screens/ApplePayScreen.tsx b/example/src/screens/ApplePayScreen.tsx index 7765a17ed..8855438dc 100644 --- a/example/src/screens/ApplePayScreen.tsx +++ b/example/src/screens/ApplePayScreen.tsx @@ -3,7 +3,7 @@ import { Alert, StyleSheet, Text, View } from 'react-native'; import { PlatformPay, AddToWalletButton, - Constants, + getIssuingApiVersion, canAddCardToWallet, PlatformPayButton, usePlatformPay, @@ -194,13 +194,14 @@ export default function ApplePayScreen() { }; const fetchEphemeralKey = async () => { + const issuingApiVersion = await getIssuingApiVersion(); const response = await fetch(`${API_URL}/ephemeral-key`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ - apiVersion: Constants.API_VERSIONS.ISSUING, + apiVersion: issuingApiVersion, issuingCardId: TEST_CARD_ID, }), }); diff --git a/example/src/screens/GooglePayScreen.tsx b/example/src/screens/GooglePayScreen.tsx index 56816858c..bdbc33d42 100644 --- a/example/src/screens/GooglePayScreen.tsx +++ b/example/src/screens/GooglePayScreen.tsx @@ -1,7 +1,6 @@ import React, { useEffect, useState } from 'react'; import { AddToWalletButton, - Constants, canAddCardToWallet, createPlatformPayPaymentMethod, createPlatformPayToken, @@ -10,6 +9,7 @@ import { isPlatformPaySupported, PlatformPay, PlatformPayButton, + getIssuingApiVersion, } from '@stripe/stripe-react-native'; import PaymentScreen from '../components/PaymentScreen'; import { API_URL } from '../Config'; @@ -183,13 +183,14 @@ export default function GooglePayScreen() { }; const fetchEphemeralKey = async () => { + const issuingApiVersion = await getIssuingApiVersion(); const response = await fetch(`${API_URL}/ephemeral-key`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ - apiVersion: Constants.API_VERSIONS.ISSUING, + apiVersion: issuingApiVersion, issuingCardId: LIVE_CARD_ID, }), }); diff --git a/ios/StripeSdk.m b/ios/StripeSdk.m index 99af1da76..c3e36bea1 100644 --- a/ios/StripeSdk.m +++ b/ios/StripeSdk.m @@ -163,6 +163,10 @@ @interface RCT_EXTERN_MODULE(StripeSdk, RCTEventEmitter) resolver: (RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject ) +RCT_EXTERN_METHOD( + getIssuingApiVersion:(RCTPromiseResolveBlock)resolve + rejecter: (RCTPromiseRejectBlock)reject + ) RCT_EXTERN_METHOD( isCardInWallet:(NSDictionary *)params resolver: (RCTPromiseResolveBlock)resolve diff --git a/ios/StripeSdk.swift b/ios/StripeSdk.swift index b396be9e0..e6f79eda3 100644 --- a/ios/StripeSdk.swift +++ b/ios/StripeSdk.swift @@ -81,10 +81,14 @@ class StripeSdk: RCTEventEmitter, STPBankSelectionViewControllerDelegate, UIAdap return [ "API_VERSIONS": [ "CORE": STPAPIClient.apiVersion, - "ISSUING": STPAPIClient.apiVersion, ] ] } + + @objc(getIssuingApiVersion:rejecter:) + func getIssuingApiVersion(resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void { + resolve(STPAPIClient.apiVersion) + } @objc(initialise:resolver:rejecter:) func initialise(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void { diff --git a/jest/mock.js b/jest/mock.js index 7858e10c2..6cf8578b8 100644 --- a/jest/mock.js +++ b/jest/mock.js @@ -108,6 +108,7 @@ const mockFunctions = { details: null, error: null, })), + getIssuingApiVersion: jest.fn(async () => ''), isCardInWallet: jest.fn(async () => ({ isInWallet: false, token: {}, diff --git a/src/NativeStripeSdk.tsx b/src/NativeStripeSdk.tsx index f1eedd590..a547b4a9e 100644 --- a/src/NativeStripeSdk.tsx +++ b/src/NativeStripeSdk.tsx @@ -86,10 +86,11 @@ type NativeStripeSdkType = { clientSecret: string, params: PaymentMethod.CollectBankAccountParams ): Promise; - getConstants(): { API_VERSIONS: { CORE: string; ISSUING: string } }; + getConstants(): { API_VERSIONS: { CORE: string } }; canAddCardToWallet( params: CanAddCardToWalletParams ): Promise; + getIssuingApiVersion(): Promise; isCardInWallet(params: { cardLastFour: string; }): Promise; diff --git a/src/functions.ts b/src/functions.ts index b39da179a..4192dc2da 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -602,6 +602,15 @@ export const canAddCardToWallet = async ( } }; +/** + * Get the supported Stripe API version for usage on your server with Issuing-related API calls. + * @returns A string representing the supported API version, or an empty string if + * the required push provisioning dependencies are not found. + */ +export const getIssuingApiVersion = async (): Promise => { + return await NativeStripeSdk.getIssuingApiVersion(); +}; + /** @deprecated Please use `canAddCardToWallet` instead. */ export const isCardInWallet = async (params: { cardLastFour: string; diff --git a/src/hooks/usePlatformPay.tsx b/src/hooks/usePlatformPay.tsx index 96f81b7de..a8fb1091f 100644 --- a/src/hooks/usePlatformPay.tsx +++ b/src/hooks/usePlatformPay.tsx @@ -19,6 +19,7 @@ export function usePlatformPay() { dismissPlatformPay, updatePlatformPaySheet, canAddCardToWallet, + getIssuingApiVersion, openPlatformPaySetup, } = useStripe(); const [loading, setLoading] = useState(false); @@ -124,6 +125,15 @@ export function usePlatformPay() { [canAddCardToWallet] ); + const _getIssuingApiVersion = useCallback(async (): Promise => { + setLoading(true); + + const result = await getIssuingApiVersion(); + setLoading(false); + + return result; + }, [getIssuingApiVersion]); + const _openPlatformPaySetup = useCallback(async (): Promise => { return openPlatformPaySetup(); }, [openPlatformPaySetup]); @@ -183,6 +193,12 @@ export function usePlatformPay() { * @returns A promise resolving to an object of type CanAddCardToWalletResult. Check the `canAddCard` field, if it's true, you should show the `` */ canAddCardToWallet: _canAddCardToWallet, + /** + * Get the supported Stripe API version for usage on your server with Issuing-related API calls. + * @returns A string representing the supported API version, or an empty string if + * the required push provisioning dependencies are not found. + */ + getIssuingApiVersion: _getIssuingApiVersion, /** * iOS only, this is a no-op on Android. Use this method to move users to the interface for adding credit cards. * This method transfers control to the Wallet app on iPhone or to the Settings diff --git a/src/hooks/useStripe.tsx b/src/hooks/useStripe.tsx index 4530fd8c4..fe8a30f8d 100644 --- a/src/hooks/useStripe.tsx +++ b/src/hooks/useStripe.tsx @@ -48,6 +48,7 @@ import { verifyMicrodepositsForPayment, verifyMicrodepositsForSetup, canAddCardToWallet, + getIssuingApiVersion, collectBankAccountToken, collectFinancialConnectionsAccounts, resetPaymentSheetCustomer, @@ -224,6 +225,10 @@ export function useStripe() { [] ); + const _getIssuingApiVersion = useCallback(async (): Promise => { + return getIssuingApiVersion(); + }, []); + const _collectBankAccountToken = useCallback( async (clientSecret: string): Promise => { return collectBankAccountToken(clientSecret); @@ -333,6 +338,7 @@ export function useStripe() { verifyMicrodepositsForPayment: _verifyMicrodepositsForPayment, verifyMicrodepositsForSetup: _verifyMicrodepositsForSetup, canAddCardToWallet: _canAddCardToWallet, + getIssuingApiVersion: _getIssuingApiVersion, collectBankAccountToken: _collectBankAccountToken, collectFinancialConnectionsAccounts: _collectFinancialConnectionsAccounts, /** From e6a85ed99881b6a61dfa741fc29cf63f7aae5b44 Mon Sep 17 00:00:00 2001 From: Charlie Cruzan Date: Thu, 14 Sep 2023 07:29:04 -1000 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a54494ae5..1a59fd6bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ **Breaking changes** -- `Constants.API_VERSIONS.ISSUING` has been removed, please use the asynchronous `getIssuingApiVersion` method instead. This should improve app startup time. +- `Constants.API_VERSIONS.ISSUING` has been removed, please use the asynchronous `getIssuingApiVersion` method instead. This should improve app startup time. [#1502](https://github.com/stripe/stripe-react-native/pull/1502) **Features**