This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
retrievePlan.js
70 lines (53 loc) · 2.84 KB
/
retrievePlan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import loadStripe from 'stripe';
import * as auth from '$lib/apis/auth-api-methods';
import * as db from '$lib/apis/db-api-methods';
import { serverResponse } from '$lib/utils/helpers';
export async function get(request) {
// LATER: update logic and UI to collect and display multiple plans
try {
const { token } = request.locals;
const accessTokenCheck = await auth.verifyCurrentToken(token);
if(!accessTokenCheck.ok) throw { statusMessage: 'error', errorMessage: 'Unauthorized Session' };
if(accessTokenCheck.ok) {
const netlifyId = accessTokenCheck.id;
const stripeId = await db.getStripeId(netlifyId);
const stripe = await loadStripe(process.env['STRIPE_SECRET_KEY']);
// logic currently presumes customer will only have one plan
// https://stripe.com/docs/api/subscriptions/list
const subscriptions = await stripe.subscriptions.list({
customer: stripeId,
status: 'active',
limit: 1,
});
// if subscriptions request does not contain plan/product, throw error
if (!subscriptions.data[0].plan.id || !subscriptions.data[0].plan.product) throw { statusMessage: 'error', errorMessage: 'Subscription not found', };
const stripePriceId = subscriptions.data[0].plan.id;
const stripeProductId = subscriptions.data[0].plan.product;
const planDetail = await stripe.prices.retrieve(stripePriceId); // https://stripe.com/docs/api/prices/retrieve
if (!planDetail) throw { statusMessage: 'error', errorMessage: 'Plan not found', };
const productDetail = await stripe.products.retrieve(stripeProductId); // https://stripe.com/docs/api/products/retrieve
if (!productDetail) throw { statusMessage: 'error', errorMessage: 'Product not found', };
const productName = productDetail.name;
const planPrice = planDetail.unit_amount; // unit amount in cents
const planCurrency = planDetail.currency;
const planInterval = planDetail.recurring?.interval;
// LATER: placeholder logic currently; update to something actually useful
let currencySymbol;
if (planCurrency === 'usd') currencySymbol = '$';
let planIntervalLabel;
if (planInterval === 'month') planIntervalLabel = 'mo';
if (planInterval === 'year') planIntervalLabel = 'yr';
const currentPlanLabel = `${productName} ・ ${currencySymbol}${Math.round(planPrice/100)}/${planIntervalLabel}`;
return serverResponse(200, true, {
plan: currentPlanLabel,
})
}
} catch (error) {
let { statusMessage, errorMessage } = error;
if (!errorMessage) console.log(new Date().toISOString(), "💥 'retrievePlan' endpoint unsuccessful : error.message :", error.message);
return serverResponse(200, false, {
statusMessage: statusMessage || 'error',
error: errorMessage || 'Unsuccessful Request',
});
}
}