Skip to content

Commit f38724e

Browse files
committed
Changed subscription logic to use backend endpoint
1 parent 0242600 commit f38724e

File tree

1 file changed

+85
-83
lines changed

1 file changed

+85
-83
lines changed

packages/firecms_cloud/src/hooks/useSubscriptionsForUserController.tsx

Lines changed: 85 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addDoc, collection, Firestore, getDocs, getFirestore, onSnapshot, query, where } from "@firebase/firestore";
1+
import { collection, Firestore, getDocs, getFirestore, onSnapshot, query, where } from "@firebase/firestore";
22
import { useEffect, useRef, useState } from "react";
33
import { ProductPrice, ProductWithPrices, Subscription, SubscriptionType } from "../types/subscriptions";
44
import { useFireCMSBackend } from "./useFireCMSBackend";
@@ -117,14 +117,7 @@ export function useSubscriptionsForUserController(): SubscriptionsController {
117117
});
118118
}, [firestoreRef, userId]);
119119

120-
const subscribe = async ({
121-
projectId,
122-
licenseId,
123-
productPrice,
124-
quantity,
125-
onCheckoutSessionReady,
126-
type
127-
}: {
120+
const subscribe = async (props: {
128121
projectId?: string,
129122
licenseId?: string,
130123
quantity?: number,
@@ -133,83 +126,92 @@ export function useSubscriptionsForUserController(): SubscriptionsController {
133126
type: SubscriptionType
134127
}
135128
) => {
136-
// console.debug("Subscribing to product", props);
137-
// const productPriceId = productPrice.id;
138-
// const productPriceType = productPrice.type;
139-
// try {
140-
// const sessionUrl: string = await projectsApi.createStripeNewSubscriptionLink({
141-
// ...props,
142-
// productPriceId,
143-
// productPriceType
144-
// });
145-
// onCheckoutSessionReady(sessionUrl, undefined);
146-
// } catch (e: any) {
147-
// console.error("Error subscribing to product", productPriceId, e);
148-
// onCheckoutSessionReady(undefined, e);
149-
// }
150-
151-
const firestore = firestoreRef.current;
152-
if (!firestore) throw new Error("Firestore not initialized");
153-
if (!userId) throw new Error("User not logged in");
154-
if (!type) throw new Error("subscription type not provided. Make sure to assign the metadata.type field in the Stripe dashboard to a product.");
155-
156-
const subscriptionPricesRequest: any = {
157-
price: productPrice.id
158-
};
159-
160-
// For prices with metered billing we need to omit the quantity parameter.
161-
// For all other prices we set quantity to 1.
162-
if (productPrice.recurring?.usage_type !== "metered")
163-
subscriptionPricesRequest.quantity = quantity ?? 1;
164-
165-
const metadata: Record<string, string> = {
129+
const {
130+
projectId,
131+
licenseId,
132+
productPrice,
133+
quantity,
134+
onCheckoutSessionReady,
166135
type
167-
};
168-
if (projectId) {
169-
metadata.projectId = projectId;
170-
}
171-
if (licenseId) {
172-
metadata.licenseId = licenseId;
173-
}
174-
175-
const checkoutSession: any = {
176-
automatic_tax: true,
177-
tax_id_collection: true,
178-
collect_shipping_address: false,
179-
allow_promotion_codes: true,
180-
line_items: [subscriptionPricesRequest],
181-
trial_from_plan: true,
182-
trial_period_days: 28,
183-
success_url: `${window.location.origin}${window.location.pathname}`,
184-
cancel_url: `${window.location.origin}${window.location.pathname}`,
185-
metadata
186-
};
187-
188-
// For one time payments set mode to payment.
189-
if (productPrice.type === "one_time") {
190-
checkoutSession.mode = "payment";
191-
checkoutSession.payment_method_types = ["card", "sepa_debit", "sofort"];
136+
} = props;
137+
138+
console.debug("Subscribing to product", props);
139+
const productPriceId = productPrice.id;
140+
const productPriceType = productPrice.type;
141+
try {
142+
const sessionUrl: string = await projectsApi.createStripeNewSubscriptionLink({
143+
...props,
144+
productPriceId,
145+
productPriceType
146+
});
147+
onCheckoutSessionReady(sessionUrl, undefined);
148+
} catch (e: any) {
149+
console.error("Error subscribing to product", productPriceId, e);
150+
onCheckoutSessionReady(undefined, e);
192151
}
193152

194-
// Save checkout session to Firestore
195-
const checkoutSessionRef = collection(firestore, CUSTOMERS_COLLECTION, userId, CHECKOUT_SESSION_COLLECTION);
196-
const docRef = await addDoc(checkoutSessionRef, checkoutSession);
197-
198-
const unsubscribe = onSnapshot(docRef, (snap) => {
199-
const {
200-
error,
201-
url
202-
} = snap.data();
203-
204-
console.debug("Checkout session updated", snap.data());
205-
onCheckoutSessionReady(url, error);
206-
207-
if (url) {
208-
unsubscribe();
209-
} else if (error) {
210-
unsubscribe();
211-
}
212-
});
153+
// const firestore = firestoreRef.current;
154+
// if (!firestore) throw new Error("Firestore not initialized");
155+
// if (!userId) throw new Error("User not logged in");
156+
// if (!type) throw new Error("subscription type not provided. Make sure to assign the metadata.type field in the Stripe dashboard to a product.");
157+
//
158+
// const subscriptionPricesRequest: any = {
159+
// price: productPrice.id
160+
// };
161+
//
162+
// // For prices with metered billing we need to omit the quantity parameter.
163+
// // For all other prices we set quantity to 1.
164+
// if (productPrice.recurring?.usage_type !== "metered")
165+
// subscriptionPricesRequest.quantity = quantity ?? 1;
166+
//
167+
// const metadata: Record<string, string> = {
168+
// type
169+
// };
170+
// if (projectId) {
171+
// metadata.projectId = projectId;
172+
// }
173+
// if (licenseId) {
174+
// metadata.licenseId = licenseId;
175+
// }
176+
//
177+
// const checkoutSession: any = {
178+
// automatic_tax: true,
179+
// tax_id_collection: true,
180+
// collect_shipping_address: false,
181+
// allow_promotion_codes: true,
182+
// line_items: [subscriptionPricesRequest],
183+
// trial_from_plan: true,
184+
// trial_period_days: 28,
185+
// success_url: `${window.location.origin}${window.location.pathname}`,
186+
// cancel_url: `${window.location.origin}${window.location.pathname}`,
187+
// metadata
188+
// };
189+
//
190+
// // For one time payments set mode to payment.
191+
// if (productPrice.type === "one_time") {
192+
// checkoutSession.mode = "payment";
193+
// checkoutSession.payment_method_types = ["card", "sepa_debit", "sofort"];
194+
// }
195+
//
196+
// // Save checkout session to Firestore
197+
// const checkoutSessionRef = collection(firestore, CUSTOMERS_COLLECTION, userId, CHECKOUT_SESSION_COLLECTION);
198+
// const docRef = await addDoc(checkoutSessionRef, checkoutSession);
199+
//
200+
// const unsubscribe = onSnapshot(docRef, (snap) => {
201+
// const {
202+
// error,
203+
// url
204+
// } = snap.data();
205+
//
206+
// console.debug("Checkout session updated", snap.data());
207+
// onCheckoutSessionReady(url, error);
208+
//
209+
// if (url) {
210+
// unsubscribe();
211+
// } else if (error) {
212+
// unsubscribe();
213+
// }
214+
// });
213215
}
214216

215217
const getSubscriptionsForProject = (projectId: string): Subscription[] => {

0 commit comments

Comments
 (0)