Skip to content

Commit a3e417e

Browse files
authored
Archive Stripe product when Stripe Payment link is paid (#168)
1 parent c79d437 commit a3e417e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/api/functions/stripe.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,16 @@ export const deactivateStripeLink = async ({
111111
active: false,
112112
});
113113
};
114+
115+
export const deactivateStripeProduct = async ({
116+
productId,
117+
stripeApiKey,
118+
}: {
119+
productId: string;
120+
stripeApiKey: string;
121+
}): Promise<void> => {
122+
const stripe = new Stripe(stripeApiKey);
123+
await stripe.products.update(productId, {
124+
active: false,
125+
});
126+
};

src/api/routes/stripe.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import {
1414
createStripeLink,
1515
deactivateStripeLink,
16+
deactivateStripeProduct,
1617
StripeLinkCreateParams,
1718
} from "api/functions/stripe.js";
1819
import { getSecretValue } from "api/plugins/auth.js";
@@ -260,9 +261,9 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
260261
const unmarshalledEntry = unmarshall(response.Items[0]) as {
261262
userId: string;
262263
invoiceId: string;
263-
amount: number;
264-
priceId: string;
265-
productId: string;
264+
amount?: number;
265+
priceId?: string;
266+
productId?: string;
266267
};
267268
if (!unmarshalledEntry.userId || !unmarshalledEntry.invoiceId) {
268269
return reply.status(200).send({
@@ -345,6 +346,16 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
345346
},
346347
],
347348
});
349+
if (unmarshalledEntry.productId) {
350+
request.log.debug(
351+
`Deactivating Stripe product ${unmarshalledEntry.productId}`,
352+
);
353+
await deactivateStripeProduct({
354+
stripeApiKey: secretApiConfig.stripe_secret_key as string,
355+
productId: unmarshalledEntry.productId,
356+
});
357+
}
358+
request.log.debug(`Deactivating Stripe link ${paymentLinkId}`);
348359
await deactivateStripeLink({
349360
stripeApiKey: secretApiConfig.stripe_secret_key as string,
350361
linkId: paymentLinkId,

0 commit comments

Comments
 (0)