From 369702884a05e6763efdbd5c922dc9ed1aa62c6f Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Mon, 24 Apr 2017 08:01:27 -0600 Subject: [PATCH] Prevented ios and android subs from being upgrade to group plans (#8646) * Prevented ios and android subs from being upgrade to group plans * Add admin email sender * Updated admin email * Updated get user email * Fixed grammar issue --- .../group-plans/group-payments-create.test.js | 58 +++++++++++++++++++ website/server/libs/payments.js | 22 ++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/test/api/v3/unit/libs/payments/group-plans/group-payments-create.test.js b/test/api/v3/unit/libs/payments/group-plans/group-payments-create.test.js index c6db6cd6d1..d39487d12a 100644 --- a/test/api/v3/unit/libs/payments/group-plans/group-payments-create.test.js +++ b/test/api/v3/unit/libs/payments/group-plans/group-payments-create.test.js @@ -603,6 +603,64 @@ describe('Purchasing a subscription for group', () => { expect(updatedUser.purchased.plan.dateCreated).to.exist; }); + it('does not modify a user with a Google subscription', async () => { + plan.customerId = 'random'; + plan.paymentMethod = api.constants.GOOGLE_PAYMENT_METHOD; + + let recipient = new User(); + recipient.profile.name = 'recipient'; + recipient.purchased.plan = plan; + recipient.guilds.push(group._id); + await recipient.save(); + + user.guilds.push(group._id); + await user.save(); + data.groupId = group._id; + + await api.createSubscription(data); + + let updatedUser = await User.findById(recipient._id).exec(); + + expect(updatedUser.purchased.plan.planId).to.eql('basic_3mo'); + expect(updatedUser.purchased.plan.customerId).to.eql('random'); + expect(updatedUser.purchased.plan.dateUpdated).to.exist; + expect(updatedUser.purchased.plan.gemsBought).to.eql(0); + expect(updatedUser.purchased.plan.paymentMethod).to.eql(api.constants.GOOGLE_PAYMENT_METHOD); + expect(updatedUser.purchased.plan.extraMonths).to.eql(0); + expect(updatedUser.purchased.plan.dateTerminated).to.eql(null); + expect(updatedUser.purchased.plan.lastBillingDate).to.exist; + expect(updatedUser.purchased.plan.dateCreated).to.exist; + }); + + it('does not modify a user with an iOS subscription', async () => { + plan.customerId = 'random'; + plan.paymentMethod = api.constants.IOS_PAYMENT_METHOD; + + let recipient = new User(); + recipient.profile.name = 'recipient'; + recipient.purchased.plan = plan; + recipient.guilds.push(group._id); + await recipient.save(); + + user.guilds.push(group._id); + await user.save(); + data.groupId = group._id; + + await api.createSubscription(data); + + let updatedUser = await User.findById(recipient._id).exec(); + + expect(updatedUser.purchased.plan.planId).to.eql('basic_3mo'); + expect(updatedUser.purchased.plan.customerId).to.eql('random'); + expect(updatedUser.purchased.plan.dateUpdated).to.exist; + expect(updatedUser.purchased.plan.gemsBought).to.eql(0); + expect(updatedUser.purchased.plan.paymentMethod).to.eql(api.constants.IOS_PAYMENT_METHOD); + expect(updatedUser.purchased.plan.extraMonths).to.eql(0); + expect(updatedUser.purchased.plan.dateTerminated).to.eql(null); + expect(updatedUser.purchased.plan.lastBillingDate).to.exist; + expect(updatedUser.purchased.plan.dateCreated).to.exist; + }); + it('updates a user with a cancelled but active group subscription', async () => { plan.key = 'basic_earned'; plan.customerId = api.constants.GROUP_PLAN_CUSTOMER_ID; diff --git a/website/server/libs/payments.js b/website/server/libs/payments.js index 8cb600da48..38f681ea07 100644 --- a/website/server/libs/payments.js +++ b/website/server/libs/payments.js @@ -1,4 +1,5 @@ import _ from 'lodash'; +import nconf from 'nconf'; import analytics from './analyticsService'; import { getUserInfo, @@ -18,12 +19,16 @@ import { } from './errors'; import slack from './slack'; +const TECH_ASSISTANCE_EMAIL = nconf.get('EMAILS:TECH_ASSISTANCE_EMAIL'); + let api = {}; api.constants = { UNLIMITED_CUSTOMER_ID: 'habitrpg', // Users with the customerId have an unlimted free subscription GROUP_PLAN_CUSTOMER_ID: 'group-plan', GROUP_PLAN_PAYMENT_METHOD: 'Group Plan', + GOOGLE_PAYMENT_METHOD: 'Google', + IOS_PAYMENT_METHOD: 'Apple', }; function revealMysteryItems (user) { @@ -77,6 +82,7 @@ api.addSubscriptionToGroupUsers = async function addSubscriptionToGroupUsers (gr */ api.addSubToGroupUser = async function addSubToGroupUser (member, group) { let customerIdsToIgnore = [this.constants.GROUP_PLAN_CUSTOMER_ID, this.constants.UNLIMITED_CUSTOMER_ID]; + let paymentMethodsToIgnore = [this.constants.GOOGLE_PAYMENT_METHOD, this.constants.IOS_PAYMENT_METHOD]; let data = { user: {}, @@ -109,7 +115,21 @@ api.addSubToGroupUser = async function addSubToGroupUser (member, group) { if (member.isSubscribed()) { let memberPlan = member.purchased.plan; let customerHasCancelledGroupPlan = memberPlan.customerId === this.constants.GROUP_PLAN_CUSTOMER_ID && !member.hasNotCancelled(); - if (customerIdsToIgnore.indexOf(memberPlan.customerId) !== -1 && !customerHasCancelledGroupPlan) return; + let ignorePaymentPlan = paymentMethodsToIgnore.indexOf(memberPlan.paymentMethod) !== -1; + let ignoreCustomerId = customerIdsToIgnore.indexOf(memberPlan.customerId) !== -1; + + if (ignorePaymentPlan) { + txnEmail(TECH_ASSISTANCE_EMAIL, 'admin-user-subscription-details', [ + {name: 'PROFILE_NAME', content: member.profile.name}, + {name: 'UUID', content: member._id}, + {name: 'EMAIL', content: getUserInfo(member, ['email']).email}, + {name: 'PAYMENT_METHOD', content: memberPlan.paymentMethod}, + {name: 'PURCHASED_PLAN', content: JSON.stringify(memberPlan)}, + {name: 'ACTION_NEEDED', content: 'User has joined group plan. Tell them to cancel subscription then give them free sub.'}, + ]); + } + + if ((ignorePaymentPlan || ignoreCustomerId) && !customerHasCancelledGroupPlan) return; if (member.hasNotCancelled()) await member.cancelSubscription();