2017-02-02 00:39:37 +00:00
|
|
|
/* eslint-disable camelcase */
|
2019-10-08 18:45:38 +00:00
|
|
|
import moment from 'moment';
|
2018-06-18 12:40:25 +00:00
|
|
|
import payments from '../../../../../website/server/libs/payments/payments';
|
|
|
|
|
import googlePayments from '../../../../../website/server/libs/payments/google';
|
|
|
|
|
import iap from '../../../../../website/server/libs/inAppPurchases';
|
2019-10-08 18:45:38 +00:00
|
|
|
import { model as User } from '../../../../../website/server/models/user';
|
2018-06-18 12:40:25 +00:00
|
|
|
import common from '../../../../../website/common';
|
2020-12-14 14:59:17 +00:00
|
|
|
import * as gems from '../../../../../website/server/libs/payments/gems';
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
const { i18n } = common;
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
describe('Google Payments', () => {
|
|
|
|
|
const subKey = 'basic_3mo';
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2023-01-20 22:14:33 +00:00
|
|
|
describe('verifyPurchase', () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
let sku; let user; let token; let receipt; let signature; let
|
2023-01-23 06:45:31 +00:00
|
|
|
headers;
|
2019-10-08 18:45:38 +00:00
|
|
|
let iapSetupStub; let iapValidateStub; let iapIsValidatedStub; let
|
2023-01-23 06:45:31 +00:00
|
|
|
paymentBuySkuStub; let validateGiftMessageStub;
|
2017-02-02 00:39:37 +00:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
sku = 'com.habitrpg.android.habitica.iap.21gems';
|
|
|
|
|
user = new User();
|
|
|
|
|
receipt = `{"token": "${token}", "productId": "${sku}"}`;
|
|
|
|
|
signature = '';
|
|
|
|
|
headers = {};
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
iapSetupStub = sinon.stub(iap, 'setup')
|
2018-10-26 16:15:28 +00:00
|
|
|
.resolves();
|
2023-01-23 06:45:31 +00:00
|
|
|
iapValidateStub = sinon.stub(iap, 'validate').resolves({ productId: sku });
|
2019-10-08 18:45:38 +00:00
|
|
|
iapIsValidatedStub = sinon.stub(iap, 'isValidated')
|
2017-02-02 00:39:37 +00:00
|
|
|
.returns(true);
|
2023-01-23 06:45:31 +00:00
|
|
|
paymentBuySkuStub = sinon.stub(payments, 'buySkuItem').resolves({});
|
2020-12-14 14:59:17 +00:00
|
|
|
validateGiftMessageStub = sinon.stub(gems, 'validateGiftMessage');
|
2017-02-02 00:39:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
2019-10-08 18:45:38 +00:00
|
|
|
iap.setup.restore();
|
|
|
|
|
iap.validate.restore();
|
|
|
|
|
iap.isValidated.restore();
|
2023-01-23 04:30:48 +00:00
|
|
|
payments.buySkuItem.restore();
|
2020-12-14 14:59:17 +00:00
|
|
|
gems.validateGiftMessage.restore();
|
2017-02-02 00:39:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if receipt is invalid', async () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
iap.isValidated.restore();
|
|
|
|
|
iapIsValidatedStub = sinon.stub(iap, 'isValidated')
|
2017-02-02 00:39:37 +00:00
|
|
|
.returns(false);
|
|
|
|
|
|
2023-01-20 22:14:33 +00:00
|
|
|
await expect(googlePayments.verifyPurchase({
|
2019-10-08 18:45:38 +00:00
|
|
|
user, receipt, signature, headers,
|
|
|
|
|
}))
|
2017-02-02 00:39:37 +00:00
|
|
|
.to.eventually.be.rejected.and.to.eql({
|
|
|
|
|
httpCode: 401,
|
|
|
|
|
name: 'NotAuthorized',
|
|
|
|
|
message: googlePayments.constants.RESPONSE_INVALID_RECEIPT,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if productId is invalid', async () => {
|
|
|
|
|
receipt = `{"token": "${token}", "productId": "invalid"}`;
|
2023-01-23 06:45:31 +00:00
|
|
|
iapValidateStub.restore();
|
|
|
|
|
iapValidateStub = sinon.stub(iap, 'validate').resolves({});
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2023-01-23 06:45:31 +00:00
|
|
|
paymentBuySkuStub.restore();
|
2023-01-20 22:14:33 +00:00
|
|
|
await expect(googlePayments.verifyPurchase({
|
2019-10-08 18:45:38 +00:00
|
|
|
user, receipt, signature, headers,
|
|
|
|
|
}))
|
2017-02-02 00:39:37 +00:00
|
|
|
.to.eventually.be.rejected.and.to.eql({
|
2023-01-23 04:30:48 +00:00
|
|
|
httpCode: 400,
|
|
|
|
|
name: 'BadRequest',
|
2017-02-02 00:39:37 +00:00
|
|
|
message: googlePayments.constants.RESPONSE_INVALID_ITEM,
|
|
|
|
|
});
|
2023-01-23 06:45:31 +00:00
|
|
|
paymentBuySkuStub = sinon.stub(payments, 'buySkuItem').resolves({});
|
2017-02-02 00:39:37 +00:00
|
|
|
});
|
|
|
|
|
|
2017-07-16 16:23:57 +00:00
|
|
|
it('should throw an error if user cannot purchase gems', async () => {
|
2018-10-26 16:15:28 +00:00
|
|
|
sinon.stub(user, 'canGetGems').resolves(false);
|
2017-07-16 16:23:57 +00:00
|
|
|
|
2023-01-20 22:14:33 +00:00
|
|
|
await expect(googlePayments.verifyPurchase({
|
2019-10-08 18:45:38 +00:00
|
|
|
user, receipt, signature, headers,
|
|
|
|
|
}))
|
2017-07-16 16:23:57 +00:00
|
|
|
.to.eventually.be.rejected.and.to.eql({
|
|
|
|
|
httpCode: 401,
|
|
|
|
|
name: 'NotAuthorized',
|
|
|
|
|
message: i18n.t('groupPolicyCannotGetGems'),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
user.canGetGems.restore();
|
|
|
|
|
});
|
|
|
|
|
|
2017-02-02 00:39:37 +00:00
|
|
|
it('purchases gems', async () => {
|
2018-10-26 16:15:28 +00:00
|
|
|
sinon.stub(user, 'canGetGems').resolves(true);
|
2023-01-20 22:14:33 +00:00
|
|
|
await googlePayments.verifyPurchase({
|
2019-10-08 18:45:38 +00:00
|
|
|
user, receipt, signature, headers,
|
|
|
|
|
});
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2020-12-14 14:59:17 +00:00
|
|
|
expect(validateGiftMessageStub).to.not.be.called;
|
|
|
|
|
|
2017-02-02 00:39:37 +00:00
|
|
|
expect(iapSetupStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledWith(iap.GOOGLE, {
|
|
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
});
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledOnce;
|
2023-01-23 06:45:31 +00:00
|
|
|
expect(iapIsValidatedStub).to.be.calledWith(
|
|
|
|
|
{ productId: sku },
|
|
|
|
|
);
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2023-01-23 06:45:31 +00:00
|
|
|
expect(paymentBuySkuStub).to.be.calledOnce;
|
|
|
|
|
expect(paymentBuySkuStub).to.be.calledWith({
|
2017-02-02 00:39:37 +00:00
|
|
|
user,
|
2023-01-23 06:45:31 +00:00
|
|
|
gift: undefined,
|
2017-02-02 00:39:37 +00:00
|
|
|
paymentMethod: googlePayments.constants.PAYMENT_METHOD_GOOGLE,
|
2023-01-23 06:45:31 +00:00
|
|
|
sku,
|
2017-02-02 00:39:37 +00:00
|
|
|
headers,
|
|
|
|
|
});
|
2017-07-16 16:23:57 +00:00
|
|
|
expect(user.canGetGems).to.be.calledOnce;
|
|
|
|
|
user.canGetGems.restore();
|
2017-02-02 00:39:37 +00:00
|
|
|
});
|
2018-12-23 18:20:14 +00:00
|
|
|
|
|
|
|
|
it('gifts gems', async () => {
|
|
|
|
|
const receivingUser = new User();
|
|
|
|
|
await receivingUser.save();
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
const gift = { uuid: receivingUser._id };
|
2023-01-20 22:14:33 +00:00
|
|
|
await googlePayments.verifyPurchase({
|
2019-10-08 18:45:38 +00:00
|
|
|
user, gift, receipt, signature, headers,
|
|
|
|
|
});
|
2018-12-23 18:20:14 +00:00
|
|
|
|
2020-12-14 14:59:17 +00:00
|
|
|
expect(validateGiftMessageStub).to.be.calledOnce;
|
|
|
|
|
expect(validateGiftMessageStub).to.be.calledWith(gift, user);
|
|
|
|
|
|
2018-12-23 18:20:14 +00:00
|
|
|
expect(iapSetupStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledWith(iap.GOOGLE, {
|
|
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
});
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledOnce;
|
2023-01-23 06:45:31 +00:00
|
|
|
expect(iapIsValidatedStub).to.be.calledWith(
|
|
|
|
|
{ productId: sku },
|
|
|
|
|
);
|
2018-12-23 18:20:14 +00:00
|
|
|
|
2023-01-23 06:45:31 +00:00
|
|
|
expect(paymentBuySkuStub).to.be.calledOnce;
|
|
|
|
|
expect(paymentBuySkuStub).to.be.calledWith({
|
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: {
|
|
|
|
|
uuid: receivingUser._id,
|
2023-01-23 06:45:31 +00:00
|
|
|
member: sinon.match({ _id: receivingUser._id }),
|
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
|
|
|
},
|
2023-01-23 06:45:31 +00:00
|
|
|
paymentMethod: googlePayments.constants.PAYMENT_METHOD_GOOGLE,
|
|
|
|
|
sku,
|
|
|
|
|
headers,
|
2018-12-23 18:20:14 +00:00
|
|
|
});
|
|
|
|
|
});
|
2017-02-02 00:39:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('subscribe', () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
let sub; let sku; let user; let token; let receipt; let signature; let headers; let
|
|
|
|
|
nextPaymentProcessing;
|
|
|
|
|
let iapSetupStub; let iapValidateStub; let iapIsValidatedStub; let
|
|
|
|
|
paymentsCreateSubscritionStub;
|
2017-02-02 00:39:37 +00:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
sub = common.content.subscriptionBlocks[subKey];
|
|
|
|
|
sku = 'com.habitrpg.android.habitica.subscription.3month';
|
|
|
|
|
|
|
|
|
|
token = 'test-token';
|
|
|
|
|
headers = {};
|
|
|
|
|
receipt = `{"token": "${token}"}`;
|
|
|
|
|
signature = '';
|
2019-10-08 18:45:38 +00:00
|
|
|
nextPaymentProcessing = moment.utc().add({ days: 2 });
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
iapSetupStub = sinon.stub(iap, 'setup')
|
2018-10-26 16:15:28 +00:00
|
|
|
.resolves();
|
2019-10-08 18:45:38 +00:00
|
|
|
iapValidateStub = sinon.stub(iap, 'validate')
|
2018-10-26 16:15:28 +00:00
|
|
|
.resolves({});
|
2019-10-08 18:45:38 +00:00
|
|
|
iapIsValidatedStub = sinon.stub(iap, 'isValidated')
|
2017-02-02 00:39:37 +00:00
|
|
|
.returns(true);
|
2018-10-26 16:15:28 +00:00
|
|
|
paymentsCreateSubscritionStub = sinon.stub(payments, 'createSubscription').resolves({});
|
2017-02-02 00:39:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
2019-10-08 18:45:38 +00:00
|
|
|
iap.setup.restore();
|
|
|
|
|
iap.validate.restore();
|
|
|
|
|
iap.isValidated.restore();
|
2017-02-02 00:39:37 +00:00
|
|
|
payments.createSubscription.restore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if receipt is invalid', async () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
iap.isValidated.restore();
|
|
|
|
|
iapIsValidatedStub = sinon.stub(iap, 'isValidated')
|
2017-02-02 00:39:37 +00:00
|
|
|
.returns(false);
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
await expect(googlePayments
|
|
|
|
|
.subscribe(sku, user, receipt, signature, headers, nextPaymentProcessing))
|
2017-02-02 00:39:37 +00:00
|
|
|
.to.eventually.be.rejected.and.to.eql({
|
|
|
|
|
httpCode: 401,
|
|
|
|
|
name: 'NotAuthorized',
|
|
|
|
|
message: googlePayments.constants.RESPONSE_INVALID_RECEIPT,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if sku is invalid', async () => {
|
|
|
|
|
sku = 'invalid';
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
await expect(googlePayments
|
|
|
|
|
.subscribe(sku, user, receipt, signature, headers, nextPaymentProcessing))
|
2017-02-02 00:39:37 +00:00
|
|
|
.to.eventually.be.rejected.and.to.eql({
|
|
|
|
|
httpCode: 401,
|
|
|
|
|
name: 'NotAuthorized',
|
|
|
|
|
message: googlePayments.constants.RESPONSE_INVALID_ITEM,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('creates a user subscription', async () => {
|
|
|
|
|
await googlePayments.subscribe(sku, user, receipt, signature, headers, nextPaymentProcessing);
|
|
|
|
|
|
|
|
|
|
expect(iapSetupStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledWith(iap.GOOGLE, {
|
|
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
});
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledOnce;
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledWith({});
|
|
|
|
|
|
|
|
|
|
expect(paymentsCreateSubscritionStub).to.be.calledOnce;
|
|
|
|
|
expect(paymentsCreateSubscritionStub).to.be.calledWith({
|
|
|
|
|
user,
|
|
|
|
|
customerId: token,
|
|
|
|
|
paymentMethod: googlePayments.constants.PAYMENT_METHOD_GOOGLE,
|
|
|
|
|
sub,
|
|
|
|
|
headers,
|
2019-10-08 18:45:38 +00:00
|
|
|
additionalData: { data: receipt, signature },
|
2017-02-02 00:39:37 +00:00
|
|
|
nextPaymentProcessing,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('cancelSubscribe ', () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
let user; let token; let receipt; let signature; let headers; let customerId; let
|
|
|
|
|
expirationDate;
|
|
|
|
|
let iapSetupStub; let iapValidateStub; let iapIsValidatedStub; let iapGetPurchaseDataStub; let
|
|
|
|
|
paymentCancelSubscriptionSpy;
|
2017-02-02 00:39:37 +00:00
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
token = 'test-token';
|
|
|
|
|
headers = {};
|
|
|
|
|
receipt = `{"token": "${token}"}`;
|
|
|
|
|
signature = '';
|
|
|
|
|
customerId = 'test-customerId';
|
|
|
|
|
expirationDate = moment.utc();
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
iapSetupStub = sinon.stub(iap, 'setup')
|
2018-10-26 16:15:28 +00:00
|
|
|
.resolves();
|
2019-10-08 18:45:38 +00:00
|
|
|
iapValidateStub = sinon.stub(iap, 'validate')
|
2018-10-26 16:15:28 +00:00
|
|
|
.resolves({
|
2017-02-02 00:39:37 +00:00
|
|
|
expirationDate,
|
|
|
|
|
});
|
2019-10-08 18:45:38 +00:00
|
|
|
iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData')
|
2022-02-22 16:13:32 +00:00
|
|
|
.returns([{ expirationDate: expirationDate.toDate(), autoRenewing: false }]);
|
2019-10-08 18:45:38 +00:00
|
|
|
iapIsValidatedStub = sinon.stub(iap, 'isValidated')
|
2017-02-02 00:39:37 +00:00
|
|
|
.returns(true);
|
|
|
|
|
|
|
|
|
|
user = new User();
|
|
|
|
|
user.profile.name = 'sender';
|
|
|
|
|
user.purchased.plan.customerId = customerId;
|
2017-02-21 18:22:13 +00:00
|
|
|
user.purchased.plan.paymentMethod = googlePayments.constants.PAYMENT_METHOD_GOOGLE;
|
2017-02-02 00:39:37 +00:00
|
|
|
user.purchased.plan.planId = subKey;
|
2019-10-08 18:45:38 +00:00
|
|
|
user.purchased.plan.additionalData = { data: receipt, signature };
|
2017-02-02 00:39:37 +00:00
|
|
|
|
2018-10-26 16:15:28 +00:00
|
|
|
paymentCancelSubscriptionSpy = sinon.stub(payments, 'cancelSubscription').resolves({});
|
2017-02-02 00:39:37 +00:00
|
|
|
});
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
afterEach(() => {
|
|
|
|
|
iap.setup.restore();
|
|
|
|
|
iap.validate.restore();
|
|
|
|
|
iap.isValidated.restore();
|
|
|
|
|
iap.getPurchaseData.restore();
|
2017-02-02 00:39:37 +00:00
|
|
|
payments.cancelSubscription.restore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if we are missing a subscription', async () => {
|
2017-02-21 18:22:13 +00:00
|
|
|
user.purchased.plan.paymentMethod = undefined;
|
2017-02-02 00:39:37 +00:00
|
|
|
|
|
|
|
|
await expect(googlePayments.cancelSubscribe(user, headers))
|
|
|
|
|
.to.eventually.be.rejected.and.to.eql({
|
|
|
|
|
httpCode: 401,
|
|
|
|
|
name: 'NotAuthorized',
|
|
|
|
|
message: i18n.t('missingSubscription'),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if receipt is invalid', async () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
iap.isValidated.restore();
|
|
|
|
|
iapIsValidatedStub = sinon.stub(iap, 'isValidated')
|
2017-02-02 00:39:37 +00:00
|
|
|
.returns(false);
|
|
|
|
|
|
|
|
|
|
await expect(googlePayments.cancelSubscribe(user, headers))
|
|
|
|
|
.to.eventually.be.rejected.and.to.eql({
|
|
|
|
|
httpCode: 401,
|
|
|
|
|
name: 'NotAuthorized',
|
|
|
|
|
message: googlePayments.constants.RESPONSE_INVALID_RECEIPT,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should cancel a user subscription', async () => {
|
|
|
|
|
await googlePayments.cancelSubscribe(user, headers);
|
|
|
|
|
|
|
|
|
|
expect(iapSetupStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledWith(iap.GOOGLE, {
|
|
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
});
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledOnce;
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledWith({
|
|
|
|
|
expirationDate,
|
|
|
|
|
});
|
|
|
|
|
expect(iapGetPurchaseDataStub).to.be.calledOnce;
|
|
|
|
|
|
|
|
|
|
expect(paymentCancelSubscriptionSpy).to.be.calledOnce;
|
|
|
|
|
expect(paymentCancelSubscriptionSpy).to.be.calledWith({
|
|
|
|
|
user,
|
|
|
|
|
paymentMethod: googlePayments.constants.PAYMENT_METHOD_GOOGLE,
|
|
|
|
|
nextBill: expirationDate.toDate(),
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-02-22 15:15:05 +00:00
|
|
|
|
2022-06-10 19:07:36 +00:00
|
|
|
it('should cancel a user subscription with multiple inactive subscriptions', async () => {
|
|
|
|
|
const laterDate = moment.utc().add(7, 'days');
|
|
|
|
|
iap.getPurchaseData.restore();
|
|
|
|
|
iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData')
|
|
|
|
|
.returns([{ expirationDate, autoRenewing: false },
|
|
|
|
|
{ expirationDate: laterDate, autoRenewing: false },
|
|
|
|
|
]);
|
|
|
|
|
await googlePayments.cancelSubscribe(user, headers);
|
|
|
|
|
|
|
|
|
|
expect(iapSetupStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledWith(iap.GOOGLE, {
|
|
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
});
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledOnce;
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledWith({
|
|
|
|
|
expirationDate,
|
|
|
|
|
});
|
|
|
|
|
expect(iapGetPurchaseDataStub).to.be.calledOnce;
|
|
|
|
|
|
|
|
|
|
expect(paymentCancelSubscriptionSpy).to.be.calledOnce;
|
|
|
|
|
expect(paymentCancelSubscriptionSpy).to.be.calledWith({
|
|
|
|
|
user,
|
|
|
|
|
paymentMethod: googlePayments.constants.PAYMENT_METHOD_GOOGLE,
|
|
|
|
|
nextBill: laterDate.toDate(),
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-22 15:15:05 +00:00
|
|
|
it('should not cancel a user subscription with autorenew', async () => {
|
2022-02-22 17:26:15 +00:00
|
|
|
iap.getPurchaseData.restore();
|
2022-02-22 15:15:05 +00:00
|
|
|
iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData')
|
2022-02-22 16:13:32 +00:00
|
|
|
.returns([{ autoRenewing: true }]);
|
2022-02-22 15:15:05 +00:00
|
|
|
await googlePayments.cancelSubscribe(user, headers);
|
|
|
|
|
|
|
|
|
|
expect(iapSetupStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledWith(iap.GOOGLE, {
|
|
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
});
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledOnce;
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledWith({
|
|
|
|
|
expirationDate,
|
|
|
|
|
});
|
|
|
|
|
expect(iapGetPurchaseDataStub).to.be.calledOnce;
|
|
|
|
|
|
|
|
|
|
expect(paymentCancelSubscriptionSpy).to.not.be.called;
|
|
|
|
|
});
|
2022-06-10 19:07:36 +00:00
|
|
|
|
|
|
|
|
it('should not cancel a user subscription with multiple subscriptions with one autorenew', async () => {
|
|
|
|
|
iap.getPurchaseData.restore();
|
|
|
|
|
iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData')
|
|
|
|
|
.returns([{ expirationDate, autoRenewing: false },
|
|
|
|
|
{ autoRenewing: true },
|
|
|
|
|
{ expirationDate, autoRenewing: false }]);
|
|
|
|
|
await googlePayments.cancelSubscribe(user, headers);
|
|
|
|
|
|
|
|
|
|
expect(iapSetupStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledOnce;
|
|
|
|
|
expect(iapValidateStub).to.be.calledWith(iap.GOOGLE, {
|
|
|
|
|
data: receipt,
|
|
|
|
|
signature,
|
|
|
|
|
});
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledOnce;
|
|
|
|
|
expect(iapIsValidatedStub).to.be.calledWith({
|
|
|
|
|
expirationDate,
|
|
|
|
|
});
|
|
|
|
|
expect(iapGetPurchaseDataStub).to.be.calledOnce;
|
|
|
|
|
|
|
|
|
|
expect(paymentCancelSubscriptionSpy).to.not.be.called;
|
|
|
|
|
});
|
2017-02-02 00:39:37 +00:00
|
|
|
});
|
|
|
|
|
});
|