const functions = require("firebase-functions");
const admin = require("firebase-admin");
const googleServiceAccountKey = require("./serviceAccount.json");
admin.initializeApp({
credential: admin.credential.cert(googleServiceAccountKey),
});
const {google} = require("googleapis");
const axios = require("axios");
exports.androidSubscriptionHandler = functions.https.onRequest((request, response) => {
// caputre the data from the request
const {purchaseObject, type} = request.body;
functions.logger.info("trying to handle " + type + " for the follow purchaseObject");
functions.logger.info(JSON.stringify(purchaseObject));
// get the token and subscription id from the request
const purchaseToken = purchaseObject.purchaseToken;
const subscriptionID = purchaseObject.productId;
// set your package id
const packageID = "edu.fit.my.jgibb2018.pob";
const returnTheResponse = (data) => {
functions.logger.log("returning the response" + JSON.stringify(data));
response.status(200).send(data);
};
const acknowledgeSubscription = (err, tokens) => {
functions.logger.info(`
Attempting to acknowledge subscription ${subscriptionID}\n
your access token for a manual retry ${tokens.access_token}\n
your purchase object ${JSON.stringify(purchaseObject)}\n\n
`);
const config = {
method: "post",
url: `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${packageID}/purchases/subscriptions/${subscriptionID}/tokens/${purchaseToken}:acknowledge`,
headers: {
"Authorization": `Bearer ${tokens.access_token}`,
},
};
functions.logger.info("acknowledge config" + JSON.stringify(config));
axios(config)
.then(function(r) {
returnTheResponse("Your transaction has been completed");
})
.catch(function(e) {
functions.logger.warn("an error occured while acknowledging the subscription");
functions.logger.error(JSON.stringify({error: e}));
returnTheResponse({error: e.data, status: e.status, message: e.message});
});
};
const verifySubscription = (err, tokens) => {
functions.logger.info("verify function");
const config = {
method: "get",
url: `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${packageID}/purchases/subscriptions/${subscriptionID}/tokens/${purchaseToken}`,
headers: {
"Authorization": `Bearer ${tokens.access_token}`,
},
};
functions.logger.info(config.url);
axios(config)
.then(function(r) {
functions.logger.info("verify success" + JSON.stringify(r.data));
returnTheResponse(r.data);
})
.catch(function(e) {
returnTheResponse(JSON.stringify({error: e.data, status: e.status, message: e.message}));
});
};
const getAccessToken = () => {
const jwtClient = new google.auth.JWT(
googleServiceAccountKey.client_email,
null,
googleServiceAccountKey.private_key,
["https://www.googleapis.com/auth/androidpublisher"],
null,
);
try {
if (type == "subscriptionAcknowledge") {
jwtClient.authorize(acknowledgeSubscription);
} else if (type == "subscriptionVerify") {
jwtClient.authorize(verifySubscription);
}
} catch (error) {
functions.logger.error(error);
response.status(500).send("issue getting getting auth", JSON.stringify(error));
}
};
getAccessToken();
});