mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
* Added payment to groups and pay with group plan with Stripe * Added edit card for Stripe * Added stripe cancel * Added subscribe with Amazon payments * Added Amazon cancel for group subscription * Added group subscription with paypal * Added paypal cancel * Added ipn cancel for Group plan * Added a subscription tab and hid only the task tab when group is not subscribed * Fixed linting issues * Fixed tests * Added payment unit tests * Added back refresh after stripe payment * Fixed style issues * Limited grouop query fields and checked access * Abstracted subscription schema * Added year group plan and more access checks * Maded purchase fields private * Removed id and timestampes * Added else checks to ensure user subscription is not altered. Removed active field from group model * Added toJSONTransform function * Moved plan active check to other toJson function * Added check to see if purchaed has been populated * Added purchase details to private * Added correct data usage when paying for group sub
293 lines
8.8 KiB
JavaScript
293 lines
8.8 KiB
JavaScript
import _ from 'lodash' ;
|
|
import analytics from './analyticsService';
|
|
import {
|
|
getUserInfo,
|
|
sendTxn as txnEmail,
|
|
} from './email';
|
|
import moment from 'moment';
|
|
import { sendNotification as sendPushNotification } from './pushNotifications';
|
|
import shared from '../../common' ;
|
|
import {
|
|
model as Group,
|
|
basicFields as basicGroupFields,
|
|
} from '../models/group';
|
|
import {
|
|
NotAuthorized,
|
|
NotFound,
|
|
} from './errors';
|
|
|
|
let api = {};
|
|
|
|
function revealMysteryItems (user) {
|
|
_.each(shared.content.gear.flat, function findMysteryItems (item) {
|
|
if (
|
|
item.klass === 'mystery' &&
|
|
moment().isAfter(shared.content.mystery[item.mystery].start) &&
|
|
moment().isBefore(shared.content.mystery[item.mystery].end) &&
|
|
!user.items.gear.owned[item.key] &&
|
|
user.purchased.plan.mysteryItems.indexOf(item.key) === -1
|
|
) {
|
|
user.purchased.plan.mysteryItems.push(item.key);
|
|
}
|
|
});
|
|
}
|
|
|
|
function _dateDiff (earlyDate, lateDate) {
|
|
if (!earlyDate || !lateDate || moment(lateDate).isBefore(earlyDate)) return 0;
|
|
|
|
return moment(lateDate).diff(earlyDate, 'months', true);
|
|
}
|
|
|
|
api.createSubscription = async function createSubscription (data) {
|
|
let recipient = data.gift ? data.gift.member : data.user;
|
|
let block = shared.content.subscriptionBlocks[data.gift ? data.gift.subscription.key : data.sub.key];
|
|
let months = Number(block.months);
|
|
let today = new Date();
|
|
let plan;
|
|
let group;
|
|
let groupId;
|
|
let itemPurchased = 'Subscription';
|
|
let purchaseType = 'subscribe';
|
|
|
|
// If we are buying a group subscription
|
|
if (data.groupId) {
|
|
let groupFields = basicGroupFields.concat(' purchased');
|
|
group = await Group.getGroup({user: data.user, groupId: data.groupId, populateLeader: false, groupFields});
|
|
|
|
if (!group) {
|
|
throw new NotFound(shared.i18n.t('groupNotFound'));
|
|
}
|
|
|
|
if (!group.leader === data.user._id) {
|
|
throw new NotAuthorized(shared.i18n.t('onlyGroupLeaderCanManageSubscription'));
|
|
}
|
|
|
|
recipient = group;
|
|
itemPurchased = 'Group-Subscription';
|
|
purchaseType = 'group-subscribe';
|
|
groupId = group._id;
|
|
}
|
|
|
|
plan = recipient.purchased.plan;
|
|
|
|
if (data.gift) {
|
|
if (plan.customerId && !plan.dateTerminated) { // User has active plan
|
|
plan.extraMonths += months;
|
|
} else {
|
|
if (!plan.dateUpdated) plan.dateUpdated = today;
|
|
if (moment(plan.dateTerminated).isAfter()) {
|
|
plan.dateTerminated = moment(plan.dateTerminated).add({months}).toDate();
|
|
} else {
|
|
plan.dateTerminated = moment().add({months}).toDate();
|
|
}
|
|
}
|
|
|
|
if (!plan.customerId) plan.customerId = 'Gift'; // don't override existing customer, but all sub need a customerId
|
|
} else {
|
|
if (!plan.dateTerminated) plan.dateTerminated = today;
|
|
|
|
_(plan).merge({ // override with these values
|
|
planId: block.key,
|
|
customerId: data.customerId,
|
|
dateUpdated: today,
|
|
paymentMethod: data.paymentMethod,
|
|
extraMonths: Number(plan.extraMonths) + _dateDiff(today, plan.dateTerminated),
|
|
dateTerminated: null,
|
|
// Specify a lastBillingDate just for Amazon Payments
|
|
// Resetted every time the subscription restarts
|
|
lastBillingDate: data.paymentMethod === 'Amazon Payments' ? today : undefined,
|
|
}).defaults({ // allow non-override if a plan was previously used
|
|
gemsBought: 0,
|
|
dateCreated: today,
|
|
mysteryItems: [],
|
|
}).value();
|
|
}
|
|
|
|
// Block sub perks
|
|
let perks = Math.floor(months / 3);
|
|
if (perks) {
|
|
plan.consecutive.offset += months;
|
|
plan.consecutive.gemCapExtra += perks * 5;
|
|
if (plan.consecutive.gemCapExtra > 25) plan.consecutive.gemCapExtra = 25;
|
|
plan.consecutive.trinkets += perks;
|
|
}
|
|
|
|
if (recipient !== group) {
|
|
revealMysteryItems(recipient);
|
|
}
|
|
|
|
if (!data.gift) {
|
|
txnEmail(data.user, 'subscription-begins');
|
|
}
|
|
|
|
analytics.trackPurchase({
|
|
uuid: data.user._id,
|
|
groupId,
|
|
itemPurchased,
|
|
sku: `${data.paymentMethod.toLowerCase()}-subscription`,
|
|
purchaseType,
|
|
paymentMethod: data.paymentMethod,
|
|
quantity: 1,
|
|
gift: Boolean(data.gift),
|
|
purchaseValue: block.price,
|
|
headers: data.headers,
|
|
});
|
|
|
|
if (!group) data.user.purchased.txnCount++;
|
|
|
|
if (data.gift) {
|
|
let message = `\`Hello ${data.gift.member.profile.name}, ${data.user.profile.name} has sent you ${shared.content.subscriptionBlocks[data.gift.subscription.key].months} months of subscription!\``;
|
|
if (data.gift.message) message += ` ${data.gift.message}`;
|
|
|
|
data.user.sendMessage(data.gift.member, message);
|
|
|
|
let byUserName = getUserInfo(data.user, ['name']).name;
|
|
|
|
if (data.gift.member.preferences.emailNotifications.giftedSubscription !== false) {
|
|
txnEmail(data.gift.member, 'gifted-subscription', [
|
|
{name: 'GIFTER', content: byUserName},
|
|
{name: 'X_MONTHS_SUBSCRIPTION', content: months},
|
|
]);
|
|
}
|
|
|
|
if (data.gift.member._id !== data.user._id) { // Only send push notifications if sending to a user other than yourself
|
|
if (data.gift.member.preferences.pushNotifications.giftedSubscription !== false) {
|
|
sendPushNotification(data.gift.member,
|
|
{
|
|
title: shared.i18n.t('giftedSubscription'),
|
|
message: shared.i18n.t('giftedSubscriptionInfo', {months, name: byUserName}),
|
|
identifier: 'giftedSubscription',
|
|
payload: {replyTo: data.user._id},
|
|
}
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (group) {
|
|
await group.save();
|
|
} else {
|
|
await data.user.save();
|
|
}
|
|
|
|
if (data.gift) await data.gift.member.save();
|
|
};
|
|
|
|
// Sets their subscription to be cancelled later
|
|
api.cancelSubscription = async function cancelSubscription (data) {
|
|
let plan;
|
|
let group;
|
|
let cancelType = 'unsubscribe';
|
|
let groupId;
|
|
|
|
// If we are buying a group subscription
|
|
if (data.groupId) {
|
|
let groupFields = basicGroupFields.concat(' purchased');
|
|
group = await Group.getGroup({user: data.user, groupId: data.groupId, populateLeader: false, groupFields});
|
|
|
|
if (!group) {
|
|
throw new NotFound(shared.i18n.t('groupNotFound'));
|
|
}
|
|
|
|
if (!group.leader === data.user._id) {
|
|
throw new NotAuthorized(shared.i18n.t('onlyGroupLeaderCanManageSubscription'));
|
|
}
|
|
plan = group.purchased.plan;
|
|
} else {
|
|
plan = data.user.purchased.plan;
|
|
}
|
|
|
|
let now = moment();
|
|
let remaining = data.nextBill ? moment(data.nextBill).diff(new Date(), 'days') : 30;
|
|
let extraDays = Math.ceil(30.5 * plan.extraMonths);
|
|
let nowStr = `${now.format('MM')}/${moment(plan.dateUpdated).format('DD')}/${now.format('YYYY')}`;
|
|
let nowStrFormat = 'MM/DD/YYYY';
|
|
|
|
plan.dateTerminated =
|
|
moment(nowStr, nowStrFormat)
|
|
.add({days: remaining})
|
|
.add({days: extraDays})
|
|
.toDate();
|
|
|
|
plan.extraMonths = 0; // clear extra time. If they subscribe again, it'll be recalculated from p.dateTerminated
|
|
|
|
if (group) {
|
|
await group.save();
|
|
} else {
|
|
await data.user.save();
|
|
}
|
|
|
|
txnEmail(data.user, 'cancel-subscription');
|
|
|
|
if (group) {
|
|
cancelType = 'group-unsubscribe';
|
|
groupId = group._id;
|
|
}
|
|
|
|
analytics.track(cancelType, {
|
|
uuid: data.user._id,
|
|
groupId,
|
|
gaCategory: 'commerce',
|
|
gaLabel: data.paymentMethod,
|
|
paymentMethod: data.paymentMethod,
|
|
headers: data.headers,
|
|
});
|
|
};
|
|
|
|
api.buyGems = async function buyGems (data) {
|
|
let amt = data.amount || 5;
|
|
amt = data.gift ? data.gift.gems.amount / 4 : amt;
|
|
|
|
(data.gift ? data.gift.member : data.user).balance += amt;
|
|
data.user.purchased.txnCount++;
|
|
|
|
if (!data.gift) txnEmail(data.user, 'donation');
|
|
|
|
analytics.trackPurchase({
|
|
uuid: data.user._id,
|
|
itemPurchased: 'Gems',
|
|
sku: `${data.paymentMethod.toLowerCase()}-checkout`,
|
|
purchaseType: 'checkout',
|
|
paymentMethod: data.paymentMethod,
|
|
quantity: 1,
|
|
gift: Boolean(data.gift),
|
|
purchaseValue: amt,
|
|
headers: data.headers,
|
|
});
|
|
|
|
if (data.gift) {
|
|
let byUsername = getUserInfo(data.user, ['name']).name;
|
|
let gemAmount = data.gift.gems.amount || 20;
|
|
|
|
let message = `\`Hello ${data.gift.member.profile.name}, ${data.user.profile.name} has sent you ${gemAmount} gems!\``;
|
|
if (data.gift.message) message += ` ${data.gift.message}`;
|
|
data.user.sendMessage(data.gift.member, message);
|
|
|
|
if (data.gift.member.preferences.emailNotifications.giftedGems !== false) {
|
|
txnEmail(data.gift.member, 'gifted-gems', [
|
|
{name: 'GIFTER', content: byUsername},
|
|
{name: 'X_GEMS_GIFTED', content: gemAmount},
|
|
]);
|
|
}
|
|
|
|
if (data.gift.member._id !== data.user._id) { // Only send push notifications if sending to a user other than yourself
|
|
if (data.gift.member.preferences.pushNotifications.giftedGems !== false) {
|
|
sendPushNotification(
|
|
data.gift.member,
|
|
{
|
|
title: shared.i18n.t('giftedGems'),
|
|
message: shared.i18n.t('giftedGemsInfo', {amount: gemAmount, name: byUsername}),
|
|
identifier: 'giftedGems',
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
await data.gift.member.save();
|
|
}
|
|
|
|
await data.user.save();
|
|
};
|
|
|
|
module.exports = api;
|