upgrade user on purchase

This commit is contained in:
adueck 2022-10-13 20:17:26 +05:00
parent 6a2edf96be
commit 2de4a45ba2
1 changed files with 6 additions and 5 deletions

View File

@ -2,6 +2,7 @@ import express from "express";
import * as T from "../../../website/src/types/account-types"; import * as T from "../../../website/src/types/account-types";
import env from "../lib/env-vars"; import env from "../lib/env-vars";
import Stripe from "stripe"; import Stripe from "stripe";
import { upgradeUser } from "../lib/user-utils";
const stripe = new Stripe(env.stripeSecretKey, { const stripe = new Stripe(env.stripeSecretKey, {
apiVersion: "2022-08-01", apiVersion: "2022-08-01",
@ -12,7 +13,7 @@ const paymentRouter = express.Router();
paymentRouter.post( paymentRouter.post(
'/webhook', '/webhook',
express.raw({ type: 'application/json' }), express.raw({ type: 'application/json' }),
(request, response) => { async (request, response) => {
let event = request.body; let event = request.body;
// Replace this endpoint secret with your endpoint's unique secret // Replace this endpoint secret with your endpoint's unique secret
// If you are testing with the CLI, find the secret by running 'stripe listen' // If you are testing with the CLI, find the secret by running 'stripe listen'
@ -49,10 +50,10 @@ paymentRouter.post(
case 'customer.subscription.created': case 'customer.subscription.created':
subscription = event.data.object; subscription = event.data.object;
status = subscription.status; status = subscription.status;
console.log(`Subscription status is ${status}.`); const userId = subscription.metadata.userId as T.UUID;
console.log({ userId: subscription.metadata.userId }); console.log(`Upgrading user ${userId}.`);
// Then define and call a method to handle the subscription created. await upgradeUser(userId);
// handleSubscriptionCreated(subscription); // TODO: save subscription to db
break; break;
default: default:
// Unexpected event type // Unexpected event type