PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.3.7
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.3.7
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Models / Payment.php

Payment.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.3.7, at includes/Models/Payment.php

244 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Payment Model
4 *
5 * @package Easy_Invoice
6 */
7
8 namespace EasyInvoice\Models;
9
10 class Payment {
11 /**
12 * Payment ID
13 *
14 * @var int
15 */
16 private $id;
17
18 /**
19 * Dynamic data storage for all fields
20 *
21 * @var array
22 */
23 private $data = [];
24
25 /**
26 * Modified flag
27 *
28 * @var bool
29 */
30 private $is_modified = false;
31
32 /**
33 * Constructor
34 *
35 * @param \WP_Post|int $post
36 */
37 public function __construct($post) {
38 if (is_numeric($post)) {
39 $post = get_post($post);
40 }
41
42 if (!$post || $post->post_type !== 'easy_invoice_payment') {
43 throw new \Exception('Invalid payment post');
44 }
45
46 $this->id = $post->ID;
47 $this->loadMeta();
48 }
49
50 /**
51 * Load payment meta data
52 */
53 private function loadMeta() {
54 $this->data = array(
55 'invoice_id' => get_post_meta($this->id, '_invoice_id', true),
56 'amount' => get_post_meta($this->id, '_amount', true),
57 'payment_method' => get_post_meta($this->id, '_payment_method', true),
58 'status' => get_post_meta($this->id, '_status', true),
59 'transaction_id' => get_post_meta($this->id, '_transaction_id', true),
60 'payment_date' => get_post_meta($this->id, '_payment_date', true),
61 'notes' => get_post_meta($this->id, '_notes', true),
62 'payment_type' => get_post_meta($this->id, '_payment_type', true),
63 'recurring_id' => get_post_meta($this->id, '_recurring_id', true),
64 'parent_payment_id' => get_post_meta($this->id, '_parent_payment_id', true),
65 'gateway_response' => get_post_meta($this->id, '_gateway_response', true),
66 'currency' => get_post_meta($this->id, '_currency', true),
67 'currency_symbol' => get_post_meta($this->id, '_currency_symbol', true),
68 );
69 }
70
71 /**
72 * Magic getter for dynamic properties
73 *
74 * @param string $name
75 * @return mixed
76 */
77 public function __get($name) {
78 if (isset($this->data[$name])) {
79 return $this->data[$name];
80 }
81 return null;
82 }
83
84 /**
85 * Magic setter for dynamic properties
86 *
87 * @param string $name
88 * @param mixed $value
89 */
90 public function __set($name, $value) {
91 $this->data[$name] = $value;
92 $this->is_modified = true;
93 }
94
95 /**
96 * Magic isset for dynamic properties
97 *
98 * @param string $name
99 * @return bool
100 */
101 public function __isset($name) {
102 return isset($this->data[$name]);
103 }
104
105 /**
106 * Magic unset for dynamic properties
107 *
108 * @param string $name
109 */
110 public function __unset($name) {
111 unset($this->data[$name]);
112 $this->is_modified = true;
113 }
114
115 /**
116 * Dynamic getter method
117 *
118 * @since 1.0.0
119 * @param string $name Method name
120 * @param array $arguments Method arguments
121 * @return mixed
122 */
123 public function __call($name, $arguments) {
124 // Handle getter methods (getFieldName)
125 if (strpos($name, 'get') === 0) {
126 $field_name = $this->camelCaseToSnakeCase(substr($name, 3)); // Remove 'get' prefix
127 return $this->__get($field_name);
128 }
129
130 // Handle setter methods (setFieldName)
131 if (strpos($name, 'set') === 0) {
132 $field_name = $this->camelCaseToSnakeCase(substr($name, 3)); // Remove 'set' prefix
133 $value = $arguments[0] ?? null;
134 $this->__set($field_name, $value);
135 return null;
136 }
137
138 // Handle isset methods (isFieldName)
139 if (strpos($name, 'is') === 0) {
140 $field_name = $this->camelCaseToSnakeCase(substr($name, 2)); // Remove 'is' prefix
141 return (bool) $this->__get($field_name);
142 }
143
144 // Handle has methods (hasFieldName)
145 if (strpos($name, 'has') === 0) {
146 $field_name = $this->camelCaseToSnakeCase(substr($name, 3)); // Remove 'has' prefix
147 return !empty($this->__get($field_name));
148 }
149
150 throw new \BadMethodCallException("Method $name does not exist");
151 }
152
153 /**
154 * Convert camelCase to snake_case
155 *
156 * @since 1.0.0
157 * @param string $camelCase
158 * @return string
159 */
160 private function camelCaseToSnakeCase($camelCase) {
161 return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $camelCase));
162 }
163
164 // Essential methods only
165 public function getId() { return $this->id; }
166 public function isModified(): bool { return $this->is_modified; }
167 public function toArray(): array { return array_merge(['id' => $this->id], $this->data); }
168
169 /**
170 * Check if payment exists
171 *
172 * @return bool
173 */
174 public function exists(): bool {
175 return $this->id > 0 && get_post($this->id) !== null;
176 }
177
178 /**
179 * Update payment data
180 *
181 * @param array $data Payment data to update
182 * @return bool True on success, false on failure
183 */
184 public function update(array $data): bool {
185 if (!$this->exists()) {
186 return false;
187 }
188
189 // Update post meta for each field
190 $meta_fields = [
191 'invoice_id' => '_invoice_id',
192 'amount' => '_amount',
193 'payment_method' => '_payment_method',
194 'status' => '_status',
195 'transaction_id' => '_transaction_id',
196 'payment_date' => '_payment_date',
197 'notes' => '_notes',
198 'payment_type' => '_payment_type',
199 'recurring_id' => '_recurring_id',
200 'parent_payment_id' => '_parent_payment_id',
201 'gateway_response' => '_gateway_response',
202 'currency' => '_currency',
203 'currency_symbol' => '_currency_symbol',
204 ];
205
206 $success = true;
207
208 foreach ($meta_fields as $field => $meta_key) {
209 if (isset($data[$field])) {
210 // update_post_meta returns false if value is unchanged (not an error)
211 // It returns meta_id on success, or true if updated
212 // We need to check if the meta exists to determine real failure
213 $old_value = get_post_meta($this->id, $meta_key, true);
214 $result = update_post_meta($this->id, $meta_key, $data[$field]);
215
216 // Only consider it a failure if the result is false AND the value wasn't already the same
217 if ($result === false && $old_value !== $data[$field]) {
218 //$success = false;
219 }
220
221 // Always update local data since we're setting it
222 $this->data[$field] = $data[$field];
223 }
224 }
225
226 // Update the post title if invoice_id is provided
227 if (isset($data['invoice_id']) && $data['invoice_id']) {
228 $invoice = new \EasyInvoice\Models\Invoice($data['invoice_id']);
229 if ($invoice->exists()) {
230 $post_title = sprintf(
231 __('Payment for Invoice %s', 'easy-invoice'),
232 $invoice->getNumber()
233 );
234 wp_update_post([
235 'ID' => $this->id,
236 'post_title' => $post_title
237 ]);
238 }
239 }
240
241 $this->is_modified = false;
242 return $success;
243 }
244 }