From f85f2a0c6d4220177ebac75dc5551cd2a9614c4e Mon Sep 17 00:00:00 2001 From: Sabe Jones Date: Thu, 8 Dec 2016 18:08:56 -0800 Subject: [PATCH] Gift Subscriptions Promo (#8270) * WIP(promo): buy-1-get-1 subs * WIP(subscriptions): Slack integration * feat(Slack): notify on sub buy --- config.json.example | 3 +- .../controllers/top-level/payments/amazon.js | 7 ++++- .../controllers/top-level/payments/paypal.js | 6 +++- .../controllers/top-level/payments/stripe.js | 7 ++++- website/server/libs/payments.js | 16 +++++++++++ website/server/libs/slack.js | 28 +++++++++++++++++++ 6 files changed, 63 insertions(+), 4 deletions(-) diff --git a/config.json.example b/config.json.example index 617022ee8a..fcb526c08a 100644 --- a/config.json.example +++ b/config.json.example @@ -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" } } diff --git a/website/server/controllers/top-level/payments/amazon.js b/website/server/controllers/top-level/payments/amazon.js index f8c5c1c856..1f474b5552 100644 --- a/website/server/controllers/top-level/payments/amazon.js +++ b/website/server/controllers/top-level/payments/amazon.js @@ -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); }, }; diff --git a/website/server/controllers/top-level/payments/paypal.js b/website/server/controllers/top-level/payments/paypal.js index bb4ffe665e..c726c88f03 100644 --- a/website/server/controllers/top-level/payments/paypal.js +++ b/website/server/controllers/top-level/payments/paypal.js @@ -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('/'); }, }; diff --git a/website/server/controllers/top-level/payments/stripe.js b/website/server/controllers/top-level/payments/stripe.js index 9fd86c703a..b21ccd6fb8 100644 --- a/website/server/controllers/top-level/payments/stripe.js +++ b/website/server/controllers/top-level/payments/stripe.js @@ -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, {}); diff --git a/website/server/libs/payments.js b/website/server/libs/payments.js index 564fbe8fa4..26de9828c6 100644 --- a/website/server/libs/payments.js +++ b/website/server/libs/payments.js @@ -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 diff --git a/website/server/libs/slack.js b/website/server/libs/slack.js index 41d38630e5..eb87d8e02e 100644 --- a/website/server/libs/slack.js +++ b/website/server/libs/slack.js @@ -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, };