Basic Stripe integration. new pages for stripe handshake and new Account page. use query instead of useQuery for trpc fetches from store

This commit is contained in:
Michael Dausmann
2023-03-18 20:37:51 +11:00
parent 2ef98d0d98
commit 7311c13db2
21 changed files with 503 additions and 34 deletions

View File

@@ -0,0 +1,54 @@
import { Account } from '@prisma/client';
import Stripe from 'stripe';
import UserAccountService from '~~/lib/services/user.account.service';
import prisma_client from '~~/prisma/prisma.client';
const config = useRuntimeConfig();
const stripe = new Stripe(config.stripeSecretKey, { apiVersion: '2022-11-15' });
export default defineEventHandler(async (event) => {
const YOUR_DOMAIN = 'http://localhost:3000'; // TODO - pull from somewhere, this is shit
const body = await readBody(event)
let { price_id, account_id} = body;
account_id = +account_id
console.log(`session.post.ts recieved price_id:${price_id}, account_id:${account_id}`);
const userService = new UserAccountService(prisma_client);
const account: Account = await userService.getAccountById(account_id);
let customer_id: string
if(!account.stripe_customer_id){
// need to pre-emptively create a Stripe user for this account (use name for now, just so is visible on dashboard) TODO - include Email
console.log(`Creating account with name ${account.name}`);
const customer = await stripe.customers.create({ name: account.name });
customer_id = customer.id;
userService.updateAccountStipeCustomerId(account_id, customer.id);
} else {
customer_id = account.stripe_customer_id;
}
const session = await stripe.checkout.sessions.create({
mode: 'subscription',
line_items: [
{
price: price_id,
// For metered billing, do not pass quantity
quantity: 1,
},
],
// {CHECKOUT_SESSION_ID} is a string literal; do not change it!
// the actual Session ID is returned in the query parameter when your customer
// is redirected to the success page.
success_url: `${YOUR_DOMAIN}/success?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${YOUR_DOMAIN}/cancel`,
customer: customer_id
});
if(session?.url){
return sendRedirect(event, session.url, 303);
} else {
return sendRedirect(event, `${YOUR_DOMAIN}/fail`, 303);
}
});

View File

@@ -0,0 +1,42 @@
import Stripe from 'stripe';
import UserAccountService from '~~/lib/services/user.account.service';
import prisma_client from '~~/prisma/prisma.client';
const config = useRuntimeConfig();
const stripe = new Stripe(config.stripeSecretKey, { apiVersion: '2022-11-15' });
export default defineEventHandler(async (event) => {
const stripeSignature = getRequestHeader(event, 'stripe-signature');
if(!stripeSignature){
throw createError({ statusCode: 400, statusMessage: 'Webhook Error: No stripe signature in header' });
}
const rawBody = await readRawBody(event)
if(!rawBody){
throw createError({ statusCode: 400, statusMessage: 'Webhook Error: No body' });
}
let stripeEvent: Stripe.Event;
try {
stripeEvent = stripe.webhooks.constructEvent(rawBody, stripeSignature, config.stripeEndpointSecret);
}
catch (err) {
console.log(err);
throw createError({ statusCode: 400, statusMessage: `Webhook Error` }); // ${(err as Error).message}
}
console.log(`****** Web Hook Recieved (${stripeEvent.type}) ******`);
if(stripeEvent.type && stripeEvent.type.startsWith('customer.subscription')){
let subscription = stripeEvent.data.object as Stripe.Subscription;
const userService = new UserAccountService(prisma_client);
let current_period_ends: Date = new Date(subscription.current_period_end * 1000);
current_period_ends.setDate(current_period_ends.getDate() + config.subscriptionGraceDays);
console.log(`updating stripe sub details subscription.current_period_end:${subscription.current_period_end}, subscription.id:${subscription.id}`);
userService.updateStripeSubscriptionDetailsForAccount(subscription.customer.toString(), subscription.id, current_period_ends)
}
return `handled ${stripeEvent.type}.`;
});

View File

@@ -33,9 +33,10 @@ export async function createContext(event: H3Event){
}
if(!supabase || !user || !prisma || !dbUser) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Unable to fetch user data, please try again later.',
message: `Unable to fetch user data, please try again later. Missing ->[supabase:${(!supabase)},user:${(!user)},prisma:${(!prisma)},dbUser:${(!dbUser)}, ]`,
});
}

View File

@@ -11,8 +11,11 @@ import { initTRPC, TRPCError } from '@trpc/server'
import { Context } from './context';
import { z } from 'zod';
import { ACCOUNT_ACCESS } from '@prisma/client';
import superjson from 'superjson';
const t = initTRPC.context<Context>().create()
const t = initTRPC.context<Context>().create({
transformer: superjson,
})
/**
* auth middlewares