@moonbase.sh/vue 0.4.26 → 0.4.27

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.
package/dist/index.cjs CHANGED
@@ -136,6 +136,65 @@ function mountCheckout(endpoint) {
136
136
  };
137
137
  }
138
138
 
139
+ // src/utils/iframe-subscription.ts
140
+ var import_zod2 = require("zod");
141
+ var closeEventSchema2 = import_zod2.z.object({
142
+ source: import_zod2.z.literal("moonbase-subscription"),
143
+ event: import_zod2.z.literal("close"),
144
+ completed: import_zod2.z.boolean()
145
+ });
146
+ var updatedPaymentMethodSchema = import_zod2.z.object({
147
+ source: import_zod2.z.literal("moonbase-subscription"),
148
+ event: import_zod2.z.literal("updated-payment-method")
149
+ });
150
+ var eventSchema2 = import_zod2.z.discriminatedUnion("event", [closeEventSchema2, updatedPaymentMethodSchema]);
151
+ function mountSubscription(endpoint) {
152
+ if (typeof window === "undefined") {
153
+ console.warn("Can not mount subscription server side");
154
+ return;
155
+ }
156
+ if (document.getElementById("moonbase-subscription"))
157
+ return;
158
+ const iframe = document.createElement("iframe");
159
+ iframe.id = "moonbase-subscription";
160
+ iframe.src = endpoint;
161
+ iframe.style.position = "fixed";
162
+ iframe.style.top = "0px";
163
+ iframe.style.left = "0px";
164
+ iframe.style.right = "0px";
165
+ iframe.style.border = "none";
166
+ iframe.style.colorScheme = "normal";
167
+ iframe.style.backgroundColor = "transparent";
168
+ iframe.style.zIndex = Number.MAX_SAFE_INTEGER.toString();
169
+ iframe.setAttribute("background", "transparent");
170
+ iframe.setAttribute("frameborder", "0");
171
+ iframe.setAttribute("allowtransparency", "true");
172
+ iframe.setAttribute("allow", "payment");
173
+ iframe.height = "100%";
174
+ iframe.width = "100%";
175
+ document.body.append(iframe);
176
+ document.documentElement.style.overflow = "hidden";
177
+ window.onmessage = function(e) {
178
+ var _a;
179
+ try {
180
+ const evnt = eventSchema2.parse(e.data);
181
+ if (evnt.event === "close") {
182
+ if (document.body.contains(iframe)) {
183
+ document.documentElement.style.removeProperty("overflow");
184
+ document.body.removeChild(iframe);
185
+ }
186
+ window.dispatchEvent(new CustomEvent("moonbase-subscription-closed", {
187
+ detail: evnt
188
+ }));
189
+ }
190
+ } catch (err) {
191
+ if (((_a = e.data) == null ? void 0 : _a.source) === "moonbase-subscription") {
192
+ console.error("Could not parse event:", e.data, err);
193
+ }
194
+ }
195
+ };
196
+ }
197
+
139
198
  // src/context.ts
140
199
  var _StorefrontContextImpl = class _StorefrontContextImpl {
141
200
  constructor(configuration, client, stateFactory) {
@@ -157,6 +216,7 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
157
216
  });
158
217
  this.storefront = stateFactory(_StorefrontContextImpl.storefrontKey, {
159
218
  suggestedCurrency: "",
219
+ enabledCurrencies: [],
160
220
  bundles: [],
161
221
  products: [],
162
222
  offers: []
@@ -211,6 +271,7 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
211
271
  } else {
212
272
  this.storefront = stateFactory(_StorefrontContextImpl.storefrontKey, {
213
273
  suggestedCurrency: "",
274
+ enabledCurrencies: [],
214
275
  bundles: [],
215
276
  products: [],
216
277
  offers: []
@@ -235,6 +296,15 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
235
296
  mountCheckout(finalEndpoint.toString());
236
297
  window.history.replaceState(null, "", window.location.href.split("?")[0]);
237
298
  }
299
+ if (intent === "update_subscription" && urlParams2.get("mb_complete") && endpoint) {
300
+ const parameters = Object.fromEntries([...urlParams2.entries()].filter(([key]) => !key.startsWith("mb_")));
301
+ const finalEndpoint = new URL(decodeURIComponent(endpoint));
302
+ for (const prop of Object.keys(parameters)) {
303
+ finalEndpoint.searchParams.append(prop, parameters[prop]);
304
+ }
305
+ mountSubscription(finalEndpoint.toString());
306
+ window.history.replaceState(null, "", window.location.href.split("?")[0]);
307
+ }
238
308
  }
239
309
  window.addEventListener("moonbase-checkout-completed", (e) => {
240
310
  const order = e.detail.order;
@@ -301,7 +371,7 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
301
371
  localStorage.setItem(_StorefrontContextImpl.storefrontKey, JSON.stringify(latestStorefront));
302
372
  this.storefront.value = latestStorefront;
303
373
  this.loadedStorefront.value = true;
304
- if (!this.currentOrder.value.currency) {
374
+ if (!this.currentOrder.value.currency || !latestStorefront.enabledCurrencies.includes(this.currentOrder.value.currency)) {
305
375
  this.currentOrder.value.currency = latestStorefront.suggestedCurrency;
306
376
  }
307
377
  this.currentOrder.value.items = this.enrichLineItems(this.currentOrder.value.items);
@@ -352,6 +422,9 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
352
422
  latestOrder.items = this.enrichLineItems(latestOrder.items);
353
423
  if (typeof window !== "undefined")
354
424
  localStorage.setItem(_StorefrontContextImpl.sessionKey, JSON.stringify(latestOrder));
425
+ if (this.storefront.value && !this.storefront.value.enabledCurrencies.includes(latestOrder.currency)) {
426
+ latestOrder.currency = this.storefront.value.suggestedCurrency;
427
+ }
355
428
  this.currentOrder.value = latestOrder;
356
429
  }
357
430
  }
@@ -933,6 +1006,30 @@ function useInventory(context) {
933
1006
  }, { once: true });
934
1007
  });
935
1008
  }
1009
+ },
1010
+ updateSubscriptionPaymentMethod: async (subscription, options) => {
1011
+ var _a;
1012
+ const fallbackPath = typeof window !== "undefined" ? window.location.pathname : "";
1013
+ const absoluteReturnUrl = new URL((_a = options.returnUrl) != null ? _a : fallbackPath, document.baseURI).href;
1014
+ const response = await storefront.client.inventory.subscriptions.getById(subscription.id, {
1015
+ toUpdatePaymentMethod: true,
1016
+ returnUrl: absoluteReturnUrl
1017
+ });
1018
+ window.dispatchEvent(new CustomEvent("moonbase-subscription-payment-method-update-initiated", {
1019
+ detail: { subscription: (0, import_vue8.toRaw)(subscription) }
1020
+ }));
1021
+ if (response.embeddedUpdatePaymentUrl && typeof window !== "undefined") {
1022
+ const embeddedUrl = response.embeddedUpdatePaymentUrl;
1023
+ return new Promise((resolve) => {
1024
+ mountSubscription(embeddedUrl.replace("acme-co.beta.moonbase.sh", "localhost:3002"));
1025
+ window.addEventListener("moonbase-subscription-closed", (e) => {
1026
+ const evnt = e;
1027
+ resolve({ completed: evnt.detail.completed });
1028
+ }, { once: true });
1029
+ });
1030
+ } else {
1031
+ throw new Error("No update URL found");
1032
+ }
936
1033
  }
937
1034
  };
938
1035
  }
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _moonbase_sh_storefront_api from '@moonbase.sh/storefront-api';
2
- import { MoonbaseConfiguration, Storefront, Order, User, UrchinTrackingModule, MoonbaseClient, LineItem, OpenOrder, Money, CompletedOrder, ActivationRequest, Address, CommunicationPreferences, StorefrontBundle, StorefrontProduct, PricingVariation, StorefrontOffer, ActivationMethod, Activation, Download, Vendor } from '@moonbase.sh/storefront-api';
2
+ import { MoonbaseConfiguration, Storefront, Order, User, UrchinTrackingModule, MoonbaseClient, LineItem, OpenOrder, Money, CompletedOrder, ActivationRequest, Address, CommunicationPreferences, StorefrontBundle, StorefrontProduct, PricingVariation, StorefrontOffer, ActivationMethod, Activation, Download, Subscription, Vendor } from '@moonbase.sh/storefront-api';
3
3
  export * from '@moonbase.sh/storefront-api';
4
4
  import * as vue from 'vue';
5
5
  import { Ref, UnwrapRef, Plugin, App, MaybeRef, InjectionKey } from 'vue';
@@ -901,6 +901,8 @@ declare function useInventory(context?: StorefrontContext): {
901
901
  }[] | undefined;
902
902
  };
903
903
  };
904
+ paymentMethod?: string | undefined;
905
+ embeddedUpdatePaymentUrl?: string | undefined;
904
906
  }>>;
905
907
  getSubscription: (subscriptionId: string) => Promise<{
906
908
  id: string;
@@ -1207,6 +1209,8 @@ declare function useInventory(context?: StorefrontContext): {
1207
1209
  }[] | undefined;
1208
1210
  };
1209
1211
  };
1212
+ paymentMethod?: string | undefined;
1213
+ embeddedUpdatePaymentUrl?: string | undefined;
1210
1214
  }>;
1211
1215
  cancelSubscription: (subscriptionId: string) => Promise<{
1212
1216
  id: string;
@@ -1513,6 +1517,8 @@ declare function useInventory(context?: StorefrontContext): {
1513
1517
  }[] | undefined;
1514
1518
  };
1515
1519
  };
1520
+ paymentMethod?: string | undefined;
1521
+ embeddedUpdatePaymentUrl?: string | undefined;
1516
1522
  }>;
1517
1523
  renewSubscription: (subscriptionId: string, options: {
1518
1524
  redirect: boolean;
@@ -1521,6 +1527,11 @@ declare function useInventory(context?: StorefrontContext): {
1521
1527
  next: string | undefined;
1522
1528
  completed: boolean;
1523
1529
  }>;
1530
+ updateSubscriptionPaymentMethod: (subscription: Subscription, options: {
1531
+ returnUrl?: string;
1532
+ }) => Promise<{
1533
+ completed: boolean;
1534
+ } | undefined>;
1524
1535
  };
1525
1536
 
1526
1537
  declare function useOffer(offerId: string, context?: StorefrontContext): Ref<StorefrontOffer | null>;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _moonbase_sh_storefront_api from '@moonbase.sh/storefront-api';
2
- import { MoonbaseConfiguration, Storefront, Order, User, UrchinTrackingModule, MoonbaseClient, LineItem, OpenOrder, Money, CompletedOrder, ActivationRequest, Address, CommunicationPreferences, StorefrontBundle, StorefrontProduct, PricingVariation, StorefrontOffer, ActivationMethod, Activation, Download, Vendor } from '@moonbase.sh/storefront-api';
2
+ import { MoonbaseConfiguration, Storefront, Order, User, UrchinTrackingModule, MoonbaseClient, LineItem, OpenOrder, Money, CompletedOrder, ActivationRequest, Address, CommunicationPreferences, StorefrontBundle, StorefrontProduct, PricingVariation, StorefrontOffer, ActivationMethod, Activation, Download, Subscription, Vendor } from '@moonbase.sh/storefront-api';
3
3
  export * from '@moonbase.sh/storefront-api';
4
4
  import * as vue from 'vue';
5
5
  import { Ref, UnwrapRef, Plugin, App, MaybeRef, InjectionKey } from 'vue';
@@ -901,6 +901,8 @@ declare function useInventory(context?: StorefrontContext): {
901
901
  }[] | undefined;
902
902
  };
903
903
  };
904
+ paymentMethod?: string | undefined;
905
+ embeddedUpdatePaymentUrl?: string | undefined;
904
906
  }>>;
905
907
  getSubscription: (subscriptionId: string) => Promise<{
906
908
  id: string;
@@ -1207,6 +1209,8 @@ declare function useInventory(context?: StorefrontContext): {
1207
1209
  }[] | undefined;
1208
1210
  };
1209
1211
  };
1212
+ paymentMethod?: string | undefined;
1213
+ embeddedUpdatePaymentUrl?: string | undefined;
1210
1214
  }>;
1211
1215
  cancelSubscription: (subscriptionId: string) => Promise<{
1212
1216
  id: string;
@@ -1513,6 +1517,8 @@ declare function useInventory(context?: StorefrontContext): {
1513
1517
  }[] | undefined;
1514
1518
  };
1515
1519
  };
1520
+ paymentMethod?: string | undefined;
1521
+ embeddedUpdatePaymentUrl?: string | undefined;
1516
1522
  }>;
1517
1523
  renewSubscription: (subscriptionId: string, options: {
1518
1524
  redirect: boolean;
@@ -1521,6 +1527,11 @@ declare function useInventory(context?: StorefrontContext): {
1521
1527
  next: string | undefined;
1522
1528
  completed: boolean;
1523
1529
  }>;
1530
+ updateSubscriptionPaymentMethod: (subscription: Subscription, options: {
1531
+ returnUrl?: string;
1532
+ }) => Promise<{
1533
+ completed: boolean;
1534
+ } | undefined>;
1524
1535
  };
1525
1536
 
1526
1537
  declare function useOffer(offerId: string, context?: StorefrontContext): Ref<StorefrontOffer | null>;
package/dist/index.js CHANGED
@@ -104,6 +104,65 @@ function mountCheckout(endpoint) {
104
104
  };
105
105
  }
106
106
 
107
+ // src/utils/iframe-subscription.ts
108
+ import { z as z2 } from "zod";
109
+ var closeEventSchema2 = z2.object({
110
+ source: z2.literal("moonbase-subscription"),
111
+ event: z2.literal("close"),
112
+ completed: z2.boolean()
113
+ });
114
+ var updatedPaymentMethodSchema = z2.object({
115
+ source: z2.literal("moonbase-subscription"),
116
+ event: z2.literal("updated-payment-method")
117
+ });
118
+ var eventSchema2 = z2.discriminatedUnion("event", [closeEventSchema2, updatedPaymentMethodSchema]);
119
+ function mountSubscription(endpoint) {
120
+ if (typeof window === "undefined") {
121
+ console.warn("Can not mount subscription server side");
122
+ return;
123
+ }
124
+ if (document.getElementById("moonbase-subscription"))
125
+ return;
126
+ const iframe = document.createElement("iframe");
127
+ iframe.id = "moonbase-subscription";
128
+ iframe.src = endpoint;
129
+ iframe.style.position = "fixed";
130
+ iframe.style.top = "0px";
131
+ iframe.style.left = "0px";
132
+ iframe.style.right = "0px";
133
+ iframe.style.border = "none";
134
+ iframe.style.colorScheme = "normal";
135
+ iframe.style.backgroundColor = "transparent";
136
+ iframe.style.zIndex = Number.MAX_SAFE_INTEGER.toString();
137
+ iframe.setAttribute("background", "transparent");
138
+ iframe.setAttribute("frameborder", "0");
139
+ iframe.setAttribute("allowtransparency", "true");
140
+ iframe.setAttribute("allow", "payment");
141
+ iframe.height = "100%";
142
+ iframe.width = "100%";
143
+ document.body.append(iframe);
144
+ document.documentElement.style.overflow = "hidden";
145
+ window.onmessage = function(e) {
146
+ var _a;
147
+ try {
148
+ const evnt = eventSchema2.parse(e.data);
149
+ if (evnt.event === "close") {
150
+ if (document.body.contains(iframe)) {
151
+ document.documentElement.style.removeProperty("overflow");
152
+ document.body.removeChild(iframe);
153
+ }
154
+ window.dispatchEvent(new CustomEvent("moonbase-subscription-closed", {
155
+ detail: evnt
156
+ }));
157
+ }
158
+ } catch (err) {
159
+ if (((_a = e.data) == null ? void 0 : _a.source) === "moonbase-subscription") {
160
+ console.error("Could not parse event:", e.data, err);
161
+ }
162
+ }
163
+ };
164
+ }
165
+
107
166
  // src/context.ts
108
167
  var _StorefrontContextImpl = class _StorefrontContextImpl {
109
168
  constructor(configuration, client, stateFactory) {
@@ -125,6 +184,7 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
125
184
  });
126
185
  this.storefront = stateFactory(_StorefrontContextImpl.storefrontKey, {
127
186
  suggestedCurrency: "",
187
+ enabledCurrencies: [],
128
188
  bundles: [],
129
189
  products: [],
130
190
  offers: []
@@ -179,6 +239,7 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
179
239
  } else {
180
240
  this.storefront = stateFactory(_StorefrontContextImpl.storefrontKey, {
181
241
  suggestedCurrency: "",
242
+ enabledCurrencies: [],
182
243
  bundles: [],
183
244
  products: [],
184
245
  offers: []
@@ -203,6 +264,15 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
203
264
  mountCheckout(finalEndpoint.toString());
204
265
  window.history.replaceState(null, "", window.location.href.split("?")[0]);
205
266
  }
267
+ if (intent === "update_subscription" && urlParams2.get("mb_complete") && endpoint) {
268
+ const parameters = Object.fromEntries([...urlParams2.entries()].filter(([key]) => !key.startsWith("mb_")));
269
+ const finalEndpoint = new URL(decodeURIComponent(endpoint));
270
+ for (const prop of Object.keys(parameters)) {
271
+ finalEndpoint.searchParams.append(prop, parameters[prop]);
272
+ }
273
+ mountSubscription(finalEndpoint.toString());
274
+ window.history.replaceState(null, "", window.location.href.split("?")[0]);
275
+ }
206
276
  }
207
277
  window.addEventListener("moonbase-checkout-completed", (e) => {
208
278
  const order = e.detail.order;
@@ -269,7 +339,7 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
269
339
  localStorage.setItem(_StorefrontContextImpl.storefrontKey, JSON.stringify(latestStorefront));
270
340
  this.storefront.value = latestStorefront;
271
341
  this.loadedStorefront.value = true;
272
- if (!this.currentOrder.value.currency) {
342
+ if (!this.currentOrder.value.currency || !latestStorefront.enabledCurrencies.includes(this.currentOrder.value.currency)) {
273
343
  this.currentOrder.value.currency = latestStorefront.suggestedCurrency;
274
344
  }
275
345
  this.currentOrder.value.items = this.enrichLineItems(this.currentOrder.value.items);
@@ -320,6 +390,9 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
320
390
  latestOrder.items = this.enrichLineItems(latestOrder.items);
321
391
  if (typeof window !== "undefined")
322
392
  localStorage.setItem(_StorefrontContextImpl.sessionKey, JSON.stringify(latestOrder));
393
+ if (this.storefront.value && !this.storefront.value.enabledCurrencies.includes(latestOrder.currency)) {
394
+ latestOrder.currency = this.storefront.value.suggestedCurrency;
395
+ }
323
396
  this.currentOrder.value = latestOrder;
324
397
  }
325
398
  }
@@ -840,7 +913,7 @@ async function useCheckout(items, options, context) {
840
913
  }
841
914
 
842
915
  // src/composables/useInventory.ts
843
- import { inject as inject7 } from "vue";
916
+ import { inject as inject7, toRaw } from "vue";
844
917
  function useInventory(context) {
845
918
  const storefront = context != null ? context : inject7(storefrontKey);
846
919
  if (!storefront)
@@ -901,6 +974,30 @@ function useInventory(context) {
901
974
  }, { once: true });
902
975
  });
903
976
  }
977
+ },
978
+ updateSubscriptionPaymentMethod: async (subscription, options) => {
979
+ var _a;
980
+ const fallbackPath = typeof window !== "undefined" ? window.location.pathname : "";
981
+ const absoluteReturnUrl = new URL((_a = options.returnUrl) != null ? _a : fallbackPath, document.baseURI).href;
982
+ const response = await storefront.client.inventory.subscriptions.getById(subscription.id, {
983
+ toUpdatePaymentMethod: true,
984
+ returnUrl: absoluteReturnUrl
985
+ });
986
+ window.dispatchEvent(new CustomEvent("moonbase-subscription-payment-method-update-initiated", {
987
+ detail: { subscription: toRaw(subscription) }
988
+ }));
989
+ if (response.embeddedUpdatePaymentUrl && typeof window !== "undefined") {
990
+ const embeddedUrl = response.embeddedUpdatePaymentUrl;
991
+ return new Promise((resolve) => {
992
+ mountSubscription(embeddedUrl.replace("acme-co.beta.moonbase.sh", "localhost:3002"));
993
+ window.addEventListener("moonbase-subscription-closed", (e) => {
994
+ const evnt = e;
995
+ resolve({ completed: evnt.detail.completed });
996
+ }, { once: true });
997
+ });
998
+ } else {
999
+ throw new Error("No update URL found");
1000
+ }
904
1001
  }
905
1002
  };
906
1003
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moonbase.sh/vue",
3
3
  "type": "module",
4
- "version": "0.4.26",
4
+ "version": "0.4.27",
5
5
  "description": "Package to let you build vue.js storefronts with Moonbase.sh as payment and delivery provider",
6
6
  "author": "Tobias Lønnerød Madsen <m@dsen.tv>",
7
7
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "@vue/devtools-api": "^6.6.3",
20
20
  "uuid": "^9.0.1",
21
21
  "zod": "^3.23.8",
22
- "@moonbase.sh/storefront-api": "0.4.26"
22
+ "@moonbase.sh/storefront-api": "0.4.27"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/uuid": "^9.0.8",