mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
port android iap code to v3
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -15,72 +15,12 @@ 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,
|
||||
module.exports = {
|
||||
setup: Bluebird.promisify(iap.setup, { context: iap }),
|
||||
validate: Bluebird.promisify(iap.validate, { context: iap }),
|
||||
GOOGLE: iap.GOOGLE,
|
||||
};
|
||||
|
||||
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) {
|
||||
// Defining these 2 variables here so they can be logged in case of error
|
||||
let token;
|
||||
|
||||
Reference in New Issue
Block a user