purchases.products.get is ignoring productId value and returns null in ProductPurchase

2k views Asked by At

I'm making request to google Android Publisher api to check if the provided purchaseToken of an in-app purchase is correct. First problem is that the product id is ignored and I can type anything as the parameter. Only package name and purchaseToken needs to be correct. Secondly why productId and purchaseToken in ProductPurchase are null. I would like to check if provided purchaseToken matches productId. When productId can be anything and it's not returned in response how can I check if this is valid for particular in-app purchase productId?

    private static Task<ProductPurchase> GetInAppPurchase(string packageName, string productId, string purchaseToken)
    {
        GoogleCredential credential;
        using (Stream stream = new FileStream(@"api.json", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            credential = GoogleCredential.FromStream(stream);
        }

        string[] scopes = {
            AndroidPublisherService.Scope.Androidpublisher
        };
        credential = credential.CreateScoped(scopes);

        BaseClientService.Initializer initializer = new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName       = "My Application"
        };
        
        
        
        AndroidPublisherService service = new AndroidPublisherService(initializer);

        return service.Purchases.Products.Get(packageName, productId, purchaseToken).ExecuteAsync();
    }

    private static void TestGooglePlayPurchaseApi()
    {
        string packageName = "validPackageName";
        string productId   = "validOrInvalidProductName";
        string purchaseToken = "validPurchaseToken";

        ProductPurchase purchase = GetInAppPurchase(packageName, productId, purchaseToken).Result;

        Console.WriteLine("Kind: " + purchase.Kind);
        Console.WriteLine("Quantity: " + purchase.Quantity);
        Console.WriteLine("AcknowledgementState: " + purchase.AcknowledgementState!);
        Console.WriteLine("ConsumptionState: " + purchase.ConsumptionState!);
        Console.WriteLine("DeveloperPayload: " + purchase.DeveloperPayload);
        Console.WriteLine("ETag: " + purchase.ETag);
        Console.WriteLine("OrderId: " + purchase.OrderId);
        Console.WriteLine("ProductId: " + purchase.ProductId);
        Console.WriteLine("PurchaseState: " + purchase.PurchaseState!);
        Console.WriteLine("PurchaseToken: " + purchase.PurchaseToken);
        Console.WriteLine("PurchaseType: " + purchase.PurchaseType!);
        Console.WriteLine("RegionCode: " + purchase.RegionCode);
        Console.WriteLine("PurchaseTimeMillis: " + purchase.PurchaseTimeMillis);
        Console.WriteLine("ObfuscatedExternalAccountId: " + purchase.ObfuscatedExternalAccountId);
        Console.WriteLine("ObfuscatedExternalProfileId: " + purchase.ObfuscatedExternalProfileId);
    }

And this is what I'm reciving:

Kind: androidpublisher#productPurchase
Quantity:
AcknowledgementState: 1
ConsumptionState: 1
DeveloperPayload: {"developerPayload":"","is_free_trial":false,"has_introductory_price_trial":false,"is_updated":false,"accountId":""}
ETag:
OrderId: YYY.XXXX-XXXX-XXXX-XXXXX
ProductId:
PurchaseState: 0
PurchaseToken:
PurchaseType: 0
RegionCode: PL
PurchaseTimeMillis: 1601915754698
ObfuscatedExternalAccountId:
ObfuscatedExternalProfileId:

Why ProductId is empty?

2

There are 2 answers

1
Santa Bracket Clause On

It will be empty if you have only one item in your database. Weird, but it is as it is. Add second item, of the same category, and you'll start getting proper values in response.

9
Linda Lawton - DaImTo On

Ok wild guess would be to add fields to the request. Fields lets you decide which fields are populated in the response some of the API methods dont return all the fields by default by saying fields = * you tell the api you want all the data.

var request = service.Purchases.Products.Get(packageName, productId, purchaseToken);
request.Fields = "*";
return request.ExecuteAsync();

If that doesnt work i am trying to track down an issue forum for that api. I will edit this if i find one.