Overview
  • Namespace
  • Class

Namespaces

  • Omnipay
    • Pagarme
      • Message

Classes

  • Omnipay\Pagarme\CreditCard
  • Omnipay\Pagarme\Gateway
  • Omnipay\Pagarme\Message\AbstractRequest
  • Omnipay\Pagarme\Message\AuthorizeRequest
  • Omnipay\Pagarme\Message\CaptureRequest
  • Omnipay\Pagarme\Message\CreateCardRequest
  • Omnipay\Pagarme\Message\CreateCustomerRequest
  • Omnipay\Pagarme\Message\InstallmentsRequest
  • Omnipay\Pagarme\Message\PurchaseRequest
  • Omnipay\Pagarme\Message\RefundRequest
  • Omnipay\Pagarme\Message\Response
  • Omnipay\Pagarme\Message\VoidRequest
  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  * Abstract Request
 10  *
 11  */
 12 abstract class AbstractRequest extends BaseAbstractRequest
 13 {
 14     /**
 15      * Live or Test Endpoint URL
 16      *
 17      * @var string URL
 18      */
 19     protected $endpoint = 'https://api.pagar.me/1/';
 20     
 21     /**
 22      * Get the card.
 23      *
 24      * @return CreditCard
 25      */
 26     public function getCard()
 27     {
 28         return $this->getParameter('card');
 29     }
 30 
 31     /**
 32      * Sets the card.
 33      *
 34      * @param CreditCard $value
 35      * @return AbstractRequest Provides a fluent interface
 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      * Get API key
 48      * 
 49      * @return string API key
 50      */
 51     public function getApiKey()
 52     {
 53         return $this->getParameter('apiKey');
 54     }
 55     
 56     /**
 57      * Set API key
 58      * 
 59      * @param string $value
 60      * @return AbstractRequest provides a fluent interface.
 61      */
 62     public function setApiKey($value)
 63     {
 64         return $this->setParameter('apiKey', $value);
 65     }
 66     
 67     /**
 68      * Get Customer Data
 69      * 
 70      * @return array customer data
 71      */
 72     public function getCustomer()
 73     {
 74         return $this->getParameter('customer');
 75     }
 76     
 77     /**
 78      * Set Customer data
 79      * 
 80      * @param array $value
 81      * @return AbstractRequest provides a fluent interface.
 82      */
 83     public function setCustomer($value)
 84     {
 85         return $this->setParameter('customer', $value);
 86     }
 87     
 88     /**
 89      * Get the customer reference
 90      *
 91      * @return string customer reference
 92      */
 93     public function getCustomerReference()
 94     {
 95         return $this->getParameter('customerReference');
 96     }
 97 
 98     /**
 99      * Set the customer reference
100      *
101      * Used when calling CreateCardRequest on an existing customer. If this
102      * parameter is not set then a new customer is created.
103      *
104      * @return AbstractRequest provides a fluent interface.
105      */
106     public function setCustomerReference($value)
107     {
108         return $this->setParameter('customerReference', $value);
109     }
110     
111     /**
112      * Get the card hash
113      *
114      * @return string card hash
115      */
116     public function getCardHash()
117     {
118         return $this->getParameter('card_hash');
119     }
120 
121     /**
122      * Set the card hash
123      *
124      * Must be a card hash like the ones returned by Pagarme.js.
125      *
126      * @return AbstractRequest provides a fluent interface.
127      */
128     public function setCardHash($value)
129     {
130         return $this->setParameter('card_hash', $value);
131     }
132     
133     /**
134      * Get Metadata
135      * 
136      * @return array metadata
137      */
138     public function getMetadata()
139     {
140         return $this->getParameter('metadata');
141     }
142     
143     /**
144      * 
145      * @param array $value
146      * @return AbstractRequest provides a fluent interface.
147      */
148     public function setMetadata($value)
149     {
150         return $this->setParameter('metadata', $value);
151     }
152     
153     /**
154      * Insert the API key into de array.
155      * 
156      * @param array $data
157      * @return array The data with the API key to be used in all Requests
158      */
159     protected function insertApiKeyToData($data)
160     {
161         $data['api_key'] = $this->getApiKey();
162         
163         return $data;
164     }
165      
166     /**
167      * Get HTTP Method.
168      *
169      * This is nearly always POST but can be over-ridden in sub classes.
170      *
171      * @return string the HTTP method
172      */
173     public function getHttpMethod()
174     {
175         return 'POST';
176     }
177     
178     public function sendData($data)
179     {
180         // don't throw exceptions for 4xx errors
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      * Get the card data.
213      *
214      * Because the pagarme gateway uses a common format for passing
215      * card data to the API, this function can be called to get the
216      * data from the associated card object in the format that the
217      * API requires.
218      *
219      * @return array card data
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      * Get the Customer data.
240      * 
241      * Because the pagarme gateway uses a common format for passing
242      * customer data to the API, this function can be called to get the
243      * data from the card object in the format that the API requires.
244      * 
245      * @return array customer data
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      * Separate DDD from phone numbers in an array 
277      * containing two keys:
278      * 
279      * * ddd
280      * * number
281      * 
282      * @param string $phoneNumber phone number with DDD (byref)
283      * @return array the Phone number and the DDD with two digits
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      * Separate data from the credit card Address in an 
305      * array containing the keys:
306      * * street
307      * * street_number
308      * * complementary
309      * 
310      * It's important to provide the parameter $address
311      * with the information in the given order and separated 
312      * by commas.
313      * 
314      * @param string $address
315      * @return array containing the street, street_number and complementary
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 }
API documentation generated by ApiGen