Passed
Branch master (461ffe)
by João Felipe Magro
05:37 queued 02:32
created

testSerializePaymentWithEmptyOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Tests\Classes\Services;
4
5
use Ipag\Classes\Authentication;
6
use Ipag\Classes\Enum\Method;
7
use Ipag\Classes\Serializer\PaymentSerializer;
8
use Ipag\Ipag;
9
use PHPUnit\Framework\TestCase;
10
11
class PaymentSerializerTest extends TestCase
12
{
13
    private $ipag;
14
    private $customer;
15
16
    public function setUp()
17
    {
18
        parent::setUp();
19
20
        $authentication = new Authentication('[email protected]');
21
22
        $this->ipag = new Ipag($authentication);
23
24
        $this->customer = $this->ipag->customer()
25
            ->setName('Fulano da Silva')
26
            ->setTaxpayerId('799.993.388-01')
27
            ->setPhone('11', '98888-3333')
28
            ->setEmail('[email protected]')
29
            ->setAddress($this->ipag->address()
30
                    ->setStreet('Rua Júlio Gonzalez')
31
                    ->setNumber('1000')
32
                    ->setNeighborhood('Barra Funda')
33
                    ->setCity('São Paulo')
34
                    ->setState('SP')
35
                    ->setZipCode('01156-060')
36
            );
37
    }
38
39
    public function testSerializeWithFullCreditCard()
40
    {
41
        $this->ipag->getAuthentication()->setIdentification2('[email protected]');
42
43
        $transaction = $this->ipag->transaction();
44
        $transaction->getOrder()
45
            ->setOrderId('10000')
46
            ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
47
            ->setAmount(10.00)
48
            ->setInstallments(1)
49
            ->setPayment($this->ipag->payment()
50
                    ->setMethod(Method::VISA)
51
                    ->setCreditCard($this->ipag->creditCard()
52
                            ->setNumber('4066553613548107')
53
                            ->setHolder('FULANO')
54
                            ->setExpiryMonth('10')
55
                            ->setExpiryYear('2025')
56
                            ->setCvc('123')
57
                            ->setSave(true)
58
                    )->setInstructions('Instrução 1')
59
                    ->setInstructions('Instrução 2')
60
                    ->setInstructions('Instrução 3')
61
                    ->setSoftDescriptor('EMPRESA')
62
            )->setCustomer($this->customer)
63
            ->setCart($this->ipag->cart()
64
                    ->addProduct($this->ipag->product()
65
                            ->setName('Produto 1')
66
                            ->setQuantity(2)
67
                            ->setUnitPrice(1.00)
68
                            ->setSku('G9F07GSD96FA8')
69
                    )
70
            );
71
72
        $paymentSerializer = new PaymentSerializer($transaction);
73
74
        $response = $paymentSerializer->serialize();
75
76
        $expected = [
77
            'identificacao'     => 'app%40test.com',
78
            'identificacao2'    => 'partner%40test.com',
79
            'url_retorno'       => 'https%3A%2F%2Fminha_loja.com.br%2Fipag%2Fcallback',
80
            'retorno_tipo'      => 'xml',
81
            'boleto_tipo'       => 'xml',
82
            'pedido'            => '10000',
83
            'operacao'          => '',
84
            'valor'             => '10',
85
            'parcelas'          => '1',
86
            'vencto'            => '',
87
            'stelo_fingerprint' => '',
88
            'metodo'            => 'visa',
89
            'num_cartao'        => '4066553613548107',
90
            'nome_cartao'       => 'FULANO',
91
            'mes_cartao'        => '10',
92
            'ano_cartao'        => '2025',
93
            'cvv_cartao'        => '123',
94
            'gera_token_cartao' => '1',
95
            'nome'              => 'Fulano+da+Silva',
96
            'email'             => 'fulanodasilva%40gmail.com',
97
            'doc'               => '79999338801',
98
            'fone'              => '11988883333',
99
            'endereco'          => 'Rua+J%C3%BAlio+Gonzalez',
100
            'numero_endereco'   => '1000',
101
            'complemento'       => '',
102
            'bairro'            => 'Barra+Funda',
103
            'cidade'            => 'S%C3%A3o+Paulo',
104
            'estado'            => 'SP',
105
            'pais'              => 'BR',
106
            'cep'               => '01156060',
107
            'instrucoes[0]'     => 'Instru%C3%A7%C3%A3o+1',
108
            'instrucoes[1]'     => 'Instru%C3%A7%C3%A3o+2',
109
            'instrucoes[2]'     => 'Instru%C3%A7%C3%A3o+3',
110
            'softdescriptor'    => 'EMPRESA',
111
            'descricao_pedido'  => '%7B%221%22%3A%7B%22descr%22%3A%22Produto+1%22%2C%22valor%22%3A1%2C%22quant%22%3A2%2C%22id%22%3A%22G9F07GSD96FA8%22%7D%7D',
112
        ];
113
114
        $this->assertEquals($expected, $response);
115
    }
116
117
    public function testSerializeWithCreditCardToken()
118
    {
119
        $transaction = $this->ipag->transaction();
120
        $transaction->getOrder()
121
            ->setOrderId('10000')
122
            ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
123
            ->setAmount(10.00)
124
            ->setInstallments(1)
125
            ->setPayment($this->ipag->payment()
126
                    ->setMethod(Method::VISA)
127
                    ->setCreditCard($this->ipag->creditCard()
128
                            ->setToken('123456789')
129
                    )
130
            )->setCustomer($this->customer);
131
132
        $paymentSerializer = new PaymentSerializer($transaction);
133
134
        $response = $paymentSerializer->serialize();
135
136
        $expected = [
137
            'identificacao'     => 'app%40test.com',
138
            'url_retorno'       => 'https%3A%2F%2Fminha_loja.com.br%2Fipag%2Fcallback',
139
            'retorno_tipo'      => 'xml',
140
            'boleto_tipo'       => 'xml',
141
            'pedido'            => '10000',
142
            'operacao'          => '',
143
            'valor'             => '10',
144
            'parcelas'          => '1',
145
            'vencto'            => '',
146
            'stelo_fingerprint' => '',
147
            'metodo'            => 'visa',
148
            'token_cartao'      => '123456789',
149
            'nome'              => 'Fulano+da+Silva',
150
            'email'             => 'fulanodasilva%40gmail.com',
151
            'doc'               => '79999338801',
152
            'fone'              => '11988883333',
153
            'endereco'          => 'Rua+J%C3%BAlio+Gonzalez',
154
            'numero_endereco'   => '1000',
155
            'complemento'       => '',
156
            'bairro'            => 'Barra+Funda',
157
            'cidade'            => 'S%C3%A3o+Paulo',
158
            'estado'            => 'SP',
159
            'pais'              => 'BR',
160
            'cep'               => '01156060',
161
        ];
162
163
        $this->assertEquals($expected, $response);
164
    }
165
166
    public function testSerializeWithoutCustomer()
167
    {
168
        $transaction = $this->ipag->transaction();
169
        $transaction->getOrder()
170
            ->setOrderId('10000')
171
            ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
172
            ->setAmount(10.00)
173
            ->setInstallments(1)
174
            ->setPayment($this->ipag->payment()
175
                    ->setMethod(Method::VISA)
176
                    ->setCreditCard($this->ipag->creditCard()
177
                            ->setToken('123456789')
178
                    )
179
            );
180
181
        $paymentSerializer = new PaymentSerializer($transaction);
182
183
        $response = $paymentSerializer->serialize();
184
185
        $expected = [
186
            'identificacao'     => 'app%40test.com',
187
            'url_retorno'       => 'https%3A%2F%2Fminha_loja.com.br%2Fipag%2Fcallback',
188
            'retorno_tipo'      => 'xml',
189
            'boleto_tipo'       => 'xml',
190
            'pedido'            => '10000',
191
            'operacao'          => '',
192
            'valor'             => '10',
193
            'parcelas'          => '1',
194
            'vencto'            => '',
195
            'stelo_fingerprint' => '',
196
            'metodo'            => 'visa',
197
            'token_cartao'      => '123456789',
198
        ];
199
200
        $this->assertEquals($expected, $response);
201
    }
202
203
    public function testSerializeWithoutCustomerAddress()
204
    {
205
        $transaction = $this->ipag->transaction();
206
        $transaction->getOrder()
207
            ->setOrderId('10000')
208
            ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
209
            ->setAmount(10.00)
210
            ->setInstallments(1)
211
            ->setPayment($this->ipag->payment()
212
                    ->setMethod(Method::VISA)
213
                    ->setCreditCard($this->ipag->creditCard()
214
                            ->setToken('123456789')
215
                    )
216
            )->setCustomer($this->ipag->customer()
217
                ->setName('Fulano da Silva')
218
                ->setTaxpayerId('799.993.388-01')
219
                ->setPhone('11', '98888-3333')
220
                ->setEmail('[email protected]'));
221
222
        $paymentSerializer = new PaymentSerializer($transaction);
223
224
        $response = $paymentSerializer->serialize();
225
226
        $expected = [
227
            'identificacao'     => 'app%40test.com',
228
            'url_retorno'       => 'https%3A%2F%2Fminha_loja.com.br%2Fipag%2Fcallback',
229
            'retorno_tipo'      => 'xml',
230
            'boleto_tipo'       => 'xml',
231
            'pedido'            => '10000',
232
            'operacao'          => '',
233
            'valor'             => '10',
234
            'parcelas'          => '1',
235
            'vencto'            => '',
236
            'stelo_fingerprint' => '',
237
            'metodo'            => 'visa',
238
            'token_cartao'      => '123456789',
239
            'nome'              => 'Fulano+da+Silva',
240
            'email'             => 'fulanodasilva%40gmail.com',
241
            'doc'               => '79999338801',
242
            'fone'              => '11988883333',
243
        ];
244
245
        $this->assertEquals($expected, $response);
246
    }
247
248
    public function testSerializeWithoutOrderShouldThrowException()
249
    {
250
        $this->expectException(\Exception::class);
251
252
        $paymentSerializer = new PaymentSerializer($this->ipag->transaction());
253
254
        $paymentSerializer->serialize();
255
    }
256
257
    public function testSerializeWithoutPaymentShouldThrowException()
258
    {
259
        $this->expectException(\Exception::class);
260
261
        $transaction = $this->ipag->transaction();
262
        $transaction->getOrder()
263
            ->setOrderId('10000')
264
            ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
265
            ->setAmount(10.00)
266
            ->setInstallments(1);
267
268
        $paymentSerializer = new PaymentSerializer($transaction);
269
270
        $paymentSerializer->serialize();
271
    }
272
273
    public function testSerializeBoletoPayment()
274
    {
275
        $transaction = $this->ipag->transaction();
276
        $transaction->getOrder()
277
            ->setOrderId('10000')
278
            ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
279
            ->setAmount(10.00)
280
            ->setInstallments(1)
281
            ->setPayment($this->ipag->payment()
282
                    ->setMethod(Method::BANKSLIP_ITAU)
283
            )->setCustomer($this->customer);
284
285
        $paymentSerializer = new PaymentSerializer($transaction);
286
287
        $response = $paymentSerializer->serialize();
288
289
        $expected = [
290
            'identificacao'     => 'app%40test.com',
291
            'url_retorno'       => 'https%3A%2F%2Fminha_loja.com.br%2Fipag%2Fcallback',
292
            'retorno_tipo'      => 'xml',
293
            'boleto_tipo'       => 'xml',
294
            'pedido'            => '10000',
295
            'operacao'          => '',
296
            'valor'             => '10',
297
            'parcelas'          => '1',
298
            'vencto'            => '',
299
            'stelo_fingerprint' => '',
300
            'metodo'            => 'boleto_itau',
301
            'nome'              => 'Fulano+da+Silva',
302
            'email'             => 'fulanodasilva%40gmail.com',
303
            'doc'               => '79999338801',
304
            'fone'              => '11988883333',
305
            'endereco'          => 'Rua+J%C3%BAlio+Gonzalez',
306
            'numero_endereco'   => '1000',
307
            'complemento'       => '',
308
            'bairro'            => 'Barra+Funda',
309
            'cidade'            => 'S%C3%A3o+Paulo',
310
            'estado'            => 'SP',
311
            'pais'              => 'BR',
312
            'cep'               => '01156060',
313
        ];
314
315
        $this->assertEquals($expected, $response);
316
    }
317
318
    public function testSerializeWithSubscription()
319
    {
320
        $transaction = $this->ipag->transaction();
321
        $transaction->getOrder()
322
            ->setOrderId('10000')
323
            ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
324
            ->setAmount(10.00)
325
            ->setInstallments(1)
326
            ->setPayment($this->ipag->payment()
327
                    ->setMethod(Method::VISA)
328
                    ->setCreditCard($this->ipag->creditCard()
329
                            ->setToken('123456789')
330
                    )
331
            )->setCustomer($this->customer)
332
            ->setSubscription($this->ipag->subscription()
333
                    ->setFrequency(1)
334
                    ->setInterval('month')
335
            );
336
337
        $paymentSerializer = new PaymentSerializer($transaction);
338
339
        $response = $paymentSerializer->serialize();
340
341
        $expected = [
342
            'identificacao'     => 'app%40test.com',
343
            'url_retorno'       => 'https%3A%2F%2Fminha_loja.com.br%2Fipag%2Fcallback',
344
            'retorno_tipo'      => 'xml',
345
            'boleto_tipo'       => 'xml',
346
            'pedido'            => '10000',
347
            'operacao'          => '',
348
            'valor'             => '10',
349
            'parcelas'          => '1',
350
            'vencto'            => '',
351
            'stelo_fingerprint' => '',
352
            'metodo'            => 'visa',
353
            'token_cartao'      => '123456789',
354
            'nome'              => 'Fulano+da+Silva',
355
            'email'             => 'fulanodasilva%40gmail.com',
356
            'doc'               => '79999338801',
357
            'fone'              => '11988883333',
358
            'endereco'          => 'Rua+J%C3%BAlio+Gonzalez',
359
            'numero_endereco'   => '1000',
360
            'complemento'       => '',
361
            'bairro'            => 'Barra+Funda',
362
            'cidade'            => 'S%C3%A3o+Paulo',
363
            'estado'            => 'SP',
364
            'pais'              => 'BR',
365
            'cep'               => '01156060',
366
            'profile_id'        => '',
367
            'frequencia'        => '1',
368
            'intervalo'         => 'month',
369
            'inicio'            => '',
370
            'ciclos'            => '',
371
            'valor_rec'         => '',
372
            'trial'             => '',
373
            'trial_ciclos'      => '',
374
            'trial_frequencia'  => '',
375
            'trial_valor'       => '',
376
        ];
377
378
        $this->assertEquals($expected, $response);
379
    }
380
381
    public function testSerializePaymentWithEmptyOrder()
382
    {
383
        $this->expectException(\Exception::class);
384
        $this->expectExceptionMessage('É necessário informar os dados do Pedido (Order)');
385
386
        $transaction = $this->ipag->transaction();
387
388
        $paymentSerializer = new PaymentSerializer($transaction);
389
390
        $paymentSerializer->serialize();
391
    }
392
}
393