diff --git a/.travis.yml b/.travis.yml index fc93903..38318c1 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: php php: + - "5.5" - "5.6" - "7.0" - "7.1" diff --git a/README.md b/README.md index e96a372..6b7d3f4 100755 --- a/README.md +++ b/README.md @@ -98,18 +98,20 @@ Or [Using a service account](https://developers.google.com/android-publisher/get Create service account [Service Account flow](https://developers.google.com/identity/protocols/OAuth2ServiceAccount) ```php -use ReceiptValidator\GooglePlay\ServiceAccountValidator as PlayValidator; -$validator = new PlayValidator([ - 'client_email' => 'xxxxxx@developer.gserviceaccount.com', - 'p12_key_path' => 'MyProject.p12', -]); +$googleClient = new \Google_Client(); +$googleClient->setScopes([\Google_Service_AndroidPublisher::ANDROIDPUBLISHER]); +$googleClient->setApplicationName('Your_Purchase_Validator_Name'); +$googleClient->setAuthConfig($pathToServiceAccountJsonFile); + +$googleAndroidPublisher = new \Google_Service_AndroidPublisher($googleClient); +$validator = new \ReceiptValidator\GooglePlay\Validator($googleAndroidPublisher); try { $response = $validator->setPackageName('PACKAGE_NAME') - ->setProductId('PRODUCT_ID') - ->setPurchaseToken('PURCHASE_TOKEN') - ->validate(); -} catch (Exception $e){ + ->setProductId('PRODUCT_ID') + ->setPurchaseToken('PURCHASE_TOKEN') + ->validateSubscription(); +} catch (\Exception $e){ var_dump($e->getMessage()); // example message: Error calling GET ....: (404) Product not found for this application. } diff --git a/composer.json b/composer.json index afb2d1b..d5b6042 100755 --- a/composer.json +++ b/composer.json @@ -26,14 +26,14 @@ "Apache-2.0" ], "require": { - "php": ">=5.6", + "php": ">=5.5", "guzzlehttp/guzzle": "~6.2", "nesbot/carbon": "~1", "google/apiclient": "~2.0", "robrichards/xmlseclibs": "^2.0" }, "require-dev": { - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^4.8||^5.7" }, "autoload": { "psr-4": { diff --git a/src/GooglePlay/AbstractResponse.php b/src/GooglePlay/AbstractResponse.php index db7204c..a7e1c9c 100644 --- a/src/GooglePlay/AbstractResponse.php +++ b/src/GooglePlay/AbstractResponse.php @@ -8,78 +8,47 @@ */ abstract class AbstractResponse { - const CONSUMPTION_STATE_YET_TO_BE_CONSUMED = 0; - const CONSUMPTION_STATE_CONSUMED = 1; - const PURCHASE_STATE_PURCHASED = 0; - const PURCHASE_STATE_CANCELED = 1; - - /** - * @var \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase - */ - protected $response; - - /** - * @var array - */ - protected $developerPayload = []; - - /** - * Constructor - * - * @param \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase $response - */ - public function __construct($response) - { - $this->response = $response; - $this->developerPayload = json_decode($this->response->developerPayload, true); - } - - /** - * @return int - */ - public function getConsumptionState() - { - return $this->response->consumptionState; - } - - /** - * @return array - */ - public function getDeveloperPayload() - { - return $this->developerPayload; - } - - /** - * @param string $key - * @return string - */ - public function getDeveloperPayloadElement($key) - { - return (isset($this->developerPayload[$key])) ? $this->developerPayload[$key] : ''; - } - - /** - * @return string - */ - public function getKind() - { - return $this->response->kind; - } - - /** - * @return string - */ - public function getPurchaseState() - { - return $this->response->purchaseState; - } - - /** - * @return \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase - */ - public function getRawResponse() - { - return $this->response; - } + const CONSUMPTION_STATE_YET_TO_BE_CONSUMED = 0; + const CONSUMPTION_STATE_CONSUMED = 1; + const PURCHASE_STATE_PURCHASED = 0; + const PURCHASE_STATE_CANCELED = 1; + + /** + * @var \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase + */ + protected $response; + + /** + * Constructor + * + * @param \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase $response + */ + public function __construct($response) + { + $this->response = $response; + } + + /** + * @return array|string + */ + public function getDeveloperPayload() + { + return $this->response->getDeveloperPayload(); + } + + /** + * @return string + */ + public function getKind() + { + return $this->response->kind; + } + + /** + * @return \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase + */ + public function getRawResponse() + { + return $this->response; + } } diff --git a/src/GooglePlay/PurchaseResponse.php b/src/GooglePlay/PurchaseResponse.php index e67fe0e..b6f9386 100644 --- a/src/GooglePlay/PurchaseResponse.php +++ b/src/GooglePlay/PurchaseResponse.php @@ -8,16 +8,54 @@ */ class PurchaseResponse extends AbstractResponse { - /** - * @var \Google_Service_AndroidPublisher_ProductPurchase - */ - protected $response; - - /** - * @return string - */ - public function getPurchaseTimeMillis() - { - return $this->response->purchaseTimeMillis; - } + /** + * @var \Google_Service_AndroidPublisher_ProductPurchase + */ + protected $response; + + protected $developerPayload = []; + + public function __construct($response) + { + parent::__construct($response); + $this->developerPayload = json_decode($this->response->developerPayload, true); + } + + /** + * @return int + */ + public function getConsumptionState() + { + return $this->response->consumptionState; + } + + /** + * @return string + */ + public function getPurchaseTimeMillis() + { + return $this->response->purchaseTimeMillis; + } + + public function getDeveloperPayload() + { + return $this->developerPayload; + } + + /** + * @param string $key + * @return string + */ + public function getDeveloperPayloadElement($key) + { + return (isset($this->developerPayload[$key])) ? $this->developerPayload[$key] : ''; + } + + /** + * @return string + */ + public function getPurchaseState() + { + return $this->response->purchaseState; + } } diff --git a/src/GooglePlay/SubscriptionResponse.php b/src/GooglePlay/SubscriptionResponse.php index 2c290e6..0fa2d28 100644 --- a/src/GooglePlay/SubscriptionResponse.php +++ b/src/GooglePlay/SubscriptionResponse.php @@ -8,64 +8,89 @@ */ class SubscriptionResponse extends AbstractResponse { - /** - * @var \Google_Service_AndroidPublisher_SubscriptionPurchase - */ - protected $response; + /** + * @var \Google_Service_AndroidPublisher_SubscriptionPurchase + */ + protected $response; - /** - * @return string - */ - public function getAutoRenewing() - { - return $this->response->autoRenewing; - } + /** + * @return bool + */ + public function getAutoRenewing() + { + return (bool)$this->response->getAutoRenewing(); + } - /** - * @return string - */ - public function getCancelReason() - { - return $this->response->cancelReason; - } + /** + * @return integer|null + */ + public function getCancelReason() + { + return $this->response->getCancelReason(); + } - /** - * @return string - */ - public function getCountryCode() - { - return $this->response->countryCode; - } + /** + * @return string + */ + public function getCountryCode() + { + return $this->response->getCountryCode(); + } - /** - * @return string - */ - public function getPriceAmountMicros() - { - return $this->response->priceAmountMicros; - } + /** + * @return integer + */ + public function getPriceAmountMicros() + { + return $this->response->getPriceAmountMicros(); + } - /** - * @return string - */ - public function getPriceCurrencyCode() - { - return $this->response->priceCurrencyCode; - } + /** + * @return string + */ + public function getPriceCurrencyCode() + { + return $this->response->getPriceCurrencyCode(); + } - /** - * @return string - */ - public function getStartTimeMillis() - { - return $this->response->startTimeMillis; - } + /** + * @return string + */ + public function getStartTimeMillis() + { + return $this->response->getStartTimeMillis(); + } - /** - * @return string - */ - public function getExpiresDate() - { - return $this->response->expiryTimeMillis; - } + /** + * @return integer + */ + public function getExpiryTimeMillis() + { + return $this->response->getExpiryTimeMillis(); + } + + /** + * @return integer|null + */ + public function getUserCancellationTimeMillis() + { + return $this->response->getUserCancellationTimeMillis(); + } + + /** + * @return integer + */ + public function getPaymentState() + { + return $this->response->getPaymentState(); + } + + /** + * @deprecated Use getExpiryTimeMillis() method instead + * @return string + */ + public function getExpiresDate() + { + return $this->response->expiryTimeMillis; + } } diff --git a/src/GooglePlay/Validator.php b/src/GooglePlay/Validator.php index 943412e..545a61c 100644 --- a/src/GooglePlay/Validator.php +++ b/src/GooglePlay/Validator.php @@ -118,4 +118,12 @@ public function validatePurchase() $this->_package_name, $this->_product_id, $this->_purchase_token )); } + + /** + * @return \Google_Service_AndroidPublisher + */ + public function getPublisherService() + { + return $this->_androidPublisherService; + } } diff --git a/tests/GooglePlay/PurchaseResponseTest.php b/tests/GooglePlay/PurchaseResponseTest.php index 9fd31df..7cde9d5 100644 --- a/tests/GooglePlay/PurchaseResponseTest.php +++ b/tests/GooglePlay/PurchaseResponseTest.php @@ -8,7 +8,7 @@ class GooglePlayPurchaseResponseTest extends \PHPUnit_Framework_TestCase { /** - * + * @link https://developers.google.com/android-publisher/api-ref/purchases/products */ public function testParsedResponse() { diff --git a/tests/GooglePlay/SubscriptionResponseTest.php b/tests/GooglePlay/SubscriptionResponseTest.php index 487ea65..373f3ec 100644 --- a/tests/GooglePlay/SubscriptionResponseTest.php +++ b/tests/GooglePlay/SubscriptionResponseTest.php @@ -4,6 +4,7 @@ /** * @group library + * @link https://developers.google.com/android-publisher/api-ref/purchases/subscriptions */ class GooglePlaySubscriptionResponseTest extends \PHPUnit_Framework_TestCase { @@ -12,36 +13,43 @@ class GooglePlaySubscriptionResponseTest extends \PHPUnit_Framework_TestCase */ public function testParsedResponse() { - $autoRenewing = 'testAutoRenewing'; - $cancelReason = 'testCancelReason'; + $autoRenewing = true; + $cancelReason = 0; $countryCode = 'testCountryCode'; $priceAmountMicros = 'testPriceAmountMicros'; $priceCurrencyCode = 'testPriceCurrencyCode'; - $startTimeMillis = 'testStartTimeMillis'; - $expiryTimeMillis = 'testExpiryTimeMillis'; + $startTimeMillis = time() * 1000; + $expiryTimeMillis = $startTimeMillis + 3600 * 24 * 7 * 1000; + $userCancellationTimeMillis = $startTimeMillis + 3600 * 24 * 1000; + $developerPayload = 'subs:developerPayload'; + $paymentState = 1; - // mock objects - $subscriptionPurchaseMock = $this->getMockBuilder('\Google_Service_AndroidPublisher_SubscriptionPurchase') - ->disableOriginalConstructor()->getMock(); + $data = [ + 'autoRenewing' => $autoRenewing, + 'cancelReason' => $cancelReason, + 'countryCode' => $countryCode, + 'priceAmountMicros' => $priceAmountMicros, + 'priceCurrencyCode' => $priceCurrencyCode, + 'startTimeMillis' => $startTimeMillis, + 'expiryTimeMillis' => $expiryTimeMillis, + 'userCancellationTimeMillis' => $userCancellationTimeMillis, + 'developerPayload' => $developerPayload, + 'paymentState' => $paymentState, + ]; - $subscriptionPurchaseMock->autoRenewing = $autoRenewing; - $subscriptionPurchaseMock->cancelReason = $cancelReason; - $subscriptionPurchaseMock->countryCode = $countryCode; - $subscriptionPurchaseMock->priceAmountMicros = $priceAmountMicros; - $subscriptionPurchaseMock->priceCurrencyCode = $priceCurrencyCode; - $subscriptionPurchaseMock->startTimeMillis = $startTimeMillis; - $subscriptionPurchaseMock->expiryTimeMillis = $expiryTimeMillis; + $subscriptionPurchase = new \Google_Service_AndroidPublisher_SubscriptionPurchase($data); + $subscriptionResponse = new SubscriptionResponse($subscriptionPurchase); - $subscriptionResponse = new SubscriptionResponse($subscriptionPurchaseMock); - - $this->assertInstanceOf('ReceiptValidator\GooglePlay\AbstractResponse', $subscriptionResponse); + $this->assertInstanceOf(AbstractResponse::class, $subscriptionResponse); $this->assertEquals($autoRenewing, $subscriptionResponse->getAutoRenewing()); $this->assertEquals($cancelReason, $subscriptionResponse->getCancelReason()); $this->assertEquals($countryCode, $subscriptionResponse->getCountryCode()); $this->assertEquals($priceAmountMicros, $subscriptionResponse->getPriceAmountMicros()); $this->assertEquals($priceCurrencyCode, $subscriptionResponse->getPriceCurrencyCode()); $this->assertEquals($startTimeMillis, $subscriptionResponse->getStartTimeMillis()); - $this->assertEquals($expiryTimeMillis, $subscriptionResponse->getExpiresDate()); - $this->assertEquals($subscriptionPurchaseMock, $subscriptionResponse->getRawResponse()); + $this->assertEquals($expiryTimeMillis, $subscriptionResponse->getExpiryTimeMillis()); + $this->assertEquals($userCancellationTimeMillis, $subscriptionResponse->getUserCancellationTimeMillis()); + $this->assertEquals($developerPayload, $subscriptionResponse->getDeveloperPayload()); + $this->assertEquals($paymentState, $subscriptionResponse->getPaymentState()); } }