Skip to content

Commit

Permalink
work in progress before deploying to staging
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarr committed Jun 27, 2024
1 parent dc6251d commit fc420e0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/editor/lib/TransformControls.js
src/lib/
public/
36 changes: 36 additions & 0 deletions public/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 (
<Modal
className={styles.modalWrapper}
Expand Down Expand Up @@ -60,7 +76,11 @@ const ProfileModal = ({ isOpen, onClose }) => {
<p>
<Action24 /> Plan: Geospatial Pro
</p>
<Button variant="ghost" className={styles.manageSubscription}>
<Button
variant="ghost"
className={styles.manageSubscription}
onClick={manageSubscription}
>
Manage subscription
</Button>
</div>
Expand Down

0 comments on commit fc420e0

Please sign in to comment.