1 | <?php |
||
2 | |||
3 | namespace Ipag\Classes; |
||
4 | |||
5 | use Ipag\Classes\Contracts\Emptiable; |
||
6 | use Ipag\Classes\Contracts\ObjectSerializable; |
||
7 | use Ipag\Classes\Traits\EmptiableTrait; |
||
8 | |||
9 | final class Payment implements Emptiable, ObjectSerializable |
||
10 | { |
||
11 | use EmptiableTrait; |
||
12 | |||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $method; |
||
17 | |||
18 | /** |
||
19 | * @var CreditCard |
||
20 | */ |
||
21 | private $creditCard; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $instructions = []; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $splitRules = []; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $softDescriptor; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $pixExpiresIn; |
||
42 | |||
43 | /** |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getMethod() |
||
47 | { |
||
48 | return $this->method; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return CreditCard |
||
53 | */ |
||
54 | public function getCreditCard() |
||
55 | { |
||
56 | if (is_null($this->creditCard)) { |
||
57 | $this->creditCard = new CreditCard(); |
||
58 | } |
||
59 | |||
60 | return $this->creditCard; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $method |
||
65 | */ |
||
66 | public function setMethod($method) |
||
67 | { |
||
68 | $this->method = $method; |
||
69 | |||
70 | return $this; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param CreditCard $creditCard |
||
75 | */ |
||
76 | public function setCreditCard(CreditCard $creditCard) |
||
77 | { |
||
78 | $this->creditCard = $creditCard; |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return array |
||
85 | */ |
||
86 | public function getInstructions() |
||
87 | { |
||
88 | return $this->instructions; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string $instructions |
||
93 | */ |
||
94 | public function setInstructions($instructions) |
||
95 | { |
||
96 | if ($this->instructionsAreNotFull()) { |
||
97 | $this->instructions[] = substr($instructions, 0, 80); |
||
98 | } |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getSoftDescriptor() |
||
107 | { |
||
108 | return $this->softDescriptor; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string $softDescriptor |
||
113 | */ |
||
114 | public function setSoftDescriptor($softDescriptor) |
||
115 | { |
||
116 | $this->softDescriptor = substr($softDescriptor, 0, 22); |
||
117 | |||
118 | return $this; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return int |
||
123 | */ |
||
124 | public function getPixExpiresIn() |
||
125 | { |
||
126 | return $this->pixExpiresIn; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param int $pixExpiresIn |
||
131 | */ |
||
132 | public function setPixExpiresIn($pixExpiresIn) |
||
133 | { |
||
134 | $this->pixExpiresIn = (int) $pixExpiresIn; |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getSplitRules() |
||
143 | { |
||
144 | return $this->splitRules; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param SplitRule $splitRule |
||
149 | * |
||
150 | * @return self |
||
151 | */ |
||
152 | public function addSplitRule(SplitRule $splitRule) |
||
153 | { |
||
154 | $this->splitRules[] = $splitRule; |
||
155 | |||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | private function instructionsAreNotFull() |
||
160 | { |
||
161 | return (bool) (count($this->instructions) < 3); |
||
162 | } |
||
163 | |||
164 | public function serialize() |
||
165 | { |
||
166 | if ($this->isEmpty()) { |
||
167 | throw new \Exception('É necessário informar os dados do Pagamento (Payment)'); |
||
168 | } |
||
169 | |||
170 | return array_merge( |
||
171 | [ |
||
172 | 'metodo' => urlencode((string) $this->getMethod()), |
||
173 | ], |
||
174 | $this->serializeInstructions(), |
||
175 | $this->serializeSoftDescriptor(), |
||
176 | $this->serializePixExpiresIn(), |
||
177 | $this->serializeSplitRules(), |
||
178 | $this->getCreditCard()->serialize() |
||
179 | ); |
||
180 | } |
||
181 | |||
182 | private function serializeSplitRules() |
||
183 | { |
||
184 | $_splitRules = []; |
||
185 | foreach ($this->getSplitRules() as $key => $splitRule) { |
||
186 | $_splitRules["split[{$key}][seller_id]"] = urlencode((string) $splitRule->getSellerId()); |
||
187 | $_splitRules["split[{$key}][percentage]"] = urlencode((string) $splitRule->getPercentage()); |
||
188 | $_splitRules["split[{$key}][amount]"] = urlencode((string) $splitRule->getAmount()); |
||
189 | $_splitRules["split[{$key}][liable]"] = urlencode((string) $splitRule->getLiable()); |
||
190 | $_splitRules["split[{$key}][charge_processing_fee]"] = urlencode((string) $splitRule->getChargeProcessingFee()); |
||
191 | $_splitRules["split[{$key}][hold_receivables]"] = urlencode((string) $splitRule->getHoldReceivables()); |
||
192 | } |
||
193 | |||
194 | return $_splitRules; |
||
195 | } |
||
196 | |||
197 | private function serializeInstructions() |
||
198 | { |
||
199 | $_instructions = []; |
||
200 | foreach ($this->getInstructions() as $key => $instruction) { |
||
201 | $_instructions["instrucoes[{$key}]"] = urlencode((string) $instruction); |
||
202 | } |
||
203 | |||
204 | return $_instructions; |
||
205 | } |
||
206 | |||
207 | private function serializeSoftDescriptor() |
||
208 | { |
||
209 | $_softDescriptor = []; |
||
210 | $softDescriptor = $this->getSoftDescriptor(); |
||
211 | |||
212 | if (!empty($softDescriptor)) { |
||
213 | $_softDescriptor['softdescriptor'] = urlencode((string) $softDescriptor); |
||
214 | } |
||
215 | |||
216 | return $_softDescriptor; |
||
217 | } |
||
218 | |||
219 | private function serializePixExpiresIn() |
||
220 | { |
||
221 | $_pixExpiresIn = []; |
||
222 | $pixExpiresIn = $this->getPixExpiresIn(); |
||
223 | |||
224 | if (!empty($pixExpiresIn)) { |
||
225 | $_pixExpiresIn['pix_expires_in'] = urlencode((string) $pixExpiresIn); |
||
226 | } |
||
227 | |||
228 | return $_pixExpiresIn; |
||
229 | } |
||
230 | } |
||
231 |