Passed
Push — master ( 907d27...5c0742 )
by João Felipe Magro
04:02
created

testExecutePaymentSuccessfullyWithAntifraud()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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