1 | <?php |
||
2 | |||
3 | namespace DansMaCulotte\Monetico\Requests; |
||
4 | |||
5 | use DansMaCulotte\Monetico\Exceptions\Exception; |
||
6 | use DateTime; |
||
7 | |||
8 | class RefundRequest extends AbstractRequest |
||
9 | { |
||
10 | /** @var \DateTime */ |
||
11 | public $dateTime; |
||
12 | |||
13 | /** @var \DateTime */ |
||
14 | public $orderDate; |
||
15 | |||
16 | /** @var \DateTime */ |
||
17 | public $recoveryDate; |
||
18 | |||
19 | /** @var string */ |
||
20 | public $authorizationNumber; |
||
21 | |||
22 | /** @var string */ |
||
23 | public $currency; |
||
24 | |||
25 | /** @var float */ |
||
26 | public $amount; |
||
27 | |||
28 | /** @var float */ |
||
29 | public $refundAmount; |
||
30 | |||
31 | /** @var float */ |
||
32 | public $maxRefundAmount; |
||
33 | |||
34 | /** @var string */ |
||
35 | public $reference; |
||
36 | |||
37 | /** @var string */ |
||
38 | public $language; |
||
39 | |||
40 | /** @var string */ |
||
41 | public $fileNumber; |
||
42 | |||
43 | /** @var string */ |
||
44 | public $invoiceType; |
||
45 | |||
46 | /** @var array */ |
||
47 | const INVOICE_TYPES = [ |
||
48 | 'preauto', |
||
49 | 'noshow', |
||
50 | ]; |
||
51 | |||
52 | /** @var string */ |
||
53 | const DATETIME_FORMAT = 'd/m/Y:H:i:s'; |
||
54 | |||
55 | /** @var string */ |
||
56 | const DATE_FORMAT = 'd/m/Y'; |
||
57 | |||
58 | /** @var string */ |
||
59 | const REQUEST_URI = 'recredit_paiement.cgi'; |
||
60 | |||
61 | /** |
||
62 | * Refund constructor. |
||
63 | * @param array $data |
||
64 | * @throws Exception |
||
65 | */ |
||
66 | public function __construct($data = []) |
||
67 | { |
||
68 | $this->dateTime = $data['dateTime']; |
||
69 | $this->orderDate = $data['orderDate']; |
||
70 | $this->recoveryDate = $data['recoveryDate']; |
||
71 | $this->authorizationNumber = $data['authorizationNumber']; |
||
72 | $this->currency = $data['currency']; |
||
73 | $this->amount = $data['amount']; |
||
74 | $this->refundAmount = $data['refundAmount']; |
||
75 | $this->maxRefundAmount = $data['maxRefundAmount']; |
||
76 | $this->reference = $data['reference']; |
||
77 | $this->language = $data['language']; |
||
78 | |||
79 | $this->validate(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | protected static function getRequestUri(): string |
||
86 | { |
||
87 | return self::REQUEST_URI; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param $value |
||
92 | */ |
||
93 | public function setFileNumber($value) |
||
94 | { |
||
95 | $this->fileNumber = $value; |
||
96 | } |
||
97 | |||
98 | |||
99 | /** |
||
100 | * @param string $invoiceType |
||
101 | * @throws Exception |
||
102 | */ |
||
103 | public function setInvoiceType(string $invoiceType) |
||
104 | { |
||
105 | if (!in_array($invoiceType, self::INVOICE_TYPES, true)) { |
||
106 | throw Exception::invalidInvoiceType($invoiceType); |
||
107 | } |
||
108 | $this->invoiceType = $invoiceType; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string $eptCode |
||
113 | * @param string $version |
||
114 | * @param string $companyCode |
||
115 | * @return array |
||
116 | */ |
||
117 | public function fieldsToArray(string $eptCode, string $version, string $companyCode): array |
||
118 | { |
||
119 | $fields = array_merge([ |
||
120 | 'TPE' => $eptCode, |
||
121 | 'date' => $this->dateTime->format(self::DATETIME_FORMAT), |
||
122 | 'date_commande' => $this->orderDate->format(self::DATE_FORMAT), |
||
123 | 'date_remise' => $this->recoveryDate->format(self::DATE_FORMAT), |
||
124 | 'num_autorisation' => $this->authorizationNumber, |
||
125 | 'montant' => $this->amount . $this->currency, |
||
126 | 'montant_recredit' => $this->refundAmount . $this->currency, |
||
127 | 'montant_possible' => $this->maxRefundAmount . $this->currency, |
||
128 | 'reference' => $this->reference, |
||
129 | 'lgue' => $this->language, |
||
130 | 'societe' => $companyCode, |
||
131 | 'version' => $version |
||
132 | ]); |
||
133 | |||
134 | if (isset($this->fileNumber)) { |
||
135 | $fields['numero_dossier'] = $this->fileNumber; |
||
136 | } |
||
137 | |||
138 | if (isset($this->invoiceType)) { |
||
139 | $fields['facture'] = $this->invoiceType; |
||
140 | } |
||
141 | |||
142 | return $fields; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @throws Exception |
||
147 | */ |
||
148 | public function validate(): bool |
||
149 | { |
||
150 | if (!$this->dateTime instanceof DateTime) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
151 | throw Exception::invalidDatetime(); |
||
152 | } |
||
153 | |||
154 | if (!$this->orderDate instanceof DateTime) { |
||
0 ignored issues
–
show
|
|||
155 | throw Exception::invalidOrderDate(); |
||
156 | } |
||
157 | |||
158 | if (!$this->recoveryDate instanceof DateTime) { |
||
0 ignored issues
–
show
|
|||
159 | throw Exception::invalidRecoveryDate(); |
||
160 | } |
||
161 | |||
162 | if (strlen($this->reference) > 12) { |
||
163 | throw Exception::invalidReference($this->reference); |
||
164 | } |
||
165 | |||
166 | if (strlen($this->language) !== 2) { |
||
167 | throw Exception::invalidLanguage($this->language); |
||
168 | } |
||
169 | |||
170 | return true; |
||
171 | } |
||
172 | } |
||
173 |