|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PaymentGateway\PayPalSdk; |
|
4
|
|
|
|
|
5
|
|
|
use EasyHttp\GuzzleLayer\GuzzleClient; |
|
6
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientResponse; |
|
7
|
|
|
use PaymentGateway\PayPalSdk\Requests\StorePlanRequest; |
|
8
|
|
|
use PaymentGateway\PayPalSdk\Requests\StoreProductRequest; |
|
9
|
|
|
use PaymentGateway\PayPalSdk\Requests\UpdatePlanRequest; |
|
10
|
|
|
use PaymentGateway\PayPalSdk\Requests\UpdateProductRequest; |
|
11
|
|
|
|
|
12
|
|
|
class PayPalService |
|
13
|
|
|
{ |
|
14
|
|
|
protected $client; |
|
15
|
|
|
protected string $baseUri; |
|
16
|
|
|
protected string $username; |
|
17
|
|
|
protected string $password; |
|
18
|
|
|
protected array $token; |
|
19
|
|
|
|
|
20
|
9 |
|
public function __construct(string $baseUri) |
|
21
|
|
|
{ |
|
22
|
9 |
|
$this->baseUri = $baseUri; |
|
23
|
9 |
|
$this->client = new GuzzleClient(); |
|
24
|
9 |
|
} |
|
25
|
|
|
|
|
26
|
9 |
|
public function setAuth(string $username, string $password) |
|
27
|
|
|
{ |
|
28
|
9 |
|
$this->username = $username; |
|
29
|
9 |
|
$this->password = $password; |
|
30
|
9 |
|
} |
|
31
|
|
|
|
|
32
|
9 |
|
public function withHandler(callable $handler) |
|
33
|
|
|
{ |
|
34
|
9 |
|
$this->client->withHandler($handler); |
|
35
|
9 |
|
} |
|
36
|
|
|
|
|
37
|
9 |
|
public function getToken(): array |
|
38
|
|
|
{ |
|
39
|
9 |
|
if ($this->token ?? null) { |
|
40
|
7 |
|
return $this->token; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
9 |
|
$client = clone $this->client; |
|
44
|
|
|
|
|
45
|
9 |
|
$client->prepareRequest('POST', $this->baseUri . '/v1/oauth2/token'); |
|
46
|
9 |
|
$client->getRequest()->setBasicAuth($this->username, $this->password); |
|
|
|
|
|
|
47
|
9 |
|
$client->getRequest()->setQuery(['grant_type' => 'client_credentials']); |
|
48
|
|
|
|
|
49
|
9 |
|
$this->token = $client->execute()->parseJson(); |
|
50
|
|
|
|
|
51
|
9 |
|
return $this->token; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
8 |
|
public function createProduct(StoreProductRequest $product): HttpClientResponse |
|
55
|
|
|
{ |
|
56
|
8 |
|
$this->client->prepareRequest('POST', $this->baseUri . '/v1/catalogs/products'); |
|
57
|
8 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
|
58
|
8 |
|
$this->client->getRequest()->setJson($product->toArray()); |
|
59
|
|
|
|
|
60
|
8 |
|
return $this->client->execute(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function getProducts(): HttpClientResponse |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->client->prepareRequest('GET', $this->baseUri . '/v1/catalogs/products'); |
|
66
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
|
67
|
|
|
|
|
68
|
1 |
|
return $this->client->execute(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function getProduct(string $id): HttpClientResponse |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->client->prepareRequest('GET', $this->baseUri . '/v1/catalogs/products/' . $id); |
|
74
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
|
75
|
|
|
|
|
76
|
1 |
|
return $this->client->execute(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function updateProduct(UpdateProductRequest $productRequest): HttpClientResponse |
|
80
|
|
|
{ |
|
81
|
1 |
|
$this->client->prepareRequest('PATCH', $this->baseUri . '/v1/catalogs/products/' . $productRequest->getId()); |
|
82
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
|
83
|
1 |
|
$this->client->getRequest()->setJson($productRequest->toArray()); |
|
84
|
|
|
|
|
85
|
1 |
|
return $this->client->execute(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
public function createPlan(StorePlanRequest $storePlanRequest): HttpClientResponse |
|
89
|
|
|
{ |
|
90
|
4 |
|
$this->client->prepareRequest('POST', $this->baseUri . '/v1/billing/plans'); |
|
91
|
4 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
|
92
|
4 |
|
$this->client->getRequest()->setJson($storePlanRequest->toArray()); |
|
93
|
|
|
|
|
94
|
4 |
|
return $this->client->execute(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
public function getPlans(): HttpClientResponse |
|
98
|
|
|
{ |
|
99
|
1 |
|
$this->client->prepareRequest('GET', $this->baseUri . '/v1/billing/plans'); |
|
100
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
|
101
|
|
|
|
|
102
|
1 |
|
return $this->client->execute(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
public function getPlan(string $id): HttpClientResponse |
|
106
|
|
|
{ |
|
107
|
1 |
|
$this->client->prepareRequest('GET', $this->baseUri . '/v1/billing/plans/' . $id); |
|
108
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
|
109
|
|
|
|
|
110
|
1 |
|
return $this->client->execute(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
public function updatePlan(UpdatePlanRequest $planRequest): HttpClientResponse |
|
114
|
|
|
{ |
|
115
|
1 |
|
$this->client->prepareRequest('PATCH', $this->baseUri . '/v1/billing/plans/' . $planRequest->getId()); |
|
116
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
|
117
|
1 |
|
$this->client->getRequest()->setJson($planRequest->toArray()); |
|
118
|
|
|
|
|
119
|
1 |
|
return $this->client->execute(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|