We'd like to use google's subscription in our app.
Currently I can buy and check subscription status on the client.
Is there way how can we check subscription status on our php backend?
On backend we have:
Yes. You can check the subscription status by sending a request with the purchase token from your server (php in your case) to the play store api. You can check the expiryTimeMillis
field in the server response and see if the purchase has expired or not.
Also check this answer - https://stackoverflow.com/a/34005001/4250161
Here is an example how to get the purchase expire date in php:
$ch = curl_init();
$TOKEN_URL = "https://accounts.google.com/o/oauth2/token";
$VALIDATE_URL = "https://www.googleapis.com/androidpublisher/v3/applications/".
$appid."/purchases/subscriptions/".
$productID."/tokens/".$purchaseToken;
$input_fields = 'refresh_token='.$refreshToken.
'&client_secret='.$clientSecret.
'&client_id='.$clientID.
'&redirect_uri='.$redirectUri.
'&grant_type=refresh_token';
//Request to google oauth for authentication
curl_setopt($ch, CURLOPT_URL, $TOKEN_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $input_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result, true);
if (!$result || !$result["access_token"]) {
//error
return;
}
//request to play store with the access token from the authentication request
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$VALIDATE_URL."?access_token=".$result["access_token"]);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result, true);
if (!$result || $result["error"] != null) {
//error
return;
}
$expireTime = date('Y-m-d H:i:s', $result["expiryTimeMillis"]/1000. - date("Z"));
//You get the purchase expire time, for example 2017-02-22 09:16:42
Where:
$appId
- your app package name, like com.facebook.messenger$refreshToken, $clientID, $clientSecret
- Your account details for oauth2. For more details on the play store api see https://developers.google.com/android-publisher/api-ref/
on how to get them see https://developers.google.com/identity/protocols/oauth2#installed