mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-03 21:15:23 +02:00
feat: add webhook
This commit is contained in:
@@ -22,194 +22,17 @@ export const getStripeItems = (serverQuantity: number, isAnnual: boolean) => {
|
||||
const items = [];
|
||||
|
||||
if (isAnnual) {
|
||||
if (serverQuantity === 1) {
|
||||
items.push({
|
||||
price: BASE_PRICE_YEARLY_ID,
|
||||
quantity: 1,
|
||||
});
|
||||
} else if (serverQuantity <= 3) {
|
||||
items.push({
|
||||
price: GROWTH_PRICE_YEARLY_ID,
|
||||
quantity: 1,
|
||||
});
|
||||
} else {
|
||||
items.push({
|
||||
price: GROWTH_PRICE_YEARLY_ID,
|
||||
quantity: 1,
|
||||
});
|
||||
items.push({
|
||||
price: ADDITIONAL_PRICE_YEARLY_ID,
|
||||
quantity: serverQuantity - 3,
|
||||
});
|
||||
}
|
||||
items.push({
|
||||
price: "price_1QC7XwF3cxQuHeOz68CpnIUZ",
|
||||
quantity: serverQuantity,
|
||||
});
|
||||
|
||||
return items;
|
||||
}
|
||||
if (serverQuantity === 1) {
|
||||
items.push({
|
||||
price: BASE_PRICE_MONTHLY_ID,
|
||||
quantity: 1,
|
||||
});
|
||||
} else if (serverQuantity <= 3) {
|
||||
items.push({
|
||||
price: GROWTH_PRICE_MONTHLY_ID,
|
||||
quantity: 1,
|
||||
});
|
||||
} else {
|
||||
items.push({
|
||||
price: GROWTH_PRICE_MONTHLY_ID,
|
||||
quantity: 1,
|
||||
});
|
||||
items.push({
|
||||
price: SERVER_ADDITIONAL_PRICE_MONTHLY_ID,
|
||||
quantity: serverQuantity - 3,
|
||||
});
|
||||
}
|
||||
items.push({
|
||||
price: "price_1QC7X5F3cxQuHeOz1859ljDP",
|
||||
quantity: serverQuantity,
|
||||
});
|
||||
|
||||
return items;
|
||||
};
|
||||
|
||||
export const getStripeSubscriptionItemsCalculate = async (
|
||||
subscriptionId: string,
|
||||
serverQuantity: number,
|
||||
isAnnual: boolean,
|
||||
) => {
|
||||
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
|
||||
const currentItems = subscription.items.data;
|
||||
const items = [];
|
||||
|
||||
const basePriceId = isAnnual ? BASE_PRICE_YEARLY_ID : BASE_PRICE_MONTHLY_ID;
|
||||
const growthPriceId = isAnnual
|
||||
? GROWTH_PRICE_YEARLY_ID
|
||||
: GROWTH_PRICE_MONTHLY_ID;
|
||||
const additionalPriceId = isAnnual
|
||||
? ADDITIONAL_PRICE_YEARLY_ID
|
||||
: SERVER_ADDITIONAL_PRICE_MONTHLY_ID;
|
||||
|
||||
const baseItem = currentItems.find(
|
||||
(item) => item.price.id === basePriceId || item.price.id === growthPriceId,
|
||||
);
|
||||
const additionalItem = currentItems.find(
|
||||
(item) => item.price.id === additionalPriceId,
|
||||
);
|
||||
|
||||
if (serverQuantity === 1) {
|
||||
if (baseItem) {
|
||||
items.push({
|
||||
id: baseItem.id,
|
||||
price: basePriceId,
|
||||
quantity: 1,
|
||||
});
|
||||
}
|
||||
} else if (serverQuantity <= 3) {
|
||||
if (baseItem) {
|
||||
items.push({
|
||||
id: baseItem.id,
|
||||
price: growthPriceId,
|
||||
quantity: 1,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (baseItem) {
|
||||
items.push({
|
||||
id: baseItem.id,
|
||||
price: growthPriceId,
|
||||
quantity: 1,
|
||||
});
|
||||
}
|
||||
|
||||
if (additionalItem) {
|
||||
items.push({
|
||||
id: additionalItem.id,
|
||||
price: additionalPriceId,
|
||||
quantity: serverQuantity - 3,
|
||||
});
|
||||
} else {
|
||||
items.push({
|
||||
price: additionalPriceId,
|
||||
quantity: serverQuantity - 3,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
};
|
||||
|
||||
export const updateBasePlan = async (
|
||||
subscriptionId: string,
|
||||
subscriptionItemId: string,
|
||||
newPriceId: string,
|
||||
) => {
|
||||
await stripe.subscriptions.update(subscriptionId, {
|
||||
items: [
|
||||
{
|
||||
id: subscriptionItemId,
|
||||
price: newPriceId,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
proration_behavior: "always_invoice",
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteAdditionalItem = async (subscriptionItemId: string) => {
|
||||
await stripe.subscriptionItems.del(subscriptionItemId);
|
||||
};
|
||||
|
||||
export const getStripeSubscriptionItems = async (
|
||||
subscriptionId: string,
|
||||
isAnual: boolean,
|
||||
) => {
|
||||
const subscription = await stripe.subscriptions.retrieve(subscriptionId, {
|
||||
expand: ["items.data.price"],
|
||||
});
|
||||
|
||||
if (isAnual) {
|
||||
const baseItem = subscription.items.data.find(
|
||||
(item) =>
|
||||
item.price.id === BASE_PRICE_YEARLY_ID ||
|
||||
item.price.id === GROWTH_PRICE_YEARLY_ID,
|
||||
);
|
||||
const additionalItem = subscription.items.data.find(
|
||||
(item) => item.price.id === ADDITIONAL_PRICE_YEARLY_ID,
|
||||
);
|
||||
|
||||
return {
|
||||
baseItem,
|
||||
additionalItem,
|
||||
};
|
||||
}
|
||||
const baseItem = subscription.items.data.find(
|
||||
(item) =>
|
||||
item.price.id === BASE_PRICE_MONTHLY_ID ||
|
||||
item.price.id === GROWTH_PRICE_MONTHLY_ID,
|
||||
);
|
||||
const additionalItem = subscription.items.data.find(
|
||||
(item) => item.price.id === SERVER_ADDITIONAL_PRICE_MONTHLY_ID,
|
||||
);
|
||||
|
||||
return {
|
||||
baseItem,
|
||||
additionalItem,
|
||||
};
|
||||
};
|
||||
|
||||
export const getStripePrices = async (isAnual: boolean) => {
|
||||
const basePrice = isAnual
|
||||
? await stripe.prices.retrieve(BASE_PRICE_YEARLY_ID)
|
||||
: await stripe.prices.retrieve(BASE_PRICE_MONTHLY_ID);
|
||||
|
||||
const growthPrice = isAnual
|
||||
? await stripe.prices.retrieve(GROWTH_PRICE_YEARLY_ID)
|
||||
: await stripe.prices.retrieve(GROWTH_PRICE_MONTHLY_ID);
|
||||
|
||||
const additionalPrice = isAnual
|
||||
? await stripe.prices.retrieve(ADDITIONAL_PRICE_YEARLY_ID)
|
||||
: await stripe.prices.retrieve(SERVER_ADDITIONAL_PRICE_MONTHLY_ID);
|
||||
|
||||
return {
|
||||
basePrice,
|
||||
growthPrice,
|
||||
additionalPrice,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user