2019-10-08 14:57:10 +00:00
|
|
|
import moment from 'moment';
|
2018-04-08 14:27:03 +00:00
|
|
|
import shared from '../../../common';
|
|
|
|
|
import iap from '../inAppPurchases';
|
2017-02-02 00:39:37 +00:00
|
|
|
import payments from './payments';
|
|
|
|
|
import {
|
|
|
|
|
NotAuthorized,
|
|
|
|
|
BadRequest,
|
2018-04-08 14:27:03 +00:00
|
|
|
} from '../errors';
|
|
|
|
|
import { model as IapPurchaseReceipt } from '../../models/iapPurchaseReceipt';
|
2019-10-08 14:57:10 +00:00
|
|
|
import { model as User } from '../../models/user';
|
2023-01-20 22:14:33 +00:00
|
|
|
import { validateGiftMessage } from './gems';
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const api = {};
|
2017-02-02 00:39:37 +00:00
|
|
|
|
|
|
|
|
api.constants = {
|
|
|
|
|
PAYMENT_METHOD_GOOGLE: 'Google',
|
2018-12-23 18:20:14 +00:00
|
|
|
PAYMENT_METHOD_GIFT: 'Google (Gift)',
|
2017-02-02 00:39:37 +00:00
|
|
|
RESPONSE_INVALID_RECEIPT: 'INVALID_RECEIPT',
|
|
|
|
|
RESPONSE_ALREADY_USED: 'RECEIPT_ALREADY_USED',
|
|
|
|
|
RESPONSE_INVALID_ITEM: 'INVALID_ITEM_PURCHASED',
|
|
|
|
|
RESPONSE_STILL_VALID: 'SUBSCRIPTION_STILL_VALID',
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-20 22:14:33 +00:00
|
|
|
api.verifyPurchase = async function verifyPurchase (options) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const {
|
|
|
|
|
gift, user, receipt, signature, headers,
|
|
|
|
|
} = options;
|
2018-12-23 18:20:14 +00:00
|
|
|
|
|
|
|
|
if (gift) {
|
|
|
|
|
gift.member = await User.findById(gift.uuid).exec();
|
2020-12-14 14:59:17 +00:00
|
|
|
validateGiftMessage(gift, user);
|
2018-12-23 18:20:14 +00:00
|
|
|
}
|
|
|
|
|
const receiver = gift ? gift.member : user;
|
|
|
|
|
const receiverCanGetGems = await receiver.canGetGems();
|
|
|
|
|
if (!receiverCanGetGems) throw new NotAuthorized(shared.i18n.t('groupPolicyCannotGetGems', user.preferences.language));
|
2017-07-16 16:23:57 +00:00
|
|
|
|
2017-02-02 00:39:37 +00:00
|
|
|
await iap.setup();
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const testObj = {
|
2017-02-02 00:39:37 +00:00
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const googleRes = await iap.validate(iap.GOOGLE, testObj);
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const isValidated = iap.isValidated(googleRes);
|
2017-02-02 00:39:37 +00:00
|
|
|
if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const receiptObj = typeof testObj.data === 'string' ? JSON.parse(testObj.data) : testObj.data; // passed as a string
|
|
|
|
|
const token = receiptObj.token || receiptObj.purchaseToken;
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const existingReceipt = await IapPurchaseReceipt.findOne({
|
2017-02-02 00:39:37 +00:00
|
|
|
_id: token,
|
|
|
|
|
}).exec();
|
|
|
|
|
if (existingReceipt) throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
|
|
|
|
|
|
|
|
|
await IapPurchaseReceipt.create({
|
|
|
|
|
_id: token,
|
|
|
|
|
consumed: true,
|
2018-12-23 18:20:14 +00:00
|
|
|
// This should always be the buying user even for a gift.
|
2017-02-02 00:39:37 +00:00
|
|
|
userId: user._id,
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-23 04:30:48 +00:00
|
|
|
await payments.buySkuItem({ // eslint-disable-line no-await-in-loop
|
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 14:22:13 +00:00
|
|
|
user,
|
|
|
|
|
gift,
|
2023-01-20 22:14:33 +00:00
|
|
|
paymentMethod: api.constants.PAYMENT_METHOD_GOOGLE,
|
|
|
|
|
sku: googleRes.productId,
|
2017-02-02 00:39:37 +00:00
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return googleRes;
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-10 18:11:50 +00:00
|
|
|
api.subscribe = async function subscribe (
|
2024-01-16 21:18:47 +00:00
|
|
|
sku,
|
|
|
|
|
user,
|
|
|
|
|
receipt,
|
|
|
|
|
signature,
|
|
|
|
|
headers,
|
|
|
|
|
nextPaymentProcessing = undefined,
|
2019-10-10 18:11:50 +00:00
|
|
|
) {
|
2017-02-02 00:39:37 +00:00
|
|
|
if (!sku) throw new BadRequest(shared.i18n.t('missingSubscriptionCode'));
|
|
|
|
|
let subCode;
|
2019-10-10 18:11:50 +00:00
|
|
|
switch (sku) { // eslint-disable-line default-case
|
2017-02-02 00:39:37 +00:00
|
|
|
case 'com.habitrpg.android.habitica.subscription.1month':
|
|
|
|
|
subCode = 'basic_earned';
|
|
|
|
|
break;
|
|
|
|
|
case 'com.habitrpg.android.habitica.subscription.3month':
|
|
|
|
|
subCode = 'basic_3mo';
|
|
|
|
|
break;
|
|
|
|
|
case 'com.habitrpg.android.habitica.subscription.6month':
|
|
|
|
|
subCode = 'basic_6mo';
|
|
|
|
|
break;
|
|
|
|
|
case 'com.habitrpg.android.habitica.subscription.12month':
|
|
|
|
|
subCode = 'basic_12mo';
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-08 14:57:10 +00:00
|
|
|
const sub = subCode ? shared.content.subscriptionBlocks[subCode] : false;
|
2017-02-02 00:39:37 +00:00
|
|
|
if (!sub) throw new NotAuthorized(this.constants.RESPONSE_INVALID_ITEM);
|
|
|
|
|
|
|
|
|
|
await iap.setup();
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const testObj = {
|
2017-02-02 00:39:37 +00:00
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const receiptObj = typeof receipt === 'string' ? JSON.parse(receipt) : receipt; // passed as a string
|
|
|
|
|
const token = receiptObj.token || receiptObj.purchaseToken;
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const existingUser = await User.findOne({
|
2017-02-02 19:51:52 +00:00
|
|
|
'purchased.plan.customerId': token,
|
2017-02-02 00:39:37 +00:00
|
|
|
}).exec();
|
|
|
|
|
if (existingUser) throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const googleRes = await iap.validate(iap.GOOGLE, testObj);
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const isValidated = iap.isValidated(googleRes);
|
2017-02-02 00:39:37 +00:00
|
|
|
if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
|
|
|
|
|
|
2019-10-10 18:11:50 +00:00
|
|
|
nextPaymentProcessing = nextPaymentProcessing || moment.utc().add({ days: 2 }); // eslint-disable-line no-param-reassign, max-len
|
2017-02-02 00:39:37 +00:00
|
|
|
|
|
|
|
|
await payments.createSubscription({
|
|
|
|
|
user,
|
|
|
|
|
customerId: token,
|
|
|
|
|
paymentMethod: this.constants.PAYMENT_METHOD_GOOGLE,
|
|
|
|
|
sub,
|
|
|
|
|
headers,
|
|
|
|
|
nextPaymentProcessing,
|
|
|
|
|
additionalData: testObj,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-23 18:20:14 +00:00
|
|
|
api.noRenewSubscribe = async function noRenewSubscribe (options) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const {
|
|
|
|
|
sku, gift, user, receipt, signature, headers,
|
|
|
|
|
} = options;
|
2018-12-23 18:20:14 +00:00
|
|
|
if (!sku) throw new BadRequest(shared.i18n.t('missingSubscriptionCode'));
|
|
|
|
|
let subCode;
|
2019-10-10 18:11:50 +00:00
|
|
|
switch (sku) { // eslint-disable-line default-case
|
2018-12-23 18:20:14 +00:00
|
|
|
case 'com.habitrpg.android.habitica.norenew_subscription.1month':
|
|
|
|
|
subCode = 'basic_earned';
|
|
|
|
|
break;
|
|
|
|
|
case 'com.habitrpg.android.habitica.norenew_subscription.3month':
|
|
|
|
|
subCode = 'basic_3mo';
|
|
|
|
|
break;
|
|
|
|
|
case 'com.habitrpg.android.habitica.norenew_subscription.6month':
|
|
|
|
|
subCode = 'basic_6mo';
|
|
|
|
|
break;
|
|
|
|
|
case 'com.habitrpg.android.habitica.norenew_subscription.12month':
|
|
|
|
|
subCode = 'basic_12mo';
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-08 14:57:10 +00:00
|
|
|
const sub = subCode ? shared.content.subscriptionBlocks[subCode] : false;
|
2018-12-23 18:20:14 +00:00
|
|
|
if (!sub) throw new NotAuthorized(this.constants.RESPONSE_INVALID_ITEM);
|
|
|
|
|
|
|
|
|
|
await iap.setup();
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const testObj = {
|
2018-12-23 18:20:14 +00:00
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const receiptObj = typeof receipt === 'string' ? JSON.parse(receipt) : receipt; // passed as a string
|
|
|
|
|
const token = receiptObj.token || receiptObj.purchaseToken;
|
2018-12-23 18:20:14 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const existingReceipt = await IapPurchaseReceipt.findOne({ // eslint-disable-line no-await-in-loop
|
2018-12-23 18:20:14 +00:00
|
|
|
_id: token,
|
|
|
|
|
}).exec();
|
|
|
|
|
if (existingReceipt) throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
|
|
|
|
|
|
|
|
|
await IapPurchaseReceipt.create({ // eslint-disable-line no-await-in-loop
|
|
|
|
|
_id: token,
|
|
|
|
|
consumed: true,
|
|
|
|
|
// This should always be the buying user even for a gift.
|
|
|
|
|
userId: user._id,
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const googleRes = await iap.validate(iap.GOOGLE, testObj);
|
2018-12-23 18:20:14 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const isValidated = iap.isValidated(googleRes);
|
2018-12-23 18:20:14 +00:00
|
|
|
if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const data = {
|
2018-12-23 18:20:14 +00:00
|
|
|
user,
|
|
|
|
|
paymentMethod: this.constants.PAYMENT_METHOD_GOOGLE,
|
|
|
|
|
headers,
|
|
|
|
|
sub,
|
|
|
|
|
autoRenews: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (gift) {
|
2020-12-14 14:59:17 +00:00
|
|
|
validateGiftMessage(gift, user);
|
2018-12-23 18:20:14 +00:00
|
|
|
gift.member = await User.findById(gift.uuid).exec();
|
|
|
|
|
gift.subscription = sub;
|
|
|
|
|
data.gift = gift;
|
|
|
|
|
data.paymentMethod = this.constants.PAYMENT_METHOD_GIFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await payments.createSubscription(data);
|
|
|
|
|
|
|
|
|
|
return googleRes;
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-02 00:39:37 +00:00
|
|
|
api.cancelSubscribe = async function cancelSubscribe (user, headers) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const { plan } = user.purchased;
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2017-02-21 18:22:13 +00:00
|
|
|
if (plan.paymentMethod !== api.constants.PAYMENT_METHOD_GOOGLE) throw new NotAuthorized(shared.i18n.t('missingSubscription'));
|
2017-02-02 00:39:37 +00:00
|
|
|
|
|
|
|
|
await iap.setup();
|
|
|
|
|
|
2018-03-30 17:24:51 +00:00
|
|
|
let dateTerminated;
|
|
|
|
|
|
|
|
|
|
try {
|
2019-10-08 14:57:10 +00:00
|
|
|
const googleRes = await iap.validate(iap.GOOGLE, plan.additionalData);
|
2018-03-30 17:24:51 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const isValidated = iap.isValidated(googleRes);
|
2018-03-30 17:24:51 +00:00
|
|
|
if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const purchases = iap.getPurchaseData(googleRes);
|
2018-03-30 17:24:51 +00:00
|
|
|
if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
|
2022-06-10 19:07:36 +00:00
|
|
|
for (const i in purchases) {
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(purchases, i)) {
|
|
|
|
|
const purchase = purchases[i];
|
|
|
|
|
if (purchase.autoRenewing !== false) return;
|
|
|
|
|
if (!dateTerminated || Number(purchase.expirationDate) > Number(dateTerminated)) {
|
|
|
|
|
dateTerminated = new Date(Number(purchase.expirationDate));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-30 17:24:51 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
// Status:410 means that the subsctiption isn't active anymore and we can safely delete it
|
|
|
|
|
if (err && err.message === 'Status:410') {
|
|
|
|
|
dateTerminated = new Date();
|
|
|
|
|
} else {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-10 19:07:36 +00:00
|
|
|
if (dateTerminated) {
|
|
|
|
|
await payments.cancelSubscription({
|
|
|
|
|
user,
|
|
|
|
|
nextBill: dateTerminated,
|
|
|
|
|
paymentMethod: this.constants.PAYMENT_METHOD_GOOGLE,
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-02-02 00:39:37 +00:00
|
|
|
};
|
|
|
|
|
|
2019-10-02 17:45:27 +00:00
|
|
|
export default api;
|