fix(iap): rewrite iap library code

This commit is contained in:
Matteo Pagliazzi
2016-06-27 14:26:20 +02:00
parent e1fc5355b8
commit 893b4d8def
3 changed files with 118 additions and 128 deletions

View File

@@ -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);
}); });
}); });
}); });

View File

@@ -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);
}, },
}; };

View File

@@ -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,33 +19,30 @@ 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;
ok: true,
data: googleRes,
};
let token = testObj.data.token;
if (!token) token = testObj.data.purchaseToken;
let existingReceipt = await IapPurchaseReceipt.findOne({ let existingReceipt = await IapPurchaseReceipt.findOne({
_id: token, _id: token,
}).exec(); }).exec();
if (!existingReceipt) { if (!existingReceipt) {
try {
await IapPurchaseReceipt.create({ await IapPurchaseReceipt.create({
token, _id: token,
consumed: true, consumed: true,
userID: user._id, userID: user._id,
}); });
@@ -55,118 +53,101 @@ async function iapAndroidVerify (user, iapBody) {
amount: 5.25, amount: 5.25,
}); });
return resObj; return {
} catch (err) { ok: true,
return resObj; data: googleRes,
};
} else {
throw new Error('RECEIPT_ALREADY_USED');
} }
} else { } else {
return resObj; throw new Error('INVALID_RECEIPT');
} }
} } catch (err) {
} catch (error) { logger.error(err, {
userId: user._id,
iapBody,
googleRes,
token,
});
return { return {
ok: false, ok: false,
data: { data: 'An error occurred while processing the purchase.',
code: INVALID_PAYLOAD,
message: error.toString(),
},
};
}
} catch (error) {
return {
ok: false,
data: 'IAP Error',
}; };
} }
} }
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
iap.validate(iap.APPLE, iapBody.transaction.receipt, (err, appleRes) => {
if (err) {
let resObj = {
ok: false,
data: {
code: INVALID_PAYLOAD,
message: err.toString(),
},
};
return resObj;
}
if (iap.isValidated(appleRes)) { if (iap.isValidated(appleRes)) {
token = 'aaa';
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');
}
let purchaseDataList = iap.getPurchaseData(appleRes); let purchaseDataList = iap.getPurchaseData(appleRes);
if (purchaseDataList.length > 0) { if (purchaseDataList.length === 0) throw new Error('NO_ITEM_PURCHASED');
let correctReceipt = true; let correctReceipt = true;
// Purchasing one item at a time (processing of await(s) below is sequential not parallel)
for (let index in purchaseDataList) { for (let index in purchaseDataList) {
switch (purchaseDataList[index].productId) { switch (purchaseDataList[index].productId) {
case 'com.habitrpg.ios.Habitica.4gems': case 'com.habitrpg.ios.Habitica.4gems':
payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 1}); await payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 1}); // eslint-disable-line babel/no-await-in-loop
break; break;
case 'com.habitrpg.ios.Habitica.8gems': case 'com.habitrpg.ios.Habitica.8gems':
payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 2}); await payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 2}); // eslint-disable-line babel/no-await-in-loop
break; break;
case 'com.habitrpg.ios.Habitica.20gems': case 'com.habitrpg.ios.Habitica.20gems':
case 'com.habitrpg.ios.Habitica.21gems': case 'com.habitrpg.ios.Habitica.21gems':
payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 5.25}); await payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 5.25}); // eslint-disable-line babel/no-await-in-loop
break; break;
case 'com.habitrpg.ios.Habitica.42gems': case 'com.habitrpg.ios.Habitica.42gems':
payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 10.5}); await payments.buyGems({user, paymentMethod: 'IAP AppleStore', amount: 10.5}); // eslint-disable-line babel/no-await-in-loop
break; break;
default: default:
correctReceipt = false; correctReceipt = false;
} }
} }
if (correctReceipt) { if (!correctReceipt) throw new Error('INVALID_ITEM_PURCHASED');
let resObj = { } else {
ok: true, throw new Error('INVALID_RECEIPT');
data: appleRes,
};
// yay good!
return resObj;
} }
} } catch (err) {
logger.error(err, {
// wrong receipt content userId: user._id,
let resObj = { iapBody,
ok: false, appleRes,
data: { token,
code: INVALID_PAYLOAD,
message: 'Incorrect receipt content',
},
};
return resObj;
}
// invalid receipt
let resObj = {
ok: false,
data: {
code: INVALID_PAYLOAD,
message: 'Invalid receipt',
},
};
return resObj;
}); });
});
}
return {
ok: false,
data: 'An error occurred while processing the purchase.',
};
}
}
module.exports = { module.exports = {
iapAndroidVerify, iapAndroidVerify,