user.email account.plan_name - cleanup context and service construction - config for stripe callback - initial plan
This commit is contained in:
@@ -1,26 +1,24 @@
|
||||
import { Account } from '@prisma/client';
|
||||
import { ACCOUNT_ACCESS } from '@prisma/client';
|
||||
import Stripe from 'stripe';
|
||||
import UserAccountService from '~~/lib/services/user.account.service';
|
||||
import prisma_client from '~~/prisma/prisma.client';
|
||||
import UserAccountService, { AccountWithMembers } from '~~/lib/services/user.account.service';
|
||||
|
||||
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);
|
||||
const userService = new UserAccountService();
|
||||
const account: AccountWithMembers = 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 });
|
||||
// need to pre-emptively create a Stripe user for this account so we know who they are when the webhook comes back
|
||||
const owner = account.members.find(member => (member.access == ACCOUNT_ACCESS.OWNER))
|
||||
console.log(`Creating account with name ${account.name} and email ${owner?.user.email}`);
|
||||
const customer = await stripe.customers.create({ name: account.name, email: owner?.user.email });
|
||||
customer_id = customer.id;
|
||||
userService.updateAccountStipeCustomerId(account_id, customer.id);
|
||||
} else {
|
||||
@@ -39,15 +37,15 @@ export default defineEventHandler(async (event) => {
|
||||
// {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`,
|
||||
success_url: `${config.stripeCallbackUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
|
||||
cancel_url: `${config.stripeCallbackUrl}/cancel`,
|
||||
customer: customer_id
|
||||
});
|
||||
|
||||
if(session?.url){
|
||||
return sendRedirect(event, session.url, 303);
|
||||
} else {
|
||||
return sendRedirect(event, `${YOUR_DOMAIN}/fail`, 303);
|
||||
return sendRedirect(event, `${config.stripeCallbackUrl}/fail`, 303);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
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' });
|
||||
@@ -22,15 +21,15 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
catch (err) {
|
||||
console.log(err);
|
||||
throw createError({ statusCode: 400, statusMessage: `Webhook Error` }); // ${(err as Error).message}
|
||||
throw createError({ statusCode: 400, statusMessage: `Error validating Webhook Event` });
|
||||
}
|
||||
|
||||
console.log(`****** Web Hook Recieved (${stripeEvent.type}) ******`);
|
||||
|
||||
if(stripeEvent.type && stripeEvent.type.startsWith('customer.subscription')){
|
||||
console.log(`****** Web Hook Recieved (${stripeEvent.type}) ******`);
|
||||
|
||||
let subscription = stripeEvent.data.object as Stripe.Subscription;
|
||||
|
||||
const userService = new UserAccountService(prisma_client);
|
||||
const userService = new UserAccountService();
|
||||
|
||||
let current_period_ends: Date = new Date(subscription.current_period_end * 1000);
|
||||
current_period_ends.setDate(current_period_ends.getDate() + config.subscriptionGraceDays);
|
||||
|
||||
Reference in New Issue
Block a user