1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2015 Alexey Maslov <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace alxmsl\Google\AndroidPublisher\InAppProducts; |
19
|
|
|
|
20
|
|
|
use alxmsl\Google\AndroidPublisher\Exception\ErrorException; |
21
|
|
|
use alxmsl\Google\AndroidPublisher\Exception\InvalidCredentialsException; |
22
|
|
|
use alxmsl\Google\OAuth2\WebServerApplication; |
23
|
|
|
use alxmsl\Network\Exception\HttpClientErrorCodeException; |
24
|
|
|
use alxmsl\Network\Exception\HttpServerErrorCodeException; |
25
|
|
|
use UnexpectedValueException; |
26
|
|
|
use RuntimeException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class for support GooglePlay InAppProducts API |
30
|
|
|
* @author alxmsl |
31
|
|
|
*/ |
32
|
|
|
final class Client extends WebServerApplication { |
33
|
|
|
/** |
34
|
|
|
* GooglePlay Purchases Products API endpoint |
35
|
|
|
*/ |
36
|
|
|
const ENDPOINT_PURCHASES = 'https://www.googleapis.com/androidpublisher/v2'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string package name |
40
|
|
|
*/ |
41
|
|
|
private $package = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $package package name |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setPackage($package) { |
48
|
|
|
$this->package = (string) $package; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string package name |
54
|
|
|
*/ |
55
|
1 |
|
public function getPackage() { |
56
|
1 |
|
return $this->package; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns information about the in-app product specified |
61
|
|
|
* @param string $productId product identifier |
62
|
|
|
* @return Resource product purchase resource instance |
63
|
|
|
* @throws ErrorException when API error acquired |
64
|
|
|
* @throws RuntimeException when type code for client is not supported now |
65
|
|
|
*/ |
66
|
1 |
|
public function get($productId) { |
67
|
1 |
|
$accessToken = $this->getAccessToken(); |
68
|
1 |
|
if (!empty($accessToken)) { |
69
|
|
|
$Request = $this->getRequest(self::ENDPOINT_PURCHASES) |
70
|
|
|
->addUrlField('applications', $this->getPackage()) |
71
|
|
|
->addUrlField('inappproducts', $productId) |
72
|
|
|
->addGetField('access_token', $accessToken); |
73
|
|
|
try { |
74
|
|
|
return Resource::initializeByString($Request->send()); |
75
|
|
|
} catch (HttpClientErrorCodeException $Ex) { |
76
|
|
|
switch ($Ex->getCode()) { |
77
|
|
|
case 401: |
78
|
|
|
throw InvalidCredentialsException::initializeByString($Ex->getMessage()); |
79
|
|
|
default: |
80
|
|
|
throw ErrorException::initializeByString($Ex->getMessage()); |
81
|
|
|
} |
82
|
|
|
} catch (HttpServerErrorCodeException $Ex) { |
83
|
|
|
throw ErrorException::initializeByString($Ex->getMessage()); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
1 |
|
throw new UnexpectedValueException('access token is empty'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|