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-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 Users = this.db.Users;
|
|
|
|
const Transactions = this.db.Transactions;
|
|
|
|
const stripeToken = this.request.body.stripeToken;
|
|
|
|
|
|
|
|
const createCustomer = {
|
|
|
|
card: stripeToken.id,
|
|
|
|
plan: 'usd_monthly',
|
2015-08-23 22:12:32 +01:00
|
|
|
email: this.session.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-06-06 15:37:00 +01:00
|
|
|
yield Users.updateOne({ _id: this.session.user.id },
|
|
|
|
{ $set: { stripe_customer: customer, type: 'Pro' } });
|
2015-07-09 23:01:43 +01:00
|
|
|
|
|
|
|
const transaction = {
|
2016-06-06 15:37:00 +01:00
|
|
|
user_id: this.session.user.id,
|
2015-07-09 23:01:43 +01:00
|
|
|
amount: customer.subscription.plan.amount,
|
|
|
|
desc: customer.subscription.plan.name,
|
2015-08-23 22:12:32 +01:00
|
|
|
date: new Date(customer.subscription.plan.created * 1000),
|
2015-07-09 23:01:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
yield Transactions.insertOne(transaction);
|
|
|
|
|
|
|
|
this.session.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({
|
|
|
|
to: this.session.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() {
|
2015-08-22 23:07:34 +01:00
|
|
|
this.assertCSRF();
|
2015-07-09 23:01:43 +01:00
|
|
|
const Users = this.db.Users;
|
2016-06-06 15:37:00 +01:00
|
|
|
const user = yield Users.findOne({ _id: this.session.user.id });
|
2015-07-09 23:01:43 +01:00
|
|
|
|
|
|
|
const confirmation = yield stripe.customers.cancelSubscription(
|
|
|
|
user.stripe_customer.id,
|
|
|
|
user.stripe_customer.subscription.id,
|
2016-06-06 15:37:00 +01:00
|
|
|
{ at_period_end: true }
|
2015-07-09 23:01:43 +01:00
|
|
|
);
|
|
|
|
|
2016-06-06 15:37:00 +01:00
|
|
|
yield Users.updateOne({ _id: this.session.user.id },
|
|
|
|
{ $set: { 'stripe_customer.subscription': confirmation, type: 'Free' } });
|
2015-07-09 23:01:43 +01:00
|
|
|
|
|
|
|
this.session.user.plan = 'Pro';
|
2016-06-06 15:37:00 +01:00
|
|
|
this.body = { status: 'inactive' };
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|