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

时间:2018-04-04 15:08:24

标签: javascript node.js google-play google-cloud-platform google-play-services

我已成功将应用结算中的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

但是收到身份验证错误。

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

1 个答案:

答案 0 :(得分:1)

错误告诉您问题究竟是什么。

"Data must be a string or buffer at Verify.update"。因此,您知道您正在使用非字符串或缓冲区调用Verify.update()

你叫什么?根据您的代码,您可以致电verify.update(signedData)。所以signedData不是字符串或缓冲区。

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

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"}';

但如果你仔细阅读错误信息,你的进步会更快 - 他们通常会仔细解释这个问题。