payme-mini-app 3.0.0 → 3.0.2

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/src/index.tsx CHANGED
@@ -1,13 +1,79 @@
1
- import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
1
+ import {
2
+ NativeEventEmitter,
3
+ NativeModules,
4
+ Platform,
5
+ type EmitterSubscription,
6
+ } from 'react-native';
2
7
  import type {
3
8
  PayMEEnv,
9
+ PayMEErrorEvent,
4
10
  PayMELocale,
5
11
  PayMEMode,
6
12
  PayMEOpeningParam,
7
13
  PayMEOpeningType,
14
+ PayMEResponseEvent,
8
15
  } from './index.type';
9
16
 
10
- const RNPayMEMiniApp = NativeModules.PaymeMiniApp;
17
+ // export type tường minh (tương thích TS < 5.0, không dùng `export type *`).
18
+ export type {
19
+ PayMEEnv,
20
+ PayMEErrorEvent,
21
+ PayMELocale,
22
+ PayMEMode,
23
+ PayMEOpeningAction,
24
+ PayMEOpeningParam,
25
+ PayMEOpeningType,
26
+ PayMEPaymentDirectParam,
27
+ PayMEPaymentParam,
28
+ PayMEPayParam,
29
+ PayMEResponseEvent,
30
+ PayMEServiceParam,
31
+ PayMETransferQRParam,
32
+ } from './index.type';
33
+
34
+ const LINKING_ERROR =
35
+ "payme-mini-app: native module chưa được link. iOS: chạy 'pod install'; " +
36
+ 'build lại app sau khi cài (không chạy được trong Expo Go).';
37
+
38
+ // Chưa link native: báo lỗi rõ ràng khi dùng thay vì crash lúc import.
39
+ const RNPayMEMiniApp =
40
+ NativeModules.PaymeMiniApp ??
41
+ new Proxy(
42
+ {},
43
+ {
44
+ get() {
45
+ return () => {
46
+ throw new Error(LINKING_ERROR);
47
+ };
48
+ },
49
+ }
50
+ );
51
+
52
+ /** Mã lỗi do bridge hoặc SDK tạo; SDK còn trả các mã khác (vd `401`). */
53
+ export const PayMEErrorCode = {
54
+ /** Gọi hàm khi chưa init (Android: hoặc open khi chưa có Activity). */
55
+ NOT_INITIALIZED: 'PAYME_NOT_INITIALIZED',
56
+ /** Thiếu/sai data hoặc action không hỗ trợ. */
57
+ INVALID_PARAM: 'INVALID_PARAM',
58
+ /** identityToken rỗng ở action bắt buộc. */
59
+ IDENTITY_TOKEN_REQUIRED: 'IDENTITY_TOKEN_REQUIRED',
60
+ /** Người dùng đóng miniapp (iOS: cả khi app gọi close(); Android: close() không báo). */
61
+ USER_CANCEL: 'USER_CANCEL',
62
+ /** iOS: SDK đóng miniapp vì không khởi động được (vd web server lỗi). */
63
+ SYSTEM_CANCEL: 'SYSTEM_CANCEL',
64
+ /** open khi đang có miniapp mở: chỉ mở một miniapp mỗi lúc, phiên đang mở giữ nguyên. */
65
+ MINIAPP_ALREADY_OPEN: 'MINIAPP_ALREADY_OPEN',
66
+ /** Không hiện được miniapp (iOS: không có VC/navigation để present; Android: modal không có FragmentActivity, show lỗi). */
67
+ MINIAPP_CANNOT_PRESENT: 'MINIAPP_CANNOT_PRESENT',
68
+ /** Android: SDK ném lỗi ngoài dự kiến (description là tên class exception). */
69
+ SDK_ERROR: 'PAYME_SDK_ERROR',
70
+ } as const;
71
+
72
+ // Native khai báo String/Map không null: JS truyền sai kiểu (null, số...) sẽ crash app,
73
+ // nên chuẩn hoá trước khi gọi native.
74
+ function str(value: unknown, fallback = ''): string {
75
+ return typeof value === 'string' ? value : fallback;
76
+ }
11
77
 
12
78
  export function init(
13
79
  appId: string,
@@ -15,42 +81,72 @@ export function init(
15
81
  privateKey: string,
16
82
  locale: PayMELocale = 'vi',
17
83
  env: PayMEEnv = 'PRODUCTION',
18
- mode: PayMEMode = 'miniapp_product'
84
+ mode?: PayMEMode
19
85
  ) {
86
+ const envValue = str(env, 'PRODUCTION');
87
+ // Không truyền mode: theo env (SANDBOX → bundle sandbox), tránh dùng bundle product cho môi trường sandbox.
88
+ const defaultMode =
89
+ envValue === 'SANDBOX' ? 'miniapp_sandbox' : 'miniapp_product';
20
90
  return RNPayMEMiniApp.initPayME(
21
- appId,
22
- publicKey,
23
- privateKey,
24
- locale,
25
- env,
26
- mode
91
+ str(appId),
92
+ str(publicKey),
93
+ str(privateKey),
94
+ str(locale, 'vi'),
95
+ envValue,
96
+ str(mode, '') || defaultMode
27
97
  );
28
98
  }
29
99
 
30
100
  export function open(type: PayMEOpeningType, openingParam: PayMEOpeningParam) {
31
- return RNPayMEMiniApp.open(type, openingParam);
101
+ // Param không phải object: native báo onError INVALID_PARAM.
102
+ const param =
103
+ typeof openingParam === 'object' &&
104
+ openingParam !== null &&
105
+ !Array.isArray(openingParam)
106
+ ? openingParam
107
+ : {};
108
+ return RNPayMEMiniApp.open(str(type, 'screen'), param);
32
109
  }
33
110
 
34
111
  /** identityToken sai hoặc hết hạn: onError trả code `401`; rỗng: `IDENTITY_TOKEN_REQUIRED`. */
35
112
  export function getBalance(identityToken: string) {
36
- return RNPayMEMiniApp.getBalance(identityToken);
113
+ return RNPayMEMiniApp.getBalance(str(identityToken));
37
114
  }
38
115
 
39
116
  /** identityToken sai hoặc hết hạn: onError trả code `401`; rỗng: `IDENTITY_TOKEN_REQUIRED`. */
40
117
  export function getAccountInfo(identityToken: string) {
41
- return RNPayMEMiniApp.getAccountInfo(identityToken);
118
+ return RNPayMEMiniApp.getAccountInfo(str(identityToken));
42
119
  }
43
120
 
121
+ /** 'en' hoặc 'vi'; giá trị khác được coi là 'vi'. */
44
122
  export function setLanguage(language: string) {
45
- return RNPayMEMiniApp.setLanguage(language);
123
+ return RNPayMEMiniApp.setLanguage(str(language, 'vi'));
46
124
  }
47
125
 
48
126
  export function close() {
49
127
  return RNPayMEMiniApp.closeMiniApp();
50
128
  }
51
129
 
52
- // Updated for React Native 0.78.0: Instantiate NativeEventEmitter without passing the native module
130
+ // iOS cần truyền module (RCTEventEmitter); Android nhận event qua DeviceEventEmitter.
53
131
  export const PayMEMiniAppEvents =
54
132
  Platform.OS === 'ios'
55
133
  ? new NativeEventEmitter(RNPayMEMiniApp)
56
134
  : new NativeEventEmitter();
135
+
136
+ /** Lắng nghe `onError` có kiểu; gọi `.remove()` để huỷ. */
137
+ export function addErrorListener(
138
+ listener: (event: PayMEErrorEvent) => void
139
+ ): EmitterSubscription {
140
+ return PayMEMiniAppEvents.addListener('onError', (event) =>
141
+ listener(event as PayMEErrorEvent)
142
+ );
143
+ }
144
+
145
+ /** Lắng nghe `onResponse` có kiểu; gọi `.remove()` để huỷ. */
146
+ export function addResponseListener(
147
+ listener: (event: PayMEResponseEvent) => void
148
+ ): EmitterSubscription {
149
+ return PayMEMiniAppEvents.addListener('onResponse', (event) =>
150
+ listener(event as PayMEResponseEvent)
151
+ );
152
+ }
package/src/index.type.ts CHANGED
@@ -1,19 +1,20 @@
1
- type PayMEPaymentParam = {
1
+ export type PayMEPaymentParam = {
2
2
  amount: number;
3
3
  transactionId: string;
4
4
  note?: string;
5
- ipnUrl: string;
5
+ /** Tuỳ chọn (native SDK không bắt buộc). */
6
+ ipnUrl?: string;
6
7
  extraData?: Record<string, string | object>;
7
8
  isShowResult?: boolean;
8
9
  };
9
10
 
10
- type PayMEPaymentDirectParam = {
11
+ export type PayMEPaymentDirectParam = {
11
12
  transaction: string;
12
13
  extraData?: Record<string, string | object>;
13
14
  isShowResult?: boolean;
14
15
  };
15
16
 
16
- type PayMETransferQRParam = {
17
+ export type PayMETransferQRParam = {
17
18
  amount: number;
18
19
  bankNumber: string;
19
20
  swiftCode: string;
@@ -24,14 +25,14 @@ type PayMETransferQRParam = {
24
25
  isShowResult?: boolean;
25
26
  };
26
27
 
27
- type PayMEServiceParam = {
28
+ export type PayMEServiceParam = {
28
29
  service: string;
29
30
  extraData?: Record<string, string | object>;
30
31
  isBackToApp?: boolean;
31
32
  isShowResult?: boolean;
32
33
  };
33
34
 
34
- type PayMEPayParam = {
35
+ export type PayMEPayParam = {
35
36
  amount?: number;
36
37
  description?: string;
37
38
  extraData?: Record<string, string | object>;
@@ -63,3 +64,23 @@ export type PayMEMode = 'miniapp_sandbox' | 'miniapp_product';
63
64
  export type PayMEOpeningType = 'screen' | 'modal';
64
65
 
65
66
  export type PayMELocale = 'vi' | 'en';
67
+
68
+ /** Action của PayMEOpeningParam. */
69
+ export type PayMEOpeningAction = PayMEOpeningParam['action'];
70
+
71
+ /**
72
+ * Payload sự kiện `onError`.
73
+ * action: action của open, hoặc `GET_BALANCE`, `GET_ACCOUNT_INFO`, `SET_LANGUAGE`, `CLOSE`, `INIT` (Android)
74
+ * (giá trị do SDK trả có thể khác nhau giữa iOS và Android).
75
+ */
76
+ export type PayMEErrorEvent = {
77
+ action: string;
78
+ code: string;
79
+ description: string;
80
+ };
81
+
82
+ /** Payload sự kiện `onResponse`: `action` cùng các field SDK trả về. */
83
+ export type PayMEResponseEvent = {
84
+ action: string;
85
+ [key: string]: unknown;
86
+ };