From 0dadaf7b4e6e9fda11f029ba50f14e9eef2dde94 Mon Sep 17 00:00:00 2001 From: "stanislav.kuryan" Date: Tue, 18 Apr 2017 11:44:21 +0300 Subject: [PATCH 1/7] Fix subscription response wrapper. Fix tests --- src/GooglePlay/AbstractResponse.php | 35 +--------------- src/GooglePlay/PurchaseResponse.php | 38 +++++++++++++++++ src/GooglePlay/SubscriptionResponse.php | 42 +++++++++++++++---- tests/GooglePlay/PurchaseResponseTest.php | 2 +- tests/GooglePlay/SubscriptionResponseTest.php | 42 ++++++++++++------- 5 files changed, 101 insertions(+), 58 deletions(-) diff --git a/src/GooglePlay/AbstractResponse.php b/src/GooglePlay/AbstractResponse.php index f69f4ed..efc7042 100644 --- a/src/GooglePlay/AbstractResponse.php +++ b/src/GooglePlay/AbstractResponse.php @@ -18,11 +18,6 @@ abstract class AbstractResponse */ protected $response; - /** - * @var array - */ - protected $developerPayload = array(); - /** * Constructor * @@ -31,32 +26,14 @@ abstract class AbstractResponse 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 + * @return array|string */ public function getDeveloperPayload() { - return $this->developerPayload; - } - - /** - * @param string $key - * @return string - */ - public function getDeveloperPayloadElement($key) - { - return (isset($this->developerPayload[$key])) ? $this->developerPayload[$key] : ''; + return $this->response->getDeveloperPayload(); } /** @@ -66,12 +43,4 @@ public function getKind() { return $this->response->kind; } - - /** - * @return string - */ - public function getPurchaseState() - { - return $this->response->purchaseState; - } } diff --git a/src/GooglePlay/PurchaseResponse.php b/src/GooglePlay/PurchaseResponse.php index 61948ec..8fca654 100644 --- a/src/GooglePlay/PurchaseResponse.php +++ b/src/GooglePlay/PurchaseResponse.php @@ -13,6 +13,22 @@ class PurchaseResponse extends AbstractResponse */ protected $response; + protected $developerPayload = array(); + + 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 */ @@ -20,4 +36,26 @@ 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 bf950dd..52fbee4 100644 --- a/src/GooglePlay/SubscriptionResponse.php +++ b/src/GooglePlay/SubscriptionResponse.php @@ -14,19 +14,19 @@ class SubscriptionResponse extends AbstractResponse protected $response; /** - * @return string + * @return bool */ public function getAutoRenewing() { - return $this->response->autoRenewing; + return (bool)$this->response->getAutoRenewing(); } /** - * @return string + * @return integer|null */ public function getCancelReason() { - return $this->response->cancelReason; + return $this->response->getCancelReason(); } /** @@ -34,15 +34,15 @@ public function getCancelReason() */ public function getCountryCode() { - return $this->response->countryCode; + return $this->response->getCountryCode(); } /** - * @return string + * @return integer */ public function getPriceAmountMicros() { - return $this->response->priceAmountMicros; + return $this->response->getPriceAmountMicros(); } /** @@ -50,7 +50,7 @@ public function getPriceAmountMicros() */ public function getPriceCurrencyCode() { - return $this->response->priceCurrencyCode; + return $this->response->getPriceCurrencyCode(); } /** @@ -58,6 +58,30 @@ public function getPriceCurrencyCode() */ public function getStartTimeMillis() { - return $this->response->startTimeMillis; + return $this->response->getStartTimeMillis(); + } + + /** + * @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(); } } diff --git a/tests/GooglePlay/PurchaseResponseTest.php b/tests/GooglePlay/PurchaseResponseTest.php index 48fe91d..18fcba1 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 fba009a..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,32 +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'; + $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; + $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->getExpiryTimeMillis()); + $this->assertEquals($userCancellationTimeMillis, $subscriptionResponse->getUserCancellationTimeMillis()); + $this->assertEquals($developerPayload, $subscriptionResponse->getDeveloperPayload()); + $this->assertEquals($paymentState, $subscriptionResponse->getPaymentState()); } } From d1c68a6b4bce997a3ff2a7ddaab81a68604a44e3 Mon Sep 17 00:00:00 2001 From: "stanislav.kuryan" Date: Tue, 18 Apr 2017 12:51:27 +0300 Subject: [PATCH 2/7] Add possibility to get AndroidPublisherService to get access to another actions (get list of voidedPurchases, refund and revoke subscriptions) --- src/GooglePlay/Validator.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/GooglePlay/Validator.php b/src/GooglePlay/Validator.php index c737ed2..eaad425 100644 --- a/src/GooglePlay/Validator.php +++ b/src/GooglePlay/Validator.php @@ -92,4 +92,12 @@ public function validatePurchase() $this->_package_name, $this->_product_id, $this->_purchase_token )); } + + /** + * @return \Google_Service_AndroidPublisher + */ + public function getPublisherService() + { + return $this->_androidPublisherService; + } } From ea96ffb4b78260d35811d0f559e338397ca1f9a7 Mon Sep 17 00:00:00 2001 From: "stanislav.kuryan" Date: Tue, 18 Apr 2017 13:33:44 +0300 Subject: [PATCH 3/7] Fix readme. Downgrade php to 5.5 in requirements (by downgrading required version of phpunit). --- README.md | 20 +++++++++++--------- composer.json | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b613e21..64b7a61 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 e8f384a..5bedb88 100755 --- a/composer.json +++ b/composer.json @@ -26,13 +26,13 @@ "Apache-2.0" ], "require": { - "php": ">=5.6", + "php": ">=5.5", "guzzlehttp/guzzle": "~6.2", "google/apiclient": "~2.0", "robrichards/xmlseclibs": "^2.0" }, "require-dev": { - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^4.8||^5.7" }, "autoload": { "psr-4": { From 88db1359f47ad4396237ebf3d1cdb0ff7d0a77ec Mon Sep 17 00:00:00 2001 From: "stanislav.kuryan" Date: Tue, 18 Apr 2017 16:17:26 +0300 Subject: [PATCH 4/7] Add php 5.5 to travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 944be1f..6d6cc21 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: php php: + - "5.5" - "5.6" - "7.0" - "7.1" From 6c5d436aa899fa5a47c4de6b1247733dfbd8164e Mon Sep 17 00:00:00 2001 From: "stanislav.kuryan" Date: Wed, 19 Apr 2017 08:47:53 +0300 Subject: [PATCH 5/7] Fix subscription response test --- tests/GooglePlay/SubscriptionResponseTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/GooglePlay/SubscriptionResponseTest.php b/tests/GooglePlay/SubscriptionResponseTest.php index 00b1f95..373f3ec 100644 --- a/tests/GooglePlay/SubscriptionResponseTest.php +++ b/tests/GooglePlay/SubscriptionResponseTest.php @@ -23,7 +23,6 @@ public function testParsedResponse() $userCancellationTimeMillis = $startTimeMillis + 3600 * 24 * 1000; $developerPayload = 'subs:developerPayload'; $paymentState = 1; - $expiryTimeMillis = 'testExpiryTimeMillis'; $data = [ 'autoRenewing' => $autoRenewing, @@ -40,7 +39,6 @@ public function testParsedResponse() $subscriptionPurchase = new \Google_Service_AndroidPublisher_SubscriptionPurchase($data); $subscriptionResponse = new SubscriptionResponse($subscriptionPurchase); - $subscriptionPurchaseMock->expiryTimeMillis = $expiryTimeMillis; $this->assertInstanceOf(AbstractResponse::class, $subscriptionResponse); $this->assertEquals($autoRenewing, $subscriptionResponse->getAutoRenewing()); From 0fecce3644b6b084954e670441a5871f26da9ee5 Mon Sep 17 00:00:00 2001 From: "stanislav.kuryan" Date: Wed, 19 Apr 2017 10:49:49 +0300 Subject: [PATCH 6/7] Fix code formatting after merge. --- src/GooglePlay/AbstractResponse.php | 21 +++-- src/GooglePlay/PurchaseResponse.php | 18 ++-- src/GooglePlay/SubscriptionResponse.php | 106 ++++++++++++------------ 3 files changed, 74 insertions(+), 71 deletions(-) diff --git a/src/GooglePlay/AbstractResponse.php b/src/GooglePlay/AbstractResponse.php index 5af53b0..a7e1c9c 100644 --- a/src/GooglePlay/AbstractResponse.php +++ b/src/GooglePlay/AbstractResponse.php @@ -8,15 +8,15 @@ */ 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; + 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 \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase + */ + protected $response; /** * Constructor @@ -29,7 +29,6 @@ public function __construct($response) } /** - * @return array|string */ public function getDeveloperPayload() @@ -46,8 +45,8 @@ public function getKind() } /** - * @return \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase - */ + * @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 1b0b303..b6f9386 100644 --- a/src/GooglePlay/PurchaseResponse.php +++ b/src/GooglePlay/PurchaseResponse.php @@ -8,12 +8,12 @@ */ class PurchaseResponse extends AbstractResponse { - /** - * @var \Google_Service_AndroidPublisher_ProductPurchase - */ - protected $response; + /** + * @var \Google_Service_AndroidPublisher_ProductPurchase + */ + protected $response; - protected $developerPayload = array(); + protected $developerPayload = []; public function __construct($response) { @@ -27,13 +27,17 @@ public function __construct($response) public function getConsumptionState() { return $this->response->consumptionState; - }/** + } + + /** * @return string */ public function getPurchaseTimeMillis() { return $this->response->purchaseTimeMillis; - }public function getDeveloperPayload() + } + + public function getDeveloperPayload() { return $this->developerPayload; } diff --git a/src/GooglePlay/SubscriptionResponse.php b/src/GooglePlay/SubscriptionResponse.php index dcc065f..9ebf263 100644 --- a/src/GooglePlay/SubscriptionResponse.php +++ b/src/GooglePlay/SubscriptionResponse.php @@ -8,57 +8,57 @@ */ class SubscriptionResponse extends AbstractResponse { - /** - * @var \Google_Service_AndroidPublisher_SubscriptionPurchase - */ - protected $response; + /** + * @var \Google_Service_AndroidPublisher_SubscriptionPurchase + */ + protected $response; - /** - * @return bool - */ - public function getAutoRenewing() - { - return (bool)$this->response->getAutoRenewing(); - } + /** + * @return bool + */ + public function getAutoRenewing() + { + return (bool)$this->response->getAutoRenewing(); + } - /** - * @return integer|null - */ - public function getCancelReason() - { - return $this->response->getCancelReason(); - } + /** + * @return integer|null + */ + public function getCancelReason() + { + return $this->response->getCancelReason(); + } - /** - * @return string - */ - public function getCountryCode() - { - return $this->response->getCountryCode(); - } + /** + * @return string + */ + public function getCountryCode() + { + return $this->response->getCountryCode(); + } - /** - * @return integer - */ - public function getPriceAmountMicros() - { - return $this->response->getPriceAmountMicros(); - } + /** + * @return integer + */ + public function getPriceAmountMicros() + { + return $this->response->getPriceAmountMicros(); + } - /** - * @return string - */ - public function getPriceCurrencyCode() - { - return $this->response->getPriceCurrencyCode(); - } + /** + * @return string + */ + public function getPriceCurrencyCode() + { + return $this->response->getPriceCurrencyCode(); + } - /** - * @return string - */ - public function getStartTimeMillis() - { - return $this->response->getStartTimeMillis(); + /** + * @return string + */ + public function getStartTimeMillis() + { + return $this->response->getStartTimeMillis(); } /** @@ -83,13 +83,13 @@ public function getUserCancellationTimeMillis() public function getPaymentState() { return $this->response->getPaymentState(); - } + } - /** - * @return string - */ - public function getExpiresDate() - { - return $this->response->expiryTimeMillis; - } + /** + * @return string + */ + public function getExpiresDate() + { + return $this->response->expiryTimeMillis; + } } From 00121cb7b944e3132d1cd3f7609396a05b300a78 Mon Sep 17 00:00:00 2001 From: "stanislav.kuryan" Date: Wed, 19 Apr 2017 11:56:52 +0300 Subject: [PATCH 7/7] Mark getExpiresDate() method as deprecated --- src/GooglePlay/SubscriptionResponse.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GooglePlay/SubscriptionResponse.php b/src/GooglePlay/SubscriptionResponse.php index 9ebf263..0fa2d28 100644 --- a/src/GooglePlay/SubscriptionResponse.php +++ b/src/GooglePlay/SubscriptionResponse.php @@ -86,6 +86,7 @@ public function getPaymentState() } /** + * @deprecated Use getExpiryTimeMillis() method instead * @return string */ public function getExpiresDate()