简体   繁体   English

从服务器端验证谷歌播放购买

[英]Verification google play purchase from server side

I now really messed up to understanding the concept and reason for need, and even don't know how to implement by coding. 我现在真的搞砸了理解需求的概念和原因,甚至不知道如何通过编码来实现。

So, I made mobile game using unity, c#. 所以,我用团结制作手机游戏,c#。 Used php, mysql for highscore and store user's in app purchased item info. 使用php,mysql进行高分,并在应用程序购买项目信息中存储用户。

I released my game a week ago at google play, my google wallet shows nobody until now purchased any items in that game. 我一周前在google play上发布了我的游戏,我的谷歌钱包显示没有人,直到现在购买该游戏中的任何项目。

But, when I checked my game server (mysql database), some users has enormous numbers of item that should be purchased. 但是,当我检查我的游戏服务器(mysql数据库)时,一些用户有大量应该购买的项目。

This means some users used hacking tool and bypass google play check process and deceive my game and made fake purchase. 这意味着一些用户使用黑客工具并绕过谷歌游戏检查过程并欺骗我的游戏并进行假购买。

So I didn't implement developerPayload, nor validation of google play purchase receipt. 所以我没有实现developerPayload,也没有验证google play购买收据。

So now I deleted whole malicious user's item at my DB,(this will set 0 to that user's client's item), and want to implement server side verification of google play purchase receipt. 所以现在我删除了我的数据库中的整个恶意用户的项目(这将设置0到该用户的客户端项目),并且想要实现google play购买收据的服务器端验证。

I searched many posts by googling, but there is no perfect solution from the first to end. 我通过谷歌搜索搜索了许多帖子,但从头到尾没有完美的解决方案。

Anyway, I implemented so far like this. 无论如何,我到目前为止实现了这样的。

First, after user ended google checkout process(whether this is real or fake), my unity client's OnPurchaseSucceded function called, there I implemented to start communicate my php script on my server. 首先,在用户结束谷歌结账过程(无论这是真的还是假的)之后,我的Unity客户端的OnPurchaseSucceded函数被调用,在那里我实现了开始在我的服务器上传递我的PHP脚本。

 private void OnPurchaseSucceded(Purchase purchase)
{
    /*
    // Optional verification of the payload. Can be moved to a custom server
    if (!VerifyDeveloperPayload(purchase.DeveloperPayload)) {
        return;
    }*/
    StartCoroutine(VerifyReceiptCo(purchase));
}

 IEnumerator VerifyReceiptCo(Purchase purchase)
{
    WWWForm hsFm = new WWWForm();
    hsFm.AddField("data", purchase.OriginalJson);
    hsFm.AddField("signature", purchase.Signature);
    WWW hs = new WWW(verifyURL, hsFm);
    yield return hs;
    Debug.Log("verify result is " + hs.text);
    // if result is true(validated), give item to user.
}

and here questions, 在这里问题,

1. [Did I do right above code? 1. [我在代码上方做得对吗? for 'data' field, I used purchase.OriginalJson, but is this right? 对于“数据”字段,我使用了buy.OriginalJson,但这是对的吗? if not, what should I use?] 如果没有,我该怎么用?]

and after receive these info from php (verifyURL of above code), 并从php(上述代码的verifyURL)收到这些信息后,

$base64EncodedPublicKey = "MY game's public key from google play console";
$signedData =  $_POST['data'];
$signature =  $_POST['signature']; 

$key = openssl_pkey_get_public($base64EncodedPublicKey);
$verified = (openssl_verify($signedData, base64_decode($signature), $key, OPENSSL_ALGO_SHA1) > 0);
$result = openssl_verify($signedData, base64_decode($signature), $key, OPENSSL_ALGO_SHA1);
if ($verified) {
    echo 'verified : result is'.$result;
} else {
    echo 'fail : result is'.$result;
}

another questions, 另一个问题,

2. [Do I need paid SSL services to use openssl_ method like above php code?] I am using hostgator.com 's web hosting, there my plan already have 'Shared SSL' service, then is this enough? 2. [我是否需要使用付费SSL服务来使用上面的php代码之类的openssl_方法?]我正在使用hostgator.com的网络托管,我的计划已经有了“共享SSL”服务,那么这就足够了吗? or should I buy another private SSL service plan?] 或者我应该购买另一个私人SSL服务计划?]

3. Are there no way to verify this without using openssl(for not using SSL services) method? 3.如果不使用openssl(不使用SSL服务)方法,是否无法验证这一点?

4. Overall am I doing right direction to deal with hacking user? 总体而言,我是否正确地指导黑客用户? Thanks much. 非常感谢。

here's my code for verify google order. 这是我的验证谷歌订单的代码。 it works fine with all products of my company. 它适用于我公司的所有产品。 Very simple code. 非常简单的代码。

    function verifyGoogleOrderLocal($packageName, $jsonData, $sig)
{
    $public_keys = array(
    'package1' => 'key1',
    'package2' => 'key2',
    );
    if(!$public_keys[$packageName]) {
        return array("success"=>0,"reason"=>'no public key defined');
    }
    $key = get_openssl_key($public_keys[$packageName]);
    if(!$key) {
        return array("success"=>0,"reason"=>'invalid public key');
    }
    $result = openssl_verify($jsonData, base64_decode($sig), $key, OPENSSL_ALGO_SHA1);
    $resp = array('success'=>$result);
    if($result==0) $resp['reason'] = 'invalid signature'; 
    return $resp;
}

function get_openssl_key($publicKey)
{
    $key = "-----BEGIN PUBLIC KEY-----\n" . chunk_split($publicKey, 64, "\n") . '-----END PUBLIC KEY-----';
    $key = openssl_get_publickey($key);
    return $key;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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