Passed
Push — master ( 3e058a...8baa4a )
by João Felipe Magro
02:13
created

PaymentTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
dl 0
loc 110
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecutePaymentSuccessfully() 0 6 1
A initCard() 0 9 1
A initCustomer() 0 14 1
A testReturnResponseErrorUnauthorized() 0 19 1
A testExecuteSubscribePaymentSuccessfully() 0 15 1
A doPayment() 0 3 1
A init() 0 14 1
A setUp() 0 4 1
A __construct() 0 4 1
1
<?php
2
3
namespace Tests;
4
5
use Ipag\Classes\Authentication;
6
use Ipag\Classes\Endpoint;
7
use Ipag\Classes\Enum\Method;
8
use Ipag\Classes\Subscription;
9
use Ipag\Ipag;
10
use PHPUnit\Framework\TestCase;
11
12
class PaymentTest extends TestCase
13
{
14
    private $transaction;
15
    private $ipag;
16
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        $this->init();
21
    }
22
23
    public function setUp()
24
    {
25
        parent::setUp();
26
        $this->init();
27
    }
28
29
    public function init()
30
    {
31
        $this->ipag = new Ipag(new Authentication(getenv('ID_IPAG'), getenv('API_KEY')), Endpoint::SANDBOX);
32
33
        $this->transaction = $this->ipag->transaction();
34
        $this->transaction->getOrder()
35
            ->setOrderId(date('mdHis'))
36
            ->setCallbackUrl(getenv('CALLBACK_URL'))
37
            ->setAmount(10.00)
38
            ->setInstallments(1)
39
            ->setPayment($this->ipag->payment()
40
                    ->setMethod(Method::VISA)
41
                    ->setCreditCard($this->initCard())
42
            )->setCustomer($this->initCustomer());
43
    }
44
45
    public function initCustomer()
46
    {
47
        return $this->ipag->customer()
48
            ->setName('Fulano da Silva')
49
            ->setTaxpayerId('799.993.388-01')
50
            ->setPhone('11', '98888-3333')
51
            ->setEmail('[email protected]')
52
            ->setAddress($this->ipag->address()
53
                    ->setStreet('Rua Júlio Gonzalez')
54
                    ->setNumber('1000')
55
                    ->setNeighborhood('Barra Funda')
56
                    ->setCity('São Paulo')
57
                    ->setState('SP')
58
                    ->setZipCode('01156-060')
59
            );
60
    }
61
62
    public function initCard()
63
    {
64
        return $this->ipag->creditCard()
65
            ->setNumber('4066553613548107')
66
            ->setHolder('FULANO')
67
            ->setExpiryMonth('10')
68
            ->setExpiryYear('2025')
69
            ->setCvc('123')
70
            ->setSave(true);
71
    }
72
73
    public function doPayment()
74
    {
75
        return $this->transaction->execute();
76
    }
77
78
    public function testExecutePaymentSuccessfully()
79
    {
80
        $transaction = $this->doPayment();
81
82
        $this->assertEquals(getenv('APPROVED'), $transaction->payment->status);
83
        $this->assertEquals(36, strlen($transaction->creditCard->token));
84
    }
85
86
    public function testExecuteSubscribePaymentSuccessfully()
87
    {
88
        $subscription = new Subscription();
89
90
        $subscription->setProfileId(time())
91
            ->setFrequency(1)
92
            ->setInterval('month')
93
            ->setStart(date('d/m/Y'));
94
95
        $this->transaction->getOrder()->setSubscription($subscription);
96
97
        $response = $this->doPayment();
98
99
        $this->assertEquals(getenv('APPROVED'), $response->payment->status);
100
        $this->assertNotEmpty($response->creditCard->token);
101
    }
102
103
    public function testReturnResponseErrorUnauthorized()
104
    {
105
        $this->ipag = new Ipag(new Authentication('no_login_error', '123'), Endpoint::SANDBOX);
106
107
        $transaction = $this->ipag->transaction();
108
        $this->order = $transaction->getOrder()
0 ignored issues
show
Bug Best Practice introduced by
The property order does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
109
            ->setOrderId(date('mdHis'))
110
            ->setCallbackUrl(getenv('CALLBACK_URL'))
111
            ->setAmount(10.00)
112
            ->setInstallments(1);
113
114
        $this->order->setPayment($this->ipag->payment()
115
                ->setMethod(Method::VISA)
116
        );
117
118
        $response = $transaction->execute();
119
120
        $this->assertEquals('099', $response->error);
121
        $this->assertEquals('Unauthorized', $response->errorMessage);
122
    }
123
}
124