|
| 1 | +export type ValidCssSelector = |
| 2 | + | ".invalid" |
| 3 | + | ".invalid.focus" |
| 4 | + | "input" |
| 5 | + | "input.disabled" |
| 6 | + | "input.focus" |
| 7 | + | "input.invalid" |
| 8 | + | "input.valid" |
| 9 | + | "input::placeholder" |
| 10 | + | "label" |
| 11 | + | "label.disabled" |
| 12 | + | "label.focus" |
| 13 | + | "label.invalid" |
| 14 | + | "label.valid" |
| 15 | + | "label::placeholder"; |
| 16 | + |
| 17 | +export type ValidCssProperties = |
| 18 | + | "appearance" |
| 19 | + | "background" |
| 20 | + | "border" |
| 21 | + | "borderRadius" |
| 22 | + | "boxShadow" |
| 23 | + | "color" |
| 24 | + | "direction" |
| 25 | + | "font" |
| 26 | + | "fontFamily" |
| 27 | + | "fontSize" |
| 28 | + | "fontSizeAdjust" |
| 29 | + | "fontStretch" |
| 30 | + | "fontStyle" |
| 31 | + | "fontVariant" |
| 32 | + | "fontVariantAlternates" |
| 33 | + | "fontVariantCaps" |
| 34 | + | "fontVariantEastAsian" |
| 35 | + | "fontVariantLigatures" |
| 36 | + | "fontVariantNumeric" |
| 37 | + | "fontWeight" |
| 38 | + | "height" |
| 39 | + | "letterSpacing" |
| 40 | + | "lineHeight" |
| 41 | + | "opacity" |
| 42 | + | "outline" |
| 43 | + | "padding" |
| 44 | + | "paddingBottom" |
| 45 | + | "paddingLeft" |
| 46 | + | "paddingRight" |
| 47 | + | "paddingTop" |
| 48 | + | "textShadow" |
| 49 | + | "transition"; |
| 50 | + |
| 51 | +export type MerchantStyleObject = Partial<{ |
| 52 | + [key in ValidCssSelector]: Partial<{ |
| 53 | + [key in ValidCssProperties]: string | number; |
| 54 | + }>; |
| 55 | +}>; |
| 56 | + |
| 57 | +export type FieldState = { |
| 58 | + isFocused: boolean; |
| 59 | + isValid: boolean; |
| 60 | + isEmpty: boolean; |
| 61 | + isPotentiallyValid: boolean; |
| 62 | +}; |
| 63 | + |
| 64 | +export type CardFieldTypes = "cvv" | "expiry" | "number"; |
| 65 | + |
| 66 | +export type MerchantMessagingEvents = |
| 67 | + | "blur" |
| 68 | + | "cardtypechange" |
| 69 | + | "change" |
| 70 | + | "empty" |
| 71 | + | "focus" |
| 72 | + | "inputsubmit" |
| 73 | + | "notempty" |
| 74 | + | "validitychange"; |
| 75 | + |
| 76 | +export type Card = { |
| 77 | + code: { |
| 78 | + name: string; |
| 79 | + size: number; |
| 80 | + }; |
| 81 | + niceType: string; |
| 82 | + type: string; |
| 83 | +}; |
| 84 | + |
| 85 | +export type EventState = { |
| 86 | + cards: Card[]; |
| 87 | + emittedBy: CardFieldTypes; |
| 88 | + number: FieldState; |
| 89 | + cvv: FieldState; |
| 90 | + expiry: FieldState; |
| 91 | +}; |
| 92 | + |
| 93 | +export type EventPayload = { |
| 94 | + data: EventState; |
| 95 | + sender: CardFieldTypes; |
| 96 | +}; |
| 97 | + |
| 98 | +export type CardFieldsEventsOptions = { |
| 99 | + [key in MerchantMessagingEvents]: (eventPayload: EventPayload) => void; |
| 100 | +}; |
| 101 | + |
| 102 | +export type OrderAmount = { |
| 103 | + value?: string; |
| 104 | + currencyCode?: string; |
| 105 | +}; |
| 106 | + |
| 107 | +export type UpdateOptions = { |
| 108 | + amount?: OrderAmount; |
| 109 | + isCobrandedEligible?: boolean; |
| 110 | +}; |
| 111 | + |
| 112 | +export type BillingAddress = { |
| 113 | + addressLine1?: string; |
| 114 | + addressLine2?: string; |
| 115 | + adminArea2?: string; |
| 116 | + adminArea1?: string; |
| 117 | + postalCode?: string; |
| 118 | + countryCode?: string; |
| 119 | +}; |
| 120 | + |
| 121 | +export type ExtraFields = { |
| 122 | + name?: string; |
| 123 | + billingAddress?: BillingAddress; |
| 124 | +}; |
| 125 | + |
| 126 | +export type CardFieldOptions = { |
| 127 | + type: CardFieldTypes; |
| 128 | + placeholder?: string; |
| 129 | + label?: string; |
| 130 | + style?: MerchantStyleObject; |
| 131 | + ariaDescription?: string; |
| 132 | + ariaLabel?: string; |
| 133 | + ariaInvalidErrorMessage?: string; |
| 134 | +}; |
| 135 | + |
| 136 | +export type StateType = "canceled" | "failed" | "succeeded"; |
| 137 | + |
| 138 | +type BaseCardFieldsSession = { |
| 139 | + /** |
| 140 | + * Use this method to create and configure individual Card Field components. |
| 141 | + * |
| 142 | + * @param config - Configuration options for creating individual Card Field components and customizing different base aspects such as type, placeholder, styling, and accessibility attributes. |
| 143 | + * @returns An instance of the created Card Field component that can be appended to the DOM |
| 144 | + * |
| 145 | + * @example |
| 146 | + * ```typescript |
| 147 | + * const numberField = cardFieldsInstance.createCardFieldsComponent({ |
| 148 | + type: "number", |
| 149 | + placeholder: "Enter a number", |
| 150 | + }); |
| 151 | + document |
| 152 | + .querySelector("#paypal-card-fields-number-container") |
| 153 | + .appendChild(numberField); |
| 154 | + * ``` |
| 155 | + */ |
| 156 | + createCardFieldsComponent: (config: CardFieldOptions) => HTMLElement; |
| 157 | + /** |
| 158 | + * Use this method to register event listeners and set callbacks for them. |
| 159 | + * |
| 160 | + * @param eventName - Name of the event to listen for. |
| 161 | + * @param callback - Callback function to be executed when the event is triggered. |
| 162 | + * |
| 163 | + * @example |
| 164 | + * ```typescript |
| 165 | + * cardFieldsInstance.on("focus", (eventPayload) => { |
| 166 | + console.log("Focus event triggered: ", eventPayload); |
| 167 | + }); |
| 168 | + * ``` |
| 169 | + */ |
| 170 | + on: ( |
| 171 | + eventName: MerchantMessagingEvents, |
| 172 | + callback: CardFieldsEventsOptions[MerchantMessagingEvents], |
| 173 | + ) => Promise<void>; |
| 174 | + /** |
| 175 | + * Use this method to update the Card Fields session with new options. |
| 176 | + * @param options - Configuration object to update the Card Fields session with new options. |
| 177 | + * |
| 178 | + * @example |
| 179 | + * ```typescript |
| 180 | + * cardFieldsInstance.update({ |
| 181 | + amount: { |
| 182 | + currencyCode: "EUR", |
| 183 | + value: "100.00", |
| 184 | + }, |
| 185 | + isCobrandedEligible: true, |
| 186 | + }); |
| 187 | + * ``` |
| 188 | + */ |
| 189 | + update: (options: UpdateOptions) => void; |
| 190 | +}; |
| 191 | + |
| 192 | +export type OneTimePaymentFlowResponse = { |
| 193 | + data: { |
| 194 | + orderId: string; |
| 195 | + message?: string; |
| 196 | + liabilityShift?: string; |
| 197 | + }; |
| 198 | + state: StateType; |
| 199 | +}; |
| 200 | + |
| 201 | +export type OneTimePaymentSubmitOptions = [orderId: string, data: ExtraFields]; |
| 202 | + |
| 203 | +export type CardFieldsOneTimePaymentSession = BaseCardFieldsSession & { |
| 204 | + /** |
| 205 | + * Use this method to submit a one-time payment using Card Fields. |
| 206 | + * |
| 207 | + * @param orderId - The unique identifier for the order to be processed. |
| 208 | + * @param data - Additional payment data. |
| 209 | + * @returns A promise that resolves to the result of the payment flow. |
| 210 | + * |
| 211 | + * @example |
| 212 | + * ```typescript |
| 213 | + * const response = await cardFieldsInstance.submit("your-order-id", { |
| 214 | + billingAddress: { |
| 215 | + postalCode: "12345", |
| 216 | + }, |
| 217 | + }); |
| 218 | + * ``` |
| 219 | + */ |
| 220 | + submit: ( |
| 221 | + ...args: OneTimePaymentSubmitOptions |
| 222 | + ) => Promise<OneTimePaymentFlowResponse>; |
| 223 | +}; |
| 224 | + |
| 225 | +export type SavePaymentFlowResponse = { |
| 226 | + data: { |
| 227 | + vaultSetupToken: string; |
| 228 | + message?: string; |
| 229 | + liabilityShift?: string; |
| 230 | + }; |
| 231 | + state: StateType; |
| 232 | +}; |
| 233 | + |
| 234 | +export type SavePaymentSubmitOptions = [ |
| 235 | + vaultSetupToken: string, |
| 236 | + data: ExtraFields, |
| 237 | +]; |
| 238 | + |
| 239 | +export type CardFieldsSavePaymentSession = BaseCardFieldsSession & { |
| 240 | + /** |
| 241 | + * Use this method to submit and save a payment method using Card Fields. |
| 242 | + * |
| 243 | + * @param vaultSetupToken - The unique token for the vault setup to be processed. |
| 244 | + * @param data - Additional payment data. |
| 245 | + * @returns A promise that resolves to the result of the save payment flow. |
| 246 | + * |
| 247 | + * @example |
| 248 | + * ```typescript |
| 249 | + * const response = await cardFieldsInstance.submit("your-vault-setup-token", { |
| 250 | + billingAddress: { |
| 251 | + postalCode: "12345", |
| 252 | + }, |
| 253 | + }); |
| 254 | + * ``` |
| 255 | + */ |
| 256 | + submit: ( |
| 257 | + ...args: SavePaymentSubmitOptions |
| 258 | + ) => Promise<SavePaymentFlowResponse>; |
| 259 | +}; |
| 260 | + |
| 261 | +/** |
| 262 | + * CardFieldsInstance interface for creating and managing different types of Card Fields sessions. |
| 263 | + * |
| 264 | + * @remarks |
| 265 | + * This interface provides methods to create the following Card Fields flows: |
| 266 | + * - One-time payments with Card Fields |
| 267 | + * - Save payment methods for future use (vaulting) |
| 268 | + * |
| 269 | + * Each method returns a session object that can be used to initiate the corresponding Card Fields flow. |
| 270 | + */ |
| 271 | +export interface CardFieldsInstance { |
| 272 | + /** |
| 273 | + * Creates a Card Fields one-time payment session for processing payments. |
| 274 | + * |
| 275 | + * @returns A session object that can be used to start the payment flow |
| 276 | + * |
| 277 | + * @example |
| 278 | + * ```typescript |
| 279 | + * const cardFieldsInstance = sdkInstance.createCardFieldsOneTimePaymentSession(); |
| 280 | + * ``` |
| 281 | + */ |
| 282 | + createCardFieldsOneTimePaymentSession: () => CardFieldsOneTimePaymentSession; |
| 283 | + /** |
| 284 | + * Creates a Card Fields save payment session for storing payment methods for future use. |
| 285 | + * |
| 286 | + * @returns A session object that can be used to start the vault setup flow |
| 287 | + * |
| 288 | + * @example |
| 289 | + * ```typescript |
| 290 | + * const cardFieldsInstance = sdkInstance.createCardFieldsSavePaymentSession(); |
| 291 | + * ``` |
| 292 | + */ |
| 293 | + createCardFieldsSavePaymentSession: () => CardFieldsSavePaymentSession; |
| 294 | +} |
0 commit comments