2

I am trying to make a request to the google to verify my IAP purchase but I keep getting the error

Invalid Value at Gaxios._request (/srv/node_modules/googleapis-common/node_modules/gaxios/build/src/gaxios.js:89:23)

I logged the values and they seem to be the correct values. The purchase itself is working in my app it's just that I can't verify it on the server. How can I see what the invalid value is?

async function verifySubscriptionAndroid(purchase: InAppPurchaseModel): Promise<boolean> {
    let status;
    console.log(packageName); //com.company.my_app_name but then it is my actual app name in that format
    console.log(purchase.id); //7162541432 which is the id I provided to the subscription in the google play console
    console.log(purchase.token); //fdlidklhmmfggackclaedklj.AO-J1Ozu3HBDHN3BqTconVT8pMfvVIi_Iw9nIaY6t5Datphb62ehBJuHwFYXKmAw2VZafChN1fmrbY6_6bdh_-Fzq0Is3-e3u829-ckImdMT9gijDVCkC4mL1kWxqtJ5pWHVNbAIzGz_

    try {
        await authClient.authorize();
        const subscription = await playDeveloperApiClient.purchases.subscriptions.get({
            packageName: packageName, 
            subscriptionId: purchase.id,
            token: purchase.token
        });
        status = subscription.status;
    } catch (error) {
        console.log(error)
    }
    return status === 200;
};

This is my model

export class IAPReceiptModel {
    source: string;
    id:string;
    token: string;
    isSubscription: boolean;


    constructor(source: string, productId:string, token: string, isSubscription: boolean) {
        this.source = source;
        this.id = productId;
        this.token= token;
        this.isSubscription = isSubscription;
    }

    static fromJSON(data: any): IAPReceiptModel {
        console.log(data);
        const source: string = data?.source ?? '';
        const productId: string = data?.id ?? '';
        const token: string = data?.token ?? '';
        const isSubscription:boolean = data?.isSubscription ?? false;
        return new IAPReceiptModel(source, productId, verificationData, isSubscription);
    }
}

I am using the google Auth api

import { google } from 'googleapis';
import * as key from './keys/service_account_key.json';

const authClient = new google.auth.JWT({
    email: key.client_email,
    key: key.private_key,
    scopes: ["https://www.googleapis.com/auth/androidpublisher"]
});

const playDeveloperApiClient = google.androidpublisher({
    version: 'v3',
    auth: authClient
});

And I have a service account setup.

It is making a request to the following url

https://www.googleapis.com/androidpublisher/v3/applications/com.company.my_app_name/purchases/products/7162541432/tokens/fdlidklhmmfggackclaedklj.AO-J1Ozu3HBDHN3BqTconVT8pMfvVIi_Iw9nIaY6t5Datphb62ehBJuHwFYXKmAw2VZafChN1fmrbY6_6bdh_-Fzq0Is3-e3u829-ckImdMT9gijDVCkC4mL1kWxqtJ5pWHVNbAIzGz_

Note: I changed the packagename, sku_id and token in my question but they check out

I looked at

Verify a Purchase using Firebase Functions

for reference and also tried to inject the authclient directly into the object like

const response = await playDeveloperApiClient.purchases.subscriptions.get({
    auth: authClient,
    packageName: packageName,
    subscriptionId: receipt.productId,
    token: receipt.verificationData
});
anonymous-dev
  • 2,897
  • 9
  • 48
  • 112
  • I'm not familiar with `async/await`, but comparing your code with mine seems like you are missing the `authClient` in `const subscription`. – Guanaco Devs Oct 01 '20 at 09:42
  • Also the `purchase.id` for you is a Number? Ideally should be something like **monthly_sub** or **yearly_sub** or something easy to read. The second column in your Subscriptions section in the Console. – Guanaco Devs Oct 01 '20 at 09:54

1 Answers1

0

Maybe you want to try initializing the Google API like this:

const playDeveloperApiClient = google.androidpublisher('v3');

And add auth to:

const subscription = await playDeveloperApiClient.purchases.subscriptions.get({
                auth: authClient,
                packageName: packageName, 
                subscriptionId: purchase.id,
                token: purchase.token
            });

For the purchase.id I find it odd that you are using a number for the Id, ideally you would want to put a string as I mentioned in the comments, just in case, it should be the Product ID in your Subscriptions section in the Play Console as highlighted in the picture below: enter image description here

Guanaco Devs
  • 1,822
  • 2
  • 21
  • 38
  • I am using a string. I added the code for the model. The string matches the productId in the developer console. I also tried to inject the authclient in the object but this also had no effect. – anonymous-dev Oct 01 '20 at 10:43
  • Maybe you want to try your data against the web API for subscriptions, just fill the appropriate data on the right panel. https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptions/get – Guanaco Devs Oct 01 '20 at 18:23