| @@ -1,0 +1,193 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.payment | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 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 | + * This class is used to wrap the details of | |
| 16 | + * a payment response. | |
| 17 | + * | |
| 18 | + * @since 10.1 | |
| 19 | + */ | |
| 20 | +class JPaymentStatus | |
| 21 | +{ | |
| 22 | + /** | |
| 23 | + * The payment status. | |
| 24 | + * | |
| 25 | + * @var boolean | |
| 26 | + */ | |
| 27 | + protected $status = false; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Property used to track what is happening | |
| 31 | + * during the validation of the payment. | |
| 32 | + * | |
| 33 | + * @var string | |
| 34 | + */ | |
| 35 | + protected $log = ''; | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * The total paid amount, if available. | |
| 39 | + * | |
| 40 | + * @var float|null | |
| 41 | + */ | |
| 42 | + protected $amount = null; | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Additional transaction data. | |
| 46 | + * | |
| 47 | + * @var array | |
| 48 | + */ | |
| 49 | + protected $data = array(); | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Magic method to access the property of the object. | |
| 53 | + * | |
| 54 | + * @param string $name The property name. | |
| 55 | + * | |
| 56 | + * @return mixed The property value. | |
| 57 | + */ | |
| 58 | + public function __get($name) | |
| 59 | + { | |
| 60 | + if (isset($this->{$name})) | |
| 61 | + { | |
| 62 | + return $this->{$name}; | |
| 63 | + } | |
| 64 | + else if (isset($this->data[$name])) | |
| 65 | + { | |
| 66 | + return $this->data[$name]; | |
| 67 | + } | |
| 68 | + | |
| 69 | + return null; | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Checks whether the payment was successful. | |
| 74 | + * | |
| 75 | + * @return boolean True on success, otherwise false. | |
| 76 | + */ | |
| 77 | + public function isVerified() | |
| 78 | + { | |
| 79 | + return $this->status; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Marks the payment status as verified or failed. | |
| 84 | + * | |
| 85 | + * @param boolean $status True if verified (default). | |
| 86 | + * | |
| 87 | + * @return self This object to support chaining. | |
| 88 | + */ | |
| 89 | + public function verified($status = true) | |
| 90 | + { | |
| 91 | + $this->status = (bool) $status; | |
| 92 | + | |
| 93 | + return $this; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Sets the total amount that have been paid. | |
| 98 | + * | |
| 99 | + * @param float $amount The total paid. | |
| 100 | + * | |
| 101 | + * @return self This object to support chaining. | |
| 102 | + */ | |
| 103 | + public function paid($amount) | |
| 104 | + { | |
| 105 | + $this->amount = (float) $amount; | |
| 106 | + | |
| 107 | + return $this; | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * Tracks the given log. | |
| 112 | + * | |
| 113 | + * @param mixed $log A string or a non-scalar value. | |
| 114 | + * An array will be logged using print_r. | |
| 115 | + * | |
| 116 | + * @return self This object to support chaining. | |
| 117 | + */ | |
| 118 | + public function setLog($log) | |
| 119 | + { | |
| 120 | + if (!is_scalar($log)) | |
| 121 | + { | |
| 122 | + // use print_r for non scalar logs | |
| 123 | + $log = print_r($log, true); | |
| 124 | + } | |
| 125 | + | |
| 126 | + $this->log = $log; | |
| 127 | + | |
| 128 | + return $this; | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Appends the given log to the existing string. | |
| 133 | + * | |
| 134 | + * @param mixed $log A string or a non-scalar value. | |
| 135 | + * An array will be logged using print_r. | |
| 136 | + * | |
| 137 | + * @return self This object to support chaining. | |
| 138 | + * | |
| 139 | + * @uses setLog() | |
| 140 | + */ | |
| 141 | + public function appendLog($log, $separator = "\n") | |
| 142 | + { | |
| 143 | + // keep current log | |
| 144 | + $current = $this->log; | |
| 145 | + | |
| 146 | + // set log using the proper method | |
| 147 | + $this->setLog($log); | |
| 148 | + | |
| 149 | + // re-build the log by prepending the existing logs | |
| 150 | + $this->log = $current . $separator . $this->log; | |
| 151 | + | |
| 152 | + return $this; | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Prepends the given log to the existing string. | |
| 157 | + * | |
| 158 | + * @param mixed $log A string or a non-scalar value. | |
| 159 | + * An array will be logged using print_r. | |
| 160 | + * | |
| 161 | + * @return self This object to support chaining. | |
| 162 | + * | |
| 163 | + * @uses setLog() | |
| 164 | + */ | |
| 165 | + public function prependLog($log, $separator = "\n") | |
| 166 | + { | |
| 167 | + // keep current log | |
| 168 | + $current = $this->log; | |
| 169 | + | |
| 170 | + // set log using the proper method | |
| 171 | + $this->setLog($log); | |
| 172 | + | |
| 173 | + // re-build the log by appending the existing logs | |
| 174 | + $this->log .= $separator . $current; | |
| 175 | + | |
| 176 | + return $this; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * Registers the additional transaction data. | |
| 181 | + * | |
| 182 | + * @param string $key The transaction key. | |
| 183 | + * @param mixed $val The transaction value. | |
| 184 | + * | |
| 185 | + * @return self This object to support chaining. | |
| 186 | + */ | |
| 187 | + public function setData($key, $val) | |
| 188 | + { | |
| 189 | + $this->data[$key] = $val; | |
| 190 | + | |
| 191 | + return $this; | |
| 192 | + } | |
| 193 | +} | |