Gift Subscriptions Promo (#8270)

* WIP(promo): buy-1-get-1 subs

* WIP(subscriptions): Slack integration

* feat(Slack): notify on sub buy
This commit is contained in:
Sabe Jones
2016-12-08 18:08:56 -08:00
committed by GitHub
parent 5efe5b7b10
commit f85f2a0c6d
6 changed files with 63 additions and 4 deletions

View File

@@ -79,6 +79,7 @@
},
"SLACK": {
"FLAGGING_URL": "https://hooks.slack.com/services/id/id/id",
"FLAGGING_FOOTER_LINK": "https://habitrpg.github.io/flag-o-rama/"
"FLAGGING_FOOTER_LINK": "https://habitrpg.github.io/flag-o-rama/",
"SUBSCRIPTIONS_URL": "https://hooks.slack.com/services/id/id/id"
}
}

View File

@@ -144,11 +144,16 @@ api.checkout = {
if (gift.type === 'subscription') method = 'createSubscription';
gift.member = await User.findById(gift ? gift.uuid : undefined);
data.gift = gift;
data.paymentMethod = 'Gift';
data.paymentMethod = 'Amazon';
}
await payments[method](data);
if (gift && gift.type === 'subscription' && gift.member._id !== user._id) {
gift.member = user;
await payments.createSubscription(data);
}
res.respond(200);
},
};

View File

@@ -139,12 +139,16 @@ api.checkoutSuccess = {
method = 'createSubscription';
}
data.paymentMethod = 'Gift';
data.paymentMethod = 'PayPal';
data.gift = gift;
}
await paypalPaymentExecute(paymentId, { payer_id: customerId });
await payments[method](data);
if (gift && gift.type === 'subscription' && gift.member._id !== data.user._id) {
gift.member = data.user;
await payments.createSubscription(data);
}
res.redirect('/');
},
};

View File

@@ -105,10 +105,15 @@ api.checkout = {
let member = await User.findById(gift.uuid);
gift.member = member;
if (gift.type === 'subscription') method = 'createSubscription';
data.paymentMethod = 'Gift';
data.paymentMethod = 'Stripe';
}
await payments[method](data);
if (gift && gift.type === 'subscription' && gift.member._id !== user._id) {
gift.member = user;
await payments.createSubscription(data);
}
}
res.respond(200, {});

View File

@@ -15,6 +15,7 @@ import {
NotAuthorized,
NotFound,
} from './errors';
import slack from './slack';
let api = {};
@@ -171,6 +172,21 @@ api.createSubscription = async function createSubscription (data) {
}
if (data.gift) await data.gift.member.save();
slack.sendSubscriptionNotification({
buyer: {
id: data.user._id,
name: data.user.profile.name,
email: getUserInfo(data.user, ['email']).email,
},
recipient: data.gift ? {
id: data.gift.member._id,
name: data.gift.member.profile.name,
email: getUserInfo(data.gift.member, ['email']).email,
} : {},
paymentMethod: data.paymentMethod,
months,
});
};
// Sets their subscription to be cancelled later

View File

@@ -6,12 +6,15 @@ import nconf from 'nconf';
const SLACK_FLAGGING_URL = nconf.get('SLACK:FLAGGING_URL');
const SLACK_FLAGGING_FOOTER_LINK = nconf.get('SLACK:FLAGGING_FOOTER_LINK');
const SLACK_SUBSCRIPTIONS_URL = nconf.get('SLACK:SUBSCRIPTIONS_URL');
const BASE_URL = nconf.get('BASE_URL');
let flagSlack;
let subscriptionSlack;
try {
flagSlack = new IncomingWebhook(SLACK_FLAGGING_URL);
subscriptionSlack = new IncomingWebhook(SLACK_SUBSCRIPTIONS_URL);
} catch (err) {
logger.error(err);
}
@@ -61,6 +64,31 @@ function sendFlagNotification ({
});
}
function sendSubscriptionNotification ({
buyer,
recipient,
paymentMethod,
months,
}) {
if (!SLACK_SUBSCRIPTIONS_URL) {
return;
}
let text;
let timestamp = new Date();
if (!recipient.id) {
text = `${buyer.name} ${buyer.id} ${buyer.email} bought a ${months}-month recurring subscription using ${paymentMethod} on ${timestamp}`;
} else if (recipient.id === buyer.id) {
text = `${buyer.name} ${buyer.id} ${buyer.email} bought a ${months}-month gift subscription for ${recipient.name} ${recipient.id} ${recipient.email} using ${paymentMethod} on ${timestamp}`;
} else {
text = `${buyer.name} ${buyer.id} ${buyer.email} bought a ${months}-month gift subscription for ${recipient.name} ${recipient.id} ${recipient.email} and got a promo using ${paymentMethod} on ${timestamp}`;
}
subscriptionSlack.send({
text,
});
}
module.exports = {
sendFlagNotification,
sendSubscriptionNotification,
};