Passed
Push — master ( cc4a0b...c8d669 )
by João Felipe Magro
02:41
created

PaymentTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 142
dl 0
loc 216
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A initCard() 0 9 1
A initCustomer() 0 14 1
A init() 0 58 1
A setUp() 0 4 1
A __construct() 0 4 1
A testExecutePaymentSuccessfully() 0 6 1
A testExecutePaymentWithCaptureSuccessfully() 0 6 1
A testSplitRuleWithPaymentInPercentage() 0 24 1
A testSplitRuleWithPaymentInAmount() 0 23 1
A testReturnResponseErrorUnauthorized() 0 18 1
A testExecuteSubscribePaymentSuccessfully() 0 15 1
A doPayment() 0 7 2
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\SplitRule;
9
use Ipag\Classes\Subscription;
10
use Ipag\Ipag;
11
use PHPUnit\Framework\TestCase;
12
13
class PaymentTest extends TestCase
14
{
15
    private $transaction;
16
    private $ipag;
17
18
    public function __construct()
19
    {
20
        parent::__construct();
21
        $this->init();
22
    }
23
24
    public function setUp()
25
    {
26
        parent::setUp();
27
        $this->init();
28
    }
29
30
    public function init()
31
    {
32
        $this->ipag = new Ipag(new Authentication(getenv('ID_IPAG'), getenv('API_KEY')), Endpoint::SANDBOX);
33
34
        $this->transaction = $this->ipag->transaction();
35
36
        $cart = $this->ipag->cart(
37
            ['APC Flotador Universal Perol 5 Litros', 55.9, 2, '000347'],
38
            ['Aplicador de gel para pneus Cadillac', 10, 2, '001567'],
39
            ['Escova Azul para Furo de Rodas Mandala', 35.9, 2, 'E01'],
40
            ['Polidor de Corte LPD Hi Cut + Lincoln 1K (nova fórmula)', 148.9, 1, '002053'],
41
            ['Lava Auto Shampoo Super Concentrado 1:300 Monster Cadillac 2L', 37.05, 2, '001646'],
42
            ['Balde com Protetor de Areia Ultimate Filter GF', 60.9, 1, '001549'],
43
            ['Balde com Protetor de Areia Ultimate Filter GF', 60.9, 1, '001549'],
44
            ['Removedor de Piche Tar Remover Sonax 300ml', 54, 1, '000586'],
45
            ['Boina de Espuma Super Macia Preta Waffle Mills 7.8\'', 60, 1, '001004'],
46
            ['Cera de Carnaúba Cleaner Wax Cadillac 300gr', 79.9, 1, '000312'],
47
            ['Cera de Carnaúba Lincoln 200g', 72.5, 1, '001051'],
48
            ['Vonixx Cera de Carnaúba Super Protetora', 52, 1, '001455'],
49
            ['Vonixx Revitalizador de Pneus 500ml', 32, 1, '001479'],
50
            ['Boina de Espuma Médio Agressiva Dune Alcance 5\' ', 41.5, 1, '002333'],
51
            ['Boina Lã S/Interface Corte Lincoln 6\' (listra Colorida)', 39, 1, '001891'],
52
            ['Desinfetante BAC 500 Perol 5L', 66.9, 1, '001286'],
53
            ['Escova Caixa de Roda Cadillac', 39.9, 1, '000544'],
54
            ['Escova para Estofados (cerdas de silicone) Cadillac', 36.9, 1, '001488'],
55
            ['Lavagem a Seco Affix Perol 5L', 216.9, 1, '000499'],
56
            ['POLIDOR DE METAIS METAL-LAC CADILLAC 150G', 37.9, 1, '831676'],
57
            ['Vonixx Restaurax 500ml', 32.9, 1, '001478'],
58
            ['Revitalizador de Plásticos Doctor Shine Cadillac 500ml', 34.1, 1, '002020'],
59
            ['Menzerna Selante Power Lock Ultimate Protection 1L', 251.9, 1, '001423'],
60
            ['Pneu Visco Cadillac 5L', 43.6, 1, '000265'],
61
            ['Metal Polish Optimum 227g', 83, 1, '001910'],
62
            ['Nano Pro (Profiline) Perfect Finish Sonax - 400g', 109.9, 1, '000427'],
63
            ['OXICLEAN – REMOVEDOR DE CHUVA ÁCIDA', 45.9, 1, '001513'],
64
            ['Pasta Abrasiva 3M Cleaner Clay + Brinde Limpeza Final 3M 500ml', 86.9, 1, '000279'],
65
            ['Shampoo automotivo Espumacar Cadillac 5L', 28, 1, '000266'],
66
            ['Suporte c/ Velcro Ventilado 5\' p/ Roto 21 - Cadillac ou Kers', 56.9, 1, '001547'],
67
            ['Toalha de Microfibra Cadillac 40x40 cm', 9.9, 1, '001554'],
68
            ['Boina de Espuma Agressiva Azul Spider Scholl 6.5\'', 113.5, 1, 'BSA165'],
69
            ['Ironlac Descontaminante de Superfícies Cadillac 5L', 113.9, 1, '001824'],
70
            ['Lustrador Alto Brilho 2x1 Lincoln', 39.9, 1, '000086'],
71
            ['Meguiars Espuma Aplicadora', 11.23, 1, '000558'],
72
            ['Melamina Kers - Esponja Mágica', 11.9, 1, '001406'],
73
            ['Escova Pneus Cadillac', 40.9, 1, '000573'],
74
            ['Mini Politriz Roto Orbital Yes GFX-5802 220V + Kit Pincel Kers', 1100, 1, '002165']
75
        );
76
77
        $this->transaction->getOrder()
78
            ->setOrderId(date('mdHis'))
79
            ->setCallbackUrl(getenv('CALLBACK_URL'))
80
            ->setAmount(10.00)
81
            ->setInstallments(1)
82
            ->setVisitorId('9as7d8s9a7da9sd7sa9889a')
83
            ->setPayment($this->ipag->payment()
84
                    ->setMethod(Method::VISA)
85
                    ->setCreditCard($this->initCard())
86
            )->setCustomer($this->initCustomer())
87
            ->setCart($cart);
88
    }
89
90
    public function initCustomer()
91
    {
92
        return $this->ipag->customer()
93
            ->setName('Fulano da Silva')
94
            ->setTaxpayerId('799.993.388-01')
95
            ->setPhone('11', '98888-3333')
96
            ->setEmail('[email protected]')
97
            ->setAddress($this->ipag->address()
98
                    ->setStreet('Rua Júlio Gonzalez')
99
                    ->setNumber('1000')
100
                    ->setNeighborhood('Barra Funda')
101
                    ->setCity('São Paulo')
102
                    ->setState('SP')
103
                    ->setZipCode('01156-060')
104
            );
105
    }
106
107
    public function initCard()
108
    {
109
        return $this->ipag->creditCard()
110
            ->setNumber('4066553613548107')
111
            ->setHolder('FULANO')
112
            ->setExpiryMonth('10')
113
            ->setExpiryYear('2025')
114
            ->setCvc('123')
115
            ->setSave(true);
116
    }
117
118
    public function doPayment($capture = false)
119
    {
120
        if ($capture) {
121
            $this->transaction->getOrder()->setCapture('c');
122
        }
123
124
        return $this->transaction->execute();
125
    }
126
127
    public function testExecutePaymentSuccessfully()
128
    {
129
        $transaction = $this->doPayment();
130
131
        $this->assertEquals(getenv('APPROVED'), $transaction->payment->status);
132
        $this->assertEquals(36, strlen($transaction->creditCard->token));
133
    }
134
135
    public function testExecutePaymentWithCaptureSuccessfully()
136
    {
137
        $transaction = $this->doPayment(true);
138
139
        $this->assertEquals(getenv('APPROVED_CAPTURED'), $transaction->payment->status);
140
        $this->assertEquals(36, strlen($transaction->creditCard->token));
141
    }
142
143
    public function testExecuteSubscribePaymentSuccessfully()
144
    {
145
        $subscription = new Subscription();
146
147
        $subscription->setProfileId(time())
148
            ->setFrequency(1)
149
            ->setInterval('month')
150
            ->setStart(date('d/m/Y'));
151
152
        $this->transaction->getOrder()->setSubscription($subscription);
153
154
        $response = $this->doPayment();
155
156
        $this->assertEquals(getenv('APPROVED'), $response->payment->status);
157
        $this->assertNotEmpty($response->creditCard->token);
158
    }
159
160
    public function testSplitRuleWithPaymentInPercentage()
161
    {
162
        $payment = $this->transaction->getOrder()->getPayment();
163
164
        $payment->addSplitRule(
165
            $this->ipag->splitRule()
166
                ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
167
                ->setPercentage(10)
168
                ->setChargeProcessingFee(0)
169
                ->setLiable(1)
170
        );
171
        $payment->addSplitRule(
172
            $this->ipag->splitRule()
173
                ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
174
                ->setPercentage(15)
175
                ->setChargeProcessingFee(0)
176
                ->setLiable(1)
177
        );
178
179
        $response = $this->doPayment();
180
181
        $this->assertEquals(getenv('APPROVED'), $response->payment->status);
182
        $this->assertNotEmpty($response->splitRules);
183
        $this->assertEquals(2, count($response->splitRules));
184
    }
185
186
    public function testSplitRuleWithPaymentInAmount()
187
    {
188
        $payment = $this->transaction->getOrder()->getPayment();
189
        $payment->addSplitRule(
190
            $this->ipag->splitRule()
191
                ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
192
                ->setAmount(1.00)
193
                ->setChargeProcessingFee(0)
194
                ->setLiable(1)
195
        );
196
        $payment->addSplitRule(
197
            $this->ipag->splitRule()
198
                ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
199
                ->setPercentage(15)
200
                ->setChargeProcessingFee(1)
201
                ->setLiable(1)
202
        );
203
204
        $response = $this->doPayment();
205
206
        $this->assertEquals(getenv('APPROVED'), $response->payment->status);
207
        $this->assertNotEmpty($response->splitRules);
208
        $this->assertEquals(2, count($response->splitRules));
209
    }
210
211
    public function testReturnResponseErrorUnauthorized()
212
    {
213
        $this->ipag = new Ipag(new Authentication('no_login_error', '123'), Endpoint::SANDBOX);
214
215
        $transaction = $this->ipag->transaction();
216
        $transaction->getOrder()
217
            ->setOrderId(date('mdHis'))
218
            ->setCallbackUrl(getenv('CALLBACK_URL'))
219
            ->setAmount(10.00)
220
            ->setInstallments(1)
221
            ->setPayment($this->ipag->payment()
222
                    ->setMethod(Method::VISA)
223
            );
224
225
        $response = $transaction->execute();
226
227
        $this->assertEquals('401', $response->error);
228
        $this->assertEquals('Unauthorized', $response->errorMessage);
229
    }
230
}
231