Files
habitica/website/server/libs/payments/gems.js
Matteo Pagliazzi 6d34319455 Stripe: upgrade module and API, switch to Checkout (#12785)
* upgrade stripe module

* switch stripe api to latest version

* fix api version in tests

* start upgrading client and server

* client: switch to redirect

* implement checkout session creation for gems, start implementing webhooks

* stripe: start refactoring one time payments

* working gems and gift payments

* start adding support for subscriptions

* stripe: migrate subscriptions and fix cancelling sub

* allow upgrading group plans

* remove console.log statements

* group plans: upgrade from static page / create new one

* fix #11885, correct group plan modal title

* silence more stripe webhooks

* fix group plans redirects

* implement editing payment method

* start cleaning up code

* fix(stripe): update in-code docs, fix eslint issues

* subscriptions tests

* remove and skip old tests

* skip integration tests

* fix client build

* stripe webhooks: throw error if request fails

* subscriptions: correctly pass groupId

* remove console.log

* stripe: add unit tests for one time payments

* wip: stripe checkout tests

* stripe createCheckoutSession unit tests

* stripe createCheckoutSession unit tests

* stripe createCheckoutSession unit tests (editing card)

* fix existing webhooks tests

* add new webhooks tests

* add more webhooks tests

* fix lint

* stripe integration tests

* better error handling when retrieving customer from stripe

* client: remove unused strings and improve error handling

* payments: limit gift message length (server)

* payments: limit gift message length (client)

* fix redirects when payment is cancelled

* add back "subUpdateCard" string

* fix redirects when editing a sub card, use proper names for products, check subs when gifting
2020-12-14 15:59:17 +01:00

131 lines
3.7 KiB
JavaScript

import { getAnalyticsServiceByEnvironment } 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';
const analytics = getAnalyticsServiceByEnvironment();
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();
}
const { MAX_GIFT_MESSAGE_LENGTH } = shared.constants;
export function validateGiftMessage (gift, user) {
if (gift.message && gift.message.length > MAX_GIFT_MESSAGE_LENGTH) {
throw new BadRequest(shared.i18n.t(
'giftMessageTooLong',
{ maxGiftMessageLength: MAX_GIFT_MESSAGE_LENGTH },
user.preferences.language,
));
}
}
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,
firstPurchase: data.user.purchased.txnCount === 1,
});
if (data.gift) await buyGemGift(data);
await data.user.save();
}