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
export function* create() {
const stripeToken = this.request.body.stripeToken;
2016-08-07 20:41:21 +01:00
const ip = this.request.headers['x-forwarded-for'] || this.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',
2016-08-07 14:38:05 +01:00
email: this.user.email,
2015-07-09 23:01:43 +01:00
};
const customer = yield stripe.customers.create(createCustomer);
this.assert(customer.subscription.status === 'active', 400, '{"status": "error"}');
delete customer.subscriptions;
2016-08-07 14:38:05 +01:00
const user = yield models.user.findById(this.user.id);
user.plan = 'Pro';
yield user.save();
2015-07-09 23:01:43 +01:00
2016-08-07 14:38:05 +01:00
const transaction = yield models.transaction.create({
userId: this.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
2016-08-07 14:38:05 +01:00
yield transaction.save();
2015-07-09 23:01:43 +01:00
2016-08-07 14:38:05 +01:00
this.user.plan = 'Pro';
2016-06-06 15:37:00 +01:00
this.body = { status: 'active' };
2015-07-09 23:01:43 +01:00
2015-08-23 22:12:32 +01:00
const html = yield render('email/inlined/pro');
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({
2016-08-07 14:38:05 +01:00
to: this.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
}
export function* cancel() {
2016-08-07 14:38:05 +01:00
const user = yield models.user.findById(this.user.id);
const transactions = yield user.getTransactions();
const transaction = transactions[0];
yield stripe.customers.cancelSubscription(
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';
yield user.save();
2015-07-09 23:01:43 +01:00
2016-08-07 14:38:05 +01:00
this.user.plan = 'Free';
2016-06-06 15:37:00 +01:00
this.body = { status: 'inactive' };
2015-07-09 23:01:43 +01:00
}