Skip to content

Commit

Permalink
feat(payments): invoice logic
Browse files Browse the repository at this point in the history
  • Loading branch information
aronbergman committed May 6, 2024
1 parent 5370fee commit 50c8009
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
32 changes: 32 additions & 0 deletions api/controllers/payments.controller.js
Original file line number Diff line number Diff line change
@@ -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']} успешно зачислены!`)
})

}
8 changes: 7 additions & 1 deletion api/routes/subs.routes.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -15,4 +16,9 @@ export default function(app) {
[authJwt.verifyToken, authJwt.isAdmin],
allSubs
);
};

app.get(
"/api/subs/payment-success",
paymentSuccess
);
};
43 changes: 21 additions & 22 deletions bot/commands/keyboard/my_account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })

Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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: не работает
]
}
Expand Down
7 changes: 7 additions & 0 deletions bot/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,55 @@ 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,
price_stripe: 'price_1PDV8G033yepPfsGBNvZ6XRr',
price_payok: '69'
},
{
tokens: 30000,
text: '30.000 🍪 for 7 days',
callback_data: 'DAYS_7_30000',
duration_days: 7,
price_stripe: 'price_1PDWkC033yepPfsGETJ8sEey',
price_payok: '199'
},
{
tokens: 150000,
text: '150.000 🍪 for 1 month',
callback_data: 'DAYS_30_150000',
duration_days: 30,
price_stripe: 'price_1PDWlg033yepPfsGa4W2wSIP',
price_payok: '499'
},
{
tokens: 620000,
text: '620.000 🍪 for 6 month',
callback_data: 'DAYS_30_620000',
duration_days: 180,
price_stripe: 'price_1PDWoJ033yepPfsGDhkbouQB',
price_payok: '1900'
},
{
tokens: 2000000,
text: '2.000.000 🍪 for 1 year',
callback_data: 'DAYS_365_2000000',
duration_days: 365,
price_stripe: 'price_1PDWrA033yepPfsGtHlwFzqC',
price_payok: '4700'
},
{
tokens: 4200000,
text: '4.200.000 🍪 for 1 year',
callback_data: 'DAYS_365_4200000',
duration_days: 365,
price_stripe: 'price_1PDWt8033yepPfsGETZl0hzT',
price_payok: '9300'
},
{
tokens: 10000000,
text: '10.000.000 🍪 for 1 year',
callback_data: 'DAYS_365_10000000',
duration_days: 365,
Expand Down
4 changes: 4 additions & 0 deletions bot/db/models/payment.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down

0 comments on commit 50c8009

Please sign in to comment.