如何在nodeJS中确认应用购买中的Android

igetnqfo  于 2023-04-04  发布在  Android
关注(0)|答案(2)|浏览(139)

目前,我正在验证我的链接android应用google服务帐户以验证并尝试在应用中确认android订阅。我可以使用GET调用验证它们,但当我进行POST并将:acknowledge附加到URL末尾时,我收到以下错误

{
    "error": {
        "code": 400,
        "message": "The product purchase is not owned by the user.",
        "errors": [
            {
                "message": "The product purchase is not owned by the user.",
                "domain": "androidpublisher",
                "reason": "productNotOwnedByUser"
            }
        ]
    }
}

服务帐户已被授予所有权限,因为我可以告诉通过android应用商店x1c 0d1x
当我在没有:acknowledge的情况下进行GET调用时,我会收到这样的响应,并且可以看到订阅仍然没有被确认。

{
    "startTimeMillis": "1647982226152",
    "expiryTimeMillis": "1647982480099",
    "autoRenewing": false,
    "priceCurrencyCode": "USD",
    "priceAmountMicros": "490000",
    "countryCode": "US",
    "developerPayload": "",
    "cancelReason": 2,
    "orderId": "GPA.3361-7700-2612-89511..0",
    "linkedPurchaseToken": "helcpfajhpkoabgbklojcjlh.AO-J1OzgU2Fpxp9CAKnnqz3kGm8-dIxNV7cO5l_lguaM-M8eTyOUCYnAo1F9xE16ynTTovA8KDyGA_qaV775sqWEEaeAJmF683GuIhIQyd-7bxV6Mk9E5Gw",
    "purchaseType": 0,
    "acknowledgementState": 0,
    "kind": "androidpublisher#subscriptionPurchase"
}

整个代码

exports.acknowledgeAndroidSubscription = functions.https.onRequest((request, response) => {
  // require these first
  const {google} = require("googleapis");
  const axios = require("axios");
  const googleServiceAccountKey = require("./pc-api-6835501382478242417-177-5781829bedc5.json"); // see docs on how to generate a service account
  
  // get the token and subscription id from the request
  const {purchaseToken, subscriptionID, type} = request.body;
  functions.logger.log('incoming data', purchaseToken, type, subscriptionID);
  
  // set your
  const packageID = "edu.fit.my.jgibb2018.pob";

  const returnTheResponse = (data) => {
    response.status(200).send(data);
  };

  const acknowledgeSubscription = (err, tokens) => {
    //making this call as a POST to  url: `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${packageID}/purchases/subscriptions/${subscriptionID}/tokens/${purchaseToken}:acknowledge` results in an error. 
    functions.logger.log("trying to verify" + tokens.access_token);
    const config = {
      method: "get",
      url: `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${packageID}/purchases/subscriptions/${subscriptionID}/tokens/${purchaseToken}`,
      headers: {
        "Authorization": `Bearer ${tokens.access_token}`,
      },
    };

    axios(config)
        .then(function(r) {
          console.log(JSON.stringify(r.data));
          returnTheResponse(r.data);
        })
        .catch(function(error) {
          console.log(error);
          returnTheResponse(error);
        });
  };

  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") {
        //this is the only potential outcome to this if/else statement
        jwtClient.authorize(acknowledgeSubscription);
      } else if (type == "subscriptionVerify") {
        //not possible at this time during testing
        jwtClient.authorize(verifySubscription);
      } else {
        //not possible at this time during testing
        jwtClient.authorize(verifyPurchase);
      }
    } catch (error) {
      functions.logger.log(error);
      response.status(500).send("getting auth", error);
    }
  };

  getAccessToken();
});

当使用google-play-billing-validator节点包时,我只看到两次成功的确认购买。这发生在我第一次尝试时,然后在测试期间取消订阅后,重试,我无法再次确认。然后他们不断取消。

exports.googlePlayBillingValidator = functions.https.onRequest((request, response) => {
  // capture the data from the request
  const { purchaseObject, subscriptionID, type } = request.body;
  console.log(subscriptionID, type);
  // inclue your app bundle id
  const bundleID = "edu.fit.my.jgibb2018.pob";
  // require the service account info
  const googleServiceAccountKey = require("./pc-api-6835501382478242417-177-5781829bedc5.json"); // see docs on how to generate a service account
  // require the verify module
  const Verifier = require("google-play-billing-validator");
  // create verifier opitons object
  const options = {
    "email": googleServiceAccountKey.client_email,
    "key": googleServiceAccountKey.private_key,
  };
  // create a new verifier instance
  const verifier = new Verifier(options);
  // create a receipt object
  const receipt = {
    packageName: bundleID,
    productId: purchaseObject.productId,
    purchaseToken: purchaseObject.purchaseToken,
    
  };
  if (type.contains("subscriptionAcknowledge")) {
    // add the following string to the receipt object `developerPayload: "some unique identifier",`
    receipt.developerPayload = "some unique identifier";
  }
  // acknowledge the receipt
  const promiseData = verifier.verifySub(receipt);

  promiseData.then(function (r) {
    response.send(r);
  })
    .catch(function (error) {
      functions.logger.error(error);
      response.send(error);
    });
});
pprl5pva

pprl5pva1#

错误似乎源于快速测试。
当我在取消订阅和下一次尝试购买之间等待很长一段时间(1+小时)时,我能够测试我的订阅购买没有问题。当我立即或在短时间内测试时,我看到上面的错误。

bmvo0sr5

bmvo0sr52#

我也收到了这个错误。但是,当我简单地再次尝试整个事情时,它起作用了。
我怀疑这里发生的事情是你在测试环境中进行购买(我知道我是)。我正在购买订阅,而测试环境中的月度订阅在5分钟后到期-当我试图确认过期的订阅(通过我的API,使用服务帐户)时,我收到了一条非常无用的消息,关于“产品购买不属于用户”。
还要注意的是,:acknowledge请求返回的是204 NoContent响应,而不是200 OK响应,因此这在您的情况下也可能是一个问题。
然而,当我得到我的鸭子排队,并能够承认订阅后不久,完成购买,它的工作很好。

相关问题