Total Complexity | 95 |
Total Lines | 896 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Payment_Form_Submission often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GetPaid_Payment_Form_Submission, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class GetPaid_Payment_Form_Submission { |
||
11 | |||
12 | /** |
||
13 | * Submission ID |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | public $id = null; |
||
18 | |||
19 | /** |
||
20 | * The raw submission data. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $data = null; |
||
25 | |||
26 | /** |
||
27 | * Submission totals |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $totals = array( |
||
32 | |||
33 | 'subtotal' => array( |
||
34 | 'initial' => 0, |
||
35 | 'recurring' => 0, |
||
36 | ), |
||
37 | |||
38 | 'discount' => array( |
||
39 | 'initial' => 0, |
||
40 | 'recurring' => 0, |
||
41 | ), |
||
42 | |||
43 | 'fees' => array( |
||
44 | 'initial' => 0, |
||
45 | 'recurring' => 0, |
||
46 | ), |
||
47 | |||
48 | 'taxes' => array( |
||
49 | 'initial' => 0, |
||
50 | 'recurring' => 0, |
||
51 | ), |
||
52 | |||
53 | ); |
||
54 | |||
55 | /** |
||
56 | * Sets the associated payment form. |
||
57 | * |
||
58 | * @var GetPaid_Payment_Form |
||
59 | */ |
||
60 | protected $payment_form = null; |
||
61 | |||
62 | /** |
||
63 | * The country for the submission. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | public $country = null; |
||
68 | |||
69 | /** |
||
70 | * The state for the submission. |
||
71 | * |
||
72 | * @since 1.0.19 |
||
73 | * @var string |
||
74 | */ |
||
75 | public $state = null; |
||
76 | |||
77 | /** |
||
78 | * The invoice associated with the submission. |
||
79 | * |
||
80 | * @var WPInv_Invoice |
||
81 | */ |
||
82 | protected $invoice = null; |
||
83 | |||
84 | /** |
||
85 | * The recurring item for the submission. |
||
86 | * |
||
87 | * @var int |
||
88 | */ |
||
89 | public $has_recurring = 0; |
||
90 | |||
91 | /** |
||
92 | * An array of fees for the submission. |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $fees = array(); |
||
97 | |||
98 | /** |
||
99 | * An array of discounts for the submission. |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $discounts = array(); |
||
104 | |||
105 | /** |
||
106 | * An array of taxes for the submission. |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $taxes = array(); |
||
111 | |||
112 | /** |
||
113 | * An array of items for the submission. |
||
114 | * |
||
115 | * @var GetPaid_Form_Item[] |
||
116 | */ |
||
117 | protected $items = array(); |
||
118 | |||
119 | /** |
||
120 | * The last error. |
||
121 | * |
||
122 | * @var string |
||
123 | */ |
||
124 | public $last_error = null; |
||
125 | |||
126 | /** |
||
127 | * The last error code. |
||
128 | * |
||
129 | * @var string |
||
130 | */ |
||
131 | public $last_error_code = null; |
||
132 | |||
133 | /** |
||
134 | * Class constructor. |
||
135 | * |
||
136 | */ |
||
137 | public function __construct() { |
||
138 | |||
139 | // Set the state and country to the default state and country. |
||
140 | $this->country = wpinv_default_billing_country(); |
||
141 | $this->state = wpinv_get_default_state(); |
||
142 | |||
143 | // Do we have an actual submission? |
||
144 | if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
||
145 | $this->load_data( $_POST ); |
||
146 | } |
||
147 | |||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Loads submission data. |
||
152 | * |
||
153 | * @param array $data |
||
154 | */ |
||
155 | public function load_data( $data ) { |
||
156 | |||
157 | // Remove slashes from the submitted data... |
||
158 | $data = wp_unslash( $data ); |
||
159 | |||
160 | // Allow plugins to filter the data. |
||
161 | $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
||
162 | |||
163 | // Cache it... |
||
164 | $this->data = $data; |
||
165 | |||
166 | // Then generate a unique id from the data. |
||
167 | $this->id = md5( wp_json_encode( $data ) ); |
||
|
|||
168 | |||
169 | // Finally, process the submission. |
||
170 | try { |
||
171 | |||
172 | // Each process is passed an instance of the class (with reference) |
||
173 | // and should throw an Exception whenever it encounters one. |
||
174 | $processors = apply_filters( |
||
175 | 'getpaid_payment_form_submission_processors', |
||
176 | array( |
||
177 | array( $this, 'process_payment_form' ), |
||
178 | array( $this, 'process_invoice' ), |
||
179 | array( $this, 'process_fees' ), |
||
180 | array( $this, 'process_items' ), |
||
181 | array( $this, 'process_discount' ), |
||
182 | array( $this, 'process_taxes' ), |
||
183 | ), |
||
184 | $this |
||
185 | ); |
||
186 | |||
187 | foreach ( $processors as $processor ) { |
||
188 | call_user_func_array( $processor, array( &$this ) ); |
||
189 | } |
||
190 | |||
191 | } catch( GetPaid_Payment_Exception $e ) { |
||
192 | $this->last_error = $e->getMessage(); |
||
193 | $this->last_error_code = $e->getErrorCode(); |
||
194 | } catch ( Exception $e ) { |
||
195 | $this->last_error = $e->getMessage(); |
||
196 | $this->last_error_code = $e->getCode(); |
||
197 | } |
||
198 | |||
199 | // Fired when we are done processing a submission. |
||
200 | do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
||
201 | |||
202 | } |
||
203 | |||
204 | /* |
||
205 | |-------------------------------------------------------------------------- |
||
206 | | Payment Forms. |
||
207 | |-------------------------------------------------------------------------- |
||
208 | | |
||
209 | | Functions for dealing with the submission's payment form. Ensure that the |
||
210 | | submission has an active payment form etc. |
||
211 | */ |
||
212 | |||
213 | /** |
||
214 | * Prepares the submission's payment form. |
||
215 | * |
||
216 | * @since 1.0.19 |
||
217 | */ |
||
218 | public function process_payment_form() { |
||
219 | |||
220 | // Every submission needs an active payment form. |
||
221 | if ( empty( $this->data['form_id'] ) ) { |
||
222 | throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
||
223 | } |
||
224 | |||
225 | // Fetch the payment form. |
||
226 | $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
||
227 | |||
228 | if ( ! $this->payment_form->is_active() ) { |
||
229 | throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
||
230 | } |
||
231 | |||
232 | do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Returns the payment form. |
||
237 | * |
||
238 | * @since 1.0.19 |
||
239 | * @return GetPaid_Payment_Form |
||
240 | */ |
||
241 | public function get_payment_form() { |
||
242 | return $this->payment_form; |
||
243 | } |
||
244 | |||
245 | /* |
||
246 | |-------------------------------------------------------------------------- |
||
247 | | Invoices. |
||
248 | |-------------------------------------------------------------------------- |
||
249 | | |
||
250 | | Functions for dealing with the submission's invoice. Some submissions |
||
251 | | might be for an existing invoice. |
||
252 | */ |
||
253 | |||
254 | /** |
||
255 | * Prepares the submission's invoice. |
||
256 | * |
||
257 | * @since 1.0.19 |
||
258 | */ |
||
259 | public function process_invoice() { |
||
260 | |||
261 | // Abort if there is no invoice. |
||
262 | if ( empty( $this->data['invoice_id'] ) ) { |
||
263 | return; |
||
264 | } |
||
265 | |||
266 | // If the submission is for an existing invoice, ensure that it exists |
||
267 | // and that it is not paid for. |
||
268 | $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
||
269 | |||
270 | if ( empty( $invoice ) ) { |
||
271 | throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
||
272 | } |
||
273 | |||
274 | if ( $invoice->is_paid() ) { |
||
275 | throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
||
276 | } |
||
277 | |||
278 | $this->payment_form->invoice = $invoice; |
||
279 | if ( ! $this->payment_form->is_default() ) { |
||
280 | |||
281 | $items = array(); |
||
282 | $item_ids = array(); |
||
283 | |||
284 | foreach ( $invoice->get_items() as $item ) { |
||
285 | if ( ! in_array( $item->get_id(), $item_ids ) ) { |
||
286 | $item_ids[] = $item->get_id(); |
||
287 | $items[] = $item; |
||
288 | } |
||
289 | } |
||
290 | |||
291 | foreach ( $this->payment_form->get_items() as $item ) { |
||
292 | if ( ! in_array( $item->get_id(), $item_ids ) ) { |
||
293 | $item_ids[] = $item->get_id(); |
||
294 | $items[] = $item; |
||
295 | } |
||
296 | } |
||
297 | |||
298 | $this->payment_form->set_items( $items ); |
||
299 | |||
300 | } else { |
||
301 | $this->payment_form->set_items( $invoice->get_items() ); |
||
302 | } |
||
303 | |||
304 | $this->country = $invoice->get_country(); |
||
305 | $this->state = $invoice->get_state(); |
||
306 | $this->invoice = $invoice; |
||
307 | |||
308 | do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Returns the associated invoice. |
||
313 | * |
||
314 | * @since 1.0.19 |
||
315 | * @return WPInv_Invoice |
||
316 | */ |
||
317 | public function get_invoice() { |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Checks whether there is an invoice associated with this submission. |
||
323 | * |
||
324 | * @since 1.0.19 |
||
325 | * @return bool |
||
326 | */ |
||
327 | public function has_invoice() { |
||
328 | return ! empty( $this->invoice ); |
||
329 | } |
||
330 | |||
331 | /* |
||
332 | |-------------------------------------------------------------------------- |
||
333 | | Items. |
||
334 | |-------------------------------------------------------------------------- |
||
335 | | |
||
336 | | Functions for dealing with the submission's items. Submissions can only have one |
||
337 | | recurring item. But can have an unlimited number of non-recurring items. |
||
338 | */ |
||
339 | |||
340 | /** |
||
341 | * Prepares the submission's items. |
||
342 | * |
||
343 | * @since 1.0.19 |
||
344 | */ |
||
345 | public function process_items() { |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Adds an item to the submission. |
||
358 | * |
||
359 | * @since 1.0.19 |
||
360 | * @param GetPaid_Form_Item $item |
||
361 | */ |
||
362 | public function add_item( $item ) { |
||
363 | |||
364 | // Make sure that it is available for purchase. |
||
365 | if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
||
366 | return; |
||
367 | } |
||
368 | |||
369 | // Each submission can only contain one recurring item. |
||
370 | if ( $item->is_recurring() ) { |
||
371 | $this->has_recurring = $item->get_id(); |
||
372 | } |
||
373 | |||
374 | // Update the items and totals. |
||
375 | $this->items[ $item->get_id() ] = $item; |
||
376 | $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
||
377 | $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
||
378 | |||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Removes a specific item. |
||
383 | * |
||
384 | * You should not call this method after the discounts and taxes |
||
385 | * have been calculated. |
||
386 | * |
||
387 | * @since 1.0.19 |
||
388 | */ |
||
389 | public function remove_item( $item_id ) { |
||
390 | |||
391 | if ( isset( $this->items[ $item_id ] ) ) { |
||
392 | $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
||
393 | $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
||
394 | |||
395 | if ( $this->items[ $item_id ]->is_recurring() ) { |
||
396 | $this->has_recurring = 0; |
||
397 | } |
||
398 | |||
399 | unset( $this->items[ $item_id ] ); |
||
400 | } |
||
401 | |||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Returns the subtotal. |
||
406 | * |
||
407 | * @since 1.0.19 |
||
408 | */ |
||
409 | public function get_subtotal() { |
||
410 | |||
411 | if ( wpinv_prices_include_tax() ) { |
||
412 | return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
||
413 | } |
||
414 | |||
415 | return $this->totals['subtotal']['initial']; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Returns the recurring subtotal. |
||
420 | * |
||
421 | * @since 1.0.19 |
||
422 | */ |
||
423 | public function get_recurring_subtotal() { |
||
424 | |||
425 | if ( wpinv_prices_include_tax() ) { |
||
426 | return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
||
427 | } |
||
428 | |||
429 | return $this->totals['subtotal']['recurring']; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Returns all items. |
||
434 | * |
||
435 | * @since 1.0.19 |
||
436 | * @return GetPaid_Form_Item[] |
||
437 | */ |
||
438 | public function get_items() { |
||
439 | return $this->items; |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Checks if there's a single subscription group in the submission. |
||
444 | * |
||
445 | * @since 2.3.0 |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function has_subscription_group() { |
||
449 | return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) ); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Checks if there are multipe subscription groups in the submission. |
||
454 | * |
||
455 | * @since 2.3.0 |
||
456 | * @return bool |
||
457 | */ |
||
458 | public function has_multiple_subscription_groups() { |
||
459 | return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) ); |
||
460 | } |
||
461 | |||
462 | /* |
||
463 | |-------------------------------------------------------------------------- |
||
464 | | Taxes |
||
465 | |-------------------------------------------------------------------------- |
||
466 | | |
||
467 | | Functions for dealing with submission taxes. Taxes can be recurring |
||
468 | | or only one-time. |
||
469 | */ |
||
470 | |||
471 | /** |
||
472 | * Prepares the submission's taxes. |
||
473 | * |
||
474 | * @since 1.0.19 |
||
475 | */ |
||
476 | public function process_taxes() { |
||
477 | |||
478 | // Abort if we're not using taxes. |
||
479 | if ( ! $this->use_taxes() ) { |
||
480 | return; |
||
481 | } |
||
482 | |||
483 | // If a custom country && state has been passed in, use it to calculate taxes. |
||
484 | $country = $this->get_field( 'wpinv_country', 'billing' ); |
||
485 | if ( ! empty( $country ) ) { |
||
486 | $this->country = $country; |
||
487 | } |
||
488 | |||
489 | $state = $this->get_field( 'wpinv_state', 'billing' ); |
||
490 | if ( ! empty( $state ) ) { |
||
491 | $this->state = $state; |
||
492 | } |
||
493 | |||
494 | // Confirm if the provided country and the ip country are similar. |
||
495 | $address_confirmed = $this->get_field( 'confirm-address' ); |
||
496 | if ( wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
||
497 | throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
||
498 | } |
||
499 | |||
500 | // Abort if the country is not taxable. |
||
501 | if ( ! wpinv_is_country_taxable( $this->country ) ) { |
||
502 | return; |
||
503 | } |
||
504 | |||
505 | $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
||
506 | |||
507 | foreach ( $processor->taxes as $tax ) { |
||
508 | $this->add_tax( $tax ); |
||
509 | } |
||
510 | |||
511 | do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Adds a tax to the submission. |
||
516 | * |
||
517 | * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
||
518 | * @since 1.0.19 |
||
519 | */ |
||
520 | public function add_tax( $tax ) { |
||
521 | |||
522 | if ( wpinv_round_tax_per_tax_rate() ) { |
||
523 | $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
||
524 | $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
||
525 | } |
||
526 | |||
527 | $this->taxes[ $tax['name'] ] = $tax; |
||
528 | $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
||
529 | $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
||
530 | |||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Removes a specific tax. |
||
535 | * |
||
536 | * @since 1.0.19 |
||
537 | */ |
||
538 | public function remove_tax( $tax_name ) { |
||
539 | |||
540 | if ( isset( $this->taxes[ $tax_name ] ) ) { |
||
541 | $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
||
542 | $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
||
543 | unset( $this->taxes[ $tax_name ] ); |
||
544 | } |
||
545 | |||
546 | } |
||
547 | |||
548 | /** |
||
549 | * Whether or not we'll use taxes for the submission. |
||
550 | * |
||
551 | * @since 1.0.19 |
||
552 | */ |
||
553 | public function use_taxes() { |
||
554 | |||
555 | $use_taxes = wpinv_use_taxes(); |
||
556 | |||
557 | if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
||
558 | $use_taxes = false; |
||
559 | } |
||
560 | |||
561 | return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
||
562 | |||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Returns the tax. |
||
567 | * |
||
568 | * @since 1.0.19 |
||
569 | */ |
||
570 | public function get_tax() { |
||
571 | return $this->totals['taxes']['initial']; |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * Returns the recurring tax. |
||
576 | * |
||
577 | * @since 1.0.19 |
||
578 | */ |
||
579 | public function get_recurring_tax() { |
||
580 | return $this->totals['taxes']['recurring']; |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Returns all taxes. |
||
585 | * |
||
586 | * @since 1.0.19 |
||
587 | */ |
||
588 | public function get_taxes() { |
||
589 | return $this->taxes; |
||
590 | } |
||
591 | |||
592 | /* |
||
593 | |-------------------------------------------------------------------------- |
||
594 | | Discounts |
||
595 | |-------------------------------------------------------------------------- |
||
596 | | |
||
597 | | Functions for dealing with submission discounts. Discounts can be recurring |
||
598 | | or only one-time. They also do not have to come from a discount code. |
||
599 | */ |
||
600 | |||
601 | /** |
||
602 | * Prepares the submission's discount. |
||
603 | * |
||
604 | * @since 1.0.19 |
||
605 | */ |
||
606 | public function process_discount() { |
||
607 | |||
608 | $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
||
609 | $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
||
610 | $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
||
611 | |||
612 | foreach ( $processor->discounts as $discount ) { |
||
613 | $this->add_discount( $discount ); |
||
614 | } |
||
615 | |||
616 | do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * Adds a discount to the submission. |
||
621 | * |
||
622 | * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
||
623 | * @since 1.0.19 |
||
624 | */ |
||
625 | public function add_discount( $discount ) { |
||
626 | $this->discounts[ $discount['name'] ] = $discount; |
||
627 | $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
||
628 | $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
||
629 | } |
||
630 | |||
631 | /** |
||
632 | * Removes a discount from the submission. |
||
633 | * |
||
634 | * @since 1.0.19 |
||
635 | */ |
||
636 | public function remove_discount( $name ) { |
||
637 | |||
638 | if ( isset( $this->discounts[ $name ] ) ) { |
||
639 | $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
||
640 | $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
||
641 | unset( $this->discounts[ $name ] ); |
||
642 | } |
||
643 | |||
644 | } |
||
645 | |||
646 | /** |
||
647 | * Checks whether there is a discount code associated with this submission. |
||
648 | * |
||
649 | * @since 1.0.19 |
||
650 | * @return bool |
||
651 | */ |
||
652 | public function has_discount_code() { |
||
653 | return ! empty( $this->discounts['discount_code'] ); |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * Returns the discount code. |
||
658 | * |
||
659 | * @since 1.0.19 |
||
660 | * @return string |
||
661 | */ |
||
662 | public function get_discount_code() { |
||
663 | return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
||
664 | } |
||
665 | |||
666 | /** |
||
667 | * Returns the discount. |
||
668 | * |
||
669 | * @since 1.0.19 |
||
670 | */ |
||
671 | public function get_discount() { |
||
672 | return $this->totals['discount']['initial']; |
||
673 | } |
||
674 | |||
675 | /** |
||
676 | * Returns the recurring discount. |
||
677 | * |
||
678 | * @since 1.0.19 |
||
679 | */ |
||
680 | public function get_recurring_discount() { |
||
681 | return $this->totals['discount']['recurring']; |
||
682 | } |
||
683 | |||
684 | /** |
||
685 | * Returns all discounts. |
||
686 | * |
||
687 | * @since 1.0.19 |
||
688 | */ |
||
689 | public function get_discounts() { |
||
690 | return $this->discounts; |
||
691 | } |
||
692 | |||
693 | /* |
||
694 | |-------------------------------------------------------------------------- |
||
695 | | Fees |
||
696 | |-------------------------------------------------------------------------- |
||
697 | | |
||
698 | | Functions for dealing with submission fees. Fees can be recurring |
||
699 | | or only one-time. Price input and Price select elements are treated as |
||
700 | | fees. |
||
701 | */ |
||
702 | |||
703 | /** |
||
704 | * Prepares the submission's fees. |
||
705 | * |
||
706 | * @since 1.0.19 |
||
707 | */ |
||
708 | public function process_fees() { |
||
709 | |||
710 | $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
||
711 | |||
712 | foreach ( $fees_processor->fees as $fee ) { |
||
713 | $this->add_fee( $fee ); |
||
714 | } |
||
715 | |||
716 | do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * Adds a fee to the submission. |
||
721 | * |
||
722 | * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
||
723 | * @since 1.0.19 |
||
724 | */ |
||
725 | public function add_fee( $fee ) { |
||
726 | |||
727 | $this->fees[ $fee['name'] ] = $fee; |
||
728 | $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
||
729 | $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
||
730 | |||
731 | } |
||
732 | |||
733 | /** |
||
734 | * Removes a fee from the submission. |
||
735 | * |
||
736 | * @since 1.0.19 |
||
737 | */ |
||
738 | public function remove_fee( $name ) { |
||
739 | |||
740 | if ( isset( $this->fees[ $name ] ) ) { |
||
741 | $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
||
742 | $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
||
743 | unset( $this->fees[ $name ] ); |
||
744 | } |
||
745 | |||
746 | } |
||
747 | |||
748 | /** |
||
749 | * Returns the fees. |
||
750 | * |
||
751 | * @since 1.0.19 |
||
752 | */ |
||
753 | public function get_fee() { |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * Returns the recurring fees. |
||
759 | * |
||
760 | * @since 1.0.19 |
||
761 | */ |
||
762 | public function get_recurring_fee() { |
||
763 | return $this->totals['fees']['recurring']; |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * Returns all fees. |
||
768 | * |
||
769 | * @since 1.0.19 |
||
770 | */ |
||
771 | public function get_fees() { |
||
772 | return $this->fees; |
||
773 | } |
||
774 | |||
775 | /** |
||
776 | * Checks if there are any fees for the form. |
||
777 | * |
||
778 | * @return bool |
||
779 | * @since 1.0.19 |
||
780 | */ |
||
781 | public function has_fees() { |
||
782 | return count( $this->fees ) !== 0; |
||
783 | } |
||
784 | |||
785 | /* |
||
786 | |-------------------------------------------------------------------------- |
||
787 | | MISC |
||
788 | |-------------------------------------------------------------------------- |
||
789 | | |
||
790 | | Extra submission functions. |
||
791 | */ |
||
792 | |||
793 | /** |
||
794 | * Checks if this is the initial fetch. |
||
795 | * |
||
796 | * @return bool |
||
797 | * @since 1.0.19 |
||
798 | */ |
||
799 | public function is_initial_fetch() { |
||
800 | return empty( $this->data['initial_state'] ); |
||
801 | } |
||
802 | |||
803 | /** |
||
804 | * Returns the total amount to collect for this submission. |
||
805 | * |
||
806 | * @since 1.0.19 |
||
807 | */ |
||
808 | public function get_total() { |
||
809 | $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount(); |
||
810 | return max( $total, 0 ); |
||
811 | } |
||
812 | |||
813 | /** |
||
814 | * Returns the recurring total amount to collect for this submission. |
||
815 | * |
||
816 | * @since 1.0.19 |
||
817 | */ |
||
818 | public function get_recurring_total() { |
||
819 | $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount(); |
||
820 | return max( $total, 0 ); |
||
821 | } |
||
822 | |||
823 | /** |
||
824 | * Whether payment details should be collected for this submission. |
||
825 | * |
||
826 | * @since 1.0.19 |
||
827 | */ |
||
828 | public function should_collect_payment_details() { |
||
829 | $initial = $this->get_total(); |
||
830 | $recurring = $this->get_recurring_total(); |
||
831 | |||
832 | if ( $this->has_recurring == 0 ) { |
||
833 | $recurring = 0; |
||
834 | } |
||
835 | |||
836 | $collect = $initial > 0 || $recurring > 0; |
||
837 | return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
||
838 | } |
||
839 | |||
840 | /** |
||
841 | * Returns the billing email of the user. |
||
842 | * |
||
843 | * @since 1.0.19 |
||
844 | */ |
||
845 | public function get_billing_email() { |
||
846 | return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
||
847 | } |
||
848 | |||
849 | /** |
||
850 | * Checks if the submitter has a billing email. |
||
851 | * |
||
852 | * @since 1.0.19 |
||
853 | */ |
||
854 | public function has_billing_email() { |
||
855 | $billing_email = $this->get_billing_email(); |
||
856 | return ! empty( $billing_email ) && is_email( $billing_email ); |
||
857 | } |
||
858 | |||
859 | /** |
||
860 | * Returns the appropriate currency for the submission. |
||
861 | * |
||
862 | * @since 1.0.19 |
||
863 | * @return string |
||
864 | */ |
||
865 | public function get_currency() { |
||
866 | return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
||
867 | } |
||
868 | |||
869 | /** |
||
870 | * Returns the raw submission data. |
||
871 | * |
||
872 | * @since 1.0.19 |
||
873 | * @return array |
||
874 | */ |
||
875 | public function get_data() { |
||
876 | return $this->data; |
||
877 | } |
||
878 | |||
879 | /** |
||
880 | * Returns a field from the submission data |
||
881 | * |
||
882 | * @param string $field |
||
883 | * @since 1.0.19 |
||
884 | * @return mixed|null |
||
885 | */ |
||
886 | public function get_field( $field, $sub_array_key = null ) { |
||
887 | return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
||
888 | } |
||
889 | |||
890 | /** |
||
891 | * Checks if a required field is set. |
||
892 | * |
||
893 | * @since 1.0.19 |
||
894 | */ |
||
895 | public function is_required_field_set( $field ) { |
||
897 | } |
||
898 | |||
899 | /** |
||
900 | * Formats an amount |
||
901 | * |
||
902 | * @since 1.0.19 |
||
903 | */ |
||
904 | public function format_amount( $amount ) { |
||
906 | } |
||
907 | |||
908 | } |
||
909 |