Files
habitica/website/server/libs/payments/gems.js
Matteo Pagliazzi 83aca20ce5 Fall Festival Gem Promo (#138)
* content: add gems blocks

* gemsBlocks: include ios and android identifiers

* wip: promo code

* split common constants into multiple files

* add second promo part

* geCurrentEvent, refactor promo

* fix lint

* fix exports, use world state api

* start adding world state tests

* remove console.log

* use gems block for purchases

* remove comments

* fix most unit tests

* restore comment

* fix lint

* prevent apple/google gift tests from breaking other tests when stub is not reset

* fix unit tests, clarify tests names

* iap: use gift object when gifting gems

* allow gift object with less data

* fix iap tests, remove findById stubs

* iap: require less data from the mobile apps

* apply discounts

* add missing worldState file

* fix lint

* add test event

* start removing 20 gems option for web

* start adding support for all gems packages on web

* fix unit tests for apple, stripe and google

* amazon: support all gems blocks

* paypal: support all gems blocks

* fix payments unit tests, add tests for getGemsBlock

* web: add gems plans with discounts, update stripe

* fix amazon and paypal clients, payments success modals

* amazon pay: disabled state

* update icons, start abstracting payments buttons

* begin redesign

* redesign gems modal

* fix buttons

* fix hover color for gems modal close icon

* add key to world state current event

* extend test event length

* implement gems modals designs

* early test fall2020

* fix header banner position

* add missing files

* use iso 8601 for dates, minor ui fixes

* fix time zones

* events: fix ISO8601 format

* fix css indentation

* start abstracting banners

* refactor payments buttons

* test spooky, fix group plans box

* implement gems promo banners, refactor banners, fixes

* fix lint

* fix dates

* remove unused i18n strings

* fix stripe integration test

* fix world state integration tests

* the current active event

* add missing unit tests

* add storybook story for payments buttons component

* fix typo

* fix(stripe): correct label when gifting subscriptions
2020-09-21 16:22:13 +02:00

117 lines
3.3 KiB
JavaScript

import * as analytics from '../analyticsService';
import { getCurrentEvent } from '../worldState'; // eslint-disable-line import/no-cycle
import { // eslint-disable-line import/no-cycle
getUserInfo,
sendTxn as txnEmail,
} from '../email';
import { sendNotification as sendPushNotification } from '../pushNotifications'; // eslint-disable-line import/no-cycle
import shared from '../../../common';
import {
BadRequest,
} from '../errors';
import apiError from '../apiError';
function getGiftMessage (data, byUsername, gemAmount, language) {
const senderMsg = shared.i18n.t('giftedGemsFull', {
username: data.gift.member.profile.name,
sender: byUsername,
gemAmount,
}, language);
const quotedMessage = `\`${senderMsg}\``;
if (data.gift.message) return `${quotedMessage} ${data.gift.message}`;
return quotedMessage;
}
async function buyGemGift (data) {
const byUsername = getUserInfo(data.user, ['name']).name;
const gemAmount = data.gift.gems.amount || 20;
const languages = [data.user.preferences.language, data.gift.member.preferences.language];
const senderMsg = getGiftMessage(data, byUsername, gemAmount, languages[0]);
const receiverMsg = getGiftMessage(data, byUsername, gemAmount, languages[1]);
data.user.sendMessage(data.gift.member, { receiverMsg, senderMsg, save: false });
if (data.gift.member.preferences.emailNotifications.giftedGems !== false) {
txnEmail(data.gift.member, 'gifted-gems', [
{ name: 'GIFTER', content: byUsername },
{ name: 'X_GEMS_GIFTED', content: gemAmount },
]);
}
// Only send push notifications if sending to a user other than yourself
if (
data.gift.member._id !== data.user._id
&& data.gift.member.preferences.pushNotifications.giftedGems !== false
) {
sendPushNotification(
data.gift.member,
{
title: shared.i18n.t('giftedGems', languages[1]),
message: shared.i18n.t('giftedGemsInfo', { amount: gemAmount, name: byUsername }, languages[1]),
identifier: 'giftedGems',
},
);
}
await data.gift.member.save();
}
export function getGemsBlock (gemsBlock) {
const block = shared.content.gems[gemsBlock];
if (!block) throw new BadRequest(apiError('invalidGemsBlock'));
return block;
}
function getAmountForGems (data) {
if (data.gift) return data.gift.gems.amount / 4;
const { gemsBlock } = data;
const currentEvent = getCurrentEvent();
if (currentEvent && currentEvent.gemsPromo && currentEvent.gemsPromo[gemsBlock.key]) {
return currentEvent.gemsPromo[gemsBlock.key] / 4;
}
return gemsBlock.gems / 4;
}
function updateUserBalance (data, amount) {
if (data.gift) {
data.gift.member.balance += amount;
return;
}
data.user.balance += amount;
}
export async function buyGems (data) {
const amt = getAmountForGems(data);
updateUserBalance(data, amt);
data.user.purchased.txnCount += 1;
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) await buyGemGift(data);
await data.user.save();
}