Server-side verification of Google Play In-app billing version 3 purchase

后端 未结 5 1729
滥情空心
滥情空心 2020-12-01 00:09

I\'m unable to find a straight answer as to how I verify an in-app billing purchase on the server before making downloadable content available to the user.

I use in

5条回答
  •  半阙折子戏
    2020-12-01 00:39

    My small contribution to reduce fraud in in-app purchases

    Signature verification on an external server, on your Android code :

    verifySignatureOnServer()

      private boolean verifySignatureOnServer(String data, String signature) {
            String retFromServer = "";
            URL url;
            HttpsURLConnection urlConnection = null;
            try {
                String urlStr = "https://www.example.com/verify.php?data=" + URLEncoder.encode(data, "UTF-8") + "&signature=" + URLEncoder.encode(signature, "UTF-8");
    
                url = new URL(urlStr);
                urlConnection = (HttpsURLConnection) url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader inRead = new InputStreamReader(in);
                retFromServer = convertStreamToString(inRead);
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
    
            return retFromServer.equals("good");
        }
    

    convertStreamToString()

     private static String convertStreamToString(java.io.InputStreamReader is) {
            java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
            return s.hasNext() ? s.next() : "";
        }
    

    verify.php on the root directory of web hosting

    
    

    NOTES:

    • You should encrypt the URL in your java code, if not the URL can be found easy with a simple text search in your decompressed app apk

    • Also better to change php file name, url arguments, good/bad reponses to something with no sense.

    • verifySignatureOnServer() should be run in a separated thread if not a network on main thread exception will be thrown.

    Hope it will help ...

提交回复
热议问题