使用NodeJs后端在应用计费中验证和验证Playstore

问题描述 投票:0回答:1

我已成功将Playstore中的Playstore添加到我的Android应用程序中。我正在尝试在我的节点js服务器上验证它。因此,在成功购买后,我将这些购买详细信息发送到我的Firebase数据库。然后根据https://emuneee.com/blog/2015/07/15/google-play-in-app-billing-server-purchase-verification/

我试过以下代码..

var crypto = require('crypto');

var publicKey = "PUBLIC_KEY";
var signedData = {"orderId":"ORDER_ID","packageName":"com.example.app","productId":"id","purchaseTime":1522843791366,"purchaseState":0,"purchaseToken":"something"};
var signature = "signature";
var wasVerified = verifyPurchase(publicKey, signedData, signature);


function verifyPurchase(publicKey, signedData, signature) {
    var decodedPublicKey = getPublicKey(publicKey);
    var verifier = crypto.createVerify('SHA1');
    verifier.update(signedData);
    return verifier.verify(decodedPublicKey, signature, 'base64');
}

function getPublicKey(publicKey) {
    if (!publicKey) {
        return null;
    }
    var key = chunkSplit(publicKey, 64, '\n');
    var pkey = '-----BEGIN PUBLIC KEY-----\n' + key + '-----END PUBLIC KEY-----\n';
    return pkey;
}

function chunkSplit(str, len, end) {
    len = parseInt(len, 10) || 76;

    if (len < 1) {
        return false;
    }
    end = end || '\r\n';
    return str.match(new RegExp('.{0,' + len + '}', 'g')).join(end);
}

但得到了这个错误,

crypto.js:99
  this._handle.update(data, encoding);
               ^

TypeError: Data must be a string or a buffer
    at Verify.update (crypto.js:99:16)

然后我试了一下

https://www.googleapis.com/androidpublisher/v2/applications/packageName/purchases/subscriptions/subscriptionId/tokens/token

但是获得身份验证错

但我对如何验证感到困惑..需要帮助:(

javascript node.js google-play google-cloud-platform google-play-services
1个回答
1
投票

该错误可以准确地告诉您问题所在。

"Data must be a string or buffer at Verify.update"。所以你知道你用不是字符串或缓冲区的东西调用Verify.update()

你在叫什么?那么根据你的代码,你打电话给verify.update(signedData)。所以signedData不是String或缓冲区。

它是什么?那么根据你的源代码:

var signedData = {"orderId":"ORDER_ID","packageName":"com.example.app","productId":"id","purchaseTime":1522843791366,"purchaseState":0,"purchaseToken":"something"};

啊哈!这是将对象分配给signedData,而不是String(或缓冲区)。如果你转到你链接到的教程,你可以在他们的示例代码中看到,他们分配了一个String

因此,您可以通过执行此操作来修复此错误消息(额外的引号使其成为字符串):

var signedData = '{"orderId":"ORDER_ID","packageName":"com.example.app","productId":"id","purchaseTime":1522843791366,"purchaseState":0,"purchaseToken":"something"}';

但是如果你仔细阅读错误信息,你的进展会更快 - 他们通常会用一点思考来解释这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.