status.php
300 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Encapsulates the status of a payment transaction. |
| 16 | * |
| 17 | * The I/O of this class MUST be the same for all the E4J programs that support |
| 18 | * extendable payment methods. |
| 19 | * |
| 20 | * @note The class prefix is equals to the 3-letter name of the program, |
| 21 | * "VAP" in this case. |
| 22 | * |
| 23 | * @since 1.7.1 |
| 24 | */ |
| 25 | class VAPPaymentStatus implements ArrayAccess |
| 26 | { |
| 27 | /** |
| 28 | * The payment status. |
| 29 | * |
| 30 | * @var boolean |
| 31 | */ |
| 32 | protected $verified = false; |
| 33 | |
| 34 | /** |
| 35 | * Property used to track what is happening |
| 36 | * during the validation of the payment. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | protected $log = ''; |
| 41 | |
| 42 | /** |
| 43 | * The total paid amount, if available. |
| 44 | * |
| 45 | * @var float|null |
| 46 | */ |
| 47 | protected $totPaid = null; |
| 48 | |
| 49 | /** |
| 50 | * Additional transaction data. |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | protected $data = array(); |
| 55 | |
| 56 | /** |
| 57 | * Magic method to check whether an internal property exists. |
| 58 | * |
| 59 | * @param string $name The property name. |
| 60 | * |
| 61 | * @return boolean True if exists, false otherwise. |
| 62 | */ |
| 63 | public function __isset($name) |
| 64 | { |
| 65 | return isset($this->{$name}) || isset($this->data[$name]); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Magic method to access the property of the object. |
| 70 | * |
| 71 | * @param string $name The property name. |
| 72 | * |
| 73 | * @return mixed The property value. |
| 74 | */ |
| 75 | public function __get($name) |
| 76 | { |
| 77 | if (property_exists($this, $name)) |
| 78 | { |
| 79 | return $this->{$name}; |
| 80 | } |
| 81 | else if (isset($this->data[$name])) |
| 82 | { |
| 83 | return $this->data[$name]; |
| 84 | } |
| 85 | |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Checks whether the payment was successful. |
| 91 | * |
| 92 | * @return boolean True on success, otherwise false. |
| 93 | */ |
| 94 | public function isVerified() |
| 95 | { |
| 96 | return $this->verified; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Marks the payment status as verified or failed. |
| 101 | * |
| 102 | * @param boolean $status True if verified (default). |
| 103 | * |
| 104 | * @return self This object to support chaining. |
| 105 | */ |
| 106 | public function setVerified($status = true) |
| 107 | { |
| 108 | $this->verified = (bool) $status; |
| 109 | |
| 110 | return $this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Sets the total amount that have been paid. |
| 115 | * |
| 116 | * @param float $amount The total paid. |
| 117 | * |
| 118 | * @return self This object to support chaining. |
| 119 | */ |
| 120 | public function setTotPaid($amount) |
| 121 | { |
| 122 | $this->totPaid = (float) $amount; |
| 123 | |
| 124 | return $this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Tracks the given log. |
| 129 | * |
| 130 | * @param mixed $log A string or a non-scalar value. |
| 131 | * An array will be logged using print_r. |
| 132 | * |
| 133 | * @return self This object to support chaining. |
| 134 | */ |
| 135 | public function setLog($log) |
| 136 | { |
| 137 | if (!is_scalar($log)) |
| 138 | { |
| 139 | // use print_r for non scalar logs |
| 140 | $log = print_r($log, true); |
| 141 | } |
| 142 | |
| 143 | $this->log = $log; |
| 144 | |
| 145 | return $this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Appends the given log to the existing string. |
| 150 | * |
| 151 | * @param mixed $log A string or a non-scalar value. |
| 152 | * An array will be logged using print_r. |
| 153 | * |
| 154 | * @return self This object to support chaining. |
| 155 | * |
| 156 | * @uses setLog() |
| 157 | */ |
| 158 | public function appendLog($log, $separator = "\n") |
| 159 | { |
| 160 | // keep current log |
| 161 | $current = $this->log; |
| 162 | |
| 163 | // set log using the proper method |
| 164 | $this->setLog($log); |
| 165 | |
| 166 | // re-build the log by prepending the existing logs |
| 167 | $this->log = $current . $separator . $this->log; |
| 168 | |
| 169 | return $this; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Prepends the given log to the existing string. |
| 174 | * |
| 175 | * @param mixed $log A string or a non-scalar value. |
| 176 | * An array will be logged using print_r. |
| 177 | * |
| 178 | * @return self This object to support chaining. |
| 179 | * |
| 180 | * @uses setLog() |
| 181 | */ |
| 182 | public function prependLog($log, $separator = "\n") |
| 183 | { |
| 184 | // keep current log |
| 185 | $current = $this->log; |
| 186 | |
| 187 | // set log using the proper method |
| 188 | $this->setLog($log); |
| 189 | |
| 190 | // re-build the log by appending the existing logs |
| 191 | $this->log .= $separator . $current; |
| 192 | |
| 193 | return $this; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Registers the additional transaction data. |
| 198 | * |
| 199 | * @param string $key The transaction key. |
| 200 | * @param mixed $val The transaction value. |
| 201 | * |
| 202 | * @return self This object to support chaining. |
| 203 | */ |
| 204 | public function setData($key, $val) |
| 205 | { |
| 206 | $this->data[$key] = $val; |
| 207 | |
| 208 | return $this; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Checks whether an offset exists in the iterator. |
| 213 | * |
| 214 | * @param mixed $offset The array offset. |
| 215 | * |
| 216 | * @return boolean True if the offset exists, false otherwise. |
| 217 | * |
| 218 | * @see ArrayAccess |
| 219 | */ |
| 220 | #[ReturnTypeWillChange] |
| 221 | public function offsetExists($offset) |
| 222 | { |
| 223 | $property = $this->attr2prop($offset); |
| 224 | |
| 225 | return isset($this->{$property}); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Gets an offset in the iterator. |
| 230 | * |
| 231 | * @param mixed $offset The array offset. |
| 232 | * |
| 233 | * @return mixed The array value if it exists, null otherwise. |
| 234 | * |
| 235 | * @see ArrayAccess |
| 236 | */ |
| 237 | #[ReturnTypeWillChange] |
| 238 | public function offsetGet($offset) |
| 239 | { |
| 240 | $property = $this->attr2prop($offset); |
| 241 | |
| 242 | if (isset($this->{$property})) |
| 243 | { |
| 244 | return $this->{$property}; |
| 245 | } |
| 246 | |
| 247 | return null; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Sets an offset in the iterator. |
| 252 | * |
| 253 | * @param mixed $offset The array offset. |
| 254 | * @param mixed $value The array value. |
| 255 | * |
| 256 | * @return void |
| 257 | * |
| 258 | * @see ArrayAccess |
| 259 | */ |
| 260 | #[ReturnTypeWillChange] |
| 261 | public function offsetSet($offset, $value) |
| 262 | { |
| 263 | // prevent manual property setter |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Unsets an offset in the iterator. |
| 268 | * |
| 269 | * @param mixed $offset The array offset. |
| 270 | * |
| 271 | * @return void |
| 272 | * |
| 273 | * @see ArrayAccess |
| 274 | */ |
| 275 | #[ReturnTypeWillChange] |
| 276 | public function offsetUnset($offset) |
| 277 | { |
| 278 | $property = $this->attr2prop($offset); |
| 279 | |
| 280 | unset($this->{$property}); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Converts a snake case text into camel case. |
| 285 | * In example "foo_bar_baz" becomes "fooBarBaz". |
| 286 | * |
| 287 | * @param string $name The attribute name. |
| 288 | * |
| 289 | * @param string The property name. |
| 290 | */ |
| 291 | private function attr2prop($name) |
| 292 | { |
| 293 | $name = preg_replace("/_+/", ' ', $name); |
| 294 | $name = ucwords($name); |
| 295 | $name = preg_replace("/\s+/", '', $name); |
| 296 | |
| 297 | return lcfirst($name); |
| 298 | } |
| 299 | } |
| 300 |