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