From 17979575665f85d009ecbcd4731a27b1bddb4625 Mon Sep 17 00:00:00 2001 From: haider ali Date: Tue, 1 Apr 2025 00:35:24 +0500 Subject: [PATCH] add support for google purchase acknowldgement --- index.js | 34 ++++++++++++++++++++++ lib/google/index.js | 69 +++++++++++++++++++++++++++++++++++++++++++++ lib/google/urls.js | 12 ++++++++ 3 files changed, 115 insertions(+) diff --git a/index.js b/index.js index 7c51396..61929dd 100644 --- a/index.js +++ b/index.js @@ -76,6 +76,37 @@ function cancelSubscription(platform, payment, cb) { }); } +function acknowledgePurchase(platform, payment, cb) { + function syncError(error) { + process.nextTick(function () { + cb(error); + }); + } + + if (!payment) { + return syncError(new Error('No payment given')); + } + + const engine = platforms[platform]; + + if (!engine) { + return syncError(new Error(`Platform ${platform} not recognized`)); + } + + if (!engine.acknowledgePurchase) { + return syncError(new Error(`Platform ${platform} does not have acknowledgePurchase method`)); + } + + engine.acknowledgePurchase(payment, function (error, result) { + if (error) { + return cb(error); + } + + cb(null, result); + }); +} + + exports.verifyPayment = (platform, payment, cb) => { return (cb ? verifyPayment(platform, payment, cb) : promisify(verifyPayment)(platform, payment)); }; @@ -83,3 +114,6 @@ exports.verifyPayment = (platform, payment, cb) => { exports.cancelSubscription = (platform, payment, cb) => { return (cb ? cancelSubscription(platform, payment, cb) : promisify(cancelSubscription)(platform, payment)); }; +exports.acknowledgePurchase = (platform, payment, cb) => { + return (cb ? acknowledgePurchase(platform, payment, cb) : promisify(acknowledgePurchase)(platform, payment)); +}; diff --git a/lib/google/index.js b/lib/google/index.js index ca4ae9e..836aede 100644 --- a/lib/google/index.js +++ b/lib/google/index.js @@ -133,3 +133,72 @@ exports.cancelSubscription = function (payment, cb) { }); }); }; + + + +exports.acknowledgePurchase = function (payment, cb) { + let keyObject; + + try { + keyObject = validatePaymentAndParseKeyObject(payment); + } catch (error) { + return process.nextTick(() => { + return cb(error); + }); + } + + jwt.getToken( + keyObject.client_email, + keyObject.private_key, + apiUrls.publisherScope, + function (error, token) { + if (error) { + return cb(error); + } + + const requestUrl = apiUrls.purchasesProductsAcknowledge( + payment.packageName, + payment.productId, + payment.receipt, + token + ); + + const postData = JSON.stringify({ + developerPayload: payment.developerPayload || 'Acknowledged by server' + }); + + const requestOptions = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + }, + body: postData + }; + + https.post( + requestUrl, + requestOptions, + function (error, res, resultString) { + if (error) { + return cb(error); + } + + if (res.statusCode === 204) { + return cb(null, { message: 'Purchase acknowledged successfully' }); + } + if (res.statusCode !== 200) { + return cb( + new Error( + `Received ${res.statusCode} status code with body: ${resultString}` + ) + ); + } + + return cb(null, { message: 'Purchase acknowledged successfully' }); + } + ); + } + ); +}; + diff --git a/lib/google/urls.js b/lib/google/urls.js index ed23521..adebfef 100644 --- a/lib/google/urls.js +++ b/lib/google/urls.js @@ -51,3 +51,15 @@ exports.purchasesSubscriptionsCancel = function (packageName, productId, receipt encodeURIComponent(accessToken) // API access token ); }; + +exports.purchasesProductsAcknowledge = function (packageName, productId, receipt, accessToken) { + const urlFormat = 'https://www.googleapis.com/androidpublisher/v3/applications/%s/purchases/products/%s/tokens/%s:acknowledge?access_token=%s'; + + return util.format( + urlFormat, + encodeURIComponent(packageName), // application package name + encodeURIComponent(productId), // productId + encodeURIComponent(receipt), // purchase token + encodeURIComponent(accessToken) // API access token + ); +};