2015-07-09 23:01:43 +01:00
|
|
|
import path from 'path';
|
|
|
|
import views from 'co-views';
|
|
|
|
import Stripe from 'stripe';
|
2018-08-11 12:08:16 +01:00
|
|
|
import sendgrid from '@sendgrid/mail';
|
2015-07-09 23:01:43 +01:00
|
|
|
|
2016-08-07 14:38:05 +01:00
|
|
|
import models from '../../models';
|
|
|
|
|
2018-06-02 18:07:00 +00:00
|
|
|
const render = views(path.join(__dirname, '/../views'), { default: 'ejs' });
|
|
|
|
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
|
2018-08-11 12:08:16 +01:00
|
|
|
sendgrid.setApiKey(process.env.SENDGRID_KEY);
|
2018-06-02 18:07:00 +00: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
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function create(ctx) {
|
2018-06-02 18:07:00 +00:00
|
|
|
const { stripeToken } = ctx.request.body;
|
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;
|
|
|
|
|
2019-06-08 07:52:57 -07:00
|
|
|
const user = await models.user.findByPk(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
|
|
|
|
`;
|
|
|
|
|
2018-08-11 12:08:16 +01:00
|
|
|
sendgrid.send({
|
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,
|
2018-08-11 12:08:16 +01:00
|
|
|
categories: [
|
|
|
|
'pro-upgrade',
|
|
|
|
],
|
2016-05-01 12:30:46 +01:00
|
|
|
});
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function cancel(ctx) {
|
2019-06-08 07:52:57 -07:00
|
|
|
const user = await models.user.findByPk(ctx.user.id);
|
2018-06-02 15:50:39 +00:00
|
|
|
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,
|
2018-06-02 18:07:00 +00:00
|
|
|
{ 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
|
|
|
}
|