diff --git a/website/server/controllers/top-level/payments/iap.js b/website/server/controllers/top-level/payments/iap.js index c28099037a..49b4bd5361 100644 --- a/website/server/controllers/top-level/payments/iap.js +++ b/website/server/controllers/top-level/payments/iap.js @@ -2,12 +2,12 @@ import { authWithHeaders, authWithUrl, } from '../../../middlewares/api-v3/auth'; +import iap from '../../../libs/api-v3/inAppPurchases'; +import payments from '../../../libs/api-v3/payments'; import { - iapAndroidVerify, - iapIOSVerify, -} from '../../../libs/api-v3/inAppPurchases'; - -// IMPORTANT: NOT PORTED TO v3 standards (not using res.respond) + NotAuthorized, +} from '../../../libs/api-v3/errors'; +import { model as IapPurchaseReceipt } from '../../../models/iapPurchaseReceipt'; let api = {}; @@ -23,14 +23,51 @@ api.iapAndroidVerify = { url: '/iap/android/verify', middlewares: [authWithUrl], async handler (req, res) { - let resObject = await iapAndroidVerify(res.locals.user, req.body); - console.log(resObject); - return res - .status(resObject.ok === true ? 200 : 500) - .json(resObject); + let user = res.locals.user; + let iapBody = req.body; + + await iap.setup(); + + 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 * @api {post} /iap/ios/verify iOS Verify IAP @@ -44,7 +81,7 @@ api.iapiOSVerify = { middlewares: [authWithHeaders()], async handler (req, res) { let resObject = await iapIOSVerify(res.locals.user, req.body); - console.log(resObject) + return res .status(resObject.ok === true ? 200 : 500) .json(resObject); diff --git a/website/server/libs/api-v3/inAppPurchases.js b/website/server/libs/api-v3/inAppPurchases.js index 63af599f58..bce8b9e829 100644 --- a/website/server/libs/api-v3/inAppPurchases.js +++ b/website/server/libs/api-v3/inAppPurchases.js @@ -15,71 +15,11 @@ iap.config({ googlePublicKeyPath: nconf.get('IAP_GOOGLE_KEYDIR'), }); -let iapSetup = Bluebird.promisify(iap.setup, { context: iap }); -let iapValidate = Bluebird.promisify(iap.validate, { context: iap }); - -async function iapAndroidVerify (user, iapBody) { - // 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.', - }; - } -} +module.exports = { + setup: Bluebird.promisify(iap.setup, { context: iap }), + validate: Bluebird.promisify(iap.validate, { context: iap }), + GOOGLE: iap.GOOGLE, +}; async function iapIOSVerify (user, iapBody) { // Defining these 2 variables here so they can be logged in case of error