Completed
Push — master ( dc30b6...cc4a0b )
by João Felipe Magro
03:10
created

testSplitRuleWithPaymentInPercentage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 24
rs 9.7
c 0
b 0
f 0
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\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()
119
    {
120
        return $this->transaction->execute();
121
    }
122
123
    public function testExecutePaymentSuccessfully()
124
    {
125
        $transaction = $this->doPayment();
126
127
        $this->assertEquals(getenv('APPROVED'), $transaction->payment->status);
128
        $this->assertEquals(36, strlen($transaction->creditCard->token));
129
    }
130
131
    public function testExecuteSubscribePaymentSuccessfully()
132
    {
133
        $subscription = new Subscription();
134
135
        $subscription->setProfileId(time())
136
            ->setFrequency(1)
137
            ->setInterval('month')
138
            ->setStart(date('d/m/Y'));
139
140
        $this->transaction->getOrder()->setSubscription($subscription);
141
142
        $response = $this->doPayment();
143
144
        $this->assertEquals(getenv('APPROVED'), $response->payment->status);
145
        $this->assertNotEmpty($response->creditCard->token);
146
    }
147
148
    public function testSplitRuleWithPaymentInPercentage()
149
    {
150
        $payment = $this->transaction->getOrder()->getPayment();
151
152
        $payment->addSplitRule(
153
            $this->ipag->splitRule()
154
                ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
155
                ->setPercentage(10)
156
                ->setChargeProcessingFee(0)
157
                ->setLiable(1)
158
        );
159
        $payment->addSplitRule(
160
            $this->ipag->splitRule()
161
                ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
162
                ->setPercentage(15)
163
                ->setChargeProcessingFee(0)
164
                ->setLiable(1)
165
        );
166
167
        $response = $this->doPayment();
168
169
        $this->assertEquals(getenv('APPROVED'), $response->payment->status);
170
        $this->assertNotEmpty($response->splitRules);
171
        $this->assertEquals(2, count($response->splitRules));
172
    }
173
174
    public function testSplitRuleWithPaymentInAmount()
175
    {
176
        $payment = $this->transaction->getOrder()->getPayment();
177
        $payment->addSplitRule(
178
            $this->ipag->splitRule()
179
                ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
180
                ->setAmount(1.00)
181
                ->setChargeProcessingFee(0)
182
                ->setLiable(1)
183
        );
184
        $payment->addSplitRule(
185
            $this->ipag->splitRule()
186
                ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
187
                ->setPercentage(15)
188
                ->setChargeProcessingFee(1)
189
                ->setLiable(1)
190
        );
191
192
        $response = $this->doPayment();
193
194
        $this->assertEquals(getenv('APPROVED'), $response->payment->status);
195
        $this->assertNotEmpty($response->splitRules);
196
        $this->assertEquals(2, count($response->splitRules));
197
    }
198
199
    public function testReturnResponseErrorUnauthorized()
200
    {
201
        $this->ipag = new Ipag(new Authentication('no_login_error', '123'), Endpoint::SANDBOX);
202
203
        $transaction = $this->ipag->transaction();
204
        $transaction->getOrder()
205
            ->setOrderId(date('mdHis'))
206
            ->setCallbackUrl(getenv('CALLBACK_URL'))
207
            ->setAmount(10.00)
208
            ->setInstallments(1)
209
            ->setPayment($this->ipag->payment()
210
                    ->setMethod(Method::VISA)
211
            );
212
213
        $response = $transaction->execute();
214
215
        $this->assertEquals('401', $response->error);
216
        $this->assertEquals('Unauthorized', $response->errorMessage);
217
    }
218
}
219