1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Shapin\Stripe\Api; |
11
|
|
|
|
12
|
|
|
use Shapin\Stripe\Configuration; |
13
|
|
|
use Shapin\Stripe\Exception; |
14
|
|
|
use Shapin\Stripe\Model\Product\Product as ProductModel; |
15
|
|
|
use Shapin\Stripe\Model\Product\ProductCollection; |
16
|
|
|
use Symfony\Component\Config\Definition\Processor; |
17
|
|
|
|
18
|
|
|
final class Product extends HttpApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @throws Exception |
22
|
|
|
*/ |
23
|
1 |
|
public function get(string $productId) |
24
|
|
|
{ |
25
|
1 |
|
$response = $this->httpGet("/v1/products/$productId"); |
26
|
|
|
|
27
|
1 |
|
if (200 !== $response->getStatusCode()) { |
28
|
|
|
$this->handleErrors($response); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
return $this->hydrator->hydrate($response, ProductModel::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws Exception |
36
|
|
|
*/ |
37
|
|
|
public function create(array $params) |
38
|
|
|
{ |
39
|
|
|
$processor = new Processor(); |
40
|
|
|
$params = $processor->processConfiguration(new Configuration\ProductCreate(), [$params]); |
41
|
|
|
|
42
|
|
|
$response = $this->httpPost('/v1/products', $params); |
43
|
|
|
|
44
|
|
|
if (200 !== $response->getStatusCode()) { |
45
|
|
|
$this->handleErrors($response); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->hydrator->hydrate($response, ProductModel::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws Exception |
53
|
|
|
*/ |
54
|
|
|
public function update(string $id, array $params) |
55
|
|
|
{ |
56
|
|
|
$processor = new Processor(); |
57
|
|
|
$params = $processor->processConfiguration(new Configuration\ProductUpdate(), [$params]); |
58
|
|
|
|
59
|
|
|
$response = $this->httpPost("/v1/products/$id", $params); |
60
|
|
|
|
61
|
|
|
if (200 !== $response->getStatusCode()) { |
62
|
|
|
$this->handleErrors($response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->hydrator->hydrate($response, ProductModel::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
1 |
|
public function all(array $params = []) |
72
|
|
|
{ |
73
|
1 |
|
$response = $this->httpGet('/v1/products', $params); |
74
|
|
|
|
75
|
1 |
|
if (200 !== $response->getStatusCode()) { |
76
|
|
|
$this->handleErrors($response); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return $this->hydrator->hydrate($response, ProductCollection::class); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|