TransactionResponseService   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 31
eloc 126
c 6
b 0
f 0
dl 0
loc 213
rs 9.92

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getObjectUtil() 0 7 2
A populate() 0 11 2
A payment() 0 7 1
A pix() 0 13 3
A transaction() 0 27 1
A order() 0 6 1
A creditCard() 0 19 4
A customer() 0 26 5
A antifraud() 0 15 3
A history() 0 21 4
A subscription() 0 7 1
A splitRules() 0 20 4
1
<?php
2
3
namespace Ipag\Classes\Services;
4
5
use Ipag\Classes\Contracts\Populable;
6
use Ipag\Classes\Util\ObjectUtil;
7
use stdClass;
8
9
final class TransactionResponseService implements Populable
10
{
11
    /**
12
     * @var ObjectUtil
13
     */
14
    private $objectUtil;
15
16
    /**
17
     * @param stdClass $response
18
     *
19
     * @return stdClass
20
     */
21
    public function populate(stdClass $response)
22
    {
23
        $transaction = $this->transaction($response);
24
25
        $transaction->creditCard = $this->creditCard($response);
26
27
        if (isset($response->id_assinatura)) {
28
            $transaction->subscription = $this->subscription($response);
29
        }
30
31
        return $transaction;
32
    }
33
34
    private function getObjectUtil()
35
    {
36
        if (is_null($this->objectUtil)) {
37
            $this->objectUtil = new ObjectUtil();
38
        }
39
40
        return $this->objectUtil;
41
    }
42
43
    private function transaction(stdClass $response)
44
    {
45
        $transaction = new stdClass();
46
        $transaction->id = $this->getObjectUtil()->getProperty($response, 'id');
47
        $transaction->tid = $this->getObjectUtil()->getProperty($response, 'id_transacao');
48
        $transaction->authId = $this->getObjectUtil()->getProperty($response, 'autorizacao_id');
49
        $transaction->nsu = $this->getObjectUtil()->getProperty($response, 'nsu');
50
        $transaction->amount = $this->getObjectUtil()->getProperty($response, 'valor');
51
        $transaction->installments = $this->getObjectUtil()->getProperty($response, 'parcelas');
52
        $transaction->acquirer = $this->getObjectUtil()->getProperty($response, 'operadora');
53
        $transaction->acquirerMessage = $this->getObjectUtil()->getProperty($response, 'operadora_mensagem');
54
        $transaction->urlAuthentication = $this->getObjectUtil()->getProperty($response, 'url_autenticacao');
55
        $transaction->digitableLine = $this->getObjectUtil()->getProperty($response, 'linha_digitavel');
56
        $transaction->urlCallback = $this->getObjectUtil()->getProperty($response, 'url_retorno');
57
        $transaction->urlRedirect = $this->getObjectUtil()->getProperty($response, 'url_redirect');
58
        $transaction->createAt = $this->getObjectUtil()->getProperty($response, 'criado_em');
59
        $transaction->payment = $this->payment($response);
60
        $transaction->order = $this->order($response);
61
        $transaction->customer = $this->customer($response);
62
        $transaction->antifraud = $this->antifraud($response);
63
        $transaction->splitRules = $this->splitRules($response);
64
        $transaction->pix = $this->pix($response);
65
        $transaction->error = $this->getObjectUtil()->getProperty($response, 'code');
66
        $transaction->errorMessage = $this->getObjectUtil()->getProperty($response, 'message');
67
        $transaction->history = $this->history($response);
68
69
        return $transaction;
70
    }
71
72
    private function payment(stdClass $response)
73
    {
74
        $payment = new stdClass();
75
        $payment->status = $this->getObjectUtil()->getProperty($response, 'status_pagamento');
76
        $payment->message = $this->getObjectUtil()->getProperty($response, 'mensagem_transacao');
77
78
        return $payment;
79
    }
80
81
    private function order(stdClass $response)
82
    {
83
        $order = new stdClass();
84
        $order->orderId = $this->getObjectUtil()->getProperty($response, 'num_pedido');
85
86
        return $order;
87
    }
88
89
    private function customer(stdClass $response)
90
    {
91
        $customer = new stdClass();
92
        $customer_params = property_exists($response, 'cliente') ? $response->cliente : null;
93
        if (!empty($customer_params)) {
94
            $customer->name = $this->getObjectUtil()->getProperty($customer_params, 'nome');
95
            $customer->email = $this->getObjectUtil()->getProperty($customer_params, 'email');
96
            $customer->phone = $this->getObjectUtil()->getProperty($customer_params, 'telefone');
97
            $customer->cpfCnpj = $this->getObjectUtil()->getProperty($customer_params, 'cnpj_cnpj');
98
            $customer_address = property_exists($customer_params, 'endereco') ? $customer_params->endereco : null;
99
100
            if (!empty($customer_address)) {
101
                $address = new stdClass();
102
                $address->street = $this->getObjectUtil()->getProperty($customer_address, 'logradouro');
103
                $address->number = $this->getObjectUtil()->getProperty($customer_address, 'numero');
104
                $address->complement = $this->getObjectUtil()->getProperty($customer_address, 'complemento');
105
                $address->district = $this->getObjectUtil()->getProperty($customer_address, 'bairro');
106
                $address->city = $this->getObjectUtil()->getProperty($customer_address, 'cidade');
107
                $address->state = $this->getObjectUtil()->getProperty($customer_address, 'estado');
108
                $address->zipCode = $this->getObjectUtil()->getProperty($customer_address, 'cep');
109
110
                $customer->address = $address;
111
            }
112
        }
113
114
        return $customer;
115
    }
116
117
    private function history(stdClass $response)
118
    {
119
        $histories = [];
120
        $histories_params = property_exists($response, 'historicos') ? $response->historicos : null;
121
        if (!empty($histories_params)) {
122
            foreach ($histories_params as $history_params) {
123
                $history = new stdClass();
124
                $history->amount = $this->getObjectUtil()->getProperty($history_params, 'valor');
125
                $history->operationType = $this->getObjectUtil()->getProperty($history_params, 'tipo');
126
                $history->status = $this->getObjectUtil()->getProperty($history_params, 'status');
127
                $history->responseCode = $this->getObjectUtil()->getProperty($history_params, 'codigo_resposta');
128
                $history->responseMessage = $this->getObjectUtil()->getProperty($history_params, 'mensagem_resposta');
129
                $history->authorizationCode = $this->getObjectUtil()->getProperty($history_params, 'codigo_autorizacao');
130
                $history->authorizationId = $this->getObjectUtil()->getProperty($history_params, 'id_autorizacao');
131
                $history->authorizationNsu = $this->getObjectUtil()->getProperty($history_params, 'nsu_autorizacao');
132
                $history->createdAt = $this->getObjectUtil()->getProperty($history_params, 'criado_em');
133
                $histories[] = $history;
134
            }
135
        }
136
137
        return $histories;
138
    }
139
140
    private function splitRules(stdClass $response)
141
    {
142
        $splitRules = [];
143
        $split_rules = property_exists($response, 'split_rules') ? $response->split_rules : null;
144
        if (!empty($split_rules)) {
145
            foreach ($split_rules as $split_rule) {
146
                $splitRule = new stdClass();
147
                $splitRule->rule = $this->getObjectUtil()->getProperty($split_rule, 'rule');
148
                $splitRule->seller_id = $this->getObjectUtil()->getProperty($split_rule, 'recipient');
149
                $splitRule->ipag_id = $this->getObjectUtil()->getProperty($split_rule, 'ipag_id');
150
                $splitRule->amount = $this->getObjectUtil()->getProperty($split_rule, 'amount');
151
                $splitRule->percentage = $this->getObjectUtil()->getProperty($split_rule, 'percentage');
152
                $splitRule->liable = $this->getObjectUtil()->getProperty($split_rule, 'liable');
153
                $splitRule->charge_processing_fee = $this->getObjectUtil()->getProperty($split_rule, 'charge_processing_fee');
154
                $splitRule->hold_receivables = $this->getObjectUtil()->getProperty($split_rule, 'hold_receivables');
155
                $splitRules[] = $splitRule;
156
            }
157
        }
158
159
        return $splitRules;
160
    }
161
162
    private function creditCard(stdClass $response)
163
    {
164
        $creditCard = new stdClass();
165
        if (isset($response->token)) {
166
            $creditCard->token = $this->getObjectUtil()->getProperty($response, 'token');
167
            $creditCard->last4 = $this->getObjectUtil()->getProperty($response, 'last4');
168
            $creditCard->expiryMonth = $this->getObjectUtil()->getProperty($response, 'mes');
169
            $creditCard->expiryYear = $this->getObjectUtil()->getProperty($response, 'ano');
170
        }
171
172
        $card = property_exists($response, 'cartao') ? $response->cartao : null;
173
        if (!empty($card)) {
174
            $creditCard->holder = $this->getObjectUtil()->getProperty($card, 'titular');
175
            $creditCard->number = $this->getObjectUtil()->getProperty($card, 'numero');
176
            $creditCard->expiry = $this->getObjectUtil()->getProperty($card, 'vencimento');
177
            $creditCard->brand = $this->getObjectUtil()->getProperty($card, 'bandeira');
178
        }
179
180
        return $creditCard;
181
    }
182
183
    private function subscription(stdClass $response)
184
    {
185
        $subscription = new stdClass();
186
        $subscription->id = $this->getObjectUtil()->getProperty($response, 'id_assinatura');
187
        $subscription->profileId = $this->getObjectUtil()->getProperty($response, 'profile_id');
188
189
        return $subscription;
190
    }
191
192
    private function pix(stdClass $response)
193
    {
194
        $pix = new stdClass();
195
        $pix->link = null;
196
        $pix->qrCode = null;
197
198
        $pix_params = property_exists($response, 'pix') ? $response->pix : null;
199
        if (!empty($pix_params)) {
200
            $pix->link = $this->getObjectUtil()->getProperty($pix_params, 'link');
201
            $pix->qrCode = $this->getObjectUtil()->getProperty($pix_params, 'qrcode');
202
        }
203
204
        return $pix;
205
    }
206
207
    private function antifraud(stdClass $response)
208
    {
209
        $antifraud = new stdClass();
210
        $antifraud->score = null;
211
        $antifraud->status = null;
212
        $antifraud->message = null;
213
214
        $antifraude_params = property_exists($response, 'antifraude') ? $response->antifraude : null;
215
        if (!empty($antifraude_params)) {
216
            $antifraud->score = $this->getObjectUtil()->getProperty($antifraude_params, 'score');
217
            $antifraud->status = $this->getObjectUtil()->getProperty($antifraude_params, 'status');
218
            $antifraud->message = strip_tags((string) $this->getObjectUtil()->getProperty($antifraude_params, 'message'));
219
        }
220
221
        return $antifraud;
222
    }
223
}
224