Verify Google pub/sub android payment notification server side C# / ASP.NET MVC

272 views Asked by At

After 3 days I am finished configurations pub/sub push notification from my android app, now my backend server catch every subscription notification made from android app, but now I'm stuck in this stage, all I need is how read full subscription details like(Who make this payment, expire date, ...etc)

I don't know how to do it and what necessary steps to make it happen.

this is what notification look like from my backend server:

{
    "message" :
    {
        "attributes":{ "key": null },
        "data": "JSON_CODE",
        "messageId": "6542662753109422",
        "message_id": "6546652753109422",
        "publishTime": "2023-01-01T15:53:42.962Z",
        "publish_time": "2023-01-01T15:53:42.962Z"
    },
    "subscription": "projects/{api_id}/subscriptions/{service_name}-sub"
}

message.data (JSON_CODE) decoded into this:

{
    "version": "1.0",
    "packageName": "com.expamle.android",
    "eventTimeMillis": "1672588422635",
    "subscriptionNotification": 
    {
        "version": "1.0",
        "notificationType": 4,
        "purchaseToken": "lkgkfeofbmfnnalianjdppej.AO-J1OxNcztkkzntpvQk4nttaBiqHJ5WMD58tb_KxCdsyPooE_QPvqXdtnoEpqD0t96j5V4lxol3_FfpuRNDvBeRTYKo_ixJtw",
        "subscriptionId": "12_month_plan"
     }
}

I have a keys.json file.

And I made all permission required in my pub/sub service accounts.

How to read the full subscription details?

1

There are 1 answers

0
MohammedIsmael On BEST ANSWER

I solved it, to any new coming users this is how to solve it .

first I've used simple .NET library "Google.Apis.Auth.OAuth2" to get access token to any query request.

by this simple code I can get token using key.json i've downloaded from my google cloud console from "**https://console.cloud.google.com/iam-admin/serviceaccounts**" and by manage keys you can generate json secret key.

Auth:

string keyFile = HttpContext.Server.MapPath("~/bin/key.json");
        var credential = GoogleCredential.FromFile(keyFile).CreateScoped(new[] { "https://www.googleapis.com/auth/androidpublisher" });
       string token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
    }

Now you can request google query GET url to get full purchased subscription info .

https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{PACKAGE_NAME}/purchases/subscriptionsv2/tokens/{purchaseTokenComesWithMessage.data}

any requests must be included with Authorization Bearer accessToken we'ave generated from Auth. section above.

the result:

{
    "kind": "androidpublisher#subscriptionPurchaseV2",
    "startTime": "2023-01-03T14:56:24.940Z",
    "regionCode": "US",
    "subscriptionState": "SUBSCRIPTION_STATE_ACTIVE",
    "latestOrderId": "GPA.3394-8372-7793-03673",
    "testPurchase": {},
    "acknowledgementState": "ACKNOWLEDGEMENT_STATE_ACKNOWLEDGED",
    "externalAccountIdentifiers": {
        "obfuscatedExternalAccountId": "92d8f330-8d2a-45ab-b6a1-6720b3d15999"
    },
    "lineItems": [
        {
            "productId": "12_month_plan",
            "expiryTime": "2023-01-03T15:28:21.928Z",
            "autoRenewingPlan": {
                "autoRenewEnabled": true
            },
            "offerDetails": {
                "basePlanId": "360day",
                "offerTags": [
                    "tools"
                ]
            }
        }
    ] }

Thanks