diff --git a/.prettierignore b/.prettierignore index 66b573540..a1d7affbe 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,3 @@ src/editor/lib/TransformControls.js src/lib/ +public/ \ No newline at end of file diff --git a/public/functions/index.js b/public/functions/index.js index 2071abf03..1be98e5f2 100644 --- a/public/functions/index.js +++ b/public/functions/index.js @@ -36,13 +36,49 @@ exports.getScene = functions.https.onRequest(async (req, res) => { exports.createStripeSession = functions.https.onCall(async (data, context) => { const stripe = require('stripe')('sk_test_30qcK5wZwyN1q6NMKIirvyD7'); + // get stripeCustomerID if it exists + const collectionRef = this.db.collection("userProfile"); + const querySnapshot = await collectionRef.where("userId", "==", data.metadata.userId).get(); + let stripeCustomerId = null; + querySnapshot.forEach((doc) => { + stripeCustomerId = doc.data().stripeCustomerId; + break; // only need the first one + }); + // update data to include stripeCustomerID (data.customer) + + if (stripeCustomerId) { + data.customer = stripeCustomerId; + } const session = await stripe.checkout.sessions.create(data); + if (!stripeCustomerId) { + // add stripeCustomerId to userProfile + await admin.firestore().collection('userProfile').doc().set({ + userId: data.metadata.userId, + stripeCustomerId: session.customer + }); + } + // add stuff to firebase (customer ID) + return { id: session.id }; }); +exports.createStripeBillingPortal = functions.https.onCall(async (data, context) => { + const stripe = require('stripe')('sk_test_30qcK5wZwyN1q6NMKIirvyD7'); + + const session = await stripe.billingPortal.sessions.create({ + customer: data.customer_id, + return_url: data.return_url + }); + + return { + url: session.url + }; +} + + exports.stripeWebhook = functions.https.onRequest(async (req, res) => { const stripe = require('stripe')('sk_test_30qcK5wZwyN1q6NMKIirvyD7'); let event; diff --git a/src/editor/components/modals/PaymentModal/PaymentModal.component.jsx b/src/editor/components/modals/PaymentModal/PaymentModal.component.jsx index 7a38f05d8..d6e0b4a28 100644 --- a/src/editor/components/modals/PaymentModal/PaymentModal.component.jsx +++ b/src/editor/components/modals/PaymentModal/PaymentModal.component.jsx @@ -42,6 +42,7 @@ const PaymentModal = ({ isOpen, onClose }) => { success_url: `${location.origin}/#/modal/payment/success`, cancel_url: `${location.origin}/#/modal/payment`, metadata: { userId: currentUser.uid }, + customer: currentUser.stripeCustomerId, subscription_data: { metadata: { userId: currentUser.uid diff --git a/src/editor/components/modals/ProfileModal/ProfileModal.component.jsx b/src/editor/components/modals/ProfileModal/ProfileModal.component.jsx index 8ba411a22..1d97c9dcd 100644 --- a/src/editor/components/modals/ProfileModal/ProfileModal.component.jsx +++ b/src/editor/components/modals/ProfileModal/ProfileModal.component.jsx @@ -4,9 +4,10 @@ import Modal from '../Modal.jsx'; import { Button } from '../../components'; import { useAuthContext } from '../../../contexts'; import { signOut } from 'firebase/auth'; -import { auth } from '../../../services/firebase'; +import { auth, functions } from '../../../services/firebase'; import Events from '../../../lib/Events.js'; import { Action24 } from '../../../icons/icons.jsx'; +import { httpsCallable } from 'firebase/functions'; const ProfileModal = ({ isOpen, onClose }) => { const { currentUser, setCurrentUser } = useAuthContext(); @@ -17,6 +18,21 @@ const ProfileModal = ({ isOpen, onClose }) => { setCurrentUser(null); }; + const manageSubscription = async () => { + const { + data: { url } + } = await httpsCallable( + functions, + 'createStripeBillingPortal' + )({ + customer_id: currentUser.stripeUserId, + return_url: `${location.origin}/#/modal/payment` + }); + + window.open(url, '_blank'); + // Replace 'https://example.com' with your desired URL + }; + return ( {

Plan: Geospatial Pro

-