diff --git a/api/controllers/payments.controller.js b/api/controllers/payments.controller.js new file mode 100644 index 0000000..77af1c7 --- /dev/null +++ b/api/controllers/payments.controller.js @@ -0,0 +1,32 @@ +import { db } from '../../bot/db/index.js' +import { Sequelize } from 'sequelize' + +export const paymentSuccess = (req, res) => { + // найти один счет, который якобы оплачен + db.payment.findOne({ + where: { + payment_id: req.query?.paymentID, + user_id: req.query?.userID, + tokens: req.query?.tokens + } + }).then(async invoice => { + console.log('invoice', invoice['dataValues']) + // если он есть - зачислить пользователю токены и дату активации и дату окончания + await db.payment.update( + { payment_confirmed: Sequelize.literal('CURRENT_TIMESTAMP') }, + { where: { payment_id: req.query?.paymentID } } + ) + + await db.subscriber.update( + { + tokens: Sequelize.literal(`tokens + ${invoice.dataValues['tokens']}`), + paid_days: invoice.dataValues['duration_days'] + }, + { where: { user_id: invoice.dataValues['user_id'] } } + ) + + // отправить html c позитивным сообщением и просьбой вернуться в бота + res.status(200).send(`${invoice['dataValues']['type_of_tariff']} успешно зачислены!`) + }) + +} \ No newline at end of file diff --git a/api/routes/subs.routes.js b/api/routes/subs.routes.js index 95ffc6e..25c3ffe 100644 --- a/api/routes/subs.routes.js +++ b/api/routes/subs.routes.js @@ -1,5 +1,6 @@ import authJwt from "./../middleware/authJwt.js"; import { allSubs } from '../controllers/subs.controller.js' +import { paymentSuccess } from '../controllers/payments.controller.js' export default function(app) { app.use(function(req, res, next) { @@ -15,4 +16,9 @@ export default function(app) { [authJwt.verifyToken, authJwt.isAdmin], allSubs ); -}; + + app.get( + "/api/subs/payment-success", + paymentSuccess + ); +}; \ No newline at end of file diff --git a/bot/commands/keyboard/my_account.js b/bot/commands/keyboard/my_account.js index dc6430e..1b3667f 100644 --- a/bot/commands/keyboard/my_account.js +++ b/bot/commands/keyboard/my_account.js @@ -7,7 +7,7 @@ import dotenv from 'dotenv' import { ct } from '../../utils/createTranslate.js' import { referralLevelCreator } from '../../utils/payments/referralLevelCreator.js' import { keyboardQuiz } from './quiz.js' -import Stripe from 'stripe'; +import Stripe from 'stripe' dotenv.config({ path: '../.env' }) @@ -112,22 +112,6 @@ export const keyboardMyAccount = async (bot, msg, prevMessageForEdit, prevLevel, // Set your secret key. Remember to switch to your live secret key in production. // See your keys here: https://dashboard.stripe.com/apikeys const stripe = new Stripe(process.env.STRIPE_API) - const paymentLink = await stripe.paymentLinks.create({ - line_items: [ - { - price: tariff[0]['price_stripe'], - quantity: 1 - } - ], - after_completion: { - type: 'redirect', - redirect: { - url: 'https://example.com' - } - } - }) - - console.log('paymentLink', paymentLink) const payok = new PAYOK({ apiId: process.env.PAYOK_APIID, @@ -142,13 +126,28 @@ export const keyboardMyAccount = async (bot, msg, prevMessageForEdit, prevLevel, duration_days: tariff[0]['duration_days'], user_id: chatId, username: msg.from.username, - }).then((invoice) => { + tokens: tariff[0]['tokens'] + }).then(async (invoice) => { + + const paymentLink = await stripe.paymentLinks.create({ + line_items: [ + { + price: tariff[0]['price_stripe'], + quantity: 1 + } + ], + after_completion: { + type: 'redirect', + redirect: { + url: `https://154.56.63.128:3012/api/subs/payment-success?paymentID=${invoice.dataValues['payment_id']}&userID=${invoice.dataValues['chat_id']}&tokens=${invoice.dataValues['tokens']}` + } + } + }) const link = payok.getPaymentLink({ amount: tariff[0]['price_payok'], - payment: invoice.dataValues.payment_id, - desc: tariff[0]['text'], - method: 'sbp' + payment: invoice.dataValues.payment_id, // TODO: не показывать ID + desc: tariff[0]['text'] }) bot.editMessageText( @@ -165,8 +164,8 @@ Payok - оплачивайте следующими способами: chat_id: chatId, reply_markup: { inline_keyboard: [ - [{ text: t('btn:payok-rub'), url: link.payUrl }], [{ text: t('btn:stripe'), url: paymentLink.url }], + // [{ text: t('btn:payok-rub'), url: link.payUrl }], [{ text: t('return_to_menu'), callback_data: `get_first_level_A_${chatId}` }] // TODO: не работает ] } diff --git a/bot/constants/index.js b/bot/constants/index.js index e874d07..fe30cb7 100644 --- a/bot/constants/index.js +++ b/bot/constants/index.js @@ -28,6 +28,7 @@ export const COMMAND_HELP_FR = '🔍 l\'Aide' export const TARIFS = [ { + tokens: 10000, text: '10.000 🍪 for 7 days', callback_data: 'DAYS_7_10000', duration_days: 7, @@ -35,6 +36,7 @@ export const TARIFS = [ price_payok: '69' }, { + tokens: 30000, text: '30.000 🍪 for 7 days', callback_data: 'DAYS_7_30000', duration_days: 7, @@ -42,6 +44,7 @@ export const TARIFS = [ price_payok: '199' }, { + tokens: 150000, text: '150.000 🍪 for 1 month', callback_data: 'DAYS_30_150000', duration_days: 30, @@ -49,6 +52,7 @@ export const TARIFS = [ price_payok: '499' }, { + tokens: 620000, text: '620.000 🍪 for 6 month', callback_data: 'DAYS_30_620000', duration_days: 180, @@ -56,6 +60,7 @@ export const TARIFS = [ price_payok: '1900' }, { + tokens: 2000000, text: '2.000.000 🍪 for 1 year', callback_data: 'DAYS_365_2000000', duration_days: 365, @@ -63,6 +68,7 @@ export const TARIFS = [ price_payok: '4700' }, { + tokens: 4200000, text: '4.200.000 🍪 for 1 year', callback_data: 'DAYS_365_4200000', duration_days: 365, @@ -70,6 +76,7 @@ export const TARIFS = [ price_payok: '9300' }, { + tokens: 10000000, text: '10.000.000 🍪 for 1 year', callback_data: 'DAYS_365_10000000', duration_days: 365, diff --git a/bot/db/models/payment.model.js b/bot/db/models/payment.model.js index e64cd30..2600765 100644 --- a/bot/db/models/payment.model.js +++ b/bot/db/models/payment.model.js @@ -23,6 +23,10 @@ export default (sequelize, DataTypes) => { type: DataTypes.DOUBLE, allowNull: false, }, + tokens: { + type: DataTypes.DOUBLE, + allowNull: false, + }, price: { // 199 (сумма проведенная через stripe) type: DataTypes.STRING },