framepayments-react-native 1.0.0
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/LICENSE +17 -0
- package/README.md +135 -0
- package/android/build.gradle +46 -0
- package/android/src/main/AndroidManifest.xml +12 -0
- package/android/src/main/java/com/framepayments/reactnativeframe/FrameCheckoutActivity.kt +40 -0
- package/android/src/main/java/com/framepayments/reactnativeframe/FrameFlowActivity.kt +84 -0
- package/android/src/main/java/com/framepayments/reactnativeframe/FrameSDKModule.kt +175 -0
- package/android/src/main/java/com/framepayments/reactnativeframe/FrameSDKPackage.kt +16 -0
- package/ios/FrameReactNative.podspec +26 -0
- package/ios/FrameSDKBridge.m +26 -0
- package/ios/FrameSDKBridge.swift +161 -0
- package/lib/errors.d.ts +25 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +40 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +9 -0
- package/lib/native.d.ts +18 -0
- package/lib/native.d.ts.map +1 -0
- package/lib/native.js +49 -0
- package/lib/types.d.ts +36 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +5 -0
- package/package.json +64 -0
- package/src/__tests__/native.test.ts +116 -0
- package/src/errors.ts +53 -0
- package/src/index.ts +12 -0
- package/src/native.ts +78 -0
- package/src/types.ts +38 -0
package/src/native.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native module bridge. Uses NativeModules for classic React Native bridge.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { NativeModules } from 'react-native';
|
|
6
|
+
import type { ChargeIntent, FrameCartItem } from './types';
|
|
7
|
+
import { ErrorCodes } from './errors';
|
|
8
|
+
|
|
9
|
+
const LINKING_ERROR =
|
|
10
|
+
`The package '@framepayments/react-native-frame' doesn't seem to be linked. Make sure you have run 'pod install' (iOS) or rebuilt the app (Android).`;
|
|
11
|
+
|
|
12
|
+
const FrameSDK = NativeModules.FrameSDK
|
|
13
|
+
? NativeModules.FrameSDK
|
|
14
|
+
: new Proxy(
|
|
15
|
+
{},
|
|
16
|
+
{
|
|
17
|
+
get() {
|
|
18
|
+
throw new Error(LINKING_ERROR);
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
let isInitialized = false;
|
|
24
|
+
|
|
25
|
+
export function initialize(options: { apiKey: string; debugMode?: boolean }): void {
|
|
26
|
+
if (!options?.apiKey) {
|
|
27
|
+
throw new Error('Frame.initialize requires apiKey');
|
|
28
|
+
}
|
|
29
|
+
FrameSDK.initialize(options.apiKey, options.debugMode ?? false);
|
|
30
|
+
isInitialized = true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function guardInitialized(): void {
|
|
34
|
+
if (!isInitialized) {
|
|
35
|
+
const message =
|
|
36
|
+
'Frame SDK must be initialized before calling presentCheckout or presentCart. Call Frame.initialize({ apiKey }) first.';
|
|
37
|
+
const err = new Error(message) as Error & { code: string };
|
|
38
|
+
err.code = ErrorCodes.NOT_INITIALIZED;
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function wrapPromise<T>(p: Promise<T>): Promise<T> {
|
|
44
|
+
return p.catch((err) => {
|
|
45
|
+
let code = 'UNKNOWN_ERROR';
|
|
46
|
+
let message = String(err?.message ?? err);
|
|
47
|
+
if (err?.code) code = err.code;
|
|
48
|
+
if (typeof err === 'object' && err !== null && 'message' in err) {
|
|
49
|
+
message = String((err as { message: string }).message);
|
|
50
|
+
}
|
|
51
|
+
throw Object.assign(new Error(message), { code, message });
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function presentCheckout(options: {
|
|
56
|
+
customerId?: string | null;
|
|
57
|
+
amount: number;
|
|
58
|
+
}): Promise<ChargeIntent> {
|
|
59
|
+
guardInitialized();
|
|
60
|
+
return wrapPromise(
|
|
61
|
+
FrameSDK.presentCheckout(options.customerId ?? null, options.amount)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function presentCart(options: {
|
|
66
|
+
customerId?: string | null;
|
|
67
|
+
items: FrameCartItem[];
|
|
68
|
+
shippingAmountInCents: number;
|
|
69
|
+
}): Promise<ChargeIntent> {
|
|
70
|
+
guardInitialized();
|
|
71
|
+
return wrapPromise(
|
|
72
|
+
FrameSDK.presentCart(
|
|
73
|
+
options.customerId ?? null,
|
|
74
|
+
options.items,
|
|
75
|
+
options.shippingAmountInCents
|
|
76
|
+
)
|
|
77
|
+
);
|
|
78
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the Frame React Native SDK modal APIs.
|
|
3
|
+
* For other types (Customer, Refund, etc.), use the framepayments (frame-node) package when calling APIs from JS.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Cart item for presentCart({ items }) */
|
|
7
|
+
export interface FrameCartItem {
|
|
8
|
+
id: string;
|
|
9
|
+
title: string;
|
|
10
|
+
amountInCents: number;
|
|
11
|
+
imageUrl: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Charge intent returned from presentCheckout / presentCart */
|
|
15
|
+
export interface ChargeIntent {
|
|
16
|
+
id: string;
|
|
17
|
+
currency: string;
|
|
18
|
+
amount: number;
|
|
19
|
+
status: string;
|
|
20
|
+
created: number;
|
|
21
|
+
updated: number;
|
|
22
|
+
livemode: boolean;
|
|
23
|
+
object: string;
|
|
24
|
+
description?: string;
|
|
25
|
+
customer?: Record<string, unknown>;
|
|
26
|
+
payment_method?: Record<string, unknown>;
|
|
27
|
+
latest_charge?: Record<string, unknown>;
|
|
28
|
+
authorization_mode?: string;
|
|
29
|
+
failure_description?: string;
|
|
30
|
+
shipping?: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Error shape when native module rejects (same as FrameErrorShape from errors.ts) */
|
|
34
|
+
export interface FrameError {
|
|
35
|
+
code: string;
|
|
36
|
+
message: string;
|
|
37
|
+
nativeError?: string;
|
|
38
|
+
}
|