Skip to content

Commit

Permalink
Add support in
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Feb 7, 2025
1 parent 406127f commit 1aac014
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class FinancialConnectionsSheetFragment : Fragment() {
private lateinit var configuration: FinancialConnectionsSheet.Configuration
private lateinit var mode: Mode

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val stripeSdkModule: StripeSdkModule? = context.getNativeModule(StripeSdkModule::class.java)
if (stripeSdkModule != null && stripeSdkModule.eventListenerCount > 0) {
FinancialConnections.setEventListener { event ->
val params = mapFromFinancialConnectionsEvent(event)
stripeSdkModule.sendEvent(context, "onFinancialConnectionsEvent", params)
}
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
return FrameLayout(requireActivity()).also {
Expand Down Expand Up @@ -55,6 +67,13 @@ class FinancialConnectionsSheetFragment : Fragment() {
}
}

override fun onDestroy() {
super.onDestroy()

// Remove any event listener that might be set
FinancialConnections.clearEventListener()
}

private fun onFinancialConnectionsSheetForTokenResult(result: FinancialConnectionsSheetForTokenResult) {
when(result) {
is FinancialConnectionsSheetForTokenResult.Canceled -> {
Expand Down
5 changes: 4 additions & 1 deletion ios/FinancialConnections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ class FinancialConnections {
internal static func present(
withClientSecret: String,
returnURL: String? = nil,
onEvent: ((FinancialConnectionsEvent) -> Void)? = nil,
resolve: @escaping RCTPromiseResolveBlock
) -> Void {
DispatchQueue.main.async {
FinancialConnectionsSheet(financialConnectionsSessionClientSecret: withClientSecret, returnURL: returnURL).present(
from: findViewControllerPresenter(from: UIApplication.shared.delegate?.window??.rootViewController ?? UIViewController()),
from: findViewControllerPresenter(from: UIApplication.shared.delegate?.window??.rootViewController ?? UIViewController()
onEvent: onEvent
),
completion: { result in
switch result {
case .completed(session: let session):
Expand Down
10 changes: 8 additions & 2 deletions ios/StripeSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ class StripeSdk: RCTEventEmitter, UIAdaptivePresentationControllerDelegate {
} else {
connectionsReturnURL = nil
}

let onEvent: (FinancialConnectionsEvent) -> Void = { [weak self] event in
let mappedEvent = Mappers.financialConnectionsEventToMap(event)
self?.sendEvent(withName: "onFinancialConnectionsEvent", body: mappedEvent)
Expand Down Expand Up @@ -1068,7 +1068,13 @@ class StripeSdk: RCTEventEmitter, UIAdaptivePresentationControllerDelegate {
} else {
returnURL = nil
}
FinancialConnections.present(withClientSecret: clientSecret, returnURL: returnURL, resolve: resolve)

let onEvent: (FinancialConnectionsEvent) -> Void = { [weak self] event in
let mappedEvent = Mappers.financialConnectionsEventToMap(event)
self?.sendEvent(withName: "onFinancialConnectionsEvent", body: mappedEvent)
}

FinancialConnections.present(withClientSecret: clientSecret, returnURL: returnURL, onEvent: onEvent, resolve: resolve)
}

@objc(configureOrderTracking:orderIdentifier:webServiceUrl:authenticationToken:resolver:rejecter:)
Expand Down
10 changes: 9 additions & 1 deletion src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
NativeModules,
EmitterSubscription,
} from 'react-native';
import { FinancialConnectionsEvent } from './types/FinancialConnections';

export const createPaymentMethod = async (
params: PaymentMethod.CreateParams,
Expand Down Expand Up @@ -566,12 +567,18 @@ export const collectBankAccountToken = async (
* @returns A promise that resolves to an object containing either a `session` field, or an error field.
*/
export const collectFinancialConnectionsAccounts = async (
clientSecret: string
clientSecret: string,
onEvent?: (event: FinancialConnectionsEvent) => void
): Promise<FinancialConnections.SessionResult> => {
const subscription =
onEvent && eventEmitter.addListener('onFinancialConnectionsEvent', onEvent);

try {
const { session, error } =
await NativeStripeSdk.collectFinancialConnectionsAccounts(clientSecret);

subscription?.remove();

if (error) {
return {
error,
Expand All @@ -581,6 +588,7 @@ export const collectFinancialConnectionsAccounts = async (
session: session!,
};
} catch (error: any) {
subscription?.remove();
return {
error: createError(error),
};
Expand Down

0 comments on commit 1aac014

Please sign in to comment.