Skip to content

Commit

Permalink
feat: updated Stripe v14.9.0 and API version 2023-10-16 (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime authored Dec 18, 2023
1 parent 0a0e248 commit 3484691
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 46 deletions.
1 change: 0 additions & 1 deletion config/interfaces/StripeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface StripeConfig {
isEnabled: boolean;
apiVersion?: string;
webhookSecret?: string;
secretKey?: string;
useAutomaticTax?: boolean;
Expand Down
1 change: 0 additions & 1 deletion config/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export function loadConfig(): Config {
},
stripeConfig: {
isEnabled: parseBooleanEnvValue(env.STRIPE_ENABLED) ?? false,
apiVersion: env.STRIPE_API_VERSION || "",
webhookSecret: env.STRIPE_WEBHOOK_SECRET || "",
secretKey: env.STRIPE_SECRET_KEY || "",
useAutomaticTax:
Expand Down
13 changes: 13 additions & 0 deletions lib/services/stripe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Stripe from "stripe";
import { loadConfig } from "../../config/loadConfig";

export function createStripeClient() {
const stripeConfig = loadConfig().server.stripeConfig;

if (!stripeConfig.secretKey) {
throw new Error("Stripe secret key is not configured");
}
return new Stripe(stripeConfig.secretKey, {
apiVersion: "2023-10-16",
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"react-dom": "18.2.0",
"react-icons": "^4.8.0",
"request-ip": "^3.3.0",
"stripe": "^13.4.0",
"stripe": "14.9.0",
"styled-components": "^6.0.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-react": "^5.0.0",
Expand Down
10 changes: 3 additions & 7 deletions pages/api/frontend/v0.1/stripe/checkoutSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe";
import { loadConfig } from "../../../../../config/loadConfig";
import prisma from "../../../../../lib/services/db";
import { createStripeClient } from "../../../../../lib/services/stripe";
import { ProductType } from "../../../../../models/productType";
import Routes from "../../../../../routes/routes";
import { getUserWithRoleFromRequest } from "../../../../../util/auth";
Expand All @@ -16,7 +17,7 @@ export default async function handler(
const stripeConfig = loadConfig().server.stripeConfig;

if (!stripeConfig.isEnabled) {
logger.error("stripe is disabled but endpoint has been called");
logger.error("Stripe is disabled but endpoint has been called");
return res
.status(StatusCodes.SERVICE_UNAVAILABLE)
.json({ message: "Endpoint is disabled" });
Expand All @@ -37,12 +38,7 @@ export default async function handler(

switch (req.method) {
case "POST":
if (!stripeConfig.secretKey) {
throw new Error("Stripe secret key is not configured");
}
const stripe = new Stripe(stripeConfig.secretKey, {
apiVersion: "2023-08-16",
});
const stripe = createStripeClient();

// check whether organisation has a stripe customer id with prisma
const org = await prisma.organisation.findUnique({
Expand Down
10 changes: 2 additions & 8 deletions pages/api/frontend/v0.1/stripe/customerPortal.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { StatusCodes } from "http-status-codes";
import type { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe";
import { loadConfig } from "../../../../../config/loadConfig";
import prisma from "../../../../../lib/services/db";
import { createStripeClient } from "../../../../../lib/services/stripe";
import Routes from "../../../../../routes/routes";
import { getUserWithRoleFromRequest } from "../../../../../util/auth";
import { Logger } from "../../../../../util/logger";
Expand Down Expand Up @@ -38,13 +38,7 @@ export default async function handler(

switch (req.method) {
case "POST":
const stripeConfig = config.server.stripeConfig;
if (!stripeConfig.secretKey) {
throw new Error("Stripe secret key is not configured");
}
const stripe = new Stripe(stripeConfig.secretKey, {
apiVersion: "2023-08-16",
});
const stripe = createStripeClient();

// check whether organisation has a stripe customer id with prisma
const orgFromDb = await prisma.organisation.findUnique({
Expand Down
11 changes: 3 additions & 8 deletions pages/api/frontend/v0.1/stripe/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe";
import { loadConfig } from "../../../../../config/loadConfig";
import redis from "../../../../../lib/services/redis";
import { createStripeClient } from "../../../../../lib/services/stripe";
import { Product } from "../../../../../models/product";
import { Logger } from "../../../../../util/logger";

Expand All @@ -12,13 +13,7 @@ const logger = new Logger(__filename);
export async function getProducts(): Promise<Product[]> {
const config = loadConfig();

const stripeConfig = loadConfig().server.stripeConfig;
if (!stripeConfig.secretKey) {
throw new Error("Stripe secret key is not configured");
}
const stripe = new Stripe(stripeConfig.secretKey, {
apiVersion: "2023-08-16",
});
const stripe = createStripeClient();

const freeProduct: Product = {
id: "FREE",
Expand Down Expand Up @@ -159,7 +154,7 @@ export default async function handler(
const stripeConfig = loadConfig().server.stripeConfig;

if (!stripeConfig.isEnabled) {
logger.error("stripe is disabled but endpoint has been called");
logger.error("Stripe is disabled but endpoint has been called");
return res
.status(StatusCodes.SERVICE_UNAVAILABLE)
.json({ message: "Endpoint is disabled" });
Expand Down
9 changes: 3 additions & 6 deletions pages/api/webhooks/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe";
import { loadConfig } from "../../../config/loadConfig";
import prisma from "../../../lib/services/db";
import { createStripeClient } from "../../../lib/services/stripe";
import { Logger } from "../../../util/logger";
import { reportOrgToStripe } from "../../../util/stripe/reportUsage";

Expand All @@ -27,12 +28,6 @@ export default async function handler(
const logger = new Logger(__filename);

const stripeConfig = loadConfig().server.stripeConfig;
if (!stripeConfig.secretKey) {
throw new Error("Stripe secret key is not configured");
}
const stripe = new Stripe(stripeConfig.secretKey, {
apiVersion: "2023-08-16",
});

if (!stripeConfig.isEnabled) {
logger.error("stripe is disabled but endpoint has been called");
Expand All @@ -41,6 +36,8 @@ export default async function handler(
.json({ message: "Endpoint is disabled" });
}

const stripe = createStripeClient();

switch (req.method) {
case "POST":
const buf = await buffer(req);
Expand Down
13 changes: 3 additions & 10 deletions util/stripe/reportUsage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import Stripe from "stripe";
import { loadConfig } from "../../config/loadConfig";
import prisma from "../../lib/services/db";
import { createStripeClient } from "../../lib/services/stripe";
import { Product } from "../../models/product";
import { getProducts } from "../../pages/api/frontend/v0.1/stripe/products";
import { Logger } from "../logger";

const stripeConfig = loadConfig().server.stripeConfig;
if (!stripeConfig.secretKey) {
throw new Error("Stripe secret key is not configured");
}
const stripe = new Stripe(stripeConfig.secretKey, {
apiVersion: "2023-08-16",
});

async function findProductDetailsById(id: string, products: any) {
const product = products.find(
(p: Product) =>
Expand Down Expand Up @@ -43,6 +34,8 @@ export async function reportOrgToStripe(
const logger = new Logger(__filename);
logger.log(`Usage reporting called for org with id '${orgId}'`);

const stripe = createStripeClient();

// Retrieve org data including the active subscription and subItems
const org = await prisma.organisation.findFirst({
where: {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8104,10 +8104,10 @@ strip-json-comments@~2.0.1:
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
integrity sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==

stripe@^13.4.0:
version "13.11.0"
resolved "https://registry.yarnpkg.com/stripe/-/stripe-13.11.0.tgz#f51e1acf1ec75e85a405130e8b28f6dfd68be34b"
integrity sha512-yPxVJxUzP1QHhHeFnYjJl48QwDS1+5befcL7ju7+t+i88D5r0rbsL+GkCCS6zgcU+TiV5bF9eMGcKyJfLf8BZQ==
stripe@14.9.0:
version "14.9.0"
resolved "https://registry.yarnpkg.com/stripe/-/stripe-14.9.0.tgz#e814a3da09f47258bec87e24516c3f3e33041148"
integrity sha512-t2XdpNbRH4x3MYEoxNWhwUPl9D80aUd5OHds0zhDiwRYPZ0H7MrUI/dj9wOSYlzycD3xdvjn0q7pWeFWljtMUQ==
dependencies:
"@types/node" ">=8.1.0"
qs "^6.11.0"
Expand Down

0 comments on commit 3484691

Please sign in to comment.