-
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce778f8
commit 37924a9
Showing
16 changed files
with
1,008 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
TOKEN=123 | ||
MONGO=mongodb://localhost:27017/shieldy | ||
MONGO=mongodb://localhost:27017/shieldy | ||
ADMIN=123 | ||
REPORT_CHAT_ID=123 | ||
PREMIUM=false | ||
STRIPE_SECRET_KEY=123 | ||
MONTHLY_PRICE=123 | ||
YEARLY_PRICE=123 | ||
LIFETIME_PRICE=123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { strings } from '@helpers/strings' | ||
import { checkIfGroup } from '@middlewares/checkIfGroup' | ||
import { Telegraf, Context } from 'telegraf' | ||
import { checkLock } from '@middlewares/checkLock' | ||
import { stripe } from '@helpers/stripe' | ||
import { Language } from '@models/Chat' | ||
import Stripe from 'stripe' | ||
|
||
const prices = { | ||
monthly: process.env.MONTHLY_PRICE, | ||
yearly: process.env.YEARLY_PRICE, | ||
lifetime: process.env.LIFETIME_PRICE, | ||
} | ||
|
||
export function setupSubscription(bot: Telegraf<Context>) { | ||
bot.command('subscription', checkIfGroup, checkLock, async (ctx) => { | ||
if (!ctx.dbchat.subscriptionId) { | ||
return sendSubscriptionButtons(ctx) | ||
} | ||
if ( | ||
ctx.from.id !== parseInt(process.env.ADMIN) && | ||
ctx.from?.username !== 'GroupAnonymousBot' && | ||
!ctx.isAdministrator | ||
) { | ||
try { | ||
await ctx.deleteMessage() | ||
} catch { | ||
// do nothing | ||
} | ||
return | ||
} | ||
return sendManageSubscription(ctx) | ||
}) | ||
} | ||
|
||
export async function sendSubscriptionButtons(ctx: Context) { | ||
await ctx.telegram.sendChatAction(ctx.chat.id, 'typing') | ||
return ctx.reply(strings(ctx.dbchat, 'noSubscription'), { | ||
reply_markup: { | ||
inline_keyboard: await subscriptionsKeyboard(ctx), | ||
}, | ||
}) | ||
} | ||
|
||
export async function sendManageSubscription(ctx: Context) { | ||
const keyboard = [] | ||
const subscription = await stripe.subscriptions.retrieve( | ||
ctx.dbchat.subscriptionId | ||
) | ||
const customerId = subscription.customer as string | ||
const url = ( | ||
await stripe.billingPortal.sessions.create({ | ||
customer: customerId, | ||
}) | ||
).url | ||
keyboard.push([ | ||
{ | ||
text: strings(ctx.dbchat, 'manageSubscription'), | ||
url, | ||
}, | ||
]) | ||
return ctx.reply(strings(ctx.dbchat, 'manageSubscription'), { | ||
reply_markup: { | ||
inline_keyboard: keyboard, | ||
}, | ||
}) | ||
} | ||
|
||
async function subscriptionsKeyboard(ctx: Context) { | ||
let unsafeLocale = ctx.dbchat.language || 'en' | ||
if (unsafeLocale === Language.UKRAINIAN) { | ||
unsafeLocale = 'en' | ||
} | ||
const locale = unsafeLocale as Stripe.Checkout.Session.Locale | ||
const getBackUrl = 'https://t.me/shieldy_premium_bot' | ||
const monthlySession = await stripe.checkout.sessions.create({ | ||
payment_method_types: ['card'], | ||
line_items: [ | ||
{ | ||
price: prices.monthly, | ||
quantity: 1, | ||
}, | ||
], | ||
success_url: getBackUrl, | ||
cancel_url: getBackUrl, | ||
client_reference_id: `${ctx.chat.id}`, | ||
locale, | ||
mode: 'subscription', | ||
allow_promotion_codes: true, | ||
}) | ||
const yearlySession = await stripe.checkout.sessions.create({ | ||
payment_method_types: ['card'], | ||
line_items: [ | ||
{ | ||
price: prices.yearly, | ||
quantity: 1, | ||
}, | ||
], | ||
success_url: getBackUrl, | ||
cancel_url: getBackUrl, | ||
client_reference_id: `${ctx.chat.id}`, | ||
locale, | ||
mode: 'subscription', | ||
allow_promotion_codes: true, | ||
}) | ||
const lifetimeSession = await stripe.checkout.sessions.create({ | ||
payment_method_types: ['card'], | ||
line_items: [ | ||
{ | ||
price: prices.lifetime, | ||
quantity: 1, | ||
}, | ||
], | ||
success_url: getBackUrl, | ||
cancel_url: getBackUrl, | ||
client_reference_id: `${ctx.chat.id}`, | ||
locale, | ||
mode: 'payment', | ||
allow_promotion_codes: true, | ||
}) | ||
return [ | ||
[ | ||
{ | ||
text: `$15/${strings(ctx.dbchat, 'monthly')}`, | ||
url: monthlySession.url, | ||
}, | ||
], | ||
[ | ||
{ | ||
text: `$108/${strings(ctx.dbchat, 'yearly')}`, | ||
url: yearlySession.url, | ||
}, | ||
], | ||
[ | ||
{ | ||
text: `$450 ${strings(ctx.dbchat, 'lifetime')}`, | ||
url: lifetimeSession.url, | ||
}, | ||
], | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { ChatModel, SubscriptionStatus } from '@models/Chat' | ||
import { Context } from 'koa' | ||
import { Controller, Ctx, Post } from 'amala' | ||
import { stripe } from '@helpers/stripe' | ||
|
||
@Controller('/webhook') | ||
export default class WebhookController { | ||
@Post('/') | ||
async webhook(@Ctx() ctx: Context) { | ||
try { | ||
// Construct event | ||
const event = stripe.webhooks.constructEvent( | ||
String(ctx.request.rawBody), | ||
ctx.headers['stripe-signature'], | ||
process.env.STRIPE_SIGNING_SECRET | ||
) | ||
// Handle event | ||
if (event.type === 'customer.subscription.deleted') { | ||
const anyData = event.data.object as any | ||
const subscriptionId = anyData.id | ||
const chat = await ChatModel.findOne({ subscriptionId }) | ||
if (!chat) { | ||
return ctx.throw( | ||
400, | ||
`Webhook Error: No chat found for subscription id ${subscriptionId}` | ||
) | ||
} | ||
if (chat.subscriptionStatus !== SubscriptionStatus.lifetime) { | ||
chat.subscriptionStatus = SubscriptionStatus.inactive | ||
} | ||
await chat.save() | ||
} else if (event.type === 'checkout.session.completed') { | ||
const anyData = event.data.object as any | ||
const chatId = +anyData.client_reference_id | ||
const chat = await ChatModel.findOne({ id: chatId }) | ||
if (!chat) { | ||
return ctx.throw( | ||
400, | ||
`Webhook Error: No chat found with id ${chatId}` | ||
) | ||
} | ||
if (anyData.mode === 'subscription') { | ||
chat.subscriptionId = anyData.subscription | ||
chat.subscriptionStatus = SubscriptionStatus.active | ||
} else { | ||
chat.subscriptionStatus = SubscriptionStatus.lifetime | ||
} | ||
await chat.save() | ||
} | ||
// Respond | ||
return { received: true } | ||
} catch (err) { | ||
return ctx.throw(400, `Webhook Error: ${err.message}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.