Google-api-php-client: Validating IAP Receipt

Created on 27 Nov 2018  路  2Comments  路  Source: googleapis/google-api-php-client

I work with Google Api PHP Client 2.2.2 and PHP 5.6.

This code works fine if all parameters there and correctly:

$packageName = "XXX";
    $subscriptionId = "XXX";

    putenv('GOOGLE_APPLICATION_CREDENTIALS=' . get_template_directory() . '/inc/google-api-php-client/api-project-XXX.json');
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/androidpublisher');
    $service = new Google_Service_AndroidPublisher($client);
    $purchase = $service->purchases_subscriptions->get($packageName, $subscriptionId, $purchaseToken);

But if there is an error, I get an Fatal Error 500 as well. For example:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "errors": [ { "domain": "androidpublisher", "reason": "purchaseTokenDoesNotMatchSubscriptionId", "message": "The subscription purchase token does not match the subscription ID.", "locationType": "parameter", "location": "token" } ], "code": 400, "message": "The subscription purchase token does not match the subscription ID." } } ' in /XXX/wp-content/themes/if/inc/google-api-php-client/src/Google/Http/REST.php:118 Stack trace: #0 /XXXwp-content/themes/if/inc/google-api-php-client/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /XXX/wp-content/themes/if/inc/google-api- in /XXX/wp-content/themes/if/inc/google-api-php-client/src/Google/Http/REST.php on line 118

Its right, the purchaseToken does Not Match the SubscriptionId, but why is there an 500 fatal error?
I just want to get the error as JSON. Thanks a lot for your help.

question

Most helpful comment

How about:

try {
    $purchase = $service->purchases_subscriptions->get($packageName, $subscriptionId, $purchaseToken);
}
catch (Google_Service_Exception $e) {
    $json = $e->getMessage();
}

All 2 comments

How about:

try {
    $purchase = $service->purchases_subscriptions->get($packageName, $subscriptionId, $purchaseToken);
}
catch (Google_Service_Exception $e) {
    $json = $e->getMessage();
}

Thank you very much.

Was this page helpful?
0 / 5 - 0 ratings