Search code examples
c#jsonunity-game-enginegoogle-play-services

find orderId from my payload receipt json of google play(Unity) c#


so when I check boughtProduct.receipt I get information about receipt containing orderId, transaction Id everything in the form of json like this

{"Payload":"{\"json\":\"{\\\"orderId\\\":\\\"GPA.3456-5593-26230\\\",\\\"packageName\\\":\\\"com.Game.Company\\\",\\\"productId\\\":\\\"com.game.company.unlockall\\\",\\\"purchaseTime\\\":1657566629633,\\\"purchaseState\\\":0,\\\"purchaseToken\\\":\\\"ogpffeblkohlejehffmpjdia.AO-J1OzgoZ0k9--lCtsD7a_vdqMNtpVO0M6twn8uojIt9nX5f15aT15xS8gflnTpi-7HCg9_1DLY-TFE-NyY3RP1moIGNjvinoMmBEtLAIGd13IJe3PhGjg\\\",\\\"acknowledged\\\":true}\",\"signature\":\"ADNDAJDAJDJAJDAJDJADJA==\",\"skuDetails\":\"{\\\"productId\\\":\\\"com.game.company.unlockall\\\",\\\"type\\\":\\\"inapp\\\",\\\"title\\\":\\\"Unlock All Chapters\\\",\\\"name\\\":\\\"Unlock All Chapters\\\",\\\"description\\\":\\\"Remove Ads and Unlock All Chapters\\\",\\\"price\\\":\\\"\\u20a9160.00\\\",\\\"price_amount_micros\\\":160000000,\\\"price_currency_code\\\":\\\"INR\\\",\\\"skuDetailsToken\\\":\\\"AEuhpteuwINhyl241tHPoLuXb\\\"}\"}","Store":"GooglePlay","TransactionID":"ogpffeblkohlejehzgoZ0k9--lCtsD7a_vdqMnX5f15aT15xS8gflnTpi-7HCg9_1DLY-GNjvinoMmBEtLAIGd13IJe3PhGjg"}

So I want to retrieve orderId from this. I dont know how to do this. I tried using Dictionary like this

var gpDetails = (Dictionary<string,object>)MiniJson.JsonDecode(boughtProduct.receipt);
      
        var gpJson = (string)gpDetails["json"];
        var gpSign = (string)gpDetails["signature"];

        Debug.Log(gpSign + " sign"); 
        Debug.Log(gpJson + " json"); //or try this


            foreach (KeyValuePair<string, object> kvp in gpDetails) //or try this
            {
                Debug.Log("Key = {0}, Value = {1}"+
                    kvp.Key);
                
            }

But I'm getting error no key found. What should I do?


Solution

  • I did like this and it worked easily

    char[] charsToTrim = { '\\', '"', ':', ',' };
    string orderId = getBetween(Payload, "orderId", "packageName").Trim(charsToTrim);
    
    
     string getBetween(string strSource,string strStart, string strEnd)
        {
            if (strSource.Contains(strStart) && strSource.Contains(strEnd))
            {
                int Start, End;
                Start = strSource.IndexOf(strStart, 0) + strStart.Length;
                End = strSource.IndexOf(strEnd, Start);
                return strSource.Substring(Start, End - Start);
            }
    
            return "";
        }