@apps-in-toss/native-modules 1.1.2 → 1.2.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.
@@ -1,16 +1,20 @@
1
+ import { noop } from 'es-toolkit';
1
2
  import { AppsInTossModule } from './AppsInTossModule';
2
3
  import { isMinVersionSupported } from './isMinVersionSupported';
4
+ import { INTERNAL__appBridgeHandler } from '../native-event-emitter/internal/appBridge';
3
5
 
4
- /**
5
- * @public
6
- * @category 인앱결제
7
- * @name IapCreateOneTimePurchaseOrderOptions
8
- * @description 인앱결제 1건을 요청할 때 필요한 정보예요.
9
- * @property {string} productId - 주문할 상품의 ID예요.
10
- */
11
- export interface IapCreateOneTimePurchaseOrderOptions {
12
- productId: string;
13
- }
6
+ type Sku =
7
+ | {
8
+ /**
9
+ * @deprecated `productId`는 더 이상 사용하지 않아요. 대신 `sku`를 사용해요.
10
+ */
11
+ productId: string;
12
+ sku?: string;
13
+ }
14
+ | {
15
+ productId?: never;
16
+ sku: string;
17
+ };
14
18
 
15
19
  /**
16
20
  * @public
@@ -35,15 +39,78 @@ export interface IapCreateOneTimePurchaseOrderResult {
35
39
  miniAppIconUrl: string | null;
36
40
  }
37
41
 
42
+ interface SuccessEvent {
43
+ type: 'success';
44
+ data: IapCreateOneTimePurchaseOrderResult;
45
+ }
46
+
47
+ interface PurchasedEvent {
48
+ type: 'purchased';
49
+ data: { orderId: string };
50
+ }
51
+
52
+ /**
53
+ * @public
54
+ * @category 인앱결제
55
+ * @name IapCreateOneTimePurchaseOrderOptions
56
+ * @property {Sku} options - 결제할 상품의 정보예요.
57
+ * @property {string} options.sku - 주문할 상품의 고유 ID예요.
58
+ * @property {(params: { orderId: string }) => boolean | Promise<boolean>} processProductGrant - 주문이 만들어진 뒤 실제로 상품을 지급할 때 호출해요. `orderId`를 받아서 지급 성공 여부를 `true` 또는 `Promise<true>`로 반환해요. 지급에 실패하면 `false`를 반환해요.
59
+ * @property {(event: SuccessEvent) => void | Promise<void>} onEvent - 결제가 성공했을 때 호출해요. `event.type`이 `'success'`이고, `event.data`에 `IapCreateOneTimePurchaseOrderResult`가 들어 있어요.
60
+ * @property {(error: unknown) => void | Promise<void>} onError - 결제 과정에서 에러가 발생했을 때 호출해요. 에러 객체를 받아서 로깅하거나 복구 절차를 실행할 수 있어요.
61
+ */
62
+ export interface IapCreateOneTimePurchaseOrderOptions {
63
+ options: Sku & { processProductGrant: (params: { orderId: string }) => boolean | Promise<boolean> };
64
+ onEvent: (event: SuccessEvent) => void | Promise<void>;
65
+ onError: (error: unknown) => void | Promise<void>;
66
+ }
67
+
68
+ interface IapRequestOneTimePurchaseOptions {
69
+ options: Sku;
70
+ onEvent: (event: PurchasedEvent | SuccessEvent) => void | Promise<void>;
71
+ onError: (error: unknown) => void | Promise<void>;
72
+ }
73
+
74
+ export function iapCreateOneTimePurchaseOrder(params: Sku) {
75
+ const sku = (params.sku ?? params.productId) as string;
76
+ return AppsInTossModule.iapCreateOneTimePurchaseOrder({ productId: sku });
77
+ }
78
+
79
+ export function processProductGrant(params: { orderId: string; isProductGranted: boolean }) {
80
+ return AppsInTossModule.processProductGrant({ orderId: params.orderId, isProductGranted: params.isProductGranted });
81
+ }
82
+
83
+ export function requestOneTimePurchase(params: IapRequestOneTimePurchaseOptions) {
84
+ const { options, onEvent, onError } = params;
85
+ const sku = (options.sku ?? options.productId) as string;
86
+
87
+ const unregisterCallbacks = INTERNAL__appBridgeHandler.invokeAppBridgeMethod(
88
+ 'requestOneTimePurchase',
89
+ { sku },
90
+ {
91
+ onPurchased: (params: { orderId: string }) => {
92
+ onEvent({ type: 'purchased', data: params });
93
+ },
94
+ onSuccess: (result: IapCreateOneTimePurchaseOrderResult) => {
95
+ onEvent({ type: 'success', data: result });
96
+ },
97
+ onError: (error: any) => {
98
+ onError(error);
99
+ },
100
+ }
101
+ );
102
+
103
+ return unregisterCallbacks;
104
+ }
105
+
38
106
  /**
39
107
  * @public
40
108
  * @category 인앱결제
41
- * @name iapCreateOneTimePurchaseOrder
109
+ * @name createOneTimePurchaseOrder
42
110
  * @description
43
111
  * 특정 인앱결제 주문서 페이지로 이동해요. 사용자가 상품 구매 버튼을 누르는 상황 등에 사용할 수 있어요. 사용자의 결제는 이동한 페이지에서 진행돼요. 만약 결제 중에 에러가 발생하면 에러 유형에 따라 에러 페이지로 이동해요.
44
112
  * @param {IapCreateOneTimePurchaseOrderOptions} params - 인앱결제를 생성할 때 필요한 정보예요.
45
- * @param {string} params.productId - 주문할 상품의 ID예요.
46
- * @returns {Promise<IapCreateOneTimePurchaseOrderResult | undefined>} 결제에 성공하면 결제 결과 객체를 반환해요. 앱 버전이 최소 지원 버전(안드로이드 5.219.0, iOS 5.219.0)보다 낮으면 인앱결제를 실행할 수 없어서 `undefined`를 반환해요.
113
+ * @returns {() => void} 앱브릿지 cleanup 함수를 반환해요. 인앱결제 기능이 끝나면 반드시 이 함수를 호출해서 리소스를 해제해야 해요.
47
114
  *
48
115
  * @throw {code: "INVALID_PRODUCT_ID"} - 유효하지 않은 상품 ID이거나, 해당 상품이 존재하지 않을 때 발생해요.
49
116
  * @throw {code: "PAYMENT_PENDING"} - 사용자가 요청한 결제가 아직 승인을 기다리고 있을 때 발생해요.
@@ -55,45 +122,104 @@ export interface IapCreateOneTimePurchaseOrderResult {
55
122
  * @throw {code: "INTERNAL_ERROR"} - 서버 내부 문제로 요청을 처리할 수 없을 때 발생해요.
56
123
  * @throw {code: "KOREAN_ACCOUNT_ONLY"} - iOS 환경에서 사용자의 계정이 한국 계정이 아닐 때 발생해요.
57
124
  * @throw {code: "USER_CANCELED"} - 사용자가 결제를 완료하지 않고 주문서 페이지를 이탈했을 때 발생해요.
125
+ * @throw {code: "PRODUCT_NOT_GRANTED_BY_PARTNER"} - 파트너사의 상품 지급이 실패했을 때 발생해요.
58
126
  *
59
127
  * @example
60
128
  * ### 특정 인앱결제 주문서 페이지로 이동하기
61
129
  *
62
130
  * ```tsx
63
131
  * import { IAP } from "@apps-in-toss/web-framework";
64
- * import { Button } from "@toss-design-system/react-native";
132
+ * import { Button } from "@toss/tds-react-native";
133
+ * import { useCallback } from "react";
65
134
  *
66
135
  * interface Props {
67
- * productId: string;
136
+ * sku: string;
68
137
  * }
69
138
  *
70
- * function IapCreateOneTimePurchaseOrderButton({ productId }: Props) {
71
- * async function handleClick() {
72
- * try {
73
- * await IAP.createOneTimePurchaseOrder({
74
- * productId,
75
- * });
76
- * console.error("인앱결제에 성공했어요");
77
- * } catch (error) {
78
- * console.error('인앱결제에 실패했어요:', error);
79
- * }
80
- * }
139
+ * function IapCreateOneTimePurchaseOrderButton({ sku }: Props) {
140
+ * const handleClick = useCallback(async () => {
141
+ *
142
+ * const cleanup = await IAP.createOneTimePurchaseOrder({
143
+ * options: {
144
+ * sku,
145
+ * processProductGrant: ({ orderId }) => {
146
+ * // 상품 지급 로직 작성
147
+ * return true; // 상품 지급 여부
148
+ * }
149
+ * },
150
+ * onEvent: (event) => {
151
+ * console.log(event);
152
+ * },
153
+ * onError: (error) => {
154
+ * console.error(error);
155
+ * },
156
+ * });
157
+ *
158
+ * return cleanup;
159
+ * }, []);
81
160
  *
82
161
  * return <Button onClick={handleClick}>구매하기</Button>;
83
162
  * }
84
163
  * ```
85
164
  */
86
- async function createOneTimePurchaseOrder(params: IapCreateOneTimePurchaseOrderOptions) {
87
- const isSupported = isMinVersionSupported({
165
+ function createOneTimePurchaseOrder(params: IapCreateOneTimePurchaseOrderOptions) {
166
+ const isIAPSupported = isMinVersionSupported({
88
167
  android: '5.219.0',
89
168
  ios: '5.219.0',
90
169
  });
91
170
 
92
- if (!isSupported) {
93
- return;
171
+ if (!isIAPSupported) {
172
+ return noop;
94
173
  }
95
174
 
96
- return AppsInTossModule.iapCreateOneTimePurchaseOrder(params);
175
+ const isProcessProductGrantSupported = isMinVersionSupported({
176
+ android: '5.231.1',
177
+ ios: '5.230.0',
178
+ });
179
+
180
+ const { options, onEvent, onError } = params;
181
+ const sku = (options.sku ?? options.productId) as string;
182
+
183
+ if (!isProcessProductGrantSupported) {
184
+ const v1 = () => {
185
+ AppsInTossModule.iapCreateOneTimePurchaseOrder({ productId: sku })
186
+ .then((response: IapCreateOneTimePurchaseOrderResult) => {
187
+ Promise.resolve(options.processProductGrant({ orderId: response.orderId }))
188
+ .then(() => {
189
+ onEvent({ type: 'success', data: response });
190
+ })
191
+ .catch((error: unknown) => {
192
+ onError(error);
193
+ });
194
+ })
195
+ .catch((error: unknown) => {
196
+ onError(error);
197
+ });
198
+
199
+ return noop;
200
+ };
201
+
202
+ return v1();
203
+ }
204
+
205
+ const unregisterCallbacks = INTERNAL__appBridgeHandler.invokeAppBridgeMethod(
206
+ 'requestOneTimePurchase',
207
+ { sku },
208
+ {
209
+ onPurchased: async (params: { orderId: string }) => {
210
+ const isProductGranted = await options.processProductGrant(params);
211
+ await AppsInTossModule.processProductGrant({ orderId: params.orderId, isProductGranted });
212
+ },
213
+ onSuccess: (result: IapCreateOneTimePurchaseOrderResult) => {
214
+ onEvent({ type: 'success', data: result });
215
+ },
216
+ onError: (error: unknown) => {
217
+ onError(error);
218
+ },
219
+ }
220
+ );
221
+
222
+ return unregisterCallbacks;
97
223
  }
98
224
 
99
225
  /**
@@ -101,7 +227,7 @@ async function createOneTimePurchaseOrder(params: IapCreateOneTimePurchaseOrderO
101
227
  * @category 인앱결제
102
228
  * @name IapProductListItem
103
229
  * @description 인앱결제로 구매할 수 있는 상품 하나의 정보를 담은 객체예요. 상품 목록을 화면에 표시할 때 사용해요.
104
- * @property {string} sku - 상품의 고유 ID예요. [IAP.createOneTimePurchaseOrder](https://developers-apps-in-toss.toss.im/bedrock/reference/native-modules/%EC%9D%B8%EC%95%B1%20%EA%B2%B0%EC%A0%9C/createOneTimePurchaseOrder.html)를 호출할때 사용하는 `productId`와 동일한 값이에요.
230
+ * @property {string} sku - 상품의 고유 ID예요.
105
231
  * @property {string} displayName - 화면에 표시할 상품 이름이에요. 상품 이름은 앱인토스 콘솔에서 설정한 값이에요.
106
232
  * @property {string} displayAmount - 통화 단위가 포함된 가격 정보예요. 예를 들어 `1,000원`으로 가격과 통화가 함께 표시돼요.
107
233
  * @property {string} iconUrl - 상품 아이콘 이미지의 URL이에요. 아이콘은 앱인토스 콘솔에서 설정한 이미지예요.
@@ -118,7 +244,7 @@ export interface IapProductListItem {
118
244
  /**
119
245
  * @public
120
246
  * @category 인앱결제
121
- * @name iapGetProductItemList
247
+ * @name getProductItemList
122
248
  * @description 인앱결제로 구매할 수 있는 상품 목록을 가져와요. 상품 목록 화면에 진입할 때 호출해요.
123
249
  * @returns {Promise<{ products: IapProductListItem[] } | undefined>} 상품 목록을 포함한 객체를 반환해요. 앱 버전이 최소 지원 버전(안드로이드 5.219.0, iOS 5.219.0)보다 낮으면 `undefined`를 반환해요.
124
250
  *
@@ -127,7 +253,7 @@ export interface IapProductListItem {
127
253
  *
128
254
  * ```tsx
129
255
  * import { IAP, IapProductListItem } from "@apps-in-toss/framework";
130
- * import { Button, List, ListRow } from "@toss-design-system/react-native";
256
+ * import { Button, List, ListRow } from "@toss/tds-react-native";
131
257
  * import { useEffect, useState } from "react";
132
258
  *
133
259
  * function IapProductList() {
@@ -199,6 +325,94 @@ async function getProductItemList() {
199
325
  return AppsInTossModule.iapGetProductItemList({});
200
326
  }
201
327
 
328
+ /**
329
+ * @public
330
+ * @category 인앱결제
331
+ * @name getPendingOrders
332
+ * @description 대기 중인 주문 목록을 가져와요. 이 함수를 사용하면 결제가 아직 완료되지 않은 주문 정보를 확인할 수 있어요.
333
+ * @returns {Promise<{orderIds: string[]}}>} 대기 중인 주문ID 배열을 반환해요. 앱 버전이 최소 지원 버전(안드로이드 5.231.0, iOS 5.231.0)보다 낮으면 `undefined`를 반환해요.
334
+ *
335
+ * @example
336
+ * ### 대기 중인 주문 목록 가져오기
337
+ * ```typescript
338
+ * import { IAP } from '@apps-in-toss/framework';
339
+ *
340
+ * async function fetchOrders() {
341
+ * try {
342
+ * const pendingOrders = await IAP.getPendingOrders();
343
+ * return pendingOrders;
344
+ * } catch (error) {
345
+ * console.error(error);
346
+ * }
347
+ * }
348
+ * ```
349
+ */
350
+ async function getPendingOrders() {
351
+ const isSupported = isMinVersionSupported({
352
+ android: '5.231.0',
353
+ ios: '5.231.0',
354
+ });
355
+
356
+ if (!isSupported) {
357
+ return;
358
+ }
359
+
360
+ return AppsInTossModule.getPendingOrders({});
361
+ }
362
+
363
+ /**
364
+ * @public
365
+ * @category 인앱결제
366
+ * @name CompletedOrRefundedOrdersResult
367
+ * @description 인앱결제로 구매하거나 환불한 주문 목록을 나타내는 객체예요. 페이지네이션 정보를 포함해요.
368
+ * @property {boolean} hasNext 다음 페이지가 있는지 여부예요. `true`면 더 많은 주문이 남아 있어요.
369
+ * @property {string | null} [nextKey] 다음 주문 목록을 조회할 때 사용할 키예요. 마지막 페이지라면 `null`이거나 생략될 수 있어요.
370
+ * @property {Array} orders 주문 정보를 담은 배열이에요. 각 요소는 하나의 주문을 나타내요.
371
+ * @property {string} orders[].orderId 주문의 고유 ID예요.
372
+ * @property {string} orders[].sku 주문 상품의 고유 ID예요.
373
+ * @property {'COMPLETED' | 'REFUNDED'} orders[].status 주문의 상태예요. 'COMPLETED'는 주문이 완료된 상태, 'REFUNDED'는 환불된 상태를 의미해요.
374
+ * @property {string} orders[].date 주문의 날짜 정보예요. ISO 8601 형식(YYYY-MM-DDTHH:mm:ss)을 사용해요. 예를 들어 "2025-09-22T00:00:00" 형식으로 제공돼요. 주문 상태가 `COMPLETED`라면 주문한 날짜를, `REFUNDED`라면 환불한 날짜를 나타내요.
375
+ */
376
+ export interface CompletedOrRefundedOrdersResult {
377
+ hasNext: boolean;
378
+ nextKey?: string | null;
379
+ orders: { orderId: string; sku: string; status: 'COMPLETED' | 'REFUNDED'; date: string }[];
380
+ }
381
+
382
+ /**
383
+ * @public
384
+ * @category 인앱결제
385
+ * @name getCompletedOrRefundedOrders
386
+ * @description 인앱결제로 구매하거나 환불한 주문 목록을 가져와요.
387
+ * @returns {Promise<CompletedOrRefundedOrdersResult>} 페이지네이션을 포함한 주문 목록 객체를 반환해요. 앱 버전이 최소 지원 버전(안드로이드 5.231.0, iOS 5.231.0)보다 낮으면 `undefined`를 반환해요.
388
+ *
389
+ * @example
390
+ * ```typescript
391
+ * import { IAP } from "@apps-in-toss/framework";
392
+ *
393
+ * async function fetchOrders() {
394
+ * try {
395
+ * const response = await IAP.getCompletedOrRefundedOrders();
396
+ * return response;
397
+ * } catch (error) {
398
+ * console.error(error);
399
+ * }
400
+ * }
401
+ * ```
402
+ */
403
+ async function getCompletedOrRefundedOrders(params?: { key?: string | null }) {
404
+ const isSupported = isMinVersionSupported({
405
+ android: '5.231.0',
406
+ ios: '5.231.0',
407
+ });
408
+
409
+ if (!isSupported) {
410
+ return;
411
+ }
412
+
413
+ return AppsInTossModule.getCompletedOrRefundedOrders(params ?? { key: null });
414
+ }
415
+
202
416
  /**
203
417
  * @public
204
418
  * @category 인앱결제
@@ -210,4 +424,6 @@ async function getProductItemList() {
210
424
  export const IAP = {
211
425
  createOneTimePurchaseOrder,
212
426
  getProductItemList,
427
+ getPendingOrders,
428
+ getCompletedOrRefundedOrders,
213
429
  };
@@ -0,0 +1,9 @@
1
+ import { getSchemeUri } from '@granite-js/react-native';
2
+
3
+ export function getReferrer() {
4
+ try {
5
+ return new URL(getSchemeUri()).searchParams.get('referrer');
6
+ } catch {
7
+ return null;
8
+ }
9
+ }
@@ -1,10 +0,0 @@
1
- import { GraniteEventDefinition } from '@granite-js/react-native';
2
-
3
- export class BackButtonClickHandleEvent extends GraniteEventDefinition<undefined, undefined> {
4
- name = 'backButtonClickEvent' as const;
5
-
6
- remove() {}
7
-
8
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
9
- listener(_: undefined) {}
10
- }
@@ -1,10 +0,0 @@
1
- import { GraniteEventDefinition } from '@granite-js/react-native';
2
-
3
- export class HomeIconButtonClickHandleEvent extends GraniteEventDefinition<undefined, undefined> {
4
- name = 'homeIconButtonClickEvent' as const;
5
-
6
- remove() {}
7
-
8
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
9
- listener(_: undefined) {}
10
- }