@chipmobilesdk/rn-payment 0.4.0 → 0.5.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,117 +1,124 @@
1
- /**
2
- * Normalized public errors.
3
- *
4
- * Error codes are **public API**: renaming one is a breaking change (FR-078,
5
- * LIB-008). They are the vocabulary an app branches on, so they must outlive any
6
- * internal refactor and must never leak a store's own code.
7
- */
8
-
9
- export const PAYMENT_ERROR_CODES = [
10
- 'PAYMENT_NOT_CONFIGURED',
11
- 'PAYMENT_INVALID_CATALOG',
12
- 'PAYMENT_NOT_READY',
13
- 'PAYMENT_DISABLED',
14
- 'PAYMENT_UNAVAILABLE',
15
- 'PAYMENT_UNSUPPORTED_PLATFORM',
16
- 'PAYMENT_ENVIRONMENT_BLOCKED',
17
- 'PAYMENT_PRODUCTION_CONFIG_IN_NON_PRODUCTION_BUILD',
18
- 'PAYMENT_PURCHASE_IN_PROGRESS',
19
- 'PAYMENT_USER_CANCELLED',
20
- 'PAYMENT_PENDING',
21
- 'PAYMENT_ALREADY_OWNED',
22
- 'PAYMENT_INELIGIBLE',
23
- 'PAYMENT_UNKNOWN_PRODUCT',
24
- 'PAYMENT_PRODUCT_UNAVAILABLE_IN_REGION',
25
- 'PAYMENT_TIMEOUT',
26
- 'PAYMENT_NETWORK_ERROR',
27
- 'PAYMENT_STORE_ERROR',
28
- 'PAYMENT_FINALIZE_FAILED',
29
- 'PAYMENT_VALIDATION_REJECTED',
30
- 'PAYMENT_VALIDATION_UNAVAILABLE',
31
- 'PAYMENT_STORAGE_UNAVAILABLE',
32
- /** `consume` on a transaction that is not a consumable, added in 0.3.2. */
33
- 'PAYMENT_NOT_CONSUMABLE',
34
- ] as const;
35
-
36
- export type PaymentErrorCode = (typeof PAYMENT_ERROR_CODES)[number];
37
-
38
- /** Which operation produced an error. Part of the public error shape (FR-078). */
39
- export type PaymentOperation =
40
- | 'configure'
41
- | 'initialize'
42
- | 'getProducts'
43
- | 'purchase'
44
- | 'finalize'
45
- | 'consume'
46
- | 'sync'
47
- | 'restore'
48
- | 'changePlan'
49
- | 'validate'
50
- | 'persist';
51
-
52
- export class PaymentError extends Error {
53
- readonly code: PaymentErrorCode;
54
- readonly operation: PaymentOperation;
55
- readonly retryable: boolean;
56
- /** Redacted where appropriate — never a raw receipt, token, or store object (FR-074). */
57
- readonly cause?: string;
58
-
59
- constructor(init: {
60
- code: PaymentErrorCode;
61
- message: string;
62
- operation: PaymentOperation;
63
- retryable?: boolean;
64
- cause?: string;
65
- }) {
66
- super(init.message);
67
- this.name = 'PaymentError';
68
- this.code = init.code;
69
- this.operation = init.operation;
70
- this.retryable = init.retryable ?? false;
71
- this.cause = init.cause;
72
- }
73
- }
74
-
75
- /**
76
- * A configuration problem the app can act on.
77
- *
78
- * Separate from `PaymentError` because configuration faults are developer-facing
79
- * and must never be shown to an end user — the message-key map keeps that
80
- * separation explicit (FR-085).
81
- */
82
- export class PaymentConfigError extends PaymentError {
83
- /** The offending catalog entries or entitlement keys, named (FR-011). */
84
- readonly offenders: string[];
85
-
86
- constructor(init: {
87
- code: PaymentErrorCode;
88
- message: string;
89
- operation: PaymentOperation;
90
- offenders?: string[];
91
- }) {
92
- super({ ...init, retryable: false });
93
- this.name = 'PaymentConfigError';
94
- this.offenders = init.offenders ?? [];
95
- }
96
- }
97
-
98
- /**
99
- * Strips anything that must never reach a log or event.
100
- *
101
- * Applied at the boundary rather than trusted to call sites: a single forgotten
102
- * interpolation is how receipts end up in logs (NFR-006).
103
- *
104
- * Identifier-only: what survives is what code defined, never what data carried.
105
- * A `PaymentError` keeps its code and an `Error` its class name; every message
106
- * and every string is dropped, because a store or consumer message can embed a
107
- * purchase token or an account id and nothing here can tell which.
108
- */
109
- export function redact(value: unknown): string {
110
- if (value instanceof PaymentError) {
111
- return value.code;
112
- }
113
- if (value instanceof Error) {
114
- return value.name;
115
- }
116
- return '[redacted]';
117
- }
1
+ /**
2
+ * Normalized public errors.
3
+ *
4
+ * Error codes are **public API**: renaming one is a breaking change (FR-078,
5
+ * LIB-008). They are the vocabulary an app branches on, so they must outlive any
6
+ * internal refactor and must never leak a store's own code.
7
+ */
8
+
9
+ import { REDACTED, SdkError, createRedactor } from '@chipmobilesdk/rn-core';
10
+
11
+ const identifiers = createRedactor();
12
+
13
+ export const PAYMENT_ERROR_CODES = [
14
+ 'PAYMENT_NOT_CONFIGURED',
15
+ 'PAYMENT_INVALID_CATALOG',
16
+ 'PAYMENT_NOT_READY',
17
+ 'PAYMENT_DISABLED',
18
+ 'PAYMENT_UNAVAILABLE',
19
+ 'PAYMENT_UNSUPPORTED_PLATFORM',
20
+ 'PAYMENT_ENVIRONMENT_BLOCKED',
21
+ 'PAYMENT_PRODUCTION_CONFIG_IN_NON_PRODUCTION_BUILD',
22
+ 'PAYMENT_PURCHASE_IN_PROGRESS',
23
+ 'PAYMENT_USER_CANCELLED',
24
+ 'PAYMENT_PENDING',
25
+ 'PAYMENT_ALREADY_OWNED',
26
+ 'PAYMENT_INELIGIBLE',
27
+ 'PAYMENT_UNKNOWN_PRODUCT',
28
+ 'PAYMENT_PRODUCT_UNAVAILABLE_IN_REGION',
29
+ 'PAYMENT_TIMEOUT',
30
+ 'PAYMENT_NETWORK_ERROR',
31
+ 'PAYMENT_STORE_ERROR',
32
+ 'PAYMENT_FINALIZE_FAILED',
33
+ 'PAYMENT_VALIDATION_REJECTED',
34
+ 'PAYMENT_VALIDATION_UNAVAILABLE',
35
+ 'PAYMENT_STORAGE_UNAVAILABLE',
36
+ /** `consume` on a transaction that is not a consumable, added in 0.3.2. */
37
+ 'PAYMENT_NOT_CONSUMABLE',
38
+ /**
39
+ * A timeout, backoff or interval outside its bounds, added in 0.5.0. These
40
+ * used to be reported as `PAYMENT_INVALID_CATALOG`, which is about products.
41
+ */
42
+ 'PAYMENT_INVALID_CONFIG',
43
+ ] as const;
44
+
45
+ export type PaymentErrorCode = (typeof PAYMENT_ERROR_CODES)[number];
46
+
47
+ /** Which operation produced an error. Part of the public error shape (FR-078). */
48
+ export type PaymentOperation =
49
+ | 'configure'
50
+ | 'initialize'
51
+ | 'getProducts'
52
+ | 'purchase'
53
+ | 'finalize'
54
+ | 'consume'
55
+ | 'sync'
56
+ | 'restore'
57
+ | 'changePlan'
58
+ | 'validate'
59
+ | 'persist';
60
+
61
+ /** The package's error; an `SdkError` (`@chipmobilesdk/rn-core`) with `pkg: 'rn-payment'`. */
62
+ export class PaymentError extends SdkError<PaymentErrorCode> {
63
+ readonly operation: PaymentOperation;
64
+ /** Redacted where appropriate — never a raw receipt, token, or store object (FR-074). */
65
+ readonly cause?: string;
66
+
67
+ constructor(init: {
68
+ code: PaymentErrorCode;
69
+ message: string;
70
+ operation: PaymentOperation;
71
+ retryable?: boolean;
72
+ cause?: string;
73
+ }) {
74
+ super('rn-payment', init.code, {
75
+ message: init.message,
76
+ retryable: init.retryable ?? false,
77
+ redactedCause: init.cause,
78
+ });
79
+ this.name = 'PaymentError';
80
+ this.operation = init.operation;
81
+ this.cause = init.cause;
82
+ }
83
+ }
84
+
85
+ /**
86
+ * A configuration problem the app can act on.
87
+ *
88
+ * Separate from `PaymentError` because configuration faults are developer-facing
89
+ * and must never be shown to an end user — the message-key map keeps that
90
+ * separation explicit (FR-085).
91
+ */
92
+ export class PaymentConfigError extends PaymentError {
93
+ /** The offending catalog entries or entitlement keys, named (FR-011). */
94
+ readonly offenders: string[];
95
+
96
+ constructor(init: {
97
+ code: PaymentErrorCode;
98
+ message: string;
99
+ operation: PaymentOperation;
100
+ offenders?: string[];
101
+ }) {
102
+ super({ ...init, retryable: false });
103
+ this.name = 'PaymentConfigError';
104
+ this.offenders = init.offenders ?? [];
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Strips anything that must never reach a log or event.
110
+ *
111
+ * Applied at the boundary rather than trusted to call sites: a single forgotten
112
+ * interpolation is how receipts end up in logs (NFR-006).
113
+ *
114
+ * Identifier-only: what survives is what code defined, never what data carried.
115
+ * A `PaymentError` keeps its code and an `Error` its class name; every message
116
+ * and every string is dropped, because a store or consumer message can embed a
117
+ * purchase token or an account id and nothing here can tell which.
118
+ */
119
+ export function redact(value: unknown): string {
120
+ // An SDK error reduces to its code and any other Error to its class name
121
+ // (rn-core's identifier-only redactor). Strings never survive here, not even
122
+ // a leading constant: a store message is free text from a payments SDK.
123
+ return value instanceof Error ? identifiers.text(value) : REDACTED;
124
+ }