1 | <?php |
||||
2 | |||||
3 | namespace Luigel\Paymongo\Traits; |
||||
4 | |||||
5 | use GuzzleHttp\Client; |
||||
6 | use GuzzleHttp\Exception\ClientException; |
||||
7 | use Illuminate\Database\Eloquent\Model; |
||||
8 | use Luigel\Paymongo\Exceptions\BadRequestException; |
||||
9 | use Luigel\Paymongo\Exceptions\NotFoundException; |
||||
10 | use Luigel\Paymongo\Exceptions\PaymentErrorException; |
||||
11 | use Luigel\Paymongo\Models\PaymentIntent; |
||||
12 | use Luigel\Paymongo\Models\Webhook; |
||||
13 | |||||
14 | trait Request |
||||
15 | { |
||||
16 | protected $data; |
||||
17 | protected $options; |
||||
18 | |||||
19 | /** |
||||
20 | * Request a create to API. |
||||
21 | * |
||||
22 | * @param array $payload |
||||
23 | * @return Model |
||||
24 | */ |
||||
25 | public function create($payload) |
||||
26 | { |
||||
27 | $this->method = 'POST'; |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
28 | $this->payload = $this->convertPayloadAmountsToInteger($payload); |
||||
0 ignored issues
–
show
|
|||||
29 | $this->formRequestData(); |
||||
30 | |||||
31 | $this->setOptions([ |
||||
32 | 'headers' => [ |
||||
33 | 'Accept' => 'application/json', |
||||
34 | 'Content-type' => 'application/json', |
||||
35 | ], |
||||
36 | 'auth' => [config('paymongo.secret_key'), ''], |
||||
37 | 'json' => $this->data, |
||||
38 | ]); |
||||
39 | |||||
40 | return $this->request(); |
||||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * Request a create to API. |
||||
45 | * |
||||
46 | * @param array $payload |
||||
47 | * @return Model |
||||
48 | */ |
||||
49 | public function find($payload) |
||||
50 | { |
||||
51 | $this->method = 'GET'; |
||||
0 ignored issues
–
show
|
|||||
52 | $this->payload = $payload; |
||||
0 ignored issues
–
show
|
|||||
53 | $this->apiUrl = $this->apiUrl.$payload; |
||||
0 ignored issues
–
show
Are you sure
$payload of type array can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
54 | |||||
55 | $this->setOptions([ |
||||
56 | 'headers' => [ |
||||
57 | 'Accept' => 'application/json', |
||||
58 | 'Content-type' => 'application/json', |
||||
59 | ], |
||||
60 | 'auth' => [config('paymongo.secret_key'), ''], |
||||
61 | ]); |
||||
62 | |||||
63 | return $this->request(); |
||||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * Request a get all to API. |
||||
68 | * |
||||
69 | * @return Model |
||||
70 | */ |
||||
71 | public function all() |
||||
72 | { |
||||
73 | $this->method = 'GET'; |
||||
0 ignored issues
–
show
|
|||||
74 | |||||
75 | $this->setOptions([ |
||||
76 | 'headers' => [ |
||||
77 | 'Accept' => 'application/json', |
||||
78 | 'Content-type' => 'application/json', |
||||
79 | ], |
||||
80 | 'auth' => [config('paymongo.secret_key'), ''], |
||||
81 | ]); |
||||
82 | |||||
83 | return $this->request(); |
||||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * Enables the webhook. |
||||
88 | * |
||||
89 | * @param Webhook $webhook |
||||
90 | * @return Model |
||||
91 | */ |
||||
92 | public function enable(Webhook $webhook) |
||||
93 | { |
||||
94 | $this->method = 'POST'; |
||||
0 ignored issues
–
show
|
|||||
95 | $this->apiUrl = $this->apiUrl.$webhook->getId().'/enable'; |
||||
0 ignored issues
–
show
|
|||||
96 | |||||
97 | $this->setOptions([ |
||||
98 | 'headers' => [ |
||||
99 | 'Accept' => 'application/json', |
||||
100 | ], |
||||
101 | 'auth' => [config('paymongo.secret_key'), ''], |
||||
102 | ]); |
||||
103 | |||||
104 | return $this->request(); |
||||
105 | } |
||||
106 | |||||
107 | /** |
||||
108 | * Disables the webhook. |
||||
109 | * |
||||
110 | * @param Webhook $webhook |
||||
111 | * @return Model |
||||
112 | */ |
||||
113 | public function disable(Webhook $webhook) |
||||
114 | { |
||||
115 | $this->method = 'POST'; |
||||
0 ignored issues
–
show
|
|||||
116 | $this->apiUrl = $this->apiUrl.$webhook->getId().'/disable'; |
||||
0 ignored issues
–
show
|
|||||
117 | |||||
118 | $this->setOptions([ |
||||
119 | 'headers' => [ |
||||
120 | 'Accept' => 'application/json', |
||||
121 | ], |
||||
122 | 'auth' => [config('paymongo.secret_key'), ''], |
||||
123 | ]); |
||||
124 | |||||
125 | return $this->request(); |
||||
126 | } |
||||
127 | |||||
128 | /** |
||||
129 | * Updates the webhook. |
||||
130 | * |
||||
131 | * @param Webhook $webhook |
||||
132 | * @param array $payload |
||||
133 | * @return Model |
||||
134 | */ |
||||
135 | public function update(Webhook $webhook, array $payload) |
||||
136 | { |
||||
137 | $this->method = 'PUT'; |
||||
0 ignored issues
–
show
|
|||||
138 | $this->payload = $this->convertPayloadAmountsToInteger($payload); |
||||
0 ignored issues
–
show
|
|||||
139 | $this->apiUrl = $this->apiUrl.$webhook->getId(); |
||||
0 ignored issues
–
show
|
|||||
140 | |||||
141 | $this->formRequestData(); |
||||
142 | $this->setOptions([ |
||||
143 | 'headers' => [ |
||||
144 | 'Accept' => 'application/json', |
||||
145 | ], |
||||
146 | 'auth' => [config('paymongo.secret_key'), ''], |
||||
147 | 'json' => $this->data, |
||||
148 | ]); |
||||
149 | |||||
150 | return $this->request(); |
||||
151 | } |
||||
152 | |||||
153 | /** |
||||
154 | * Cancels the payment intent. |
||||
155 | * |
||||
156 | * @param PaymentIntent $intent |
||||
157 | * @return Model |
||||
158 | */ |
||||
159 | public function cancel(PaymentIntent $intent) |
||||
160 | { |
||||
161 | $this->method = 'POST'; |
||||
0 ignored issues
–
show
|
|||||
162 | $this->apiUrl = $this->apiUrl.$intent->getId().'/cancel'; |
||||
0 ignored issues
–
show
|
|||||
163 | |||||
164 | $this->setOptions([ |
||||
165 | 'headers' => [ |
||||
166 | 'Accept' => 'application/json', |
||||
167 | ], |
||||
168 | 'auth' => [config('paymongo.secret_key'), ''], |
||||
169 | ]); |
||||
170 | |||||
171 | return $this->request(); |
||||
172 | } |
||||
173 | |||||
174 | /** |
||||
175 | * Attach the payment method in the payment intent. |
||||
176 | * |
||||
177 | * @param PaymentIntent $intent |
||||
178 | * @param string $paymentMethodId |
||||
179 | * @return Model |
||||
180 | */ |
||||
181 | public function attach(PaymentIntent $intent, $paymentMethodId) |
||||
182 | { |
||||
183 | $this->method = 'POST'; |
||||
0 ignored issues
–
show
|
|||||
184 | $this->apiUrl = $this->apiUrl.$intent->getId().'/attach'; |
||||
0 ignored issues
–
show
|
|||||
185 | $this->payload = ['payment_method' => $paymentMethodId]; |
||||
0 ignored issues
–
show
|
|||||
186 | |||||
187 | $this->formRequestData(); |
||||
188 | $this->setOptions([ |
||||
189 | 'headers' => [ |
||||
190 | 'Accept' => 'application/json', |
||||
191 | ], |
||||
192 | 'json' => $this->data, |
||||
193 | 'auth' => [config('paymongo.secret_key'), ''], |
||||
194 | ]); |
||||
195 | |||||
196 | return $this->request(); |
||||
197 | } |
||||
198 | |||||
199 | /** |
||||
200 | * Send request to API. |
||||
201 | * |
||||
202 | * @return mixed|Throwable |
||||
0 ignored issues
–
show
|
|||||
203 | */ |
||||
204 | protected function request() |
||||
205 | { |
||||
206 | $client = new Client(); |
||||
207 | |||||
208 | try { |
||||
209 | $response = $client->request($this->method, $this->apiUrl, $this->options); |
||||
210 | |||||
211 | $array = $this->parseToArray((string) $response->getBody()); |
||||
212 | |||||
213 | return $this->setReturnModel($array); |
||||
214 | } catch (ClientException $e) { |
||||
215 | $response = json_decode($e->getResponse()->getBody()->getContents(), true); |
||||
216 | if ($e->getCode() === 400) { |
||||
217 | throw new BadRequestException($response['errors'][0]['detail'], $e->getCode()); |
||||
218 | } elseif ($e->getCode() === 402) { |
||||
219 | throw new PaymentErrorException($response['errors'][0]['detail'], $e->getCode()); |
||||
220 | } elseif ($e->getCode() === 404) { |
||||
221 | throw new NotFoundException($response['errors'][0]['detail'], $e->getCode()); |
||||
222 | } |
||||
223 | } |
||||
224 | } |
||||
225 | |||||
226 | /** |
||||
227 | * Sets the data to add data wrapper of the payload. |
||||
228 | * |
||||
229 | * @return void |
||||
230 | */ |
||||
231 | protected function formRequestData() |
||||
232 | { |
||||
233 | $this->data = [ |
||||
234 | 'data' => [ |
||||
235 | 'attributes' => $this->payload, |
||||
236 | ], |
||||
237 | ]; |
||||
238 | } |
||||
239 | |||||
240 | protected function parseToArray($jsonString) |
||||
241 | { |
||||
242 | return json_decode($jsonString, true); |
||||
243 | } |
||||
244 | |||||
245 | protected function setReturnModel($array) |
||||
246 | { |
||||
247 | return (new $this->returnModel)->setData($array['data']); |
||||
248 | } |
||||
249 | |||||
250 | protected function setOptions($options) |
||||
251 | { |
||||
252 | $this->options = $options; |
||||
253 | } |
||||
254 | |||||
255 | protected function convertPayloadAmountsToInteger($payload) |
||||
256 | { |
||||
257 | if (isset($payload['amount'])) { |
||||
258 | $payload['amount'] *= 100; |
||||
259 | } |
||||
260 | |||||
261 | return $payload; |
||||
262 | } |
||||
263 | } |
||||
264 |