简体   繁体   中英

Authenticating and verifying playstore in app billing using NodeJs backend

I have successfully added Playstore in app billing to my android app. And I'm trying to verify it on my node js server. So after a succesfull purchase I send those puchase details to my Firebase database. Then according to https://emuneee.com/blog/2015/07/15/google-play-in-app-billing-server-purchase-verification/

I tried the following code..

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);
}

But got this error,

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

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

Then I tried

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

but getting authentication error.

But I'm confused on how to authenticate.. Need Help :(

The error tells you exactly what the problem is.

"Data must be a string or buffer at Verify.update" . So you know you are calling Verify.update() with something that isn't a string or buffer.

What are you calling it with? Well according to your code, you call verify.update(signedData) . So signedData isn't a String or buffer.

What is it? Well according to your source code:

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

Aha! This is assigning an object to signedData, not a String (or buffer). If you go to the tutorial you link to, you can see in their sample code, they assign a String .

So you can probably fix this error message by doing (extra quotes to make it a string):

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

But you'll progress faster if you read error messages carefully - they normally explain the problem with a bit of thought.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM