@strabl-engineering/strabl-web-sdk 0.0.0-stage → 1.0.1

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.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The per-state `style` option: `{ base, focus, invalid, placeholder }`, each
3
+ * a map of CSS property names to values. Every name must be on the allowlist
4
+ * and every value must pass {@link isSafeStyleValue}. The hosted page applies
5
+ * each declaration as one `style.setProperty` call on the input, so nothing is
6
+ * concatenated into a stylesheet and a value can never become a selector, a
7
+ * rule or a second declaration.
8
+ */
9
+ export type StyleState = "base" | "focus" | "invalid" | "placeholder";
10
+ export type StyleDeclarations = Record<string, string>;
11
+ export type ElementStyle = Partial<Record<StyleState, StyleDeclarations>>;
12
+ export declare const STYLE_PROPERTIES: ReadonlySet<string>;
13
+ export declare const MAX_STYLE_VALUE_LENGTH = 120;
14
+ /**
15
+ * Short, free of CSS structure, and free of anything that fetches or runs.
16
+ * The only functions allowed are the colour notations.
17
+ */
18
+ export declare function isSafeStyleValue(value: unknown): value is string;
19
+ export interface SanitizedElementStyle {
20
+ base: StyleDeclarations;
21
+ focus: StyleDeclarations;
22
+ invalid: StyleDeclarations;
23
+ placeholderColor?: string;
24
+ }
25
+ /** Drops anything not allowed instead of throwing: a typo must not stop a payment. */
26
+ export declare function sanitizeElementStyle(style: unknown): {
27
+ style: SanitizedElementStyle;
28
+ rejected: string[];
29
+ };
@@ -0,0 +1,23 @@
1
+ export type Environment = "sandbox" | "prod";
2
+ /**
3
+ * The card page's host per key type: `pk_live_` keys load production, every
4
+ * other key sandbox. Both hosts serve the same build; the page picks the API
5
+ * and checkout from the key (`API_BASE_URL`, `CHECKOUT_ORIGIN`).
6
+ */
7
+ export declare const ELEMENTS_ORIGIN: Record<Environment, string>;
8
+ export declare const API_BASE_URL: Record<Environment, string>;
9
+ /** Strabl checkout: its `/3ds` page ends an MPGS challenge with `strabl-3ds-auth-complete`. */
10
+ export declare const CHECKOUT_ORIGIN: Record<Environment, string>;
11
+ /**
12
+ * Every Strabl checkout host. MPGS returns the payer to the checkout set in
13
+ * the gateway's configuration (e.g. sandbox checkout even for a local
14
+ * backend, or the .sa host for Saudi merchants), not necessarily the one
15
+ * this build was pointed at.
16
+ */
17
+ export declare const CHECKOUT_ORIGINS: readonly string[];
18
+ /**
19
+ * The file itself, not its folder: S3 behind CloudFront does not turn
20
+ * `/v1/card-form/` into its index.html.
21
+ */
22
+ export declare const FRAME_PATH = "/v1/card-form/index.html";
23
+ export declare function environmentFor(publishableKey: string): Environment;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Web fonts for the hosted form. Fonts loaded by the merchant page are not
3
+ * visible inside the iframe, so merchants pass the font sources here and the
4
+ * iframe loads them itself with the FontFace API. No merchant CSS is ever
5
+ * injected: a Google Fonts stylesheet is fetched and only its @font-face
6
+ * family, URL, weight, style and unicode-range are read.
7
+ */
8
+ /** A font file, e.g. `{ family: "Brand Sans", src: "https://cdn.shop.example/brand.woff2" }`. */
9
+ export interface FontFileSource {
10
+ family: string;
11
+ src: string;
12
+ weight?: string;
13
+ style?: "normal" | "italic";
14
+ unicodeRange?: string;
15
+ }
16
+ /** A Google Fonts stylesheet URL, e.g. `https://fonts.googleapis.com/css2?family=Poppins:wght@400;500`. */
17
+ export interface FontCssSource {
18
+ cssSrc: string;
19
+ }
20
+ export type FontSource = FontFileSource | FontCssSource;
21
+ export declare const MAX_FONT_SOURCES = 10;
22
+ export declare const FONT_CSS_HOSTS: string[];
23
+ export declare const FONT_FILE_HOSTS_FROM_CSS: string[];
24
+ /** HTTPS URL with no characters that could break out of `url("…")`. */
25
+ export declare function isSafeHttpsUrl(value: unknown, hosts?: string[]): value is string;
26
+ export declare function sanitizeFontSources(input: unknown): {
27
+ fonts: FontSource[];
28
+ rejected: number;
29
+ };
30
+ export interface ParsedFontFace {
31
+ family: string;
32
+ src: string;
33
+ weight?: string;
34
+ style?: string;
35
+ unicodeRange?: string;
36
+ }
37
+ /**
38
+ * Reads the @font-face rules of a Google Fonts stylesheet. Only fonts served
39
+ * from fonts.gstatic.com are kept; every other rule in the text is ignored.
40
+ */
41
+ export declare function parseFontFaceCss(css: string): ParsedFontFace[];
@@ -0,0 +1,7 @@
1
+ export * from "./types.js";
2
+ export * from "./validate.js";
3
+ export * from "./styles.js";
4
+ export * from "./currency.js";
5
+ export * from "./environments.js";
6
+ export * from "./fonts.js";
7
+ export * from "./element-style.js";
@@ -0,0 +1,6 @@
1
+ import type { StyleTokens } from "./types.js";
2
+ export interface SanitizeResult {
3
+ tokens: StyleTokens;
4
+ rejected: string[];
5
+ }
6
+ export declare function sanitizeStyleTokens(input: unknown): SanitizeResult;
@@ -0,0 +1,276 @@
1
+ /**
2
+ * Message protocol between the merchant page (loader) and the Strabl hosted
3
+ * card page (iframe). Card data never appears in any of these messages.
4
+ *
5
+ * Every message is wrapped in an {@link Envelope}. The only message sent with
6
+ * `window.postMessage` is `hello`; everything after it travels over the private
7
+ * `MessagePort` the loader hands back in response.
8
+ */
9
+ import type { ElementStyle } from "./element-style.js";
10
+ import type { FontSource } from "./fonts.js";
11
+ export declare const PROTOCOL_VERSION = 1;
12
+ export declare const MESSAGE_SOURCE = "strabl-elements";
13
+ export type Language = "en" | "ar";
14
+ /**
15
+ * `tabby` and `tamara` are offered only when the platform's `sdkCustomization`
16
+ * (set in the Strabl dashboard) lists them; listing them in `paymentMethods`
17
+ * changes nothing.
18
+ */
19
+ export type PaymentMethod = "card" | "applepay" | "googlepay" | "tabby" | "tamara";
20
+ /** A third-party instalment checkout the payer is sent to by full-page redirect. */
21
+ export type InstalmentProvider = "TABBY" | "TAMARA";
22
+ export type FieldInputStyle = "floating-label" | "outlined";
23
+ export type FieldName = "cardHolderName" | "cardNumber" | "expiry" | "cvc";
24
+ export type FieldErrorCode = "required" | "invalid_card_number" | "invalid_expiry" | "expiry_in_past" | "invalid_length" | "invalid_name";
25
+ export interface StyleTokens {
26
+ textColor?: string;
27
+ errorColor?: string;
28
+ btnRadius?: string;
29
+ btnPadding?: string;
30
+ backgroundColor?: string;
31
+ accentColor?: string;
32
+ /** Kept with v1's spelling so existing integrations keep working. */
33
+ continerMaxWidth?: string;
34
+ inputRadius?: string;
35
+ inputBorderColor?: string;
36
+ inputBorderWidth?: string;
37
+ fontFamily?: string;
38
+ /** Body text of options, notes and buttons (Inter by default). */
39
+ secondaryFontFamily?: string;
40
+ /** Selection, links and icons. */
41
+ primaryColor?: string;
42
+ headingColor?: string;
43
+ mutedTextColor?: string;
44
+ /** Unselected option and card labels. */
45
+ labelColor?: string;
46
+ selectedLabelColor?: string;
47
+ /** Dividers and unselected option cards. */
48
+ borderColor?: string;
49
+ /** The border around the whole form. */
50
+ containerBorderColor?: string;
51
+ containerRadius?: string;
52
+ successColor?: string;
53
+ /** The "Transaction Failed" alert. */
54
+ failureColor?: string;
55
+ failureBackgroundColor?: string;
56
+ footerTextColor?: string;
57
+ }
58
+ /**
59
+ * A DirectPay session, created by the merchant's backend with its secret key.
60
+ * The amount and currency live in the session: nothing in the browser sets them.
61
+ */
62
+ export interface SessionCredentials {
63
+ sessionId: string;
64
+ clientSecret: string;
65
+ }
66
+ /**
67
+ * What the Apple Pay sheet shows besides the amount, which comes from the
68
+ * session. Both are optional: the label defaults to the merchant page's host
69
+ * name, the country to the one the session's currency implies.
70
+ */
71
+ export interface ApplePayOptions {
72
+ /** The merchant name on the sheet's total line. */
73
+ label?: string;
74
+ /** ISO 3166 alpha-2 country of the merchant, e.g. "SA". */
75
+ countryCode?: string;
76
+ }
77
+ export interface DisplayOptions {
78
+ language: Language;
79
+ fieldInputStyle: FieldInputStyle;
80
+ paymentMethods: PaymentMethod[];
81
+ customstyles: StyleTokens;
82
+ /** Per-state input styles from an allowlist; see element-style.ts. */
83
+ style: ElementStyle;
84
+ /** Web fonts to load inside the iframe; see fonts.ts. */
85
+ fonts: FontSource[];
86
+ applePay: ApplePayOptions;
87
+ /**
88
+ * Ask for the payer's email above the payment methods (with the one-time
89
+ * code that finds their saved cards). False hides that step entirely.
90
+ */
91
+ enableEmailCollection: boolean;
92
+ /**
93
+ * Offer "Save this card" for a new card. False never saves a new card;
94
+ * cards the payer saved before are still offered.
95
+ */
96
+ enableSaveCard: boolean;
97
+ }
98
+ /**
99
+ * What `init` carries. `enableEmailCollection` and `enableSaveCard` are
100
+ * optional: older loaders leave them out, and the form then uses the
101
+ * defaults (both on).
102
+ */
103
+ export interface InitPayload extends Omit<DisplayOptions, "enableEmailCollection" | "enableSaveCard">, Partial<Pick<DisplayOptions, "enableEmailCollection" | "enableSaveCard">> {
104
+ /** Optional at init: the StrablElements API passes it with confirm(). */
105
+ session?: SessionCredentials;
106
+ /** True when the merchant passed onPaymentSubmission. */
107
+ hasBeforeSubmit: boolean;
108
+ /**
109
+ * Width of the merchant page's viewport. Media queries inside the iframe
110
+ * measure the iframe, so the form's breakpoints use this instead.
111
+ */
112
+ viewportWidth: number;
113
+ /** The payer is back from 3-D Secure: finish the session with resume. */
114
+ resume: boolean;
115
+ /** Back from Tabby or Tamara: the outcome the return URL carried. */
116
+ providerReturn?: ProviderReturn;
117
+ /**
118
+ * The merchant page, for the load report (the frame cannot see it). URLs
119
+ * without their query or fragment. Optional: older loaders do not send it.
120
+ */
121
+ page?: PageInfo;
122
+ }
123
+ export interface PageInfo {
124
+ /** The page's origin and path. */
125
+ href?: string;
126
+ /** The page the payer came from (origin and path); absent when direct. */
127
+ referrer?: string;
128
+ }
129
+ /**
130
+ * What a Tabby or Tamara return page says, read by the loader from the
131
+ * merchant page's URL (the frame cannot see it). `unknown` when the URL
132
+ * carries no status: the session is then resumed through the API instead.
133
+ */
134
+ export interface ProviderReturn {
135
+ provider?: InstalmentProvider;
136
+ status: "success" | "failed" | "cancelled" | "unknown";
137
+ /** The order the checkout created, remembered from the hand-off. */
138
+ orderShortCode?: string;
139
+ }
140
+ export type UpdatePayload = Partial<DisplayOptions> & {
141
+ session?: SessionCredentials;
142
+ hasBeforeSubmit?: boolean;
143
+ };
144
+ /** The transaction fields the iframe passes on from the API, nothing else. */
145
+ export interface Transaction {
146
+ /** API status: CAPTURED, AUTHORIZED, CAPTURE_FAILED, … */
147
+ status: string;
148
+ orderShortCode?: string;
149
+ paymentReference?: string;
150
+ /** Set when the payer must authenticate before the session can resume. */
151
+ threeDsUrl?: string;
152
+ /** Set with `threeDsUrl` when that URL is a Tabby or Tamara checkout. */
153
+ provider?: InstalmentProvider;
154
+ amount?: number;
155
+ currency?: string;
156
+ }
157
+ export type PaymentResult = {
158
+ status: "success";
159
+ orderShortCode: string;
160
+ transaction: Transaction;
161
+ }
162
+ /**
163
+ * 3-D Secure, or a Tabby / Tamara checkout (`provider`): send the payer to
164
+ * `url`, then resume the session when they come back.
165
+ */
166
+ | {
167
+ status: "requires_action";
168
+ url: string;
169
+ transaction: Transaction;
170
+ provider?: InstalmentProvider;
171
+ } | {
172
+ status: "failed";
173
+ code: string;
174
+ message: string;
175
+ transaction?: Transaction;
176
+ };
177
+ /** `requestId` is echoed when the result answers a submit or resume request. */
178
+ export type ResultPayload = PaymentResult & {
179
+ requestId?: string;
180
+ };
181
+ export interface ChangePayload {
182
+ brand: string | null;
183
+ empty: boolean;
184
+ complete: boolean;
185
+ errors: Partial<Record<FieldName, FieldErrorCode>>;
186
+ /** The method the payer selected; the rest describes its fields. */
187
+ paymentMethod?: PaymentMethod;
188
+ }
189
+ /** Messages the loader sends to the iframe. */
190
+ export interface PageMessageMap {
191
+ init: InitPayload;
192
+ update: UpdatePayload;
193
+ /** Without a requestId the form handles invalid input itself and sends no result. */
194
+ submit: {
195
+ requestId?: string;
196
+ session?: SessionCredentials;
197
+ };
198
+ resume: {
199
+ requestId?: string;
200
+ session?: SessionCredentials;
201
+ providerReturn?: ProviderReturn;
202
+ };
203
+ beforeSubmitResult: {
204
+ requestId: string;
205
+ proceed: boolean;
206
+ };
207
+ /** The merchant page was resized. */
208
+ viewport: {
209
+ width: number;
210
+ };
211
+ focus: {
212
+ field: FieldName;
213
+ };
214
+ clear: Record<string, never>;
215
+ /** Read the session's summary again: the merchant's server changed it. */
216
+ refresh: Record<string, never>;
217
+ }
218
+ /** Messages the iframe sends to the loader. */
219
+ export interface FrameMessageMap {
220
+ hello: {
221
+ instanceId: string;
222
+ };
223
+ ready: {
224
+ paymentMethods: PaymentMethod[];
225
+ };
226
+ change: ChangePayload;
227
+ focus: {
228
+ field: FieldName;
229
+ };
230
+ blur: {
231
+ field: FieldName;
232
+ };
233
+ resize: {
234
+ height: number;
235
+ };
236
+ processing: {
237
+ active: boolean;
238
+ };
239
+ beforeSubmit: {
240
+ requestId: string;
241
+ };
242
+ /** An in-frame 3DS challenge needs the whole viewport; the loader expands the iframe. */
243
+ overlay: {
244
+ open: boolean;
245
+ };
246
+ result: ResultPayload;
247
+ error: {
248
+ code: string;
249
+ message: string;
250
+ };
251
+ }
252
+ export type PageMessageType = keyof PageMessageMap;
253
+ export type FrameMessageType = keyof FrameMessageMap;
254
+ export interface Envelope<T extends string, P> {
255
+ source: typeof MESSAGE_SOURCE;
256
+ v: typeof PROTOCOL_VERSION;
257
+ type: T;
258
+ payload: P;
259
+ }
260
+ export type PageMessage = {
261
+ [K in PageMessageType]: Envelope<K, PageMessageMap[K]>;
262
+ }[PageMessageType];
263
+ export type FrameMessage = {
264
+ [K in FrameMessageType]: Envelope<K, FrameMessageMap[K]>;
265
+ }[FrameMessageType];
266
+ export declare function envelope<T extends string, P>(type: T, payload: P): Envelope<T, P>;
267
+ /** Query parameters the loader puts on the iframe URL. */
268
+ export interface FrameUrlParams {
269
+ /** Publishable key; the frame's API calls are made with it. */
270
+ key: string;
271
+ /** Origin of the merchant page, checked against ancestorOrigins. */
272
+ origin: string;
273
+ /** Random per-instance id, echoed in `hello`. */
274
+ id: string;
275
+ v: string;
276
+ }
@@ -0,0 +1,4 @@
1
+ import { type FrameMessage, type PageMessage, type SessionCredentials } from "./types.js";
2
+ export declare function isSessionCredentials(v: unknown): v is SessionCredentials;
3
+ export declare const parsePageMessage: (data: unknown) => PageMessage | null;
4
+ export declare const parseFrameMessage: (data: unknown) => FrameMessage | null;
@@ -0,0 +1,32 @@
1
+ import type { InstalmentProvider, ProviderReturn } from "./protocol/index.js";
2
+ /**
3
+ * Tabby and Tamara send the payer back to the session's `redirectUrl` with
4
+ * their own parameters, and no confirm or verify call follows: the outcome is
5
+ * read from the URL.
6
+ *
7
+ * - Tamara adds `paymentStatus` (`approved`, `declined`, `canceled`) and
8
+ * `orderId`.
9
+ * - Tabby adds `payment_id`. Its status comes from a `status` parameter when
10
+ * the return URL carries one.
11
+ *
12
+ * Anything that is not one of the known words is `unknown`, and the frame
13
+ * then asks the API through resume rather than guessing.
14
+ */
15
+ export declare const PROVIDER_RETURN_PARAMS: string[];
16
+ export declare function readReturnStatus(params: URLSearchParams): ProviderReturn["status"];
17
+ /** A URL a provider sent the payer back to, even when nothing on this page started it. */
18
+ export declare function looksLikeProviderReturn(params: URLSearchParams): boolean;
19
+ /** Remembered across the redirect; holds the session id, never its secret. */
20
+ export interface AwaitingPayer {
21
+ sessionId: string;
22
+ provider?: InstalmentProvider;
23
+ orderShortCode?: string;
24
+ }
25
+ export declare function parseAwaitingPayer(raw: string | null): AwaitingPayer | null;
26
+ /**
27
+ * The provider outcome for this page load, or undefined when this is not a
28
+ * provider return: a 3-D Secure return is resumed through the API, and a
29
+ * page reached with Back from the provider's page carries no return
30
+ * parameters at all.
31
+ */
32
+ export declare function providerReturnFor(awaiting: AwaitingPayer | null, sessionId: string, params: URLSearchParams): ProviderReturn | undefined;
@@ -0,0 +1,14 @@
1
+ import * as React from "react";
2
+ import { type StrablCardForm } from "../element.js";
3
+ import type { CardFormOptions } from "../options.js";
4
+ export type { CardFormOptions, PaymentSuccessDetails, CardFormChange, } from "../options.js";
5
+ export interface StrablCardFormProps extends CardFormOptions {
6
+ className?: string;
7
+ style?: React.CSSProperties;
8
+ }
9
+ /**
10
+ * React wrapper around `<strabl-card-form>`. Props are set as element
11
+ * properties, which works the same on React 18 and 19. Callbacks are passed
12
+ * as stable wrappers so a re-render does not resend options to the iframe.
13
+ */
14
+ export declare const StrablCardPaymentForm: React.ForwardRefExoticComponent<StrablCardFormProps & React.RefAttributes<StrablCardForm>>;
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@strabl-engineering/strabl-web-sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "Strabl Web SDK: accept cards, Apple Pay, Google Pay, Tabby and Tamara in a secure Strabl-hosted payment form. Card details never touch your page.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/types/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ },
15
+ "./react": {
16
+ "types": "./dist/types/react/index.d.ts",
17
+ "import": "./dist/react.js",
18
+ "require": "./dist/react.cjs"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "unpkg": "./dist/strabl-elements.js",
23
+ "jsdelivr": "./dist/strabl-elements.js",
24
+ "files": [
25
+ "dist",
26
+ "README.md"
27
+ ],
28
+ "sideEffects": [
29
+ "./dist/index.js",
30
+ "./dist/index.cjs",
31
+ "./dist/react.js",
32
+ "./dist/react.cjs",
33
+ "./dist/strabl-elements.js"
34
+ ],
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "build": "vite build && vite build --config vite.cdn.config.ts && tsc -p tsconfig.build.json",
40
+ "typecheck": "tsc -p tsconfig.json --noEmit"
41
+ },
42
+ "license": "ISC",
43
+ "peerDependencies": {
44
+ "react": ">=18"
45
+ },
46
+ "peerDependenciesMeta": {
47
+ "react": {
48
+ "optional": true
49
+ }
50
+ }
51
+ }