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