Skip to content

Commit

Permalink
add half secure pay webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
theADAMJR committed Apr 17, 2020
1 parent e0a42de commit 708e1ca
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 500 deletions.
43 changes: 23 additions & 20 deletions api/routes/api-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Router } from 'express';
import { SavedCommand, CommandDocument } from '../../models/command';
import { AuthClient, stripe } from '../server';
import * as config from '../../config.json';
import Stripe from 'stripe';
import bodyParser from 'body-parser';

import { router as guildsRoutes } from './guilds-routes';
import { router as userRoutes } from './user-routes';
import Stripe from 'stripe';
import bodyParser from 'body-parser';
import { SavedUser } from '../../models/user';

export const router = Router(),
endpointSecret = 'whsec_uNgUHx7T0J1vbOgcTuRCEjXGZYTMvqs0';
Expand All @@ -25,27 +26,29 @@ router.get('/auth', async (req, res) => {
} catch { res.status(400).send('Bad Request'); }
});

const items = [
{
name: '2PG+',
description: 'Support 2PG, and unlock exclusive user features!',
amount: 500,
currency: 'usd',
quantity: 1,
router.post('/webhook', async(req, res) => {
try {
// stripe.webhooks.signature
// .parseHeader(req.headers['stripe-signature']);

const id = req.body.data.object.metadata.id;
if (req.body.type === 'checkout.session.completed') {
await giveUserPlus(id);
return res.json({ success: true });
}
];
router.get('/pay', async(req, res) => {
try {
const session = await stripe.checkout.sessions.create({
success_url: `${config.webapp.url}/payment-success`,
cancel_url: `${config.webapp.url}/plus`,
payment_method_types: ['card'],
line_items: items
});
res.send(session);
} catch (error) { res.status(400).send(error); }
res.json({ received: true });
} catch (error) { res.status(400).send(error); console.log(error);
}
});

async function giveUserPlus(id: string) {
console.log('give ' + id + ' premium');

const savedUser = await SavedUser.findById(id);
savedUser.premium = true;
savedUser.save();
}

router.get('/invite', (req, res) =>
res.redirect(`https://discordapp.com/api/oauth2/authorize?client_id=${config.bot.id}&redirect_uri=${config.webapp.url}/dashboard&permissions=8&scope=bot`));

Expand Down
7 changes: 3 additions & 4 deletions api/routes/guilds-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ router.get('/', async (req, res) => {
});

router.put('/:id/:module', async (req, res) => {
try {
try {
const { id, module } = req.params;

const isValidModule = config.modules.some(m => m.toLowerCase() === module);
Expand All @@ -56,7 +56,7 @@ router.put('/:id/:module', async (req, res) => {
await log.save();

res.json(savedGuild);
} catch (error) { res.status(400).send(error); console.log(error) }
} catch (error) { res.status(400).send(error); }
});

router.get('/:id/config', async (req, res) => {
Expand Down Expand Up @@ -163,8 +163,7 @@ router.get('/:guildId/members/:memberId/xp-card', async (req, res) => {

res.set({'Content-Type': 'image/png'}).send(image);
}
catch (error) { res.status(400).send('Bad Request'); console.log(error);
}
catch (error) { res.status(400).send(error?.message); }
});

async function validateGuildManager(key: string, id: string) {
Expand Down
27 changes: 26 additions & 1 deletion api/routes/user-routes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Router } from 'express';
import { XPCardGenerator } from '../modules/image/xp-card-generator';
import { SavedMember } from '../../models/member';
import { AuthClient } from '../server';
import { AuthClient, stripe } from '../server';
import { bot } from '../../bot';
import Deps from '../../utils/deps';
import Users from '../../data/users';
import config from '../../config.json';

export const router = Router();

Expand All @@ -15,6 +16,30 @@ router.get('/', async (req, res) => {
} catch { res.status(400).send('Bad Request'); }
});

const items = [
{
name: '2PG+',
description: 'Support 2PG, and unlock exclusive user features!',
amount: 500,
currency: 'usd',
quantity: 1,
}
];
router.get('/pay', async(req, res) => {
try {
const user = await getUser(req.query.key);

const session = await stripe.checkout.sessions.create({
success_url: `${config.webapp.url}/payment-success`,
cancel_url: `${config.webapp.url}/plus`,
payment_method_types: ['card'],
metadata: { 'id': user.id },
line_items: items
});
res.send(session);
} catch (error) { res.status(400).send(error); }
});

router.get('/saved', async (req, res) => {
try {
const user = await getUser(req.query.key);
Expand Down
614 changes: 139 additions & 475 deletions dist/lavalink/logs/spring.log

Large diffs are not rendered by default.

Binary file added dist/lavalink/logs/spring.log.2020-04-13.0.gz
Binary file not shown.
Binary file added dist/lavalink/logs/spring.log.2020-04-14.0.gz
Binary file not shown.
Binary file added dist/lavalink/logs/spring.log.2020-04-15.0.gz
Binary file not shown.
Binary file added dist/lavalink/logs/spring.log.2020-04-16.0.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export class XPCard {

const userSchema = new Schema({
_id: String,
premium: Boolean,
votes: Number,
xpCard: { type: Object, default: new XPCard() }
});

export interface UserDocument extends Document {
_id: string;
premium: boolean;
votes: number;
xpCard: XPCard;
}
Expand Down

0 comments on commit 708e1ca

Please sign in to comment.