-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from martinbutt/master
@pasxel's service accounts patch with readme fix
- Loading branch information
Showing
5 changed files
with
216 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,29 @@ try { | |
// success | ||
``` | ||
|
||
Or [Using a service account](https://developers.google.com/android-publisher/getting_started#using_a_service_account) | ||
|
||
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' => file_get_contents('MyProject.p12'), | ||
]); | ||
|
||
try { | ||
$response = $validator->setPackageName('PACKAGE_NAME') | ||
->setProductId('PRODUCT_ID') | ||
->setPurchaseToken('PURCHASE_TOKEN') | ||
->validate(); | ||
} catch (Exception $e){ | ||
var_dump($e->getMessage()); | ||
// example message: Error calling GET ....: (404) Product not found for this application. | ||
} | ||
// success | ||
``` | ||
|
||
|
||
### Amazon App Store ### | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
error_reporting(E_ALL | E_STRICT); | ||
ini_set('display_errors', 1); | ||
|
||
$root = realpath(dirname(dirname(__FILE__))); | ||
$library = "$root/library"; | ||
|
||
$path = array($library, get_include_path()); | ||
set_include_path(implode(PATH_SEPARATOR, $path)); | ||
|
||
require_once $root . '/vendor/autoload.php'; | ||
|
||
use ReceiptValidator\GooglePlay\ServiceAccountValidator as PlayValidator; | ||
|
||
// google authencation | ||
$client_email = '[email protected]'; | ||
$p12_key_path = 'MyProject.p12'; | ||
|
||
// receipt data | ||
$package_name = 'com.example'; | ||
$product_id = 'coins_10000'; | ||
$purchase_token = 'xxxxxx'; | ||
|
||
$validator = new PlayValidator(['client_email' => $client_email, 'p12_key_path' => $p12_key_path]); | ||
|
||
try { | ||
$response = $validator->setPackageName($package_name)->setProductId($product_id)->setPurchaseToken($purchase_token)->validate(); | ||
} catch (Exception $e) { | ||
echo 'got error = ' . $e->getMessage() . PHP_EOL; | ||
} | ||
|
||
print_R($response); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
namespace ReceiptValidator\GooglePlay; | ||
|
||
abstract class AbstractValidator | ||
{ | ||
const TYPE_PURCHASE = 1; | ||
const TYPE_SUBSCRIPTION = 2; | ||
|
||
/** | ||
* google client | ||
* | ||
* @var \Google_Client | ||
*/ | ||
protected $_client = null; | ||
|
||
/** | ||
* @var \Google_Service_AndroidPublisher | ||
*/ | ||
protected $_androidPublisherService = null; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $_package_name = null; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $_purchase_token = null; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $_purchase_type = self::TYPE_PURCHASE; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $_product_id = null; | ||
|
||
public function __construct($options = []) | ||
{ | ||
$this->initClient($options); | ||
$this->_androidPublisherService = new \Google_Service_AndroidPublisher($this->_client); | ||
} | ||
|
||
abstract protected function initClient($options = []); | ||
|
||
/** | ||
* | ||
* @param string $package_name | ||
* @return \ReceiptValidator\GooglePlay\Validator | ||
*/ | ||
public function setPackageName($package_name) | ||
{ | ||
$this->_package_name = $package_name; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $purchase_token | ||
* @return \ReceiptValidator\GooglePlay\Validator | ||
*/ | ||
public function setPurchaseToken($purchase_token) | ||
{ | ||
$this->_purchase_token = $purchase_token; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* @param int $purchase_type | ||
* @return \ReceiptValidator\GooglePlay\Validator | ||
*/ | ||
public function setPurchaseType($purchase_type) | ||
{ | ||
$this->_purchase_type = $purchase_type; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $product_id | ||
* @return \ReceiptValidator\GooglePlay\Validator | ||
*/ | ||
public function setProductId($product_id) | ||
{ | ||
$this->_product_id = $product_id; | ||
|
||
return $this; | ||
} | ||
|
||
public function validate() | ||
{ | ||
switch ($this->_purchase_type) { | ||
case self::TYPE_SUBSCRIPTION: | ||
$request = $this->_androidPublisherService->purchases_subscriptions; | ||
break; | ||
default: | ||
$request = $this->_androidPublisherService->purchases_products; | ||
} | ||
|
||
$response = $request->get( | ||
$this->_package_name, $this->_product_id, $this->_purchase_token | ||
); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace ReceiptValidator\GooglePlay; | ||
|
||
class ServiceAccountValidator extends AbstractValidator | ||
{ | ||
protected function initClient($options = []) | ||
{ | ||
$credentials = new \Google_Auth_AssertionCredentials( | ||
$options['client_email'], | ||
[\Google_Service_AndroidPublisher::ANDROIDPUBLISHER], | ||
$options['p12_key_path'] | ||
); | ||
$this->_client = new \Google_Client(); | ||
$this->_client->setAssertionCredentials($credentials); | ||
if ($this->_client->getAuth()->isAccessTokenExpired()) { | ||
$this->_client->getAuth()->refreshTokenWithAssertion(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters