hostr/api/routes/pro.js

86 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
import path from 'path';
import views from 'co-views';
2016-06-06 15:37:00 +01:00
const render = views(path.join(__dirname, '/../views'), { default: 'ejs' });
2015-07-09 23:01:43 +01:00
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
2016-05-01 12:30:46 +01:00
import sendgridInit from 'sendgrid';
const sendgrid = sendgridInit(process.env.SENDGRID_KEY);
2015-07-09 23:01:43 +01:00
2016-08-07 14:38:05 +01:00
import models from '../../models';
2016-06-06 15:37:00 +01:00
const from = process.env.EMAIL_FROM;
const fromname = process.env.EMAIL_NAME;
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
export async function create(ctx) {
const stripeToken = ctx.request.body.stripeToken;
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
const ip = ctx.request.headers['x-forwarded-for'] || ctx.req.connection.remoteAddress;
2016-08-07 14:38:05 +01:00
2015-07-09 23:01:43 +01:00
const createCustomer = {
card: stripeToken.id,
plan: 'usd_monthly',
2018-06-02 15:50:39 +00:00
email: ctx.user.email,
2015-07-09 23:01:43 +01:00
};
2018-06-02 15:50:39 +00:00
const customer = await stripe.customers.create(createCustomer);
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
ctx.assert(customer.subscription.status === 'active', 400, '{"status": "error"}');
2015-07-09 23:01:43 +01:00
delete customer.subscriptions;
2018-06-02 15:50:39 +00:00
const user = await models.user.findById(ctx.user.id);
2016-08-07 14:38:05 +01:00
user.plan = 'Pro';
2018-06-02 15:50:39 +00:00
await user.save();
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
const transaction = await models.transaction.create({
userId: ctx.user.id,
2015-07-09 23:01:43 +01:00
amount: customer.subscription.plan.amount,
2016-08-07 14:38:05 +01:00
description: customer.subscription.plan.name,
data: customer,
type: 'direct',
ip,
});
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
await transaction.save();
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
ctx.user.plan = 'Pro';
ctx.body = { status: 'active' };
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
const html = await render('email/inlined/pro');
2015-08-23 22:12:32 +01:00
const text = `Hey, thanks for upgrading to Hostr Pro!
2015-07-09 23:01:43 +01:00
You've signed up for Hostr Pro Monthly at $6/Month.
Jonathan Cremin, Hostr Founder
`;
2016-05-01 12:30:46 +01:00
const mail = new sendgrid.Email({
2018-06-02 15:50:39 +00:00
to: ctx.user.email,
2015-07-09 23:01:43 +01:00
subject: 'Hostr Pro',
2016-06-06 15:37:00 +01:00
from,
fromname,
html,
text,
2016-05-01 12:30:46 +01:00
});
mail.addCategory('pro-upgrade');
sendgrid.send(mail);
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function cancel(ctx) {
const user = await models.user.findById(ctx.user.id);
const transactions = await user.getTransactions();
2016-08-07 14:38:05 +01:00
const transaction = transactions[0];
2018-06-02 15:50:39 +00:00
await stripe.customers.cancelSubscription(
2016-08-07 14:38:05 +01:00
transaction.data.id,
transaction.data.subscription.id,
{ at_period_end: false }
2015-07-09 23:01:43 +01:00
);
2016-08-07 14:38:05 +01:00
user.plan = 'Free';
2018-06-02 15:50:39 +00:00
await user.save();
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
ctx.user.plan = 'Free';
ctx.body = { status: 'inactive' };
2015-07-09 23:01:43 +01:00
}