1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PaymentGateway\PayPalSdk; |
4
|
|
|
|
5
|
|
|
use EasyHttp\GuzzleLayer\GuzzleClient; |
6
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientResponse; |
7
|
|
|
use PaymentGateway\PayPalSdk\Requests\StoreProductRequest; |
8
|
|
|
use PaymentGateway\PayPalSdk\Requests\UpdateProductRequest; |
9
|
|
|
|
10
|
|
|
class PayPalService |
11
|
|
|
{ |
12
|
|
|
protected $client; |
13
|
|
|
protected string $baseUri; |
14
|
|
|
protected string $username; |
15
|
|
|
protected string $password; |
16
|
|
|
protected array $token; |
17
|
|
|
|
18
|
4 |
|
public function __construct(string $baseUri) |
19
|
|
|
{ |
20
|
4 |
|
$this->baseUri = $baseUri; |
21
|
4 |
|
$this->client = new GuzzleClient(); |
22
|
4 |
|
} |
23
|
|
|
|
24
|
4 |
|
public function setAuth(string $username, string $password) |
25
|
|
|
{ |
26
|
4 |
|
$this->username = $username; |
27
|
4 |
|
$this->password = $password; |
28
|
4 |
|
} |
29
|
|
|
|
30
|
4 |
|
public function withHandler(callable $handler) |
31
|
|
|
{ |
32
|
4 |
|
$this->client->withHandler($handler); |
33
|
4 |
|
} |
34
|
|
|
|
35
|
4 |
|
public function getToken(): array |
36
|
|
|
{ |
37
|
4 |
|
if ($this->token ?? null) { |
38
|
|
|
return $this->token; |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
$client = clone $this->client; |
42
|
|
|
|
43
|
4 |
|
$client->prepareRequest('POST', $this->baseUri . '/v1/oauth2/token'); |
44
|
4 |
|
$client->getRequest()->setBasicAuth($this->username, $this->password); |
|
|
|
|
45
|
4 |
|
$client->getRequest()->setQuery(['grant_type' => 'client_credentials']); |
46
|
|
|
|
47
|
4 |
|
$this->token = $client->execute()->parseJson(); |
48
|
|
|
|
49
|
4 |
|
return $this->token; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function createProduct(StoreProductRequest $product): HttpClientResponse |
53
|
|
|
{ |
54
|
1 |
|
$this->client->prepareRequest('POST', $this->baseUri . '/v1/catalogs/products'); |
55
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
56
|
1 |
|
$this->client->getRequest()->setJson($product->toArray()); |
57
|
|
|
|
58
|
1 |
|
return $this->client->execute(); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function getProducts(): HttpClientResponse |
62
|
|
|
{ |
63
|
1 |
|
$this->client->prepareRequest('GET', $this->baseUri . '/v1/catalogs/products'); |
64
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
65
|
|
|
|
66
|
1 |
|
return $this->client->execute(); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function updateProduct(UpdateProductRequest $product): HttpClientResponse |
70
|
|
|
{ |
71
|
1 |
|
$this->client->prepareRequest('PATCH', $this->baseUri . '/v1/catalogs/products/' . $product->getId()); |
72
|
1 |
|
$this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']); |
73
|
1 |
|
$this->client->getRequest()->setJson($product->toArray()); |
74
|
|
|
|
75
|
1 |
|
return $this->client->execute(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|