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\Subscription\Item; |
15
|
|
|
use Shapin\Stripe\Model\Subscription\Subscription as SubscriptionModel; |
16
|
|
|
use Shapin\Stripe\Model\Subscription\SubscriptionCollection; |
17
|
|
|
use Symfony\Component\Config\Definition\Processor; |
18
|
|
|
|
19
|
|
|
final class Subscription extends HttpApi |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @throws Exception |
23
|
|
|
*/ |
24
|
1 |
|
public function get(string $id) |
25
|
|
|
{ |
26
|
1 |
|
$response = $this->httpGet("/v1/subscriptions/$id"); |
27
|
|
|
|
28
|
1 |
|
if (200 !== $response->getStatusCode()) { |
29
|
|
|
$this->handleErrors($response); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
return $this->hydrator->hydrate($response, SubscriptionModel::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
1 |
|
public function all(array $params = []) |
39
|
|
|
{ |
40
|
1 |
|
$response = $this->httpGet('/v1/subscriptions', $params); |
41
|
|
|
|
42
|
1 |
|
if (200 !== $response->getStatusCode()) { |
43
|
|
|
$this->handleErrors($response); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return $this->hydrator->hydrate($response, SubscriptionCollection::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
|
|
public function create(array $params) |
53
|
|
|
{ |
54
|
|
|
$processor = new Processor(); |
55
|
|
|
$params = $processor->processConfiguration(new Configuration\SubscriptionCreate(), [$params]); |
56
|
|
|
|
57
|
|
|
$response = $this->httpPost('/v1/subscriptions', $params); |
58
|
|
|
|
59
|
|
|
if (200 !== $response->getStatusCode()) { |
60
|
|
|
$this->handleErrors($response); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->hydrator->hydrate($response, SubscriptionModel::class); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws Exception |
68
|
|
|
*/ |
69
|
|
|
public function cancel(string $id, array $params = []) |
70
|
|
|
{ |
71
|
|
|
$processor = new Processor(); |
72
|
|
|
$params = $processor->processConfiguration(new Configuration\SubscriptionCancel(), [$params]); |
73
|
|
|
|
74
|
|
|
$response = $this->httpDelete("/v1/subscriptions/$id", $params); |
75
|
|
|
|
76
|
|
|
if (200 !== $response->getStatusCode()) { |
77
|
|
|
$this->handleErrors($response); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->hydrator->hydrate($response, SubscriptionModel::class); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @throws Exception |
85
|
|
|
*/ |
86
|
1 |
|
public function getItem(string $id) |
87
|
|
|
{ |
88
|
1 |
|
$response = $this->httpGet("/v1/subscription_items/$id"); |
89
|
|
|
|
90
|
1 |
|
if (200 !== $response->getStatusCode()) { |
91
|
|
|
$this->handleErrors($response); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
return $this->hydrator->hydrate($response, Item::class); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws Exception |
99
|
|
|
*/ |
100
|
|
|
public function update(string $id, array $params) |
101
|
|
|
{ |
102
|
|
|
$processor = new Processor(); |
103
|
|
|
$params = $processor->processConfiguration(new Configuration\SubscriptionUpdate(), [$params]); |
104
|
|
|
|
105
|
|
|
$response = $this->httpPost("/v1/subscriptions/$id", $params); |
106
|
|
|
|
107
|
|
|
if (200 !== $response->getStatusCode()) { |
108
|
|
|
$this->handleErrors($response); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->hydrator->hydrate($response, SubscriptionModel::class); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|