port android iap code to v3

This commit is contained in:
Matteo Pagliazzi
2016-07-04 15:45:52 +02:00
parent f8760a337a
commit a2da583db8
2 changed files with 53 additions and 76 deletions

View File

@@ -2,12 +2,12 @@ import {
authWithHeaders, authWithHeaders,
authWithUrl, authWithUrl,
} from '../../../middlewares/api-v3/auth'; } from '../../../middlewares/api-v3/auth';
import iap from '../../../libs/api-v3/inAppPurchases';
import payments from '../../../libs/api-v3/payments';
import { import {
iapAndroidVerify, NotAuthorized,
iapIOSVerify, } from '../../../libs/api-v3/errors';
} from '../../../libs/api-v3/inAppPurchases'; import { model as IapPurchaseReceipt } from '../../../models/iapPurchaseReceipt';
// IMPORTANT: NOT PORTED TO v3 standards (not using res.respond)
let api = {}; let api = {};
@@ -23,14 +23,51 @@ api.iapAndroidVerify = {
url: '/iap/android/verify', url: '/iap/android/verify',
middlewares: [authWithUrl], middlewares: [authWithUrl],
async handler (req, res) { async handler (req, res) {
let resObject = await iapAndroidVerify(res.locals.user, req.body); let user = res.locals.user;
console.log(resObject); let iapBody = req.body;
return res
.status(resObject.ok === true ? 200 : 500) await iap.setup();
.json(resObject);
let testObj = {
data: iapBody.transaction.receipt,
signature: iapBody.transaction.signature,
};
let googleRes = await iap.validate(iap.GOOGLE, testObj);
if (iap.isValidated(googleRes)) {
let receiptObj = JSON.parse(testObj.data); // passed as a string
let token = receiptObj.token || receiptObj.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,
});
} else {
throw new NotAuthorized('RECEIPT_ALREADY_USED');
}
} else {
throw new NotAuthorized('INVALID_RECEIPT');
}
res.respond(200, googleRes);
}, },
}; };
// IMPORTANT: NOT PORTED TO v3 standards (not using res.respond)
/** /**
* @apiIgnore Payments are considered part of the private API * @apiIgnore Payments are considered part of the private API
* @api {post} /iap/ios/verify iOS Verify IAP * @api {post} /iap/ios/verify iOS Verify IAP
@@ -44,7 +81,7 @@ 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);
console.log(resObject)
return res return res
.status(resObject.ok === true ? 200 : 500) .status(resObject.ok === true ? 200 : 500)
.json(resObject); .json(resObject);

View File

@@ -15,71 +15,11 @@ iap.config({
googlePublicKeyPath: nconf.get('IAP_GOOGLE_KEYDIR'), googlePublicKeyPath: nconf.get('IAP_GOOGLE_KEYDIR'),
}); });
let iapSetup = Bluebird.promisify(iap.setup, { context: iap }); module.exports = {
let iapValidate = Bluebird.promisify(iap.validate, { context: iap }); setup: Bluebird.promisify(iap.setup, { context: iap }),
validate: Bluebird.promisify(iap.validate, { context: iap }),
async function iapAndroidVerify (user, iapBody) { GOOGLE: iap.GOOGLE,
// Defining these 2 variables here so they can be logged in case of error };
let googleRes;
let token;
try {
await iapSetup();
console.log('iapbody', JSON.stringify(iapBody), typeof iapBody.transaction.receipt);
let testObj = {
data: iapBody.transaction.receipt,
signature: iapBody.transaction.signature,
};
googleRes = await iapValidate(iap.GOOGLE, testObj);
if (iap.isValidated(googleRes)) {
let receiptObj = JSON.parse(testObj.data);
console.log(receiptObj);
token = receiptObj.token || receiptObj.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,
data: googleRes,
};
} else {
throw new Error('RECEIPT_ALREADY_USED');
}
} else {
throw new Error('INVALID_RECEIPT');
}
} catch (err) {
logger.error(err, {
userId: user._id,
iapBody,
googleRes,
token,
});
return {
ok: false,
data: 'An error occurred while processing the purchase.',
};
}
}
async function iapIOSVerify (user, iapBody) { async function iapIOSVerify (user, iapBody) {
// Defining these 2 variables here so they can be logged in case of error // Defining these 2 variables here so they can be logged in case of error