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