Passed
Push — master ( d57609...3cea45 )
by Brian
05:00
created
includes/payments/class-getpaid-payment-form-submission.php 1 patch
Indentation   +761 added lines, -761 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 if ( ! defined( 'ABSPATH' ) ) {
3
-	exit;
3
+    exit;
4 4
 }
5 5
 
6 6
 /**
@@ -10,187 +10,187 @@  discard block
 block discarded – undo
10 10
 class GetPaid_Payment_Form_Submission {
11 11
 
12 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
-	 */
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 60
     protected $payment_form = null;
61 61
 
62 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
-	 * Class constructor.
128
-	 *
129
-	 */
130
-	public function __construct() {
131
-
132
-		// Set the state and country to the default state and country.
133
-		$this->country = wpinv_default_billing_country();
134
-		$this->state   = wpinv_get_default_state();
135
-
136
-		// Do we have an actual submission?
137
-		if ( isset( $_POST['getpaid_payment_form_submission'] ) ) {
138
-			$this->load_data( $_POST );
139
-		}
140
-
141
-	}
142
-
143
-	/**
144
-	 * Loads submission data.
145
-	 *
146
-	 * @param array $data
147
-	 */
148
-	public function load_data( $data ) {
149
-
150
-		// Remove slashes from the submitted data...
151
-		$data       = wp_unslash( $data );
152
-
153
-		// Allow plugins to filter the data.
154
-		$data       = apply_filters( 'getpaid_submission_data', $data, $this );
155
-
156
-		// Cache it...
157
-		$this->data = $data;
158
-
159
-		// Then generate a unique id from the data.
160
-		$this->id   = md5( wp_json_encode( $data ) );
161
-
162
-		// Finally, process the submission.
163
-		try {
164
-
165
-			// Each process is passed an instance of the class (with reference)
166
-			// and should throw an Exception whenever it encounters one.
167
-			$processors = apply_filters(
168
-				'getpaid_payment_form_submission_processors',
169
-				array(
170
-					array( $this, 'process_payment_form' ),
171
-					array( $this, 'process_invoice' ),
172
-					array( $this, 'process_fees' ),
173
-					array( $this, 'process_items' ),
174
-					array( $this, 'process_taxes' ),
175
-					array( $this, 'process_discount' ),
176
-				),
177
-				$this		
178
-			);
179
-
180
-			foreach ( $processors as $processor ) {
181
-				call_user_func_array( $processor, array( &$this ) );
182
-			}
183
-
184
-		} catch ( Exception $e ) {
185
-			$this->last_error = $e->getMessage();
186
-		}
187
-
188
-		// Fired when we are done processing a submission.
189
-		do_action_ref_array( 'getpaid_process_submission', array( &$this ) );
190
-
191
-	}
192
-
193
-	/*
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
+     * Class constructor.
128
+     *
129
+     */
130
+    public function __construct() {
131
+
132
+        // Set the state and country to the default state and country.
133
+        $this->country = wpinv_default_billing_country();
134
+        $this->state   = wpinv_get_default_state();
135
+
136
+        // Do we have an actual submission?
137
+        if ( isset( $_POST['getpaid_payment_form_submission'] ) ) {
138
+            $this->load_data( $_POST );
139
+        }
140
+
141
+    }
142
+
143
+    /**
144
+     * Loads submission data.
145
+     *
146
+     * @param array $data
147
+     */
148
+    public function load_data( $data ) {
149
+
150
+        // Remove slashes from the submitted data...
151
+        $data       = wp_unslash( $data );
152
+
153
+        // Allow plugins to filter the data.
154
+        $data       = apply_filters( 'getpaid_submission_data', $data, $this );
155
+
156
+        // Cache it...
157
+        $this->data = $data;
158
+
159
+        // Then generate a unique id from the data.
160
+        $this->id   = md5( wp_json_encode( $data ) );
161
+
162
+        // Finally, process the submission.
163
+        try {
164
+
165
+            // Each process is passed an instance of the class (with reference)
166
+            // and should throw an Exception whenever it encounters one.
167
+            $processors = apply_filters(
168
+                'getpaid_payment_form_submission_processors',
169
+                array(
170
+                    array( $this, 'process_payment_form' ),
171
+                    array( $this, 'process_invoice' ),
172
+                    array( $this, 'process_fees' ),
173
+                    array( $this, 'process_items' ),
174
+                    array( $this, 'process_taxes' ),
175
+                    array( $this, 'process_discount' ),
176
+                ),
177
+                $this		
178
+            );
179
+
180
+            foreach ( $processors as $processor ) {
181
+                call_user_func_array( $processor, array( &$this ) );
182
+            }
183
+
184
+        } catch ( Exception $e ) {
185
+            $this->last_error = $e->getMessage();
186
+        }
187
+
188
+        // Fired when we are done processing a submission.
189
+        do_action_ref_array( 'getpaid_process_submission', array( &$this ) );
190
+
191
+    }
192
+
193
+    /*
194 194
 	|--------------------------------------------------------------------------
195 195
 	| Payment Forms.
196 196
 	|--------------------------------------------------------------------------
@@ -199,39 +199,39 @@  discard block
 block discarded – undo
199 199
 	| submission has an active payment form etc.
200 200
     */
201 201
 
202
-	/**
203
-	 * Prepares the submission's payment form.
204
-	 *
205
-	 * @since 1.0.19
206
-	 */
207
-	public function process_payment_form() {
202
+    /**
203
+     * Prepares the submission's payment form.
204
+     *
205
+     * @since 1.0.19
206
+     */
207
+    public function process_payment_form() {
208 208
 
209
-		// Every submission needs an active payment form.
210
-		if ( empty( $this->data['form_id'] ) ) {
211
-			throw new Exception( __( 'Missing payment form', 'invoicing' ) );
212
-		}
209
+        // Every submission needs an active payment form.
210
+        if ( empty( $this->data['form_id'] ) ) {
211
+            throw new Exception( __( 'Missing payment form', 'invoicing' ) );
212
+        }
213 213
 
214
-		// Fetch the payment form.
215
-		$this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] );
214
+        // Fetch the payment form.
215
+        $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] );
216 216
 
217
-		if ( ! $this->payment_form->is_active() ) {
218
-			throw new Exception( __( 'Payment form not active', 'invoicing' ) );
219
-		}
217
+        if ( ! $this->payment_form->is_active() ) {
218
+            throw new Exception( __( 'Payment form not active', 'invoicing' ) );
219
+        }
220 220
 
221
-		do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) );
222
-	}
221
+        do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) );
222
+    }
223 223
 
224 224
     /**
225
-	 * Returns the payment form.
226
-	 *
227
-	 * @since 1.0.19
228
-	 * @return GetPaid_Payment_Form
229
-	 */
230
-	public function get_payment_form() {
231
-		return $this->payment_form;
232
-	}
225
+     * Returns the payment form.
226
+     *
227
+     * @since 1.0.19
228
+     * @return GetPaid_Payment_Form
229
+     */
230
+    public function get_payment_form() {
231
+        return $this->payment_form;
232
+    }
233 233
 
234
-	/*
234
+    /*
235 235
 	|--------------------------------------------------------------------------
236 236
 	| Invoices.
237 237
 	|--------------------------------------------------------------------------
@@ -240,61 +240,61 @@  discard block
 block discarded – undo
240 240
 	| might be for an existing invoice.
241 241
 	*/
242 242
 
243
-	/**
244
-	 * Prepares the submission's invoice.
245
-	 *
246
-	 * @since 1.0.19
247
-	 */
248
-	public function process_invoice() {
243
+    /**
244
+     * Prepares the submission's invoice.
245
+     *
246
+     * @since 1.0.19
247
+     */
248
+    public function process_invoice() {
249 249
 
250
-		// Abort if there is no invoice.
251
-		if ( empty( $this->data['invoice_id'] ) ) {
252
-			return;
253
-		}
250
+        // Abort if there is no invoice.
251
+        if ( empty( $this->data['invoice_id'] ) ) {
252
+            return;
253
+        }
254 254
 
255
-		// If the submission is for an existing invoice, ensure that it exists
256
-		// and that it is not paid for.
257
-		$invoice = wpinv_get_invoice( $this->data['invoice_id'] );
255
+        // If the submission is for an existing invoice, ensure that it exists
256
+        // and that it is not paid for.
257
+        $invoice = wpinv_get_invoice( $this->data['invoice_id'] );
258 258
 
259 259
         if ( empty( $invoice ) ) {
260
-			throw new Exception( __( 'Invalid invoice', 'invoicing' ) );
261
-		}
262
-
263
-		if ( $invoice->is_paid() ) {
264
-			throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) );
265
-		}
266
-
267
-		$this->payment_form->set_items( $invoice->get_items() );
268
-		$this->payment_form->invoice = $invoice;
269
-
270
-		$this->country = $invoice->get_country();
271
-		$this->state   = $invoice->get_state();
272
-		$this->invoice = $invoice;
273
-
274
-		do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) );
275
-	}
276
-
277
-	/**
278
-	 * Returns the associated invoice.
279
-	 *
280
-	 * @since 1.0.19
281
-	 * @return WPInv_Invoice
282
-	 */
283
-	public function get_invoice() {
284
-		return $this->invoice;
285
-	}
286
-
287
-	/**
288
-	 * Checks whether there is an invoice associated with this submission.
289
-	 *
290
-	 * @since 1.0.19
291
-	 * @return bool
292
-	 */
293
-	public function has_invoice() {
294
-		return ! empty( $this->invoice );
295
-	}
296
-
297
-	/*
260
+            throw new Exception( __( 'Invalid invoice', 'invoicing' ) );
261
+        }
262
+
263
+        if ( $invoice->is_paid() ) {
264
+            throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) );
265
+        }
266
+
267
+        $this->payment_form->set_items( $invoice->get_items() );
268
+        $this->payment_form->invoice = $invoice;
269
+
270
+        $this->country = $invoice->get_country();
271
+        $this->state   = $invoice->get_state();
272
+        $this->invoice = $invoice;
273
+
274
+        do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) );
275
+    }
276
+
277
+    /**
278
+     * Returns the associated invoice.
279
+     *
280
+     * @since 1.0.19
281
+     * @return WPInv_Invoice
282
+     */
283
+    public function get_invoice() {
284
+        return $this->invoice;
285
+    }
286
+
287
+    /**
288
+     * Checks whether there is an invoice associated with this submission.
289
+     *
290
+     * @since 1.0.19
291
+     * @return bool
292
+     */
293
+    public function has_invoice() {
294
+        return ! empty( $this->invoice );
295
+    }
296
+
297
+    /*
298 298
 	|--------------------------------------------------------------------------
299 299
 	| Items.
300 300
 	|--------------------------------------------------------------------------
@@ -303,115 +303,115 @@  discard block
 block discarded – undo
303 303
 	| recurring item. But can have an unlimited number of non-recurring items.
304 304
 	*/
305 305
 
306
-	/**
307
-	 * Prepares the submission's items.
308
-	 *
309
-	 * @since 1.0.19
310
-	 */
311
-	public function process_items() {
312
-
313
-		$processor = new GetPaid_Payment_Form_Submission_Items( $this );
314
-
315
-		foreach ( $processor->items as $item ) {
316
-			$this->add_item( $item );
317
-		}
318
-
319
-		do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) );
320
-	}
321
-
322
-	/**
323
-	 * Adds an item to the submission.
324
-	 *
325
-	 * @since 1.0.19
326
-	 * @param GetPaid_Form_Item $item
327
-	 */
328
-	public function add_item( $item ) {
329
-
330
-		// Make sure that it is available for purchase.
331
-		if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) {
332
-			return;
333
-		}
334
-
335
-		// Each submission can only contain one recurring item.
336
-		if ( $item->is_recurring() ) {
337
-
338
-			if ( $this->has_recurring != 0 ) {
339
-				throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) );
340
-			}
341
-
342
-			$this->has_recurring = $item->get_id();
343
-
344
-		}
345
-
346
-		// Update the items and totals.
347
-		$this->items[ $item->get_id() ]         = $item;
348
-		$this->totals['subtotal']['initial']   += $item->get_sub_total();
349
-		$this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total();
350
-
351
-	}
352
-
353
-	/**
354
-	 * Removes a specific item.
355
-	 * 
356
-	 * You should not call this method after the discounts and taxes
357
-	 * have been calculated.
358
-	 *
359
-	 * @since 1.0.19
360
-	 */
361
-	public function remove_item( $item_id ) {
362
-
363
-		if ( isset( $this->items[ $item_id ] ) ) {
364
-			$this->totals['subtotal']['initial']   -= $this->items[ $item_id ]->get_sub_total();
365
-			$this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total();
366
-
367
-			if ( $this->items[ $item_id ]->is_recurring() ) {
368
-				$this->has_recurring = 0;
369
-			}
370
-
371
-			unset( $this->items[ $item_id ] );
372
-		}
373
-
374
-	}
375
-
376
-	/**
377
-	 * Returns the subtotal.
378
-	 *
379
-	 * @since 1.0.19
380
-	 */
381
-	public function get_subtotal() {
382
-
383
-		if ( wpinv_prices_include_tax() ) {
384
-			return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial'];
385
-		}
386
-
387
-		return $this->totals['subtotal']['initial'];
388
-	}
389
-
390
-	/**
391
-	 * Returns the recurring subtotal.
392
-	 *
393
-	 * @since 1.0.19
394
-	 */
395
-	public function get_recurring_subtotal() {
396
-
397
-		if ( wpinv_prices_include_tax() ) {
398
-			return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring'];
399
-		}
400
-
401
-		return $this->totals['subtotal']['recurring'];
402
-	}
403
-
404
-	/**
405
-	 * Returns all items.
406
-	 *
407
-	 * @since 1.0.19
408
-	 * @return GetPaid_Form_Item[]
409
-	 */
410
-	public function get_items() {
411
-		return $this->items;
412
-	}
413
-
414
-	/*
306
+    /**
307
+     * Prepares the submission's items.
308
+     *
309
+     * @since 1.0.19
310
+     */
311
+    public function process_items() {
312
+
313
+        $processor = new GetPaid_Payment_Form_Submission_Items( $this );
314
+
315
+        foreach ( $processor->items as $item ) {
316
+            $this->add_item( $item );
317
+        }
318
+
319
+        do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) );
320
+    }
321
+
322
+    /**
323
+     * Adds an item to the submission.
324
+     *
325
+     * @since 1.0.19
326
+     * @param GetPaid_Form_Item $item
327
+     */
328
+    public function add_item( $item ) {
329
+
330
+        // Make sure that it is available for purchase.
331
+        if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) {
332
+            return;
333
+        }
334
+
335
+        // Each submission can only contain one recurring item.
336
+        if ( $item->is_recurring() ) {
337
+
338
+            if ( $this->has_recurring != 0 ) {
339
+                throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) );
340
+            }
341
+
342
+            $this->has_recurring = $item->get_id();
343
+
344
+        }
345
+
346
+        // Update the items and totals.
347
+        $this->items[ $item->get_id() ]         = $item;
348
+        $this->totals['subtotal']['initial']   += $item->get_sub_total();
349
+        $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total();
350
+
351
+    }
352
+
353
+    /**
354
+     * Removes a specific item.
355
+     * 
356
+     * You should not call this method after the discounts and taxes
357
+     * have been calculated.
358
+     *
359
+     * @since 1.0.19
360
+     */
361
+    public function remove_item( $item_id ) {
362
+
363
+        if ( isset( $this->items[ $item_id ] ) ) {
364
+            $this->totals['subtotal']['initial']   -= $this->items[ $item_id ]->get_sub_total();
365
+            $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total();
366
+
367
+            if ( $this->items[ $item_id ]->is_recurring() ) {
368
+                $this->has_recurring = 0;
369
+            }
370
+
371
+            unset( $this->items[ $item_id ] );
372
+        }
373
+
374
+    }
375
+
376
+    /**
377
+     * Returns the subtotal.
378
+     *
379
+     * @since 1.0.19
380
+     */
381
+    public function get_subtotal() {
382
+
383
+        if ( wpinv_prices_include_tax() ) {
384
+            return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial'];
385
+        }
386
+
387
+        return $this->totals['subtotal']['initial'];
388
+    }
389
+
390
+    /**
391
+     * Returns the recurring subtotal.
392
+     *
393
+     * @since 1.0.19
394
+     */
395
+    public function get_recurring_subtotal() {
396
+
397
+        if ( wpinv_prices_include_tax() ) {
398
+            return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring'];
399
+        }
400
+
401
+        return $this->totals['subtotal']['recurring'];
402
+    }
403
+
404
+    /**
405
+     * Returns all items.
406
+     *
407
+     * @since 1.0.19
408
+     * @return GetPaid_Form_Item[]
409
+     */
410
+    public function get_items() {
411
+        return $this->items;
412
+    }
413
+
414
+    /*
415 415
 	|--------------------------------------------------------------------------
416 416
 	| Taxes
417 417
 	|--------------------------------------------------------------------------
@@ -420,122 +420,122 @@  discard block
 block discarded – undo
420 420
 	| or only one-time.
421 421
     */
422 422
 
423
-	/**
424
-	 * Prepares the submission's taxes.
425
-	 *
426
-	 * @since 1.0.19
427
-	 */
428
-	public function process_taxes() {
429
-
430
-		// Abort if we're not using taxes.
431
-		if ( ! $this->use_taxes() ) {
432
-			return;
433
-		}
434
-
435
-		// If a custom country && state has been passed in, use it to calculate taxes.
436
-		$country = $this->get_field( 'wpinv_country', 'billing' );
437
-		if ( ! empty( $country ) ) {
438
-			$this->country = $country;
439
-		}
440
-
441
-		$state = $this->get_field( 'wpinv_state', 'billing' );
442
-		if ( ! empty( $state ) ) {
443
-			$this->state = $state;
444
-		}
445
-
446
-		// Abort if the country is not taxable.
447
-		if ( ! wpinv_is_country_taxable( $this->country ) ) {
448
-			return;
449
-		}
450
-
451
-		$processor = new GetPaid_Payment_Form_Submission_Taxes( $this );
452
-
453
-		foreach ( $processor->taxes as $tax ) {
454
-			$this->add_tax( $tax );
455
-		}
456
-
457
-		do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) );
458
-	}
459
-
460
-	/**
461
-	 * Adds a tax to the submission.
462
-	 *
463
-	 * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required.
464
-	 * @since 1.0.19
465
-	 */
466
-	public function add_tax( $tax ) {
467
-
468
-		if ( wpinv_round_tax_per_tax_rate() ) {
469
-			$tax['initial_tax']   = wpinv_round_amount( $tax['initial_tax'] );
470
-			$tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] );
471
-		}
472
-
473
-		$this->taxes[ $tax['name'] ]         = $tax;
474
-		$this->totals['taxes']['initial']   += wpinv_sanitize_amount( $tax['initial_tax'] );
475
-		$this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] );
476
-
477
-	}
478
-
479
-	/**
480
-	 * Removes a specific tax.
481
-	 *
482
-	 * @since 1.0.19
483
-	 */
484
-	public function remove_tax( $tax_name ) {
485
-
486
-		if ( isset( $this->taxes[ $tax_name ] ) ) {
487
-			$this->totals['taxes']['initial']   -= $this->taxes[ $tax_name ]['initial_tax'];
488
-			$this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax'];
489
-			unset( $this->taxes[ $tax_name ] );
490
-		}
491
-
492
-	}
493
-
494
-	/**
495
-	 * Whether or not we'll use taxes for the submission.
496
-	 *
497
-	 * @since 1.0.19
498
-	 */
499
-	public function use_taxes() {
500
-
501
-		$use_taxes = wpinv_use_taxes();
502
-
503
-		if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) {
504
-			$use_taxes = false;
505
-		}
506
-
507
-		return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this );
508
-
509
-	}
510
-
511
-	/**
512
-	 * Returns the tax.
513
-	 *
514
-	 * @since 1.0.19
515
-	 */
516
-	public function get_tax() {
517
-		return $this->totals['taxes']['initial'];
518
-	}
519
-
520
-	/**
521
-	 * Returns the recurring tax.
522
-	 *
523
-	 * @since 1.0.19
524
-	 */
525
-	public function get_recurring_tax() {
526
-		return $this->totals['taxes']['recurring'];
527
-	}
528
-
529
-	/**
530
-	 * Returns all taxes.
531
-	 *
532
-	 * @since 1.0.19
533
-	 */
534
-	public function get_taxes() {
535
-		return $this->taxes;
536
-	}
537
-
538
-	/*
423
+    /**
424
+     * Prepares the submission's taxes.
425
+     *
426
+     * @since 1.0.19
427
+     */
428
+    public function process_taxes() {
429
+
430
+        // Abort if we're not using taxes.
431
+        if ( ! $this->use_taxes() ) {
432
+            return;
433
+        }
434
+
435
+        // If a custom country && state has been passed in, use it to calculate taxes.
436
+        $country = $this->get_field( 'wpinv_country', 'billing' );
437
+        if ( ! empty( $country ) ) {
438
+            $this->country = $country;
439
+        }
440
+
441
+        $state = $this->get_field( 'wpinv_state', 'billing' );
442
+        if ( ! empty( $state ) ) {
443
+            $this->state = $state;
444
+        }
445
+
446
+        // Abort if the country is not taxable.
447
+        if ( ! wpinv_is_country_taxable( $this->country ) ) {
448
+            return;
449
+        }
450
+
451
+        $processor = new GetPaid_Payment_Form_Submission_Taxes( $this );
452
+
453
+        foreach ( $processor->taxes as $tax ) {
454
+            $this->add_tax( $tax );
455
+        }
456
+
457
+        do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) );
458
+    }
459
+
460
+    /**
461
+     * Adds a tax to the submission.
462
+     *
463
+     * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required.
464
+     * @since 1.0.19
465
+     */
466
+    public function add_tax( $tax ) {
467
+
468
+        if ( wpinv_round_tax_per_tax_rate() ) {
469
+            $tax['initial_tax']   = wpinv_round_amount( $tax['initial_tax'] );
470
+            $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] );
471
+        }
472
+
473
+        $this->taxes[ $tax['name'] ]         = $tax;
474
+        $this->totals['taxes']['initial']   += wpinv_sanitize_amount( $tax['initial_tax'] );
475
+        $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] );
476
+
477
+    }
478
+
479
+    /**
480
+     * Removes a specific tax.
481
+     *
482
+     * @since 1.0.19
483
+     */
484
+    public function remove_tax( $tax_name ) {
485
+
486
+        if ( isset( $this->taxes[ $tax_name ] ) ) {
487
+            $this->totals['taxes']['initial']   -= $this->taxes[ $tax_name ]['initial_tax'];
488
+            $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax'];
489
+            unset( $this->taxes[ $tax_name ] );
490
+        }
491
+
492
+    }
493
+
494
+    /**
495
+     * Whether or not we'll use taxes for the submission.
496
+     *
497
+     * @since 1.0.19
498
+     */
499
+    public function use_taxes() {
500
+
501
+        $use_taxes = wpinv_use_taxes();
502
+
503
+        if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) {
504
+            $use_taxes = false;
505
+        }
506
+
507
+        return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this );
508
+
509
+    }
510
+
511
+    /**
512
+     * Returns the tax.
513
+     *
514
+     * @since 1.0.19
515
+     */
516
+    public function get_tax() {
517
+        return $this->totals['taxes']['initial'];
518
+    }
519
+
520
+    /**
521
+     * Returns the recurring tax.
522
+     *
523
+     * @since 1.0.19
524
+     */
525
+    public function get_recurring_tax() {
526
+        return $this->totals['taxes']['recurring'];
527
+    }
528
+
529
+    /**
530
+     * Returns all taxes.
531
+     *
532
+     * @since 1.0.19
533
+     */
534
+    public function get_taxes() {
535
+        return $this->taxes;
536
+    }
537
+
538
+    /*
539 539
 	|--------------------------------------------------------------------------
540 540
 	| Discounts
541 541
 	|--------------------------------------------------------------------------
@@ -544,99 +544,99 @@  discard block
 block discarded – undo
544 544
 	| or only one-time. They also do not have to come from a discount code.
545 545
     */
546 546
 
547
-	/**
548
-	 * Prepares the submission's discount.
549
-	 *
550
-	 * @since 1.0.19
551
-	 */
552
-	public function process_discount() {
553
-
554
-		$initial_total    = $this->get_subtotal() + $this->get_fee() + $this->get_tax();
555
-		$recurring_total  = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax();
556
-		$processor        = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total );
557
-
558
-		foreach ( $processor->discounts as $discount ) {
559
-			$this->add_discount( $discount );
560
-		}
561
-
562
-		do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) );
563
-	}
564
-
565
-	/**
566
-	 * Adds a discount to the submission.
567
-	 *
568
-	 * @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.
569
-	 * @since 1.0.19
570
-	 */
571
-	public function add_discount( $discount ) {
572
-		$this->discounts[ $discount['name'] ]   = $discount;
573
-		$this->totals['discount']['initial']   += wpinv_sanitize_amount( $discount['initial_discount'] );
574
-		$this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] );
575
-	}
576
-
577
-	/**
578
-	 * Removes a discount from the submission.
579
-	 *
580
-	 * @since 1.0.19
581
-	 */
582
-	public function remove_discount( $name ) {
583
-
584
-		if ( isset( $this->discounts[ $name ] ) ) {
585
-			$this->totals['discount']['initial']   -= $this->discounts[ $name ]['initial_discount'];
586
-			$this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount'];
587
-			unset( $this->discounts[ $name ] );
588
-		}
589
-
590
-	}
591
-
592
-	/**
593
-	 * Checks whether there is a discount code associated with this submission.
594
-	 *
595
-	 * @since 1.0.19
596
-	 * @return bool
597
-	 */
598
-	public function has_discount_code() {
599
-		return ! empty( $this->discounts['discount_code'] );
600
-	}
601
-
602
-	/**
603
-	 * Returns the discount code.
604
-	 *
605
-	 * @since 1.0.19
606
-	 * @return string
607
-	 */
608
-	public function get_discount_code() {
609
-		return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : '';
610
-	}
611
-
612
-	/**
613
-	 * Returns the discount.
614
-	 *
615
-	 * @since 1.0.19
616
-	 */
617
-	public function get_discount() {
618
-		return $this->totals['discount']['initial'];
619
-	}
620
-
621
-	/**
622
-	 * Returns the recurring discount.
623
-	 *
624
-	 * @since 1.0.19
625
-	 */
626
-	public function get_recurring_discount() {
627
-		return $this->totals['discount']['recurring'];
628
-	}
629
-
630
-	/**
631
-	 * Returns all discounts.
632
-	 *
633
-	 * @since 1.0.19
634
-	 */
635
-	public function get_discounts() {
636
-		return $this->discounts;
637
-	}
638
-
639
-	/*
547
+    /**
548
+     * Prepares the submission's discount.
549
+     *
550
+     * @since 1.0.19
551
+     */
552
+    public function process_discount() {
553
+
554
+        $initial_total    = $this->get_subtotal() + $this->get_fee() + $this->get_tax();
555
+        $recurring_total  = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax();
556
+        $processor        = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total );
557
+
558
+        foreach ( $processor->discounts as $discount ) {
559
+            $this->add_discount( $discount );
560
+        }
561
+
562
+        do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) );
563
+    }
564
+
565
+    /**
566
+     * Adds a discount to the submission.
567
+     *
568
+     * @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.
569
+     * @since 1.0.19
570
+     */
571
+    public function add_discount( $discount ) {
572
+        $this->discounts[ $discount['name'] ]   = $discount;
573
+        $this->totals['discount']['initial']   += wpinv_sanitize_amount( $discount['initial_discount'] );
574
+        $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] );
575
+    }
576
+
577
+    /**
578
+     * Removes a discount from the submission.
579
+     *
580
+     * @since 1.0.19
581
+     */
582
+    public function remove_discount( $name ) {
583
+
584
+        if ( isset( $this->discounts[ $name ] ) ) {
585
+            $this->totals['discount']['initial']   -= $this->discounts[ $name ]['initial_discount'];
586
+            $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount'];
587
+            unset( $this->discounts[ $name ] );
588
+        }
589
+
590
+    }
591
+
592
+    /**
593
+     * Checks whether there is a discount code associated with this submission.
594
+     *
595
+     * @since 1.0.19
596
+     * @return bool
597
+     */
598
+    public function has_discount_code() {
599
+        return ! empty( $this->discounts['discount_code'] );
600
+    }
601
+
602
+    /**
603
+     * Returns the discount code.
604
+     *
605
+     * @since 1.0.19
606
+     * @return string
607
+     */
608
+    public function get_discount_code() {
609
+        return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : '';
610
+    }
611
+
612
+    /**
613
+     * Returns the discount.
614
+     *
615
+     * @since 1.0.19
616
+     */
617
+    public function get_discount() {
618
+        return $this->totals['discount']['initial'];
619
+    }
620
+
621
+    /**
622
+     * Returns the recurring discount.
623
+     *
624
+     * @since 1.0.19
625
+     */
626
+    public function get_recurring_discount() {
627
+        return $this->totals['discount']['recurring'];
628
+    }
629
+
630
+    /**
631
+     * Returns all discounts.
632
+     *
633
+     * @since 1.0.19
634
+     */
635
+    public function get_discounts() {
636
+        return $this->discounts;
637
+    }
638
+
639
+    /*
640 640
 	|--------------------------------------------------------------------------
641 641
 	| Fees
642 642
 	|--------------------------------------------------------------------------
@@ -646,89 +646,89 @@  discard block
 block discarded – undo
646 646
 	| fees.
647 647
     */
648 648
 
649
-	/**
650
-	 * Prepares the submission's fees.
651
-	 *
652
-	 * @since 1.0.19
653
-	 */
654
-	public function process_fees() {
655
-
656
-		$fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this );
657
-
658
-		foreach ( $fees_processor->fees as $fee ) {
659
-			$this->add_fee( $fee );
660
-		}
661
-
662
-		do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) );
663
-	}
664
-
665
-	/**
666
-	 * Adds a fee to the submission.
667
-	 *
668
-	 * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required.
669
-	 * @since 1.0.19
670
-	 */
671
-	public function add_fee( $fee ) {
672
-
673
-		$this->fees[ $fee['name'] ]         = $fee;
674
-		$this->totals['fees']['initial']   += wpinv_sanitize_amount( $fee['initial_fee'] );
675
-		$this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] );
676
-
677
-	}
678
-
679
-	/**
680
-	 * Removes a fee from the submission.
681
-	 *
682
-	 * @since 1.0.19
683
-	 */
684
-	public function remove_fee( $name ) {
685
-
686
-		if ( isset( $this->fees[ $name ] ) ) {
687
-			$this->totals['fees']['initial']   -= $this->fees[ $name ]['initial_fee'];
688
-			$this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee'];
689
-			unset( $this->fees[ $name ] );
690
-		}
691
-
692
-	}
693
-
694
-	/**
695
-	 * Returns the fees.
696
-	 *
697
-	 * @since 1.0.19
698
-	 */
699
-	public function get_fee() {
700
-		return $this->totals['fees']['initial'];
701
-	}
702
-
703
-	/**
704
-	 * Returns the recurring fees.
705
-	 *
706
-	 * @since 1.0.19
707
-	 */
708
-	public function get_recurring_fee() {
709
-		return $this->totals['fees']['recurring'];
710
-	}
711
-
712
-	/**
713
-	 * Returns all fees.
714
-	 *
715
-	 * @since 1.0.19
716
-	 */
717
-	public function get_fees() {
718
-		return $this->fees;
719
-	}
720
-
721
-	/**
722
-	 * Checks if there are any fees for the form.
723
-	 *
724
-	 * @return bool
725
-	 * @since 1.0.19
726
-	 */
727
-	public function has_fees() {
728
-		return count( $this->fees ) !== 0;
729
-	}
730
-
731
-	/*
649
+    /**
650
+     * Prepares the submission's fees.
651
+     *
652
+     * @since 1.0.19
653
+     */
654
+    public function process_fees() {
655
+
656
+        $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this );
657
+
658
+        foreach ( $fees_processor->fees as $fee ) {
659
+            $this->add_fee( $fee );
660
+        }
661
+
662
+        do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) );
663
+    }
664
+
665
+    /**
666
+     * Adds a fee to the submission.
667
+     *
668
+     * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required.
669
+     * @since 1.0.19
670
+     */
671
+    public function add_fee( $fee ) {
672
+
673
+        $this->fees[ $fee['name'] ]         = $fee;
674
+        $this->totals['fees']['initial']   += wpinv_sanitize_amount( $fee['initial_fee'] );
675
+        $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] );
676
+
677
+    }
678
+
679
+    /**
680
+     * Removes a fee from the submission.
681
+     *
682
+     * @since 1.0.19
683
+     */
684
+    public function remove_fee( $name ) {
685
+
686
+        if ( isset( $this->fees[ $name ] ) ) {
687
+            $this->totals['fees']['initial']   -= $this->fees[ $name ]['initial_fee'];
688
+            $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee'];
689
+            unset( $this->fees[ $name ] );
690
+        }
691
+
692
+    }
693
+
694
+    /**
695
+     * Returns the fees.
696
+     *
697
+     * @since 1.0.19
698
+     */
699
+    public function get_fee() {
700
+        return $this->totals['fees']['initial'];
701
+    }
702
+
703
+    /**
704
+     * Returns the recurring fees.
705
+     *
706
+     * @since 1.0.19
707
+     */
708
+    public function get_recurring_fee() {
709
+        return $this->totals['fees']['recurring'];
710
+    }
711
+
712
+    /**
713
+     * Returns all fees.
714
+     *
715
+     * @since 1.0.19
716
+     */
717
+    public function get_fees() {
718
+        return $this->fees;
719
+    }
720
+
721
+    /**
722
+     * Checks if there are any fees for the form.
723
+     *
724
+     * @return bool
725
+     * @since 1.0.19
726
+     */
727
+    public function has_fees() {
728
+        return count( $this->fees ) !== 0;
729
+    }
730
+
731
+    /*
732 732
 	|--------------------------------------------------------------------------
733 733
 	| MISC
734 734
 	|--------------------------------------------------------------------------
@@ -736,109 +736,109 @@  discard block
 block discarded – undo
736 736
 	| Extra submission functions.
737 737
     */
738 738
 
739
-	/**
740
-	 * Returns the total amount to collect for this submission.
741
-	 *
742
-	 * @since 1.0.19
743
-	 */
744
-	public function get_total() {
745
-		$total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount();
746
-		return max( $total, 0 );
747
-	}
748
-
749
-	/**
750
-	 * Returns the recurring total amount to collect for this submission.
751
-	 *
752
-	 * @since 1.0.19
753
-	 */
754
-	public function get_recurring_total() {
755
-		$total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount();
756
-		return max( $total, 0 );
757
-	}
758
-
759
-	/**
760
-	 * Whether payment details should be collected for this submission.
761
-	 *
762
-	 * @since 1.0.19
763
-	 */
764
-	public function should_collect_payment_details() {
765
-		$initial   = $this->get_total();
766
-		$recurring = $this->get_recurring_total();
767
-
768
-		if ( $this->has_recurring == 0 ) {
769
-			$recurring = 0;
770
-		}
771
-
772
-		$collect = $initial > 0 || $recurring > 0;
773
-		return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this  );
774
-	}
775
-
776
-	/**
777
-	 * Returns the billing email of the user.
778
-	 *
779
-	 * @since 1.0.19
780
-	 */
781
-	public function get_billing_email() {
782
-		return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this  );
783
-	}
784
-
785
-	/**
786
-	 * Checks if the submitter has a billing email.
787
-	 *
788
-	 * @since 1.0.19
789
-	 */
790
-	public function has_billing_email() {
791
-		$billing_email = $this->get_billing_email();
792
-		return ! empty( $billing_email ) && is_email( $billing_email );
793
-	}
794
-
795
-	/**
796
-	 * Returns the appropriate currency for the submission.
797
-	 *
798
-	 * @since 1.0.19
799
-	 * @return string
800
-	 */
801
-	public function get_currency() {
802
-		return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency();
803
-    }
804
-
805
-    /**
806
-	 * Returns the raw submission data.
807
-	 *
808
-	 * @since 1.0.19
809
-	 * @return array
810
-	 */
811
-	public function get_data() {
812
-		return $this->data;
813
-	}
814
-
815
-	/**
816
-	 * Returns a field from the submission data
817
-	 *
818
-	 * @param string $field
819
-	 * @since 1.0.19
820
-	 * @return mixed|null
821
-	 */
822
-	public function get_field( $field, $sub_array_key = null ) {
823
-		return getpaid_get_array_field( $this->data, $field, $sub_array_key );
824
-	}
825
-
826
-	/**
827
-	 * Checks if a required field is set.
828
-	 *
829
-	 * @since 1.0.19
830
-	 */
831
-	public function is_required_field_set( $field ) {
832
-		return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] );
833
-	}
834
-
835
-	/**
836
-	 * Formats an amount
837
-	 *
838
-	 * @since 1.0.19
839
-	 */
840
-	public function format_amount( $amount ) {
841
-		return wpinv_price( $amount, $this->get_currency() );
842
-	}
739
+    /**
740
+     * Returns the total amount to collect for this submission.
741
+     *
742
+     * @since 1.0.19
743
+     */
744
+    public function get_total() {
745
+        $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount();
746
+        return max( $total, 0 );
747
+    }
748
+
749
+    /**
750
+     * Returns the recurring total amount to collect for this submission.
751
+     *
752
+     * @since 1.0.19
753
+     */
754
+    public function get_recurring_total() {
755
+        $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount();
756
+        return max( $total, 0 );
757
+    }
758
+
759
+    /**
760
+     * Whether payment details should be collected for this submission.
761
+     *
762
+     * @since 1.0.19
763
+     */
764
+    public function should_collect_payment_details() {
765
+        $initial   = $this->get_total();
766
+        $recurring = $this->get_recurring_total();
767
+
768
+        if ( $this->has_recurring == 0 ) {
769
+            $recurring = 0;
770
+        }
771
+
772
+        $collect = $initial > 0 || $recurring > 0;
773
+        return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this  );
774
+    }
775
+
776
+    /**
777
+     * Returns the billing email of the user.
778
+     *
779
+     * @since 1.0.19
780
+     */
781
+    public function get_billing_email() {
782
+        return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this  );
783
+    }
784
+
785
+    /**
786
+     * Checks if the submitter has a billing email.
787
+     *
788
+     * @since 1.0.19
789
+     */
790
+    public function has_billing_email() {
791
+        $billing_email = $this->get_billing_email();
792
+        return ! empty( $billing_email ) && is_email( $billing_email );
793
+    }
794
+
795
+    /**
796
+     * Returns the appropriate currency for the submission.
797
+     *
798
+     * @since 1.0.19
799
+     * @return string
800
+     */
801
+    public function get_currency() {
802
+        return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency();
803
+    }
804
+
805
+    /**
806
+     * Returns the raw submission data.
807
+     *
808
+     * @since 1.0.19
809
+     * @return array
810
+     */
811
+    public function get_data() {
812
+        return $this->data;
813
+    }
814
+
815
+    /**
816
+     * Returns a field from the submission data
817
+     *
818
+     * @param string $field
819
+     * @since 1.0.19
820
+     * @return mixed|null
821
+     */
822
+    public function get_field( $field, $sub_array_key = null ) {
823
+        return getpaid_get_array_field( $this->data, $field, $sub_array_key );
824
+    }
825
+
826
+    /**
827
+     * Checks if a required field is set.
828
+     *
829
+     * @since 1.0.19
830
+     */
831
+    public function is_required_field_set( $field ) {
832
+        return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] );
833
+    }
834
+
835
+    /**
836
+     * Formats an amount
837
+     *
838
+     * @since 1.0.19
839
+     */
840
+    public function format_amount( $amount ) {
841
+        return wpinv_price( $amount, $this->get_currency() );
842
+    }
843 843
 
844 844
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission-taxes.php 1 patch
Indentation   +195 added lines, -195 removed lines patch added patch discarded remove patch
@@ -12,213 +12,213 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Taxes {
14 14
 
15
-	/**
16
-	 * Submission taxes.
17
-	 * @var array
18
-	 */
19
-	public $taxes = array();
15
+    /**
16
+     * Submission taxes.
17
+     * @var array
18
+     */
19
+    public $taxes = array();
20
+
21
+    /**
22
+     * Class constructor
23
+     *
24
+     * @param GetPaid_Payment_Form_Submission $submission
25
+     */
26
+    public function __construct( $submission ) {
27
+
28
+        // Validate VAT number.
29
+        $this->validate_vat( $submission );
30
+
31
+        foreach ( $submission->get_items() as $item ) {
32
+            $this->process_item_tax( $item, $submission );
33
+        }
34
+
35
+        // Process any existing invoice taxes.
36
+        if ( $submission->has_invoice() ) {
37
+            $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes );
38
+        }
39
+
40
+    }
41
+
42
+    /**
43
+     * Maybe process tax.
44
+     *
45
+     * @since 1.0.19
46
+     * @param GetPaid_Form_Item $item
47
+     * @param GetPaid_Payment_Form_Submission $submission
48
+     */
49
+    public function process_item_tax( $item, $submission ) {
50
+
51
+        $rates    = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state );
52
+        $rates    = getpaid_filter_item_tax_rates( $item, $rates );
53
+        $taxes    = getpaid_calculate_item_taxes( $item->get_sub_total(), $rates );
54
+        $r_taxes  = getpaid_calculate_item_taxes( $item->get_recurring_sub_total(), $rates );
55
+
56
+        foreach ( $taxes as $name => $amount ) {
57
+            $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0;
58
+            $tax       = getpaid_prepare_item_tax( $item, $name, $amount, $recurring );
59
+
60
+            if ( ! isset( $this->taxes[ $name ] ) ) {
61
+                $this->taxes[ $name ] = $tax;
62
+                continue;
63
+            }
64
+
65
+            $this->taxes[ $name ]['initial_tax']   += $tax['initial_tax'];
66
+            $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax'];
67
+
68
+        }
69
+
70
+    }
20 71
 
21 72
     /**
22
-	 * Class constructor
23
-	 *
24
-	 * @param GetPaid_Payment_Form_Submission $submission
25
-	 */
26
-	public function __construct( $submission ) {
27
-
28
-		// Validate VAT number.
29
-		$this->validate_vat( $submission );
30
-
31
-		foreach ( $submission->get_items() as $item ) {
32
-			$this->process_item_tax( $item, $submission );
33
-		}
34
-
35
-		// Process any existing invoice taxes.
36
-		if ( $submission->has_invoice() ) {
37
-			$this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes );
38
-		}
39
-
40
-	}
41
-
42
-	/**
43
-	 * Maybe process tax.
44
-	 *
45
-	 * @since 1.0.19
46
-	 * @param GetPaid_Form_Item $item
47
-	 * @param GetPaid_Payment_Form_Submission $submission
48
-	 */
49
-	public function process_item_tax( $item, $submission ) {
50
-
51
-		$rates    = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state );
52
-		$rates    = getpaid_filter_item_tax_rates( $item, $rates );
53
-		$taxes    = getpaid_calculate_item_taxes( $item->get_sub_total(), $rates );
54
-		$r_taxes  = getpaid_calculate_item_taxes( $item->get_recurring_sub_total(), $rates );
55
-
56
-		foreach ( $taxes as $name => $amount ) {
57
-			$recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0;
58
-			$tax       = getpaid_prepare_item_tax( $item, $name, $amount, $recurring );
59
-
60
-			if ( ! isset( $this->taxes[ $name ] ) ) {
61
-				$this->taxes[ $name ] = $tax;
62
-				continue;
63
-			}
64
-
65
-			$this->taxes[ $name ]['initial_tax']   += $tax['initial_tax'];
66
-			$this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax'];
67
-
68
-		}
69
-
70
-	}
71
-
72
-	/**
73
-	 * Checks if the submission has a digital item.
74
-	 *
75
-	 * @param GetPaid_Payment_Form_Submission $submission
76
-	 * @since 1.0.19
77
-	 * @return bool
78
-	 */
79
-	public function has_digital_item( $submission ) {
80
-
81
-		foreach ( $submission->get_items() as $item ) {
82
-
83
-			if ( 'digital' == $item->get_vat_rule() ) {
84
-				return true;
85
-			}
86
-
87
-		}
88
-
89
-		return false;
90
-	}
91
-
92
-	/**
93
-	 * Checks if this is an eu store.
94
-	 *
95
-	 * @since 1.0.19
96
-	 * @return bool
97
-	 */
98
-	public function is_eu_store() {
99
-		return $this->is_eu_country( wpinv_get_default_country() );
100
-	}
101
-
102
-	/**
103
-	 * Checks if this is an eu country.
104
-	 *
105
-	 * @param string $country
106
-	 * @since 1.0.19
107
-	 * @return bool
108
-	 */
109
-	public function is_eu_country( $country ) {
110
-		return getpaid_is_eu_state( $country ) || getpaid_is_gst_country( $country );
111
-	}
112
-
113
-	/**
114
-	 * Checks if this is an eu purchase.
115
-	 *
116
-	 * @param string $customer_country
117
-	 * @since 1.0.19
118
-	 * @return bool
119
-	 */
120
-	public function is_eu_transaction( $customer_country ) {
121
-		return $this->is_eu_country( $customer_country ) && $this->is_eu_store();
122
-	}
123
-
124
-	/**
125
-	 * Retrieves the vat number.
126
-	 *
127
-	 * @param GetPaid_Payment_Form_Submission $submission
128
-	 * @since 1.0.19
129
-	 * @return string
130
-	 */
131
-	public function get_vat_number( $submission ) {
132
-
133
-		// Retrieve from the posted number.
134
-		$vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' );
135
-		if ( ! empty( $vat_number ) ) {
136
-			return wpinv_clean( $vat_number );
137
-		}
138
-
139
-		// Retrieve from the invoice.
140
-		return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : '';
141
-	}
142
-
143
-	/**
144
-	 * Retrieves the company.
145
-	 *
146
-	 * @param GetPaid_Payment_Form_Submission $submission
147
-	 * @since 1.0.19
148
-	 * @return string
149
-	 */
150
-	public function get_company( $submission ) {
151
-
152
-		// Retrieve from the posted data.
153
-		$company = $submission->get_field( 'wpinv_company', 'billing' );
154
-		if ( ! empty( $company ) ) {
155
-			return wpinv_clean( $company );
156
-		}
157
-
158
-		// Retrieve from the invoice.
159
-		return $submission->has_invoice() ? $submission->get_invoice()->get_company() : '';
160
-	}
161
-
162
-	/**
163
-	 * Checks if we require a VAT number.
164
-	 *
165
-	 * @param bool $ip_in_eu Whether the customer IP is from the EU
166
-	 * @param bool $country_in_eu Whether the customer country is from the EU
167
-	 * @since 1.0.19
168
-	 * @return string
169
-	 */
170
-	public function requires_vat( $ip_in_eu, $country_in_eu ) {
171
-
172
-		$prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' );
173
-		$prevent_b2c = ! empty( $prevent_b2c );
174
-		$is_eu       = $ip_in_eu || $country_in_eu;
175
-
176
-		return $prevent_b2c && $is_eu;
177
-	}
178
-
179
-	/**
180
-	 * Validate VAT data.
181
-	 *
182
-	 * @param GetPaid_Payment_Form_Submission $submission
183
-	 * @since 1.0.19
184
-	 */
185
-	public function validate_vat( $submission ) {
186
-
187
-		$in_eu = $this->is_eu_transaction( $submission->country );
188
-
189
-		// Abort if we are not validating vat numbers.
190
-		if ( ! $in_eu ) {
73
+     * Checks if the submission has a digital item.
74
+     *
75
+     * @param GetPaid_Payment_Form_Submission $submission
76
+     * @since 1.0.19
77
+     * @return bool
78
+     */
79
+    public function has_digital_item( $submission ) {
80
+
81
+        foreach ( $submission->get_items() as $item ) {
82
+
83
+            if ( 'digital' == $item->get_vat_rule() ) {
84
+                return true;
85
+            }
86
+
87
+        }
88
+
89
+        return false;
90
+    }
91
+
92
+    /**
93
+     * Checks if this is an eu store.
94
+     *
95
+     * @since 1.0.19
96
+     * @return bool
97
+     */
98
+    public function is_eu_store() {
99
+        return $this->is_eu_country( wpinv_get_default_country() );
100
+    }
101
+
102
+    /**
103
+     * Checks if this is an eu country.
104
+     *
105
+     * @param string $country
106
+     * @since 1.0.19
107
+     * @return bool
108
+     */
109
+    public function is_eu_country( $country ) {
110
+        return getpaid_is_eu_state( $country ) || getpaid_is_gst_country( $country );
111
+    }
112
+
113
+    /**
114
+     * Checks if this is an eu purchase.
115
+     *
116
+     * @param string $customer_country
117
+     * @since 1.0.19
118
+     * @return bool
119
+     */
120
+    public function is_eu_transaction( $customer_country ) {
121
+        return $this->is_eu_country( $customer_country ) && $this->is_eu_store();
122
+    }
123
+
124
+    /**
125
+     * Retrieves the vat number.
126
+     *
127
+     * @param GetPaid_Payment_Form_Submission $submission
128
+     * @since 1.0.19
129
+     * @return string
130
+     */
131
+    public function get_vat_number( $submission ) {
132
+
133
+        // Retrieve from the posted number.
134
+        $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' );
135
+        if ( ! empty( $vat_number ) ) {
136
+            return wpinv_clean( $vat_number );
137
+        }
138
+
139
+        // Retrieve from the invoice.
140
+        return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : '';
141
+    }
142
+
143
+    /**
144
+     * Retrieves the company.
145
+     *
146
+     * @param GetPaid_Payment_Form_Submission $submission
147
+     * @since 1.0.19
148
+     * @return string
149
+     */
150
+    public function get_company( $submission ) {
151
+
152
+        // Retrieve from the posted data.
153
+        $company = $submission->get_field( 'wpinv_company', 'billing' );
154
+        if ( ! empty( $company ) ) {
155
+            return wpinv_clean( $company );
156
+        }
157
+
158
+        // Retrieve from the invoice.
159
+        return $submission->has_invoice() ? $submission->get_invoice()->get_company() : '';
160
+    }
161
+
162
+    /**
163
+     * Checks if we require a VAT number.
164
+     *
165
+     * @param bool $ip_in_eu Whether the customer IP is from the EU
166
+     * @param bool $country_in_eu Whether the customer country is from the EU
167
+     * @since 1.0.19
168
+     * @return string
169
+     */
170
+    public function requires_vat( $ip_in_eu, $country_in_eu ) {
171
+
172
+        $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' );
173
+        $prevent_b2c = ! empty( $prevent_b2c );
174
+        $is_eu       = $ip_in_eu || $country_in_eu;
175
+
176
+        return $prevent_b2c && $is_eu;
177
+    }
178
+
179
+    /**
180
+     * Validate VAT data.
181
+     *
182
+     * @param GetPaid_Payment_Form_Submission $submission
183
+     * @since 1.0.19
184
+     */
185
+    public function validate_vat( $submission ) {
186
+
187
+        $in_eu = $this->is_eu_transaction( $submission->country );
188
+
189
+        // Abort if we are not validating vat numbers.
190
+        if ( ! $in_eu ) {
191 191
             return;
192
-		}
192
+        }
193 193
 
194
-		// Prepare variables.
195
-		$vat_number  = $this->get_vat_number( $submission );
196
-		$ip_country  = getpaid_get_ip_country();
194
+        // Prepare variables.
195
+        $vat_number  = $this->get_vat_number( $submission );
196
+        $ip_country  = getpaid_get_ip_country();
197 197
         $is_eu       = $this->is_eu_country( $submission->country );
198 198
         $is_ip_eu    = $this->is_eu_country( $ip_country );
199 199
 
200
-		// If we're preventing business to consumer purchases,
201
-		if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) {
200
+        // If we're preventing business to consumer purchases,
201
+        if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) {
202 202
 
203
-			// Ensure that a vat number has been specified.
204
-			throw new Exception(
205
-				wp_sprintf(
206
-					__( 'Please enter your %s number to verify your purchase is by an EU business.', 'invoicing' ),
207
-					getpaid_vat_name()
208
-				)
209
-			);
203
+            // Ensure that a vat number has been specified.
204
+            throw new Exception(
205
+                wp_sprintf(
206
+                    __( 'Please enter your %s number to verify your purchase is by an EU business.', 'invoicing' ),
207
+                    getpaid_vat_name()
208
+                )
209
+            );
210 210
 
211
-		}
211
+        }
212 212
 
213
-		// Abort if we are not validating vat (vat number should exist, user should be in eu and business too).
214
-		if ( ! $in_eu ) {
213
+        // Abort if we are not validating vat (vat number should exist, user should be in eu and business too).
214
+        if ( ! $in_eu ) {
215 215
             return;
216
-		}
216
+        }
217 217
 
218
-		if ( ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) {
219
-			throw new Exception( __( 'Your VAT number is invalid', 'invoicing' ) );
220
-		}
218
+        if ( ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) {
219
+            throw new Exception( __( 'Your VAT number is invalid', 'invoicing' ) );
220
+        }
221 221
 
222
-	}
222
+    }
223 223
 
224 224
 }
Please login to merge, or discard this patch.