@flopay/vue 1.16.4 → 1.16.5

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,6 +1,10 @@
1
- import type { ButtonsLayoutStyles, ButtonsLayoutTheme, CheckoutButtonMethod, CheckoutItem, CheckoutProduct, CheckoutSession, CheckoutSubscription, DeclineEvent, FloPayAppearance, InlineSessionDraft, PaymentResult, ThemeId } from '@flopay/shared';
1
+ import type { ButtonsLayoutStyles, ButtonsLayoutTheme, CheckoutButtonMethod, CheckoutItem, CheckoutProduct, CheckoutSession, CheckoutSubscription, DeclineEvent, FloPayAppearance, InlineSessionDraft, OneClickSession, PaymentResult, ThemeId } from '@flopay/shared';
2
2
  import { FloPayError } from '@flopay/shared';
3
3
  import { type PropType } from 'vue';
4
+ /** Fetches a server-minted one-click session on click; React's `getSession` prop. */
5
+ type GetOneClickSession = (options: {
6
+ signal: AbortSignal;
7
+ }) => Promise<OneClickSession>;
4
8
  /** Payload of the `success` event — identical to React's `onSuccess` argument. */
5
9
  export interface FloPayAutomaticPaymentSuccessEvent {
6
10
  result: PaymentResult;
@@ -20,6 +24,11 @@ export interface FloPayAutomaticPaymentButtonProps {
20
24
  /** Session-bound checkout token for a supplied {@link sessionId}. */
21
25
  nonce?: string;
22
26
  createSession?: InlineSessionDraft;
27
+ /**
28
+ * Called on click to fetch a server-minted one-click session (`{ sessionId, nonce }`),
29
+ * as in React. Give exactly one of `sessionId`, `getSession` or an inline draft.
30
+ */
31
+ getSession?: GetOneClickSession;
23
32
  /**
24
33
  * @deprecated Ignored, as in React: the backend picks the customer's most
25
34
  * recent vaulted method. Accepted only so existing integrations keep working.
@@ -74,6 +83,10 @@ export declare const FloPayAutomaticPaymentButton: import("vue").DefineComponent
74
83
  type: PropType<InlineSessionDraft>;
75
84
  default: undefined;
76
85
  };
86
+ getSession: {
87
+ type: PropType<GetOneClickSession>;
88
+ default: undefined;
89
+ };
77
90
  paymentMethodId: {
78
91
  type: StringConstructor;
79
92
  default: undefined;
@@ -169,6 +182,10 @@ export declare const FloPayAutomaticPaymentButton: import("vue").DefineComponent
169
182
  type: PropType<InlineSessionDraft>;
170
183
  default: undefined;
171
184
  };
185
+ getSession: {
186
+ type: PropType<GetOneClickSession>;
187
+ default: undefined;
188
+ };
172
189
  paymentMethodId: {
173
190
  type: StringConstructor;
174
191
  default: undefined;
@@ -253,6 +270,7 @@ export declare const FloPayAutomaticPaymentButton: import("vue").DefineComponent
253
270
  sessionId: string;
254
271
  nonce: string;
255
272
  createSession: InlineSessionDraft;
273
+ getSession: GetOneClickSession;
256
274
  paymentMethodId: string;
257
275
  checkoutMethod: CheckoutButtonMethod;
258
276
  clientId: string;
@@ -275,3 +293,4 @@ export declare const FloPayAutomaticPaymentButton: import("vue").DefineComponent
275
293
  buttonsTheme: ButtonsLayoutTheme;
276
294
  buttonsStyles: ButtonsLayoutStyles;
277
295
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
296
+ export {};
package/dist/index.cjs CHANGED
@@ -4115,6 +4115,37 @@ var FloPayCheckout = (0, import_vue7.defineComponent)({
4115
4115
 
4116
4116
  // src/automatic-payment-button.ts
4117
4117
  var SUCCESS_DELAY_MS2 = SAVED_PAYMENT_SUCCESS_DELAY_MS;
4118
+ var GET_SESSION_TIMEOUT_MS = 15e3;
4119
+ function sessionFetchFailed(message, cause) {
4120
+ return new import_shared10.FloPayError(message, "api_error", {
4121
+ code: "session_fetch_failed",
4122
+ ...cause === void 0 ? {} : { cause }
4123
+ });
4124
+ }
4125
+ function settleUnlessAborted(promise, signal) {
4126
+ return new Promise((resolve, reject) => {
4127
+ const onAbort = () => reject(signal.reason);
4128
+ if (signal.aborted) {
4129
+ onAbort();
4130
+ return;
4131
+ }
4132
+ signal.addEventListener("abort", onAbort, { once: true });
4133
+ promise.then(
4134
+ (value) => {
4135
+ signal.removeEventListener("abort", onAbort);
4136
+ resolve(value);
4137
+ },
4138
+ (error) => {
4139
+ signal.removeEventListener("abort", onAbort);
4140
+ reject(error);
4141
+ }
4142
+ );
4143
+ });
4144
+ }
4145
+ function isOneClickSession(value) {
4146
+ const session = value;
4147
+ return typeof session?.sessionId === "string" && session.sessionId.length > 0 && typeof session.nonce === "string" && session.nonce.length > 0;
4148
+ }
4118
4149
  function resolveDraft(props) {
4119
4150
  if (props.createSession) return { ...props.createSession, checkoutMode: "auto" };
4120
4151
  if (!props.clientId || !props.account || !props.successUrl || !props.cancelUrl) return null;
@@ -4146,6 +4177,7 @@ var FloPayAutomaticPaymentButton = (0, import_vue8.defineComponent)({
4146
4177
  sessionId: { type: String, default: void 0 },
4147
4178
  nonce: { type: String, default: void 0 },
4148
4179
  createSession: { type: Object, default: void 0 },
4180
+ getSession: { type: Function, default: void 0 },
4149
4181
  // Deprecated and ignored, as in React (see the props interface).
4150
4182
  paymentMethodId: { type: String, default: void 0 },
4151
4183
  checkoutMethod: { type: String, default: void 0 },
@@ -4181,9 +4213,11 @@ var FloPayAutomaticPaymentButton = (0, import_vue8.defineComponent)({
4181
4213
  const fallback = (0, import_vue8.ref)(null);
4182
4214
  let mounted = true;
4183
4215
  const lifecycle = new AbortController();
4216
+ let sessionFetch = null;
4184
4217
  (0, import_vue8.onBeforeUnmount)(() => {
4185
4218
  mounted = false;
4186
4219
  lifecycle.abort();
4220
+ sessionFetch?.abort();
4187
4221
  });
4188
4222
  const billingApiUrl = () => (0, import_shared10.resolveBillingApiUrl)(props.billingApiUrl);
4189
4223
  const themeBundle = (0, import_vue8.computed)(() => (0, import_shared10.resolveTheme)(props.theme));
@@ -4251,15 +4285,50 @@ var FloPayAutomaticPaymentButton = (0, import_vue8.defineComponent)({
4251
4285
  fallback.value = { sessionId, ...nonce ? { nonce } : {} };
4252
4286
  }
4253
4287
  };
4288
+ const fetchOneClickSession = async (getSession) => {
4289
+ const controller = new AbortController();
4290
+ sessionFetch = controller;
4291
+ let timedOut = false;
4292
+ const timer = setTimeout(() => {
4293
+ timedOut = true;
4294
+ controller.abort();
4295
+ }, GET_SESSION_TIMEOUT_MS);
4296
+ try {
4297
+ const fetched = await settleUnlessAborted(
4298
+ (async () => getSession({ signal: controller.signal }))(),
4299
+ controller.signal
4300
+ );
4301
+ if (!mounted) return null;
4302
+ if (!isOneClickSession(fetched)) {
4303
+ throw sessionFetchFailed(
4304
+ "getSession must resolve to a server-minted { sessionId, nonce }."
4305
+ );
4306
+ }
4307
+ return { sessionId: fetched.sessionId, nonce: fetched.nonce };
4308
+ } catch (error) {
4309
+ if (!mounted) return null;
4310
+ if (error instanceof import_shared10.FloPayError && error.code === "session_fetch_failed") throw error;
4311
+ if (timedOut) {
4312
+ throw sessionFetchFailed(
4313
+ `getSession did not resolve within ${GET_SESSION_TIMEOUT_MS / 1e3} seconds.`
4314
+ );
4315
+ }
4316
+ throw sessionFetchFailed("Could not fetch the one-click session.", error);
4317
+ } finally {
4318
+ clearTimeout(timer);
4319
+ if (sessionFetch === controller) sessionFetch = null;
4320
+ }
4321
+ };
4254
4322
  const run = async () => {
4255
4323
  if (processing.value || props.disabled) return;
4256
4324
  fallback.value = null;
4257
4325
  notice.value = null;
4258
4326
  const draft = resolveDraft(props);
4259
- if (Boolean(props.sessionId) === Boolean(draft)) {
4327
+ const sourceCount = [props.sessionId, props.getSession, draft].filter(Boolean).length;
4328
+ if (sourceCount !== 1) {
4260
4329
  fail(
4261
4330
  new import_shared10.FloPayError(
4262
- props.sessionId ? "Provide either sessionId or create-session props, not both." : "Provide a sessionId or the props required to create an automatic payment session.",
4331
+ sourceCount > 1 ? "Provide exactly one of sessionId, getSession or create-session props." : "Provide a sessionId or the props required to create an automatic payment session.",
4263
4332
  "validation_error"
4264
4333
  ),
4265
4334
  null
@@ -4274,6 +4343,12 @@ var FloPayAutomaticPaymentButton = (0, import_vue8.defineComponent)({
4274
4343
  let result;
4275
4344
  if (props.sessionId) {
4276
4345
  result = await api.getUnifiedCheckoutSession(props.sessionId, props.nonce);
4346
+ } else if (props.getSession) {
4347
+ const fetched = await fetchOneClickSession(props.getSession);
4348
+ if (!fetched) return;
4349
+ sessionId = fetched.sessionId;
4350
+ nonce = fetched.nonce;
4351
+ result = await api.getUnifiedCheckoutSession(fetched.sessionId, fetched.nonce);
4277
4352
  } else {
4278
4353
  try {
4279
4354
  result = await api.createAndFetchSession(draft);
package/dist/index.mjs CHANGED
@@ -3015,6 +3015,37 @@ var FloPayCheckout = defineComponent4({
3015
3015
 
3016
3016
  // src/automatic-payment-button.ts
3017
3017
  var SUCCESS_DELAY_MS2 = SAVED_PAYMENT_SUCCESS_DELAY_MS;
3018
+ var GET_SESSION_TIMEOUT_MS = 15e3;
3019
+ function sessionFetchFailed(message, cause) {
3020
+ return new FloPayError5(message, "api_error", {
3021
+ code: "session_fetch_failed",
3022
+ ...cause === void 0 ? {} : { cause }
3023
+ });
3024
+ }
3025
+ function settleUnlessAborted(promise, signal) {
3026
+ return new Promise((resolve, reject) => {
3027
+ const onAbort = () => reject(signal.reason);
3028
+ if (signal.aborted) {
3029
+ onAbort();
3030
+ return;
3031
+ }
3032
+ signal.addEventListener("abort", onAbort, { once: true });
3033
+ promise.then(
3034
+ (value) => {
3035
+ signal.removeEventListener("abort", onAbort);
3036
+ resolve(value);
3037
+ },
3038
+ (error) => {
3039
+ signal.removeEventListener("abort", onAbort);
3040
+ reject(error);
3041
+ }
3042
+ );
3043
+ });
3044
+ }
3045
+ function isOneClickSession(value) {
3046
+ const session = value;
3047
+ return typeof session?.sessionId === "string" && session.sessionId.length > 0 && typeof session.nonce === "string" && session.nonce.length > 0;
3048
+ }
3018
3049
  function resolveDraft(props) {
3019
3050
  if (props.createSession) return { ...props.createSession, checkoutMode: "auto" };
3020
3051
  if (!props.clientId || !props.account || !props.successUrl || !props.cancelUrl) return null;
@@ -3046,6 +3077,7 @@ var FloPayAutomaticPaymentButton = defineComponent5({
3046
3077
  sessionId: { type: String, default: void 0 },
3047
3078
  nonce: { type: String, default: void 0 },
3048
3079
  createSession: { type: Object, default: void 0 },
3080
+ getSession: { type: Function, default: void 0 },
3049
3081
  // Deprecated and ignored, as in React (see the props interface).
3050
3082
  paymentMethodId: { type: String, default: void 0 },
3051
3083
  checkoutMethod: { type: String, default: void 0 },
@@ -3081,9 +3113,11 @@ var FloPayAutomaticPaymentButton = defineComponent5({
3081
3113
  const fallback = ref5(null);
3082
3114
  let mounted = true;
3083
3115
  const lifecycle = new AbortController();
3116
+ let sessionFetch = null;
3084
3117
  onBeforeUnmount5(() => {
3085
3118
  mounted = false;
3086
3119
  lifecycle.abort();
3120
+ sessionFetch?.abort();
3087
3121
  });
3088
3122
  const billingApiUrl = () => resolveBillingApiUrl4(props.billingApiUrl);
3089
3123
  const themeBundle = computed3(() => resolveTheme3(props.theme));
@@ -3151,15 +3185,50 @@ var FloPayAutomaticPaymentButton = defineComponent5({
3151
3185
  fallback.value = { sessionId, ...nonce ? { nonce } : {} };
3152
3186
  }
3153
3187
  };
3188
+ const fetchOneClickSession = async (getSession) => {
3189
+ const controller = new AbortController();
3190
+ sessionFetch = controller;
3191
+ let timedOut = false;
3192
+ const timer = setTimeout(() => {
3193
+ timedOut = true;
3194
+ controller.abort();
3195
+ }, GET_SESSION_TIMEOUT_MS);
3196
+ try {
3197
+ const fetched = await settleUnlessAborted(
3198
+ (async () => getSession({ signal: controller.signal }))(),
3199
+ controller.signal
3200
+ );
3201
+ if (!mounted) return null;
3202
+ if (!isOneClickSession(fetched)) {
3203
+ throw sessionFetchFailed(
3204
+ "getSession must resolve to a server-minted { sessionId, nonce }."
3205
+ );
3206
+ }
3207
+ return { sessionId: fetched.sessionId, nonce: fetched.nonce };
3208
+ } catch (error) {
3209
+ if (!mounted) return null;
3210
+ if (error instanceof FloPayError5 && error.code === "session_fetch_failed") throw error;
3211
+ if (timedOut) {
3212
+ throw sessionFetchFailed(
3213
+ `getSession did not resolve within ${GET_SESSION_TIMEOUT_MS / 1e3} seconds.`
3214
+ );
3215
+ }
3216
+ throw sessionFetchFailed("Could not fetch the one-click session.", error);
3217
+ } finally {
3218
+ clearTimeout(timer);
3219
+ if (sessionFetch === controller) sessionFetch = null;
3220
+ }
3221
+ };
3154
3222
  const run = async () => {
3155
3223
  if (processing.value || props.disabled) return;
3156
3224
  fallback.value = null;
3157
3225
  notice.value = null;
3158
3226
  const draft = resolveDraft(props);
3159
- if (Boolean(props.sessionId) === Boolean(draft)) {
3227
+ const sourceCount = [props.sessionId, props.getSession, draft].filter(Boolean).length;
3228
+ if (sourceCount !== 1) {
3160
3229
  fail(
3161
3230
  new FloPayError5(
3162
- props.sessionId ? "Provide either sessionId or create-session props, not both." : "Provide a sessionId or the props required to create an automatic payment session.",
3231
+ sourceCount > 1 ? "Provide exactly one of sessionId, getSession or create-session props." : "Provide a sessionId or the props required to create an automatic payment session.",
3163
3232
  "validation_error"
3164
3233
  ),
3165
3234
  null
@@ -3174,6 +3243,12 @@ var FloPayAutomaticPaymentButton = defineComponent5({
3174
3243
  let result;
3175
3244
  if (props.sessionId) {
3176
3245
  result = await api.getUnifiedCheckoutSession(props.sessionId, props.nonce);
3246
+ } else if (props.getSession) {
3247
+ const fetched = await fetchOneClickSession(props.getSession);
3248
+ if (!fetched) return;
3249
+ sessionId = fetched.sessionId;
3250
+ nonce = fetched.nonce;
3251
+ result = await api.getUnifiedCheckoutSession(fetched.sessionId, fetched.nonce);
3177
3252
  } else {
3178
3253
  try {
3179
3254
  result = await api.createAndFetchSession(draft);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flopay/vue",
3
- "version": "1.16.4",
3
+ "version": "1.16.5",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "publishConfig": {
@@ -36,8 +36,8 @@
36
36
  "dist"
37
37
  ],
38
38
  "dependencies": {
39
- "@flopay/js": "1.16.4",
40
- "@flopay/shared": "1.16.4",
39
+ "@flopay/js": "1.16.5",
40
+ "@flopay/shared": "1.16.5",
41
41
  "@paypal/paypal-js": "^11.3.0",
42
42
  "@stripe/stripe-js": "^10.3.0"
43
43
  },