@juspay-tech/react-native-hyperswitch 1.3.3 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/hyperswitchSDK/Core/NativeModule/RNHeadlessManager.swift +13 -7
- package/lib/commonjs/index.bundle.js +2 -2
- package/lib/commonjs/index.bundle.js.map +1 -1
- package/lib/module/index.bundle.js +2 -2
- package/lib/module/index.bundle.js.map +1 -1
- package/lib/typescript/src/views/ApplePayButton.d.ts +3 -1
- package/lib/typescript/src/views/ApplePayButton.d.ts.map +1 -1
- package/lib/typescript/src/views/GooglePayButton.d.ts +3 -1
- package/lib/typescript/src/views/GooglePayButton.d.ts.map +1 -1
- package/lib/typescript/src/views/useWalletReady.d.ts +22 -0
- package/lib/typescript/src/views/useWalletReady.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/views/ApplePayButton.tsx +13 -0
- package/src/views/GooglePayButton.tsx +13 -0
- package/src/views/useWalletReady.ts +109 -0
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from 'react-native';
|
|
8
8
|
import type { ViewProps, ViewStyle } from 'react-native';
|
|
9
9
|
import { useWalletLaunch } from './useWalletLaunch';
|
|
10
|
+
import { useWalletReady } from './useWalletReady';
|
|
10
11
|
import { resolveApplePayProps } from './walletButtonConfig';
|
|
11
12
|
import type { PaymentSession } from '../types/definitions';
|
|
12
13
|
import type { PaymentResult } from '../types/paymentresult';
|
|
@@ -46,6 +47,8 @@ export type ApplePayButtonProps = {
|
|
|
46
47
|
options?: PaymentSheetConfiguration;
|
|
47
48
|
session?: PaymentSession;
|
|
48
49
|
onPaymentResult?: (result: PaymentResult) => void;
|
|
50
|
+
onReady?: () => void;
|
|
51
|
+
onLoadError?: (error: PaymentResult) => void;
|
|
49
52
|
disabled?: boolean;
|
|
50
53
|
style?: ViewStyle;
|
|
51
54
|
};
|
|
@@ -54,6 +57,8 @@ export function ApplePayButton({
|
|
|
54
57
|
options,
|
|
55
58
|
session,
|
|
56
59
|
onPaymentResult,
|
|
60
|
+
onReady,
|
|
61
|
+
onLoadError,
|
|
57
62
|
disabled = false,
|
|
58
63
|
style,
|
|
59
64
|
}: ApplePayButtonProps) {
|
|
@@ -65,6 +70,14 @@ export function ApplePayButton({
|
|
|
65
70
|
onPaymentResult,
|
|
66
71
|
});
|
|
67
72
|
|
|
73
|
+
useWalletReady({
|
|
74
|
+
wallet: 'apple_pay',
|
|
75
|
+
label: 'Apple Pay',
|
|
76
|
+
session,
|
|
77
|
+
onReady,
|
|
78
|
+
onLoadError,
|
|
79
|
+
});
|
|
80
|
+
|
|
68
81
|
const { hidden, type, buttonStyle, borderRadius } = resolveApplePayProps(
|
|
69
82
|
options,
|
|
70
83
|
colorScheme === 'dark' ? 'dark' : 'light'
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from 'react-native';
|
|
8
8
|
import type { ViewProps, ViewStyle } from 'react-native';
|
|
9
9
|
import { useWalletLaunch } from './useWalletLaunch';
|
|
10
|
+
import { useWalletReady } from './useWalletReady';
|
|
10
11
|
import { resolveGooglePayProps } from './walletButtonConfig';
|
|
11
12
|
import type { PaymentSession } from '../types/definitions';
|
|
12
13
|
import type { PaymentResult } from '../types/paymentresult';
|
|
@@ -45,6 +46,8 @@ export type GooglePayButtonProps = {
|
|
|
45
46
|
options?: PaymentSheetConfiguration;
|
|
46
47
|
session?: PaymentSession;
|
|
47
48
|
onPaymentResult?: (result: PaymentResult) => void;
|
|
49
|
+
onReady?: () => void;
|
|
50
|
+
onLoadError?: (error: PaymentResult) => void;
|
|
48
51
|
style?: ViewStyle;
|
|
49
52
|
};
|
|
50
53
|
|
|
@@ -52,6 +55,8 @@ export function GooglePayButton({
|
|
|
52
55
|
options,
|
|
53
56
|
session,
|
|
54
57
|
onPaymentResult,
|
|
58
|
+
onReady,
|
|
59
|
+
onLoadError,
|
|
55
60
|
style,
|
|
56
61
|
}: GooglePayButtonProps) {
|
|
57
62
|
const colorScheme = useColorScheme();
|
|
@@ -62,6 +67,14 @@ export function GooglePayButton({
|
|
|
62
67
|
onPaymentResult,
|
|
63
68
|
});
|
|
64
69
|
|
|
70
|
+
useWalletReady({
|
|
71
|
+
wallet: 'google_pay',
|
|
72
|
+
label: 'Google Pay',
|
|
73
|
+
session,
|
|
74
|
+
onReady,
|
|
75
|
+
onLoadError,
|
|
76
|
+
});
|
|
77
|
+
|
|
65
78
|
const { hidden, type, appearance, borderRadius } = resolveGooglePayProps(
|
|
66
79
|
options,
|
|
67
80
|
colorScheme === 'dark' ? 'dark' : 'light'
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
useOptionalPaymentSession,
|
|
4
|
+
useOptionalWalletSession,
|
|
5
|
+
} from '../context/HyperElements';
|
|
6
|
+
import type { PaymentSession } from '../types/definitions';
|
|
7
|
+
import type { PaymentResult } from '../types/paymentresult';
|
|
8
|
+
import type { WalletName, WalletSessionHandle } from '../types/walletSession';
|
|
9
|
+
|
|
10
|
+
export type WalletReadyOptions = {
|
|
11
|
+
wallet: WalletName;
|
|
12
|
+
label: string;
|
|
13
|
+
session?: PaymentSession;
|
|
14
|
+
onReady?: () => void;
|
|
15
|
+
onLoadError?: (error: PaymentResult) => void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolves wallet eligibility for a button, then calls `onReady` once when the
|
|
20
|
+
* wallet is pressable or `onLoadError` once when it is not. Exactly one of them
|
|
21
|
+
* fires per intent. The button owns this — merchants never load the wallet
|
|
22
|
+
* session themselves.
|
|
23
|
+
*
|
|
24
|
+
* Inside `<HyperElements>` the shared session is reused, so several buttons
|
|
25
|
+
* resolve off one round trip. With an explicit `session` prop the button
|
|
26
|
+
* resolves against that session directly.
|
|
27
|
+
*/
|
|
28
|
+
export function useWalletReady({
|
|
29
|
+
wallet,
|
|
30
|
+
label,
|
|
31
|
+
session,
|
|
32
|
+
onReady,
|
|
33
|
+
onLoadError,
|
|
34
|
+
}: WalletReadyOptions): void {
|
|
35
|
+
const contextSession = useOptionalPaymentSession();
|
|
36
|
+
const { walletSession, load } = useOptionalWalletSession();
|
|
37
|
+
|
|
38
|
+
const onReadyRef = useRef(onReady);
|
|
39
|
+
const onLoadErrorRef = useRef(onLoadError);
|
|
40
|
+
const walletSessionRef = useRef(walletSession);
|
|
41
|
+
const loadRef = useRef(load);
|
|
42
|
+
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
onReadyRef.current = onReady;
|
|
45
|
+
onLoadErrorRef.current = onLoadError;
|
|
46
|
+
walletSessionRef.current = walletSession;
|
|
47
|
+
loadRef.current = load;
|
|
48
|
+
}, [onReady, onLoadError, walletSession, load]);
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
let cancelled = false;
|
|
52
|
+
|
|
53
|
+
const fail = (type: string, message: string) => {
|
|
54
|
+
onLoadErrorRef.current?.({ status: 'failed', type, message });
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const resolveWallets = async (): Promise<WalletSessionHandle | null> => {
|
|
58
|
+
if (session) {
|
|
59
|
+
return session.getWalletSession();
|
|
60
|
+
}
|
|
61
|
+
if (walletSessionRef.current) {
|
|
62
|
+
return walletSessionRef.current;
|
|
63
|
+
}
|
|
64
|
+
if (contextSession) {
|
|
65
|
+
return loadRef.current();
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const resolveEligibility = async () => {
|
|
71
|
+
try {
|
|
72
|
+
const wallets = await resolveWallets();
|
|
73
|
+
if (cancelled) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!wallets) {
|
|
78
|
+
fail(
|
|
79
|
+
'no_session',
|
|
80
|
+
`${label} has no payment session. Pass a "session" prop or render it inside <HyperElements>.`
|
|
81
|
+
);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const eligible = await wallets.isWalletEligible(wallet);
|
|
86
|
+
if (cancelled) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (eligible) {
|
|
91
|
+
onReadyRef.current?.();
|
|
92
|
+
} else {
|
|
93
|
+
fail('not_eligible', `${label} is not eligible for this payment.`);
|
|
94
|
+
}
|
|
95
|
+
} catch (e) {
|
|
96
|
+
if (cancelled) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
fail('launch_failed', e instanceof Error ? e.message : String(e));
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
void resolveEligibility();
|
|
104
|
+
|
|
105
|
+
return () => {
|
|
106
|
+
cancelled = true;
|
|
107
|
+
};
|
|
108
|
+
}, [session, contextSession, wallet, label]);
|
|
109
|
+
}
|