Skip to content

Commit

Permalink
Merge pull request #49 from Stafox/master
Browse files Browse the repository at this point in the history
Fix requirements. Fix tests. Move some methods from abstract class to subscription/purchase classes. Fix getting developer payload for subscription.
  • Loading branch information
aporat authored Apr 19, 2017
2 parents 44eddef + 00121cb commit d570a5c
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 170 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php
php:
- "5.5"
- "5.6"
- "7.0"
- "7.1"
Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
'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.
}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
117 changes: 43 additions & 74 deletions src/GooglePlay/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
62 changes: 50 additions & 12 deletions src/GooglePlay/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
131 changes: 78 additions & 53 deletions src/GooglePlay/SubscriptionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions src/GooglePlay/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading

0 comments on commit d570a5c

Please sign in to comment.