PaymentSerializerTest::serializePayment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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