@dotted-labs/ngx-supabase-stripe 0.1.7 → 0.1.8

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,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { InjectionToken, inject, Injectable, computed, Component, input, output, effect, signal } from '@angular/core';
2
+ import { InjectionToken, inject, Injectable, computed, Component, input, output, signal } from '@angular/core';
3
3
  import { signalStore, withState, withComputed, withMethods, patchState, withHooks } from '@ngrx/signals';
4
4
  import { loadStripe } from '@stripe/stripe-js';
5
5
  import { createClient } from '@supabase/supabase-js';
@@ -310,6 +310,7 @@ class StripeClientService {
310
310
  config = inject(STRIPE_CONFIG);
311
311
  supabase = inject(SupabaseClientService);
312
312
  stripe;
313
+ embeddedCheckout = null;
313
314
  constructor() {
314
315
  this.stripe = loadStripe(this.config.publishableKey);
315
316
  console.log('🔌 [StripeClientService]: Loaded Stripe Client from @stripe/stripe-js');
@@ -320,6 +321,32 @@ class StripeClientService {
320
321
  async getStripe() {
321
322
  return this.stripe;
322
323
  }
324
+ /**
325
+ * Initialize the embedded checkout
326
+ * @param clientSecret The client secret for the checkout session
327
+ */
328
+ async initEmbeddedCheckout(clientSecret) {
329
+ const stripe = await this.getStripe();
330
+ this.embeddedCheckout = await stripe?.initEmbeddedCheckout({
331
+ clientSecret
332
+ }) ?? null;
333
+ }
334
+ /**
335
+ * Mount the embedded checkout
336
+ * @param elementId The ID of the element to mount the checkout on
337
+ */
338
+ mountEmbeddedCheckout(elementId = '#embedded-checkout') {
339
+ this.embeddedCheckout?.mount(elementId);
340
+ }
341
+ /**
342
+ * Destroy the embedded checkout
343
+ */
344
+ destroyEmbeddedCheckout() {
345
+ console.log('🧹 [StripeClientService] destroying embedded checkout');
346
+ this.embeddedCheckout?.destroy();
347
+ this.embeddedCheckout = null;
348
+ console.log('🧹 [StripeClientService] destroyed embedded checkout');
349
+ }
323
350
  /**
324
351
  * Create a checkout session
325
352
  * @param priceId The price ID for the subscription
@@ -646,7 +673,6 @@ function parseProduct(product, prices = []) {
646
673
 
647
674
  const initialSubscriptionState = {
648
675
  subscriptions: null,
649
- embeddedSubscription: null,
650
676
  currentSubscription: null,
651
677
  status: 'idle',
652
678
  error: null,
@@ -687,13 +713,10 @@ const SubscriptionsStore = signalStore({ providedIn: 'root' }, withState(initial
687
713
  });
688
714
  }
689
715
  else {
690
- const embeddedCheckout = await stripe.initEmbeddedCheckout({
691
- clientSecret: clientSecret
692
- });
693
- //embeddedCheckout?.mount('#embedded-checkout');
716
+ await stripeService.initEmbeddedCheckout(clientSecret);
717
+ stripeService.mountEmbeddedCheckout();
694
718
  patchState(store, {
695
719
  status: 'success',
696
- embeddedSubscription: embeddedCheckout
697
720
  });
698
721
  }
699
722
  }
@@ -807,9 +830,7 @@ const SubscriptionsStore = signalStore({ providedIn: 'root' }, withState(initial
807
830
  * Destroy the embedded subscription
808
831
  */
809
832
  destroyEmbeddedSubscription() {
810
- console.log('🧹 [SubscriptionsStore] destroying embedded subscription', store.embeddedSubscription());
811
- store.embeddedSubscription()?.destroy();
812
- console.log('🧹 [SubscriptionsStore] destroyed embedded subscription', store.embeddedSubscription());
833
+ stripeService.destroyEmbeddedCheckout();
813
834
  },
814
835
  /**
815
836
  * Reset the store to initial state
@@ -979,7 +1000,6 @@ function parsePaymentIntent(paymentIntent) {
979
1000
  }
980
1001
 
981
1002
  const initialCheckoutState = {
982
- embeddedCheckout: null,
983
1003
  status: 'idle',
984
1004
  sessionId: null,
985
1005
  returnPagePath: '/return',
@@ -1023,13 +1043,10 @@ const CheckoutStore = signalStore({ providedIn: 'root' }, withState(initialCheck
1023
1043
  });
1024
1044
  }
1025
1045
  else {
1026
- const embeddedCheckout = await stripe.initEmbeddedCheckout({
1027
- clientSecret: clientSecret
1028
- });
1029
- embeddedCheckout?.mount('#embedded-checkout');
1046
+ await stripeService.initEmbeddedCheckout(clientSecret);
1047
+ stripeService.mountEmbeddedCheckout();
1030
1048
  patchState(store, {
1031
1049
  status: 'success',
1032
- embeddedCheckout
1033
1050
  });
1034
1051
  }
1035
1052
  }
@@ -1064,9 +1081,7 @@ const CheckoutStore = signalStore({ providedIn: 'root' }, withState(initialCheck
1064
1081
  * Destroy the embedded checkout
1065
1082
  */
1066
1083
  destroyEmbeddedCheckout() {
1067
- console.log('🧹 [CheckoutStore] destroying embedded checkout', store.embeddedCheckout());
1068
- store.embeddedCheckout()?.destroy();
1069
- console.log('🧹 [CheckoutStore] destroyed embedded checkout', store.embeddedCheckout());
1084
+ stripeService.destroyEmbeddedCheckout();
1070
1085
  },
1071
1086
  /**
1072
1087
  * Reset the store to initial state
@@ -1078,8 +1093,7 @@ const CheckoutStore = signalStore({ providedIn: 'root' }, withState(initialCheck
1078
1093
  onInit() {
1079
1094
  console.log('🔍 [CheckoutStore] initialized');
1080
1095
  },
1081
- onDestroy(store) {
1082
- store.embeddedCheckout()?.destroy();
1096
+ onDestroy() {
1083
1097
  console.log('🧹 [CheckoutStore] destroyed');
1084
1098
  }
1085
1099
  }));
@@ -1255,15 +1269,6 @@ class EmbeddedSubscriptionComponent {
1255
1269
  priceId = input.required();
1256
1270
  returnPagePath = input('/subscription-return');
1257
1271
  customer = computed(() => this.customerStore.customer().data);
1258
- constructor() {
1259
- effect(() => {
1260
- const embeddedSubscription = this.subscriptionsStore.embeddedSubscription();
1261
- if (embeddedSubscription) {
1262
- console.log('🎯 [EmbeddedSubscriptionComponent] Mounting embedded subscription');
1263
- embeddedSubscription.mount('#embedded-checkout');
1264
- }
1265
- }, { allowSignalWrites: false });
1266
- }
1267
1272
  async ngOnInit() {
1268
1273
  this.createSubscription();
1269
1274
  }
@@ -1281,7 +1286,7 @@ class EmbeddedSubscriptionComponent {
1281
1286
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: EmbeddedSubscriptionComponent, decorators: [{
1282
1287
  type: Component,
1283
1288
  args: [{ selector: 'lib-embedded-subscription', standalone: true, imports: [CommonModule, EmbeddedSkeletonComponent], template: "<div class=\"subscription-container\">\n <div class=\"info-panel p-4 bg-primary-100 rounded-lg mb-4\">\n <div class=\"flex items-center\">\n <svg xmlns=\"http://www.w3.org/2000/svg\" class=\"h-6 w-6 text-primary mr-2\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\">\n <path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z\" />\n </svg>\n <p class=\"text-primary-700\">Plan de suscripci\u00F3n: <span class=\"font-semibold\">{{ priceId() }}</span></p>\n </div>\n </div>\n \n @if (subscriptionsStore.isStatusLoading()) {\n <lib-embedded-skeleton />\n }\n \n @if (subscriptionsStore.isStatusError()) {\n <div class=\"error\">\n <p>Error: {{ subscriptionsStore.error() }}</p>\n </div>\n }\n \n <div id=\"embedded-checkout\" class=\"mt-4\"></div>\n</div> " }]
1284
- }], ctorParameters: () => [] });
1289
+ }] });
1285
1290
 
1286
1291
  class SubscriptionReturnPageComponent {
1287
1292
  route = inject(ActivatedRoute);