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