1 <?php
2 /**
3 * Pagarme Authorize Request
4 */
5
6 namespace Omnipay\Pagarme\Message;
7
8 /**
9 * Pagarme Authorize Request
10 *
11 * An Authorize request is similar to a purchase request but the
12 * charge issues an authorization (or pre-authorization), and no money
13 * is transferred. The transaction will need to be captured later
14 * in order to effect payment. Uncaptured transactions expire in 5 days.
15 *
16 * Either a card object or card_id is required by default. Otherwise,
17 * you must provide a card_hash, like the ones returned by Pagarme.js
18 * or use the boleto's payment method.
19 *
20 * Pagarme gateway supports only two types of "payment_method":
21 *
22 * * credit_card
23 * * boleto
24 *
25 *
26 * Optionally, you can provide the customer details to use the antifraude
27 * feature. These details is passed using the following attributes available
28 * on credit card object:
29 *
30 * * firstName
31 * * lastName
32 * * name
33 * * birthday
34 * * gender
35 * * address1 (must be in the format "street, street_number and neighborhood")
36 * * address2 (used to specify the optional parameter "street_complementary")
37 * * postcode
38 * * phone (must be in the format "DDD PhoneNumber" e.g. "19 98888 5555")
39 * * holder_document_number (CPF or CNPJ)
40 *
41 * Example:
42 *
43 * <code>
44 * // Create a gateway for the Pagarme Gateway
45 * // (routes to GatewayFactory::create)
46 * $gateway = Omnipay::create('Pagarme');
47 *
48 * // Initialise the gateway
49 * $gateway->initialize(array(
50 * 'apiKey' => 'MyApiKey',
51 * ));
52 *
53 * // Create a credit card object
54 * // This card can be used for testing.
55 * $card = new CreditCard(array(
56 * 'firstName' => 'Example',
57 * 'lastName' => 'Customer',
58 * 'number' => '4242424242424242',
59 * 'expiryMonth' => '01',
60 * 'expiryYear' => '2020',
61 * 'cvv' => '123',
62 * 'email' => 'customer@example.com',
63 * 'address1' => 'Street name, Street number, Complementary',
64 * 'address2' => 'Neighborhood',
65 * 'postcode' => '05443100',
66 * 'phone' => '19 3242 8855',
67 * 'holder_document_number' => '214.278.589-40',
68 * ));
69 *
70 * // Do an authorize transaction on the gateway
71 * $transaction = $gateway->authorize(array(
72 * 'amount' => '10.00',
73 * 'soft_descriptor' => 'test',
74 * 'payment_method' => 'credit_card',
75 * 'card' => $card,
76 * 'metadata' => array(
77 * 'product_id' => 'ID1111',
78 * 'invoice_id' => 'IV2222',
79 * ),
80 * ));
81 * $response = $transaction->send();
82 * if ($response->isSuccessful()) {
83 * echo "Authorize transaction was successful!\n";
84 * $sale_id = $response->getTransactionReference();
85 * $customer_id = $response->getCustomerReference();
86 * $card_id = $response->getCardReference();
87 * echo "Transaction reference = " . $sale_id . "\n";
88 * }
89 * </code>
90 *
91 * @see https://docs.pagar.me/capturing-card-data/
92 * @see \Omnipay\Pagarme\Gateway
93 * @see \Omnipay\Pagarme\Message\CaptureRequest
94 * @link https://docs.pagar.me/api/?shell#objeto-transaction
95 */
96 class AuthorizeRequest extends AbstractRequest
97 {
98 /**
99 * Get postback URL.
100 *
101 * @return string
102 */
103 public function getPostbackUrl()
104 {
105 return $this->getParameter('postback_url');
106 }
107
108 /**
109 * Set postback URL.
110 *
111 * @param string $value
112 * @return AuthorizeRequest provides a fluent interface.
113 */
114 public function setPostbackUrl($value)
115 {
116 return $this->setParameter('postback_url', $value);
117 }
118
119 /**
120 * Get installments.
121 *
122 * @return integer the number of installments
123 */
124 public function getInstallments()
125 {
126 return $this->getParameter('installments');
127 }
128
129 /**
130 * Set Installments.
131 *
132 * The number must be between 1 and 12.
133 * If the payment method is boleto defaults to 1.
134 *
135 * @param integer $value
136 * @return AuthorizeRequest provides a fluent interface.
137 */
138 public function setInstallments($value)
139 {
140 return $this->setParameter('installments', (int)$value);
141 }
142
143 /**
144 * Get soft description.
145 *
146 * @return string small description
147 */
148 public function getSoftDescriptor()
149 {
150 return $this->getParameter('soft_descriptor');
151 }
152
153 /**
154 * Set soft description.
155 *
156 * The Pagarme gateway allow 13 characters in the soft_descriptor.
157 * The provided string will be truncated if lengh > 13.
158 *
159 * @param string $value
160 * @return AuthorizeRequest provides a fluent interface.
161 */
162 public function setSoftDescriptor($value)
163 {
164 return $this->setParameter('soft_descriptor', substr($value, 0, 13));
165 }
166
167 /**
168 * Get the boleto expiration date
169 *
170 * @return string boleto expiration date
171 */
172 public function getBoletoExpirationDate($format = 'Y-m-d\TH:i:s')
173 {
174 $value = $this->getParameter('boleto_expiration_date');
175
176 return $value ? $value->format($format) : null;
177 }
178
179 /**
180 * Set the boleto expiration date
181 *
182 * @param string $value defaults to atual date + 7 days
183 * @return AuthorizeRequest provides a fluent interface
184 */
185 public function setBoletoExpirationDate($value)
186 {
187 if ($value) {
188 $value = new \DateTime($value, new \DateTimeZone('UTC'));
189 $value = new \DateTime($value->format('Y-m-d\T03:00:00'), new \DateTimeZone('UTC'));
190 } else {
191 $value = null;
192 }
193
194 return $this->setParameter('boleto_expiration_date', $value);
195 }
196
197 public function getData()
198 {
199 $this->validate('amount');
200
201 $data = array();
202
203 $data['amount'] = $this->getAmountInteger();
204 $data['payment_method'] = $this->getPaymentMethod();
205 $data['postback_url'] = $this->getPostbackUrl();
206 $data['installments'] = $this->getInstallments();
207 $data['soft_descriptor'] = $this->getSoftDescriptor();
208 $data['metadata'] = $this->getMetadata();
209 if ( $this->getPaymentMethod() && ($this->getPaymentMethod() == 'boleto') ) {
210 if ( $this->getBoletoExpirationDate() ) {
211 $data['boleto_expiration_date'] = $this->getBoletoExpirationDate();
212 }
213 $data['payment_method'] = $this->getPaymentMethod();
214 if ( $this->getCard() ) {
215 $data = array_merge($data, $this->getCustomerData());
216 } elseif ($this->getCustomer()) {
217 $this->setCard($this->getCustomer());
218 $data = array_merge($data, $this->getCustomerData());
219 }
220 } else {
221 if ( $this->getCard() ) {
222 $data = array_merge($data, $this->getCardData(), $this->getCustomerData());
223 } elseif ( $this->getCardHash() ) {
224 $data['card_hash'] = $this->getCardHash();
225 } elseif( $this->getCardReference() ) {
226 $data['card_id'] = $this->getCardReference();
227 } else {
228 $this->validate('card');
229 }
230 }
231 $data['capture'] = 'false';
232
233 return $data;
234 }
235
236 public function getEndpoint()
237 {
238 return $this->endpoint.'transactions';
239 }
240 }
241