1 <?php
2
3 namespace Omnipay\Pagarme\Message;
4
5 use Omnipay\Pagarme\CreditCard;
6 use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
7
8 9 10 11
12 abstract class AbstractRequest extends BaseAbstractRequest
13 {
14 15 16 17 18
19 protected $endpoint = 'https://api.pagar.me/1/';
20
21 22 23 24 25
26 public function getCard()
27 {
28 return $this->getParameter('card');
29 }
30
31 32 33 34 35 36
37 public function setCard($value)
38 {
39 if ($value && !$value instanceof CreditCard) {
40 $value = new CreditCard($value);
41 }
42
43 return $this->setParameter('card', $value);
44 }
45
46 47 48 49 50
51 public function getApiKey()
52 {
53 return $this->getParameter('apiKey');
54 }
55
56 57 58 59 60 61
62 public function setApiKey($value)
63 {
64 return $this->setParameter('apiKey', $value);
65 }
66
67 68 69 70 71
72 public function getCustomer()
73 {
74 return $this->getParameter('customer');
75 }
76
77 78 79 80 81 82
83 public function setCustomer($value)
84 {
85 return $this->setParameter('customer', $value);
86 }
87
88 89 90 91 92
93 public function getCustomerReference()
94 {
95 return $this->getParameter('customerReference');
96 }
97
98 99 100 101 102 103 104 105
106 public function setCustomerReference($value)
107 {
108 return $this->setParameter('customerReference', $value);
109 }
110
111 112 113 114 115
116 public function getCardHash()
117 {
118 return $this->getParameter('card_hash');
119 }
120
121 122 123 124 125 126 127
128 public function setCardHash($value)
129 {
130 return $this->setParameter('card_hash', $value);
131 }
132
133 134 135 136 137
138 public function getMetadata()
139 {
140 return $this->getParameter('metadata');
141 }
142
143 144 145 146 147
148 public function setMetadata($value)
149 {
150 return $this->setParameter('metadata', $value);
151 }
152
153 154 155 156 157 158
159 protected function insertApiKeyToData($data)
160 {
161 $data['api_key'] = $this->getApiKey();
162
163 return $data;
164 }
165
166 167 168 169 170 171 172
173 public function getHttpMethod()
174 {
175 return 'POST';
176 }
177
178 public function sendData($data)
179 {
180
181 $this->httpClient->getEventDispatcher()->addListener(
182 'request.error',
183 function ($event) {
184 if ($event['response']->isClientError()) {
185 $event->stopPropagation();
186 }
187 }
188 );
189
190 $httpRequest = $this->httpClient->createRequest(
191 $this->getHttpMethod(),
192 $this->getEndpoint(),
193 null,
194 $this->insertApiKeyToData($data)
195 );
196 $httpResponse = $httpRequest->send();
197
198 return $this->response = new Response($this, $httpResponse->json());
199 }
200
201 protected function getEndpoint()
202 {
203 return $this->endpoint;
204 }
205
206 protected function createResponse($data)
207 {
208 return $this->response = new Response($this, $data);
209 }
210
211 212 213 214 215 216 217 218 219 220
221 protected function getCardData()
222 {
223 $card = $this->getCard();
224 $data = array();
225
226 $card->validate();
227 $data['object'] = 'card';
228 $data['card_number'] = $card->getNumber();
229 $data['card_expiration_date'] = sprintf('%02d',$card->getExpiryMonth()).(string)$card->getExpiryYear();
230 if ( $card->getCvv() ) {
231 $data['card_cvv'] = $card->getCvv();
232 }
233 $data['card_holder_name'] = $card->getName();
234
235 return $data;
236 }
237
238 239 240 241 242 243 244 245 246
247 protected function getCustomerData()
248 {
249 $card = $this->getCard();
250 $data = array();
251
252 $data['customer']['name'] = $card->getName();
253 $data['customer']['email'] = $card->getEmail();
254 $data['customer']['sex'] = $card->getGender();
255 $data['customer']['born_at'] = $card->getBirthday('m-d-Y');
256 $data['customer']['document_number'] = $card->getHolderDocumentNumber();
257
258 $arrayAddress = $this->extractAddress($card->getAddress1());
259 if ( ! empty($arrayAddress['street']) ) {
260 $data['customer']['address'] = $arrayAddress;
261 $data['customer']['address']['zipcode'] = $card->getPostcode();
262 if ( $card->getAddress2() ) {
263 $data['customer']['address']['neighborhood'] = $card->getAddress2();
264 }
265 }
266
267 $arrayPhone = $this->extractDddPhone($card->getPhone());
268 if ( ! empty($arrayPhone['ddd']) ) {
269 $data['customer']['phone'] = $arrayPhone;
270 }
271
272 return $data;
273 }
274
275 276 277 278 279 280 281 282 283 284
285 protected function extractDddPhone($phoneNumber)
286 {
287 $arrayPhone = array();
288 $phone = preg_replace("/[^0-9]/", "", $phoneNumber);
289 if(substr( $phone, 0, 1 ) === "0"){
290 $arrayPhone['ddd'] = substr($phone, 1, 2);
291 $arrayPhone['number'] = substr($phone, 3);
292 } elseif (strlen($phone) < 10 ) {
293 $arrayPhone['ddd'] = '';
294 $arrayPhone['number'] = $phone;
295 } else {
296 $arrayPhone['ddd'] = substr($phone, 0, 2);
297 $arrayPhone['number'] = substr($phone, 2);
298 }
299
300 return $arrayPhone;
301 }
302
303 304 305 306 307 308 309 310 311 312 313 314 315 316
317 protected function extractAddress($address)
318 {
319 $result = array();
320 $explode = array_map('trim', explode(',', $address));
321
322 $result['street'] = $explode[0];
323 $result['street_number'] = isset($explode[1]) ? $explode[1] : '';
324 $result['complementary'] = isset($explode[2]) ? $explode[2] : '';
325
326 return $result;
327 }
328 }