mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
fix(iap): rewrite iap library code
This commit is contained in:
@@ -2,15 +2,19 @@ import { model as User } from '../../../../../website/server/models/user';
|
|||||||
import iapLibrary from 'in-app-purchase';
|
import iapLibrary from 'in-app-purchase';
|
||||||
import iap from '../../../../../website/server/libs/api-v3/inAppPurchases';
|
import iap from '../../../../../website/server/libs/api-v3/inAppPurchases';
|
||||||
|
|
||||||
describe.only('In App Purchases', () => {
|
describe('In App Purchases', () => {
|
||||||
let user;
|
let user;
|
||||||
|
let setupSpy;
|
||||||
|
let validateSpy;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
user = new User();
|
user = new User();
|
||||||
|
setupSpy = sinon.spy();
|
||||||
|
validateSpy = sinon.spy();
|
||||||
|
|
||||||
sandbox.stub(iapLibrary, 'setup').yields();
|
sandbox.stub(iapLibrary, 'setup').yields();
|
||||||
sandbox.stub(iapLibrary, 'validate').yields();
|
sandbox.stub(iapLibrary, 'validate').yields();
|
||||||
sandbox.stub(iapLibrary, 'isValidated').yields();
|
sandbox.stub(iapLibrary, 'isValidated').returns();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -19,21 +23,22 @@ describe.only('In App Purchases', () => {
|
|||||||
|
|
||||||
describe('Android', () => {
|
describe('Android', () => {
|
||||||
it('applies new valid receipt', async () => {
|
it('applies new valid receipt', async () => {
|
||||||
let res = await iap.iapAndroidVerify(user, {
|
iapLibrary.setup.yields(undefined, setupSpy);
|
||||||
|
|
||||||
|
await iap.iapAndroidVerify(user, {
|
||||||
receipt: {token: 1},
|
receipt: {token: 1},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(res);
|
expect(setupSpy).to.have.been.called;
|
||||||
|
expect(validateSpy).to.have.been.called;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('example with stub yielding an error', async () => {
|
it('example with stub yielding an error', async () => {
|
||||||
iapLibrary.setup.yields(new Error('an error'));
|
iapLibrary.setup.yields(new Error('an error'));
|
||||||
|
|
||||||
let res = await iap.iapAndroidVerify(user, {
|
await iap.iapAndroidVerify(user, {
|
||||||
receipt: {token: 1},
|
receipt: {token: 1},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(res);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -24,7 +24,9 @@ api.iapAndroidVerify = {
|
|||||||
middlewares: [authWithUrl],
|
middlewares: [authWithUrl],
|
||||||
async handler (req, res) {
|
async handler (req, res) {
|
||||||
let resObject = await iapAndroidVerify(res.locals.user, req.body);
|
let resObject = await iapAndroidVerify(res.locals.user, req.body);
|
||||||
return res.json(resObject);
|
return res
|
||||||
|
.status(resObject.ok ? 200 : 500)
|
||||||
|
.json(resObject);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -41,7 +43,9 @@ api.iapiOSVerify = {
|
|||||||
middlewares: [authWithHeaders()],
|
middlewares: [authWithHeaders()],
|
||||||
async handler (req, res) {
|
async handler (req, res) {
|
||||||
let resObject = await iapIOSVerify(res.locals.user, req.body);
|
let resObject = await iapIOSVerify(res.locals.user, req.body);
|
||||||
return res.json(resObject);
|
return res
|
||||||
|
.status(resObject.ok ? 200 : 500)
|
||||||
|
.json(resObject);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,14 +3,15 @@ import iap from 'in-app-purchase';
|
|||||||
import payments from './payments';
|
import payments from './payments';
|
||||||
import { model as IapPurchaseReceipt } from '../../models/iapPurchaseReceipt';
|
import { model as IapPurchaseReceipt } from '../../models/iapPurchaseReceipt';
|
||||||
import Bluebird from 'bluebird';
|
import Bluebird from 'bluebird';
|
||||||
|
import logger from './logger';
|
||||||
|
|
||||||
// Validation ERROR Codes
|
// Validation ERROR Codes
|
||||||
const INVALID_PAYLOAD = 6778001;
|
// const INVALID_PAYLOAD = 6778001;
|
||||||
// const CONNECTION_FAILED = 6778002;
|
// const CONNECTION_FAILED = 6778002;
|
||||||
// const PURCHASE_EXPIRED = 6778003;
|
// const PURCHASE_EXPIRED = 6778003;
|
||||||
|
|
||||||
iap.config({
|
iap.config({
|
||||||
// this is the path to the directory containing iap-sanbox/iap-live files
|
// This is the path to the directory containing iap-sanbox/iap-live files
|
||||||
googlePublicKeyPath: nconf.get('IAP_GOOGLE_KEYDIR'),
|
googlePublicKeyPath: nconf.get('IAP_GOOGLE_KEYDIR'),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -18,155 +19,135 @@ let iapSetup = Bluebird.promisify(iap.setup, { context: iap });
|
|||||||
let iapValidate = Bluebird.promisify(iap.validate, { context: iap });
|
let iapValidate = Bluebird.promisify(iap.validate, { context: iap });
|
||||||
|
|
||||||
async function iapAndroidVerify (user, iapBody) {
|
async function iapAndroidVerify (user, iapBody) {
|
||||||
|
// Defining these 2 variables here so they can be logged in case of error
|
||||||
|
let googleRes;
|
||||||
|
let token;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await iapSetup();
|
await iapSetup();
|
||||||
|
|
||||||
let testObj = {
|
let testObj = {
|
||||||
data: iapBody.transaction.receipt,
|
data: iapBody.transaction.receipt,
|
||||||
signature: iapBody.transaction.signature,
|
signature: iapBody.transaction.signature,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
googleRes = await iapValidate(iap.GOOGLE, testObj);
|
||||||
let googleRes = iapValidate(iap.GOOGLE, testObj);
|
|
||||||
|
|
||||||
if (iap.isValidated(googleRes)) {
|
if (iap.isValidated(googleRes)) {
|
||||||
let resObj = {
|
token = testObj.data.token || testObj.data.purchaseToken;
|
||||||
|
|
||||||
|
let existingReceipt = await IapPurchaseReceipt.findOne({
|
||||||
|
_id: token,
|
||||||
|
}).exec();
|
||||||
|
|
||||||
|
if (!existingReceipt) {
|
||||||
|
await IapPurchaseReceipt.create({
|
||||||
|
_id: token,
|
||||||
|
consumed: true,
|
||||||
|
userID: user._id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await payments.buyGems({
|
||||||
|
user,
|
||||||
|
paymentMethod: 'IAP GooglePlay',
|
||||||
|
amount: 5.25,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
ok: true,
|
ok: true,
|
||||||
data: googleRes,
|
data: googleRes,
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
let token = testObj.data.token;
|
throw new Error('RECEIPT_ALREADY_USED');
|
||||||
if (!token) token = testObj.data.purchaseToken;
|
|
||||||
|
|
||||||
let existingReceipt = await IapPurchaseReceipt.findOne({
|
|
||||||
_id: token,
|
|
||||||
}).exec();
|
|
||||||
|
|
||||||
if (!existingReceipt) {
|
|
||||||
try {
|
|
||||||
await IapPurchaseReceipt.create({
|
|
||||||
token,
|
|
||||||
consumed: true,
|
|
||||||
userID: user._id,
|
|
||||||
});
|
|
||||||
|
|
||||||
await payments.buyGems({
|
|
||||||
user,
|
|
||||||
paymentMethod: 'IAP GooglePlay',
|
|
||||||
amount: 5.25,
|
|
||||||
});
|
|
||||||
|
|
||||||
return resObj;
|
|
||||||
} catch (err) {
|
|
||||||
return resObj;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return resObj;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} else {
|
||||||
return {
|
throw new Error('INVALID_RECEIPT');
|
||||||
ok: false,
|
|
||||||
data: {
|
|
||||||
code: INVALID_PAYLOAD,
|
|
||||||
message: error.toString(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (err) {
|
||||||
|
logger.error(err, {
|
||||||
|
userId: user._id,
|
||||||
|
iapBody,
|
||||||
|
googleRes,
|
||||||
|
token,
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
data: 'IAP Error',
|
data: 'An error occurred while processing the purchase.',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function iapIOSVerify (user, iapBody) {
|
async function iapIOSVerify (user, iapBody) {
|
||||||
iap.setup(function iosSetupResult (error) {
|
// Defining these 2 variables here so they can be logged in case of error
|
||||||
if (error) {
|
let token;
|
||||||
let resObj = {
|
let appleRes;
|
||||||
ok: false,
|
|
||||||
data: 'IAP Error',
|
|
||||||
};
|
|
||||||
|
|
||||||
return resObj;
|
try {
|
||||||
}
|
await iapSetup();
|
||||||
|
appleRes = await iapValidate(iap.APPLE, iapBody.transaction.receipt);
|
||||||
|
|
||||||
// iap is ready
|
if (iap.isValidated(appleRes)) {
|
||||||
iap.validate(iap.APPLE, iapBody.transaction.receipt, (err, appleRes) => {
|
token = 'aaa';
|
||||||
if (err) {
|
|
||||||
let resObj = {
|
|
||||||
ok: false,
|
|
||||||
data: {
|
|
||||||
code: INVALID_PAYLOAD,
|
|
||||||
message: err.toString(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return resObj;
|
let existingReceipt = await IapPurchaseReceipt.findOne({
|
||||||
|
_id: token,
|
||||||
|
}).exec();
|
||||||
|
|
||||||
|
if (!existingReceipt) {
|
||||||
|
await IapPurchaseReceipt.create({
|
||||||
|
_id: token,
|
||||||
|
consumed: true,
|
||||||
|
userID: user._id,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw new Error('RECEIPT_ALREADY_USED');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iap.isValidated(appleRes)) {
|
let purchaseDataList = iap.getPurchaseData(appleRes);
|
||||||
let purchaseDataList = iap.getPurchaseData(appleRes);
|
if (purchaseDataList.length === 0) throw new Error('NO_ITEM_PURCHASED');
|
||||||
if (purchaseDataList.length > 0) {
|
|
||||||
let correctReceipt = true;
|
|
||||||
|
|
||||||
for (let index in purchaseDataList) {
|
let correctReceipt = true;
|
||||||
switch (purchaseDataList[index].productId) {
|
|
||||||
case 'com.habitrpg.ios.Habitica.4gems':
|
|
||||||
payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 1});
|
|
||||||
break;
|
|
||||||
case 'com.habitrpg.ios.Habitica.8gems':
|
|
||||||
payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 2});
|
|
||||||
break;
|
|
||||||
case 'com.habitrpg.ios.Habitica.20gems':
|
|
||||||
case 'com.habitrpg.ios.Habitica.21gems':
|
|
||||||
payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 5.25});
|
|
||||||
break;
|
|
||||||
case 'com.habitrpg.ios.Habitica.42gems':
|
|
||||||
payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 10.5});
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
correctReceipt = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (correctReceipt) {
|
// Purchasing one item at a time (processing of await(s) below is sequential not parallel)
|
||||||
let resObj = {
|
for (let index in purchaseDataList) {
|
||||||
ok: true,
|
switch (purchaseDataList[index].productId) {
|
||||||
data: appleRes,
|
case 'com.habitrpg.ios.Habitica.4gems':
|
||||||
};
|
await payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 1}); // eslint-disable-line babel/no-await-in-loop
|
||||||
|
break;
|
||||||
// yay good!
|
case 'com.habitrpg.ios.Habitica.8gems':
|
||||||
return resObj;
|
await payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 2}); // eslint-disable-line babel/no-await-in-loop
|
||||||
}
|
break;
|
||||||
|
case 'com.habitrpg.ios.Habitica.20gems':
|
||||||
|
case 'com.habitrpg.ios.Habitica.21gems':
|
||||||
|
await payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 5.25}); // eslint-disable-line babel/no-await-in-loop
|
||||||
|
break;
|
||||||
|
case 'com.habitrpg.ios.Habitica.42gems':
|
||||||
|
await payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 10.5}); // eslint-disable-line babel/no-await-in-loop
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
correctReceipt = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// wrong receipt content
|
|
||||||
let resObj = {
|
|
||||||
ok: false,
|
|
||||||
data: {
|
|
||||||
code: INVALID_PAYLOAD,
|
|
||||||
message: 'Incorrect receipt content',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return resObj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// invalid receipt
|
if (!correctReceipt) throw new Error('INVALID_ITEM_PURCHASED');
|
||||||
let resObj = {
|
} else {
|
||||||
ok: false,
|
throw new Error('INVALID_RECEIPT');
|
||||||
data: {
|
}
|
||||||
code: INVALID_PAYLOAD,
|
} catch (err) {
|
||||||
message: 'Invalid receipt',
|
logger.error(err, {
|
||||||
},
|
userId: user._id,
|
||||||
};
|
iapBody,
|
||||||
|
appleRes,
|
||||||
return resObj;
|
token,
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
data: 'An error occurred while processing the purchase.',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
iapAndroidVerify,
|
iapAndroidVerify,
|
||||||
|
|||||||
Reference in New Issue
Block a user