aporat / store-receipt-validator

PHP receipt validator for Apple iTunes, Google Play and Amazon App Store
Apache License 2.0
633 stars 153 forks source link

Changes to the Google Play Developer API - update to version 3 before December 1st #101

Closed AdelBachene closed 5 years ago

AdelBachene commented 5 years ago

Starting December 1, 2019, versions 1 and 2 of the Google Play Developer API will no longer be available so we need to update to version 3 ahead of this date.

source: https://android-developers.googleblog.com/2019/03/changes-to-google-play-developer-api.html

Stafox commented 5 years ago

Thanks for the info. WIll do.

Stafox commented 5 years ago

@AdelBachene This is not a responsibility of this library. To use v3 API you should update the google/apiclient library. Look at vendor/google/apiclient-services/src/Google/Service/AndroidPublisher.php:64.

I use the v2.2.2 and it already use publisher v3 api

Defcon0 commented 5 years ago

I also got this warning from Google. Currently I use version 2.0.2 of your module and updated google/apiclient from v2.2.0 to v2.2.3 and google/apiclient-services from v0.25 to v0.104.

Is it likely that this will make problems? I quite don't want to update your library to the latest one since I spend days for implementing the subscription checking backend and don't want to touch it without an urgent need.

Or is there some upgrade guide from version 2.x to 3.x of your library?

Stafox commented 5 years ago

There is no upgrade notes. It depends what exactly you had used. If your code depends on internal classes like Response, which currently separated into AbstractResponse, ProductionResponse, SandboxResponse you will need to upgrade. Just try to update and run your tests

Defcon0 commented 5 years ago

Thanks for the quick answer.

Could you please take a short look? I think I didn't use internal classes, do I?

$objClient = new \Google_Client();
$objClient->setScopes([\Google_Service_AndroidPublisher::ANDROIDPUBLISHER]);
$objClient->setApplicationName('ABC');
$objClient->setAuthConfig(
    TL_ROOT . '/vendor/acme/contao-abc-bundle/src/Resources/keys/play-store/abc-a50cdcf0d7c0.json'
);

$objValidator = new GooglePlayValidator(new \Google_Service_AndroidPublisher($objClient));

try {
    $objServerResponse =
        $objValidator->setPackageName('com.abc')->setProductId($strProductId)->setPurchaseToken(
            $strPurchaseToken
        )->validateSubscription();
} catch (\Exception $e) {
    \System::getContainer()->get('monolog.logger.contao')->log(
        ContaoContext::ERROR,
        $e->getMessage(),
        [
            'contao' => new ContaoContext(
                'MemberData::validateSubscription -> Android', ContaoContext::ERROR
            )
        ]
    );

    return false;
}

$arrData = [
    'autoRenewing'       => $objServerResponse->getAutoRenewing(),
    'expirationDateTime' => intval($objServerResponse->getExpiryTimeMillis() / 1000),
    'purchaseDateTime'   => intval($objServerResponse->getStartTimeMillis() / 1000)
];

if ($objServerResponse->getExpiryTimeMillis() / 1000 > time()) {
    if ($objServerResponse->getUserCancellationTimeMillis() > 0) {
        $arrData['cancellationDateTime'] = intval($objServerResponse->getUserCancellationTimeMillis() / 1000);
    }

    // IMPORTANT: also canceled subscriptions in Android are still active until expiration
    $strState = 'active';
} else {
    $strState = 'expired';
}

// developerPayload is set to the username of the user in order to avoid restoring purchases for
// another account
if ($strState == 'active' && $objServerResponse->getDeveloperPayload() != $objMember->username) {
    return ['state' => 'notyours'];
}

$strServerResponse = json_encode($objServerResponse->getRawResponse());
Stafox commented 5 years ago

Looks like update to 3.0 should not break anything

Stafox commented 5 years ago

@aporat this may be closed. P.s. what about providing some permissions for me on this repo? At least to manage issues.

aporat commented 5 years ago

closed. @Stafox added you as a collaborator in this repo. thanks for all the help!