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\Webhook; |
12
|
|
|
|
13
|
|
|
trait Request |
14
|
|
|
{ |
15
|
|
|
protected $data; |
16
|
|
|
protected $options; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Request a create to API |
20
|
|
|
* |
21
|
|
|
* @param array $payload |
22
|
|
|
* @return Model |
23
|
|
|
*/ |
24
|
|
|
public function create($payload) |
25
|
|
|
{ |
26
|
|
|
$this->method = 'POST'; |
|
|
|
|
27
|
|
|
$this->payload = $this->convertPayloadAmountsToInteger($payload); |
|
|
|
|
28
|
|
|
$this->formRequestData(); |
29
|
|
|
|
30
|
|
|
$this->setOptions([ |
31
|
|
|
'headers' => [ |
32
|
|
|
'Accept' => 'application/json', |
33
|
|
|
'Content-type' => 'application/json' |
34
|
|
|
], |
35
|
|
|
'auth' => [config('paymongo.secret_key'), ''], |
36
|
|
|
'json' => $this->data, |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
return $this->request(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Request a create to API |
44
|
|
|
* |
45
|
|
|
* @param array $payload |
46
|
|
|
* @return Model |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function find($payload) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this->method = 'GET'; |
51
|
|
|
$this->payload = $payload; |
52
|
|
|
$this->apiUrl = $this->apiUrl . $payload; |
|
|
|
|
53
|
|
|
|
54
|
|
|
$this->setOptions([ |
55
|
|
|
'headers' => [ |
56
|
|
|
'Accept' => 'application/json', |
57
|
|
|
'Content-type' => 'application/json' |
58
|
|
|
], |
59
|
|
|
'auth' => [config('paymongo.secret_key'), ''], |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
return $this->request(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function all() |
66
|
|
|
{ |
67
|
|
|
$this->method = 'GET'; |
68
|
|
|
|
69
|
|
|
$this->setOptions([ |
70
|
|
|
'headers' => [ |
71
|
|
|
'Accept' => 'application/json', |
72
|
|
|
'Content-type' => 'application/json' |
73
|
|
|
], |
74
|
|
|
'auth' => [config('paymongo.secret_key'), ''], |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
return $this->request(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function enable(Webhook $webhook) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->method = 'POST'; |
83
|
|
|
$this->apiUrl = $this->apiUrl . "$webhook->id/enable"; |
84
|
|
|
|
85
|
|
|
$this->setOptions([ |
86
|
|
|
'headers' => [ |
87
|
|
|
'Accept' => 'application/json', |
88
|
|
|
], |
89
|
|
|
'auth' => [config('paymongo.secret_key'), ''], |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
return $this->request(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function disable(Webhook $webhook) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$this->method = 'POST'; |
98
|
|
|
$this->apiUrl = $this->apiUrl . "$webhook->id/disable"; |
99
|
|
|
|
100
|
|
|
$this->setOptions([ |
101
|
|
|
'headers' => [ |
102
|
|
|
'Accept' => 'application/json', |
103
|
|
|
], |
104
|
|
|
'auth' => [config('paymongo.secret_key'), ''], |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
return $this->request(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function update(Webhook $webhook, $payload) |
111
|
|
|
{ |
112
|
|
|
$this->method = 'PUT'; |
113
|
|
|
$this->payload = $this->convertPayloadAmountsToInteger($payload); |
114
|
|
|
$this->apiUrl = $this->apiUrl . $webhook->id; |
115
|
|
|
|
116
|
|
|
$this->formRequestData(); |
117
|
|
|
$this->setOptions([ |
118
|
|
|
'headers' => [ |
119
|
|
|
'Accept' => 'application/json', |
120
|
|
|
], |
121
|
|
|
'auth' => [config('paymongo.secret_key'), ''], |
122
|
|
|
'json' => $this->data, |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
return $this->request(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Send request to API |
130
|
|
|
* |
131
|
|
|
* @return mixed|Throwable |
132
|
|
|
*/ |
133
|
|
|
protected function request() |
134
|
|
|
{ |
135
|
|
|
$client = new Client(); |
136
|
|
|
|
137
|
|
|
try |
138
|
|
|
{ |
139
|
|
|
$response = $client->request($this->method, $this->apiUrl, $this->options); |
140
|
|
|
|
141
|
|
|
$array = $this->parseToArray((string) $response->getBody()); |
142
|
|
|
return $this->setReturnModel($array); |
143
|
|
|
} |
144
|
|
|
catch (ClientException $e) |
145
|
|
|
{ |
146
|
|
|
$response = json_decode($e->getResponse()->getBody()->getContents(), true); |
147
|
|
|
if ($e->getCode() === 400) |
148
|
|
|
{ |
149
|
|
|
throw new BadRequestException($response['errors'][0]['detail']); |
150
|
|
|
} |
151
|
|
|
else if ($e->getCode() === 402) |
152
|
|
|
{ |
153
|
|
|
throw new PaymentErrorException($response['errors'][0]['detail']); |
154
|
|
|
} |
155
|
|
|
else if ($e->getCode() === 404) |
156
|
|
|
{ |
157
|
|
|
throw new NotFoundException($response['errors'][0]['detail']); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
protected function formRequestData() |
166
|
|
|
{ |
167
|
|
|
$this->data = [ |
168
|
|
|
'data' => [ |
169
|
|
|
'attributes' => $this->payload |
170
|
|
|
] |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function parseToArray($jsonString) |
175
|
|
|
{ |
176
|
|
|
return json_decode($jsonString, true); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function setReturnModel($array) |
180
|
|
|
{ |
181
|
|
|
return (new $this->returnModel)->setData($array['data']); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function setOptions($options) |
185
|
|
|
{ |
186
|
|
|
$this->options = $options; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function convertPayloadAmountsToInteger($payload) |
190
|
|
|
{ |
191
|
|
|
if (isset($payload['amount'])) |
192
|
|
|
{ |
193
|
|
|
$payload['amount'] *= 100; |
194
|
|
|
} |
195
|
|
|
return $payload; |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: