prettier fixes #16
This commit is contained in:
@@ -6,20 +6,31 @@ import { AccountWithMembers } from '~~/lib/services/service.types';
|
||||
const config = useRuntimeConfig();
|
||||
const stripe = new Stripe(config.stripeSecretKey, { apiVersion: '2022-11-15' });
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
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}`);
|
||||
export default defineEventHandler(async event => {
|
||||
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 accountService = new AccountService();
|
||||
const account: AccountWithMembers = await accountService.getAccountById(account_id);
|
||||
let customer_id: string
|
||||
if(!account.stripe_customer_id){
|
||||
const account: AccountWithMembers = await accountService.getAccountById(
|
||||
account_id
|
||||
);
|
||||
let customer_id: string;
|
||||
if (!account.stripe_customer_id) {
|
||||
// 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 });
|
||||
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;
|
||||
accountService.updateAccountStipeCustomerId(account_id, customer.id);
|
||||
} else {
|
||||
@@ -31,8 +42,8 @@ export default defineEventHandler(async (event) => {
|
||||
line_items: [
|
||||
{
|
||||
price: price_id,
|
||||
quantity: 1,
|
||||
},
|
||||
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
|
||||
@@ -42,11 +53,9 @@ export default defineEventHandler(async (event) => {
|
||||
customer: customer_id
|
||||
});
|
||||
|
||||
if(session?.url){
|
||||
if (session?.url) {
|
||||
return sendRedirect(event, session.url, 303);
|
||||
} else {
|
||||
return sendRedirect(event, `${config.public.siteRootUrl}/fail`, 303);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -4,46 +4,77 @@ import AccountService from '~~/lib/services/account.service';
|
||||
const config = useRuntimeConfig();
|
||||
const stripe = new Stripe(config.stripeSecretKey, { apiVersion: '2022-11-15' });
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
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' });
|
||||
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' });
|
||||
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) {
|
||||
stripeEvent = stripe.webhooks.constructEvent(
|
||||
rawBody,
|
||||
stripeSignature,
|
||||
config.stripeEndpointSecret
|
||||
);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw createError({ statusCode: 400, statusMessage: `Error validating Webhook Event` });
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: `Error validating Webhook Event`
|
||||
});
|
||||
}
|
||||
|
||||
if(stripeEvent.type && stripeEvent.type.startsWith('customer.subscription')){
|
||||
if (
|
||||
stripeEvent.type &&
|
||||
stripeEvent.type.startsWith('customer.subscription')
|
||||
) {
|
||||
console.log(`****** Web Hook Recieved (${stripeEvent.type}) ******`);
|
||||
|
||||
let subscription = stripeEvent.data.object as Stripe.Subscription;
|
||||
if(subscription.status == 'active'){
|
||||
const sub_item = subscription.items.data.find(item => item?.object && item?.object == 'subscription_item')
|
||||
|
||||
let subscription = stripeEvent.data.object as Stripe.Subscription;
|
||||
if (subscription.status == 'active') {
|
||||
const sub_item = subscription.items.data.find(
|
||||
item => item?.object && item?.object == 'subscription_item'
|
||||
);
|
||||
|
||||
const stripe_product_id = sub_item?.plan.product?.toString(); // TODO - is the product ever a product object and in that case should I check for deleted?
|
||||
if(!stripe_product_id){
|
||||
throw createError({ statusCode: 400, statusMessage: `Error validating Webhook Event` });
|
||||
if (!stripe_product_id) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: `Error validating Webhook Event`
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const accountService = new AccountService();
|
||||
|
||||
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}, stripe_product_id:${stripe_product_id}`);
|
||||
accountService.updateStripeSubscriptionDetailsForAccount(subscription.customer.toString(), subscription.id, current_period_ends, stripe_product_id);
|
||||
|
||||
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}, stripe_product_id:${stripe_product_id}`
|
||||
);
|
||||
accountService.updateStripeSubscriptionDetailsForAccount(
|
||||
subscription.customer.toString(),
|
||||
subscription.id,
|
||||
current_period_ends,
|
||||
stripe_product_id
|
||||
);
|
||||
}
|
||||
}
|
||||
return `handled ${stripeEvent.type}.`;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user