@@ -12,38 +12,38 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Payment_Form_Submission_Items { |
14 | 14 | |
15 | - /** |
|
16 | - * Submission items. |
|
17 | - * @var GetPaid_Form_Item[] |
|
18 | - */ |
|
19 | - public $items = array(); |
|
15 | + /** |
|
16 | + * Submission items. |
|
17 | + * @var GetPaid_Form_Item[] |
|
18 | + */ |
|
19 | + public $items = array(); |
|
20 | 20 | |
21 | 21 | /** |
22 | - * Class constructor |
|
23 | - * |
|
24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
25 | - */ |
|
26 | - public function __construct( $submission ) { |
|
27 | - |
|
28 | - $data = $submission->get_data(); |
|
29 | - $payment_form = $submission->get_payment_form(); |
|
30 | - |
|
31 | - // Prepare the selected items. |
|
32 | - $selected_items = array(); |
|
33 | - if ( ! empty( $data['getpaid-items'] ) ) { |
|
34 | - $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
35 | - } |
|
36 | - |
|
37 | - // (Maybe) set form items. |
|
38 | - if ( isset( $data['getpaid-form-items'] ) ) { |
|
39 | - |
|
40 | - // Confirm items key. |
|
41 | - $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
|
42 | - if ( ! isset( $data['getpaid-form-items-key'] ) || $data['getpaid-form-items-key'] !== md5( NONCE_KEY . AUTH_KEY . $form_items ) ) { |
|
43 | - throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
|
44 | - } |
|
45 | - |
|
46 | - $items = array(); |
|
22 | + * Class constructor |
|
23 | + * |
|
24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
25 | + */ |
|
26 | + public function __construct( $submission ) { |
|
27 | + |
|
28 | + $data = $submission->get_data(); |
|
29 | + $payment_form = $submission->get_payment_form(); |
|
30 | + |
|
31 | + // Prepare the selected items. |
|
32 | + $selected_items = array(); |
|
33 | + if ( ! empty( $data['getpaid-items'] ) ) { |
|
34 | + $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
35 | + } |
|
36 | + |
|
37 | + // (Maybe) set form items. |
|
38 | + if ( isset( $data['getpaid-form-items'] ) ) { |
|
39 | + |
|
40 | + // Confirm items key. |
|
41 | + $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
|
42 | + if ( ! isset( $data['getpaid-form-items-key'] ) || $data['getpaid-form-items-key'] !== md5( NONCE_KEY . AUTH_KEY . $form_items ) ) { |
|
43 | + throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
|
44 | + } |
|
45 | + |
|
46 | + $items = array(); |
|
47 | 47 | $item_ids = array(); |
48 | 48 | |
49 | 49 | foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) { |
@@ -74,58 +74,58 @@ discard block |
||
74 | 74 | |
75 | 75 | $payment_form->set_items( $items ); |
76 | 76 | |
77 | - } |
|
78 | - |
|
79 | - // Process each individual item. |
|
80 | - foreach ( $payment_form->get_items() as $item ) { |
|
81 | - $this->process_item( $item, $selected_items, $submission ); |
|
82 | - } |
|
83 | - |
|
84 | - } |
|
77 | + } |
|
85 | 78 | |
86 | - /** |
|
87 | - * Process a single item. |
|
88 | - * |
|
89 | - * @param GetPaid_Form_Item $item |
|
90 | - * @param array $selected_items |
|
91 | - * @param GetPaid_Payment_Form_Submission $submission |
|
92 | - */ |
|
93 | - public function process_item( $item, $selected_items, $submission ) { |
|
79 | + // Process each individual item. |
|
80 | + foreach ( $payment_form->get_items() as $item ) { |
|
81 | + $this->process_item( $item, $selected_items, $submission ); |
|
82 | + } |
|
94 | 83 | |
95 | - // Abort if this is an optional item and it has not been selected. |
|
96 | - if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
97 | - return; |
|
98 | - } |
|
84 | + } |
|
99 | 85 | |
100 | - // (maybe) let customers change the quantities and prices. |
|
101 | - if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
102 | - |
|
103 | - // Maybe change the quantities. |
|
104 | - if ( $item->allows_quantities() ) { |
|
105 | - $item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] ); |
|
106 | - } |
|
86 | + /** |
|
87 | + * Process a single item. |
|
88 | + * |
|
89 | + * @param GetPaid_Form_Item $item |
|
90 | + * @param array $selected_items |
|
91 | + * @param GetPaid_Payment_Form_Submission $submission |
|
92 | + */ |
|
93 | + public function process_item( $item, $selected_items, $submission ) { |
|
94 | + |
|
95 | + // Abort if this is an optional item and it has not been selected. |
|
96 | + if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
97 | + return; |
|
98 | + } |
|
99 | + |
|
100 | + // (maybe) let customers change the quantities and prices. |
|
101 | + if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
102 | + |
|
103 | + // Maybe change the quantities. |
|
104 | + if ( $item->allows_quantities() ) { |
|
105 | + $item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] ); |
|
106 | + } |
|
107 | 107 | |
108 | - // Maybe change the price. |
|
109 | - if ( $item->user_can_set_their_price() ) { |
|
110 | - $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
108 | + // Maybe change the price. |
|
109 | + if ( $item->user_can_set_their_price() ) { |
|
110 | + $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
111 | 111 | |
112 | - if ( $item->get_minimum_price() > $price ) { |
|
113 | - throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) ); |
|
114 | - } |
|
112 | + if ( $item->get_minimum_price() > $price ) { |
|
113 | + throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) ); |
|
114 | + } |
|
115 | 115 | |
116 | - $item->set_price( $price ); |
|
116 | + $item->set_price( $price ); |
|
117 | 117 | |
118 | - } |
|
118 | + } |
|
119 | 119 | |
120 | - } |
|
120 | + } |
|
121 | 121 | |
122 | - if ( 0 == $item->get_quantity() ) { |
|
123 | - return; |
|
124 | - } |
|
122 | + if ( 0 == $item->get_quantity() ) { |
|
123 | + return; |
|
124 | + } |
|
125 | 125 | |
126 | - // Save the item. |
|
127 | - $this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item' , $item, $submission ); |
|
126 | + // Save the item. |
|
127 | + $this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item' , $item, $submission ); |
|
128 | 128 | |
129 | - } |
|
129 | + } |
|
130 | 130 | |
131 | 131 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
11 | - exit; // Exit if accessed directly |
|
11 | + exit; // Exit if accessed directly |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | /** |
@@ -17,10 +17,10 @@ discard block |
||
17 | 17 | class GetPaid_Meta_Box_Discount_Details { |
18 | 18 | |
19 | 19 | /** |
20 | - * Output the metabox. |
|
21 | - * |
|
22 | - * @param WP_Post $post |
|
23 | - */ |
|
20 | + * Output the metabox. |
|
21 | + * |
|
22 | + * @param WP_Post $post |
|
23 | + */ |
|
24 | 24 | public static function output( $post ) { |
25 | 25 | |
26 | 26 | // Prepare the discount. |
@@ -368,34 +368,34 @@ discard block |
||
368 | 368 | } |
369 | 369 | |
370 | 370 | /** |
371 | - * Save meta box data. |
|
372 | - * |
|
373 | - * @param int $post_id |
|
374 | - */ |
|
375 | - public static function save( $post_id ) { |
|
371 | + * Save meta box data. |
|
372 | + * |
|
373 | + * @param int $post_id |
|
374 | + */ |
|
375 | + public static function save( $post_id ) { |
|
376 | 376 | |
377 | 377 | // Prepare the discount. |
378 | 378 | $discount = new WPInv_Discount( $post_id ); |
379 | 379 | |
380 | 380 | // Load new data. |
381 | 381 | $discount->set_props( |
382 | - array( |
|
383 | - 'code' => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null, |
|
384 | - 'amount' => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null, |
|
385 | - 'start' => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null, |
|
386 | - 'expiration' => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null, |
|
387 | - 'is_single_use' => isset( $_POST['wpinv_discount_single_use'] ), |
|
382 | + array( |
|
383 | + 'code' => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null, |
|
384 | + 'amount' => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null, |
|
385 | + 'start' => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null, |
|
386 | + 'expiration' => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null, |
|
387 | + 'is_single_use' => isset( $_POST['wpinv_discount_single_use'] ), |
|
388 | 388 | 'type' => isset( $_POST['wpinv_discount_type'] ) ? wpinv_clean( $_POST['wpinv_discount_type'] ) : null, |
389 | - 'is_recurring' => isset( $_POST['wpinv_discount_recurring'] ), |
|
390 | - 'items' => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(), |
|
391 | - 'excluded_items' => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(), |
|
392 | - 'max_uses' => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null, |
|
393 | - 'min_total' => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null, |
|
394 | - 'max_total' => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null, |
|
395 | - ) |
|
389 | + 'is_recurring' => isset( $_POST['wpinv_discount_recurring'] ), |
|
390 | + 'items' => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(), |
|
391 | + 'excluded_items' => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(), |
|
392 | + 'max_uses' => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null, |
|
393 | + 'min_total' => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null, |
|
394 | + 'max_total' => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null, |
|
395 | + ) |
|
396 | 396 | ); |
397 | 397 | |
398 | - $discount->save(); |
|
399 | - do_action( 'getpaid_discount_metabox_save', $post_id, $discount ); |
|
400 | - } |
|
398 | + $discount->save(); |
|
399 | + do_action( 'getpaid_discount_metabox_save', $post_id, $discount ); |
|
400 | + } |
|
401 | 401 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -10,198 +10,198 @@ discard block |
||
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 | - * 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_kses_post_deep( 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 | - /* |
|
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_kses_post_deep( 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 | 205 | |-------------------------------------------------------------------------- |
206 | 206 | | Payment Forms. |
207 | 207 | |-------------------------------------------------------------------------- |
@@ -210,39 +210,39 @@ discard block |
||
210 | 210 | | submission has an active payment form etc. |
211 | 211 | */ |
212 | 212 | |
213 | - /** |
|
214 | - * Prepares the submission's payment form. |
|
215 | - * |
|
216 | - * @since 1.0.19 |
|
217 | - */ |
|
218 | - public function process_payment_form() { |
|
213 | + /** |
|
214 | + * Prepares the submission's payment form. |
|
215 | + * |
|
216 | + * @since 1.0.19 |
|
217 | + */ |
|
218 | + public function process_payment_form() { |
|
219 | 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 | - } |
|
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 | 224 | |
225 | - // Fetch the payment form. |
|
226 | - $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
225 | + // Fetch the payment form. |
|
226 | + $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
227 | 227 | |
228 | - if ( ! $this->payment_form->is_active() ) { |
|
229 | - throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
230 | - } |
|
228 | + if ( ! $this->payment_form->is_active() ) { |
|
229 | + throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
230 | + } |
|
231 | 231 | |
232 | - do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
233 | - } |
|
232 | + do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
233 | + } |
|
234 | 234 | |
235 | 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 | - } |
|
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 | 244 | |
245 | - /* |
|
245 | + /* |
|
246 | 246 | |-------------------------------------------------------------------------- |
247 | 247 | | Invoices. |
248 | 248 | |-------------------------------------------------------------------------- |
@@ -251,84 +251,84 @@ discard block |
||
251 | 251 | | might be for an existing invoice. |
252 | 252 | */ |
253 | 253 | |
254 | - /** |
|
255 | - * Prepares the submission's invoice. |
|
256 | - * |
|
257 | - * @since 1.0.19 |
|
258 | - */ |
|
259 | - public function process_invoice() { |
|
254 | + /** |
|
255 | + * Prepares the submission's invoice. |
|
256 | + * |
|
257 | + * @since 1.0.19 |
|
258 | + */ |
|
259 | + public function process_invoice() { |
|
260 | 260 | |
261 | - // Abort if there is no invoice. |
|
262 | - if ( empty( $this->data['invoice_id'] ) ) { |
|
263 | - return; |
|
264 | - } |
|
261 | + // Abort if there is no invoice. |
|
262 | + if ( empty( $this->data['invoice_id'] ) ) { |
|
263 | + return; |
|
264 | + } |
|
265 | 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'] ); |
|
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 | 269 | |
270 | 270 | if ( empty( $invoice ) ) { |
271 | - throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
272 | - } |
|
271 | + throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
272 | + } |
|
273 | 273 | |
274 | - if ( $invoice->is_paid() ) { |
|
275 | - throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
276 | - } |
|
274 | + if ( $invoice->is_paid() ) { |
|
275 | + throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
276 | + } |
|
277 | 277 | |
278 | - $this->payment_form->invoice = $invoice; |
|
279 | - if ( ! $this->payment_form->is_default() ) { |
|
278 | + $this->payment_form->invoice = $invoice; |
|
279 | + if ( ! $this->payment_form->is_default() ) { |
|
280 | 280 | |
281 | - $items = array(); |
|
282 | - $item_ids = array(); |
|
281 | + $items = array(); |
|
282 | + $item_ids = array(); |
|
283 | 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 | - } |
|
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 | 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 | - } |
|
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 | 297 | |
298 | - $this->payment_form->set_items( $items ); |
|
298 | + $this->payment_form->set_items( $items ); |
|
299 | 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() { |
|
318 | - return $this->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 | - /* |
|
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() { |
|
318 | + return $this->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 | 332 | |-------------------------------------------------------------------------- |
333 | 333 | | Items. |
334 | 334 | |-------------------------------------------------------------------------- |
@@ -337,129 +337,129 @@ discard block |
||
337 | 337 | | recurring item. But can have an unlimited number of non-recurring items. |
338 | 338 | */ |
339 | 339 | |
340 | - /** |
|
341 | - * Prepares the submission's items. |
|
342 | - * |
|
343 | - * @since 1.0.19 |
|
344 | - */ |
|
345 | - public function process_items() { |
|
346 | - |
|
347 | - $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
348 | - |
|
349 | - foreach ( $processor->items as $item ) { |
|
350 | - $this->add_item( $item ); |
|
351 | - } |
|
352 | - |
|
353 | - do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
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 | - /* |
|
340 | + /** |
|
341 | + * Prepares the submission's items. |
|
342 | + * |
|
343 | + * @since 1.0.19 |
|
344 | + */ |
|
345 | + public function process_items() { |
|
346 | + |
|
347 | + $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
348 | + |
|
349 | + foreach ( $processor->items as $item ) { |
|
350 | + $this->add_item( $item ); |
|
351 | + } |
|
352 | + |
|
353 | + do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
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 | 463 | |-------------------------------------------------------------------------- |
464 | 464 | | Taxes |
465 | 465 | |-------------------------------------------------------------------------- |
@@ -468,128 +468,128 @@ discard block |
||
468 | 468 | | or only one-time. |
469 | 469 | */ |
470 | 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 ( isset( $_POST['billing']['country'] ) && 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 | - /* |
|
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 ( isset( $_POST['billing']['country'] ) && 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 | 593 | |-------------------------------------------------------------------------- |
594 | 594 | | Discounts |
595 | 595 | |-------------------------------------------------------------------------- |
@@ -598,99 +598,99 @@ discard block |
||
598 | 598 | | or only one-time. They also do not have to come from a discount code. |
599 | 599 | */ |
600 | 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 | - /* |
|
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 | 694 | |-------------------------------------------------------------------------- |
695 | 695 | | Fees |
696 | 696 | |-------------------------------------------------------------------------- |
@@ -700,89 +700,89 @@ discard block |
||
700 | 700 | | fees. |
701 | 701 | */ |
702 | 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() { |
|
754 | - return $this->totals['fees']['initial']; |
|
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 | - /* |
|
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() { |
|
754 | + return $this->totals['fees']['initial']; |
|
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 | 786 | |-------------------------------------------------------------------------- |
787 | 787 | | MISC |
788 | 788 | |-------------------------------------------------------------------------- |
@@ -790,119 +790,119 @@ discard block |
||
790 | 790 | | Extra submission functions. |
791 | 791 | */ |
792 | 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 ) { |
|
896 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
897 | - } |
|
898 | - |
|
899 | - /** |
|
900 | - * Formats an amount |
|
901 | - * |
|
902 | - * @since 1.0.19 |
|
903 | - */ |
|
904 | - public function format_amount( $amount ) { |
|
905 | - return wpinv_price( $amount, $this->get_currency() ); |
|
906 | - } |
|
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 ) { |
|
896 | + return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
897 | + } |
|
898 | + |
|
899 | + /** |
|
900 | + * Formats an amount |
|
901 | + * |
|
902 | + * @since 1.0.19 |
|
903 | + */ |
|
904 | + public function format_amount( $amount ) { |
|
905 | + return wpinv_price( $amount, $this->get_currency() ); |
|
906 | + } |
|
907 | 907 | |
908 | 908 | } |
@@ -13,65 +13,65 @@ discard block |
||
13 | 13 | class GetPaid_Worldpay_Gateway extends GetPaid_Payment_Gateway { |
14 | 14 | |
15 | 15 | /** |
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | 20 | public $id = 'worldpay'; |
21 | 21 | |
22 | 22 | /** |
23 | - * Payment method order. |
|
24 | - * |
|
25 | - * @var int |
|
26 | - */ |
|
23 | + * Payment method order. |
|
24 | + * |
|
25 | + * @var int |
|
26 | + */ |
|
27 | 27 | public $order = 5; |
28 | 28 | |
29 | 29 | /** |
30 | - * Endpoint for requests from Worldpay. |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $notify_url; |
|
35 | - |
|
36 | - /** |
|
37 | - * Endpoint for requests to Worldpay. |
|
38 | - * |
|
39 | - * @var string |
|
40 | - */ |
|
30 | + * Endpoint for requests from Worldpay. |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $notify_url; |
|
35 | + |
|
36 | + /** |
|
37 | + * Endpoint for requests to Worldpay. |
|
38 | + * |
|
39 | + * @var string |
|
40 | + */ |
|
41 | 41 | protected $endpoint; |
42 | 42 | |
43 | 43 | /** |
44 | - * An array of features that this gateway supports. |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
44 | + * An array of features that this gateway supports. |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | 48 | protected $supports = array( 'sandbox' ); |
49 | 49 | |
50 | 50 | /** |
51 | - * Currencies this gateway is allowed for. |
|
52 | - * |
|
53 | - * @var array |
|
54 | - */ |
|
55 | - public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' ); |
|
51 | + * Currencies this gateway is allowed for. |
|
52 | + * |
|
53 | + * @var array |
|
54 | + */ |
|
55 | + public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' ); |
|
56 | 56 | |
57 | 57 | /** |
58 | - * URL to view a transaction. |
|
59 | - * |
|
60 | - * @var string |
|
61 | - */ |
|
58 | + * URL to view a transaction. |
|
59 | + * |
|
60 | + * @var string |
|
61 | + */ |
|
62 | 62 | public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s'; |
63 | 63 | |
64 | 64 | /** |
65 | - * URL to view a subscription. |
|
66 | - * |
|
67 | - * @var string |
|
68 | - */ |
|
69 | - public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
65 | + * URL to view a subscription. |
|
66 | + * |
|
67 | + * @var string |
|
68 | + */ |
|
69 | + public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
70 | 70 | |
71 | 71 | /** |
72 | - * Class constructor. |
|
73 | - */ |
|
74 | - public function __construct() { |
|
72 | + * Class constructor. |
|
73 | + */ |
|
74 | + public function __construct() { |
|
75 | 75 | |
76 | 76 | $this->method_title = __( 'Worldpay', 'invoicing' ); |
77 | 77 | $this->title = __( 'Worldpay - Credit Card / Debit Card', 'invoicing' ); |
@@ -85,15 +85,15 @@ discard block |
||
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
88 | - * Process Payment. |
|
89 | - * |
|
90 | - * |
|
91 | - * @param WPInv_Invoice $invoice Invoice. |
|
92 | - * @param array $submission_data Posted checkout fields. |
|
93 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
88 | + * Process Payment. |
|
89 | + * |
|
90 | + * |
|
91 | + * @param WPInv_Invoice $invoice Invoice. |
|
92 | + * @param array $submission_data Posted checkout fields. |
|
93 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
97 | 97 | |
98 | 98 | // Get redirect url. |
99 | 99 | $worldpay_redirect = esc_url( $this->get_request_url( $invoice ) ); |
@@ -128,31 +128,31 @@ discard block |
||
128 | 128 | } |
129 | 129 | |
130 | 130 | /** |
131 | - * Get the Worldpay request URL for an invoice. |
|
132 | - * |
|
133 | - * @param WPInv_Invoice $invoice Invoice object. |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - public function get_request_url( $invoice ) { |
|
131 | + * Get the Worldpay request URL for an invoice. |
|
132 | + * |
|
133 | + * @param WPInv_Invoice $invoice Invoice object. |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + public function get_request_url( $invoice ) { |
|
137 | 137 | |
138 | 138 | // Endpoint for this request |
139 | - $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
139 | + $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
140 | 140 | |
141 | 141 | return $this->endpoint; |
142 | 142 | |
143 | - } |
|
143 | + } |
|
144 | 144 | |
145 | 145 | /** |
146 | - * Get Worldpay Args for passing to Worldpay. |
|
147 | - * |
|
148 | - * @param WPInv_Invoice $invoice Invoice object. |
|
149 | - * @return array |
|
150 | - */ |
|
151 | - protected function get_worldpay_args( $invoice ) { |
|
152 | - |
|
153 | - return apply_filters( |
|
154 | - 'getpaid_worldpay_args', |
|
155 | - array( |
|
146 | + * Get Worldpay Args for passing to Worldpay. |
|
147 | + * |
|
148 | + * @param WPInv_Invoice $invoice Invoice object. |
|
149 | + * @return array |
|
150 | + */ |
|
151 | + protected function get_worldpay_args( $invoice ) { |
|
152 | + |
|
153 | + return apply_filters( |
|
154 | + 'getpaid_worldpay_args', |
|
155 | + array( |
|
156 | 156 | 'amount' => wpinv_sanitize_amount( $invoice->get_total() ), // mandatory |
157 | 157 | 'cartId' => wpinv_clean( $invoice->get_number() ), // mandatory reference for the item purchased |
158 | 158 | 'currency' => wpinv_clean( $invoice->get_currency() ), // mandatory |
@@ -177,18 +177,18 @@ discard block |
||
177 | 177 | 'countryString' => wpinv_clean( wpinv_country_name( $invoice->get_country() ) ), |
178 | 178 | 'compName' => wpinv_clean( $invoice->get_company() ), |
179 | 179 | ), |
180 | - $invoice |
|
181 | - ); |
|
180 | + $invoice |
|
181 | + ); |
|
182 | 182 | |
183 | 183 | } |
184 | 184 | |
185 | 185 | /** |
186 | - * Secures worldpay args with an md5 hash. |
|
187 | - * |
|
188 | - * @param array $args Gateway args. |
|
189 | - * @return array |
|
190 | - */ |
|
191 | - public function hash_args( $args ) { |
|
186 | + * Secures worldpay args with an md5 hash. |
|
187 | + * |
|
188 | + * @param array $args Gateway args. |
|
189 | + * @return array |
|
190 | + */ |
|
191 | + public function hash_args( $args ) { |
|
192 | 192 | |
193 | 193 | $md5_secret = $this->get_option( 'md5_secret' ); |
194 | 194 | |
@@ -204,16 +204,16 @@ discard block |
||
204 | 204 | } |
205 | 205 | |
206 | 206 | /** |
207 | - * Processes ipns and marks payments as complete. |
|
208 | - * |
|
209 | - * @return void |
|
210 | - */ |
|
211 | - public function verify_ipn() { |
|
207 | + * Processes ipns and marks payments as complete. |
|
208 | + * |
|
209 | + * @return void |
|
210 | + */ |
|
211 | + public function verify_ipn() { |
|
212 | 212 | |
213 | 213 | // Validate the IPN. |
214 | 214 | if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
215 | - wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) ); |
|
216 | - } |
|
215 | + wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) ); |
|
216 | + } |
|
217 | 217 | |
218 | 218 | // Process the IPN. |
219 | 219 | $posted = wp_kses_post_deep( wp_unslash( $_POST ) ); |
@@ -229,8 +229,8 @@ discard block |
||
229 | 229 | $invoice->set_transaction_id( wpinv_clean( $posted['transId'] ) ); |
230 | 230 | } |
231 | 231 | |
232 | - // Update the ip address. |
|
233 | - if ( ! empty( $posted['ipAddress'] ) ) { |
|
232 | + // Update the ip address. |
|
233 | + if ( ! empty( $posted['ipAddress'] ) ) { |
|
234 | 234 | $invoice->set_ip( wpinv_clean( $posted['ipAddress'] ) ); |
235 | 235 | } |
236 | 236 | |
@@ -257,9 +257,9 @@ discard block |
||
257 | 257 | } |
258 | 258 | |
259 | 259 | /** |
260 | - * Check Worldpay IPN validity. |
|
261 | - */ |
|
262 | - public function validate_ipn() { |
|
260 | + * Check Worldpay IPN validity. |
|
261 | + */ |
|
262 | + public function validate_ipn() { |
|
263 | 263 | |
264 | 264 | wpinv_error_log( 'Validating Worldpay IPN response' ); |
265 | 265 | |
@@ -305,11 +305,11 @@ discard block |
||
305 | 305 | } |
306 | 306 | |
307 | 307 | /** |
308 | - * Filters the gateway settings. |
|
309 | - * |
|
310 | - * @param array $admin_settings |
|
311 | - */ |
|
312 | - public function admin_settings( $admin_settings ) { |
|
308 | + * Filters the gateway settings. |
|
309 | + * |
|
310 | + * @param array $admin_settings |
|
311 | + */ |
|
312 | + public function admin_settings( $admin_settings ) { |
|
313 | 313 | |
314 | 314 | $currencies = sprintf( |
315 | 315 | __( 'Supported Currencies: %s', 'invoicing' ), |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | 'readonly' => true |
351 | 351 | ); |
352 | 352 | |
353 | - return $admin_settings; |
|
354 | - } |
|
353 | + return $admin_settings; |
|
354 | + } |
|
355 | 355 | |
356 | 356 | } |
@@ -15,31 +15,31 @@ discard block |
||
15 | 15 | abstract class GetPaid_Authorize_Net_Legacy_Gateway extends GetPaid_Payment_Gateway { |
16 | 16 | |
17 | 17 | /** |
18 | - * Class constructor. |
|
19 | - */ |
|
20 | - public function __construct() { |
|
18 | + * Class constructor. |
|
19 | + */ |
|
20 | + public function __construct() { |
|
21 | 21 | parent::__construct(); |
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
25 | - * Returns the API URL. |
|
26 | - * |
|
27 | - * |
|
28 | - * @param WPInv_Invoice $invoice Invoice. |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function get_api_url( $invoice ) { |
|
25 | + * Returns the API URL. |
|
26 | + * |
|
27 | + * |
|
28 | + * @param WPInv_Invoice $invoice Invoice. |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function get_api_url( $invoice ) { |
|
32 | 32 | return $this->is_sandbox( $invoice ) ? 'https://apitest.authorize.net/xml/v1/request.api' : 'https://api.authorize.net/xml/v1/request.api'; |
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
36 | - * Communicates with authorize.net |
|
37 | - * |
|
38 | - * |
|
39 | - * @param array $post Data to post. |
|
36 | + * Communicates with authorize.net |
|
37 | + * |
|
38 | + * |
|
39 | + * @param array $post Data to post. |
|
40 | 40 | * @param WPInv_Invoice $invoice Invoice. |
41 | - * @return stdClass|WP_Error |
|
42 | - */ |
|
41 | + * @return stdClass|WP_Error |
|
42 | + */ |
|
43 | 43 | public function post( $post, $invoice ){ |
44 | 44 | |
45 | 45 | $url = $this->get_api_url( $invoice ); |
@@ -89,12 +89,12 @@ discard block |
||
89 | 89 | } |
90 | 90 | |
91 | 91 | /** |
92 | - * Returns the API authentication params. |
|
93 | - * |
|
94 | - * |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - public function get_auth_params() { |
|
92 | + * Returns the API authentication params. |
|
93 | + * |
|
94 | + * |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + public function get_auth_params() { |
|
98 | 98 | |
99 | 99 | return array( |
100 | 100 | 'name' => $this->get_option( 'login_id' ), |
@@ -104,13 +104,13 @@ discard block |
||
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
107 | - * Cancels a subscription remotely |
|
108 | - * |
|
109 | - * |
|
110 | - * @param WPInv_Subscription $subscription Subscription. |
|
107 | + * Cancels a subscription remotely |
|
108 | + * |
|
109 | + * |
|
110 | + * @param WPInv_Subscription $subscription Subscription. |
|
111 | 111 | * @param WPInv_Invoice $invoice Invoice. |
112 | - */ |
|
113 | - public function cancel_subscription( $subscription, $invoice ) { |
|
112 | + */ |
|
113 | + public function cancel_subscription( $subscription, $invoice ) { |
|
114 | 114 | |
115 | 115 | // Backwards compatibility. New version do not use authorize.net subscriptions. |
116 | 116 | $this->post( |
@@ -126,17 +126,17 @@ discard block |
||
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
129 | - * Processes ipns. |
|
130 | - * |
|
131 | - * @return void |
|
132 | - */ |
|
133 | - public function verify_ipn() { |
|
129 | + * Processes ipns. |
|
130 | + * |
|
131 | + * @return void |
|
132 | + */ |
|
133 | + public function verify_ipn() { |
|
134 | 134 | |
135 | 135 | $this->maybe_process_old_ipn(); |
136 | 136 | |
137 | 137 | // Validate the IPN. |
138 | 138 | if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
139 | - wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 500 ) ); |
|
139 | + wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 500 ) ); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | // Event type. |
@@ -175,24 +175,24 @@ discard block |
||
175 | 175 | } |
176 | 176 | |
177 | 177 | /** |
178 | - * Validates IPN invoices. |
|
179 | - * |
|
178 | + * Validates IPN invoices. |
|
179 | + * |
|
180 | 180 | * @param WPInv_Invoice $invoice |
181 | 181 | * @param object $payload |
182 | - * @return void |
|
183 | - */ |
|
184 | - public function validate_ipn_invoice( $invoice, $payload ) { |
|
182 | + * @return void |
|
183 | + */ |
|
184 | + public function validate_ipn_invoice( $invoice, $payload ) { |
|
185 | 185 | if ( ! $invoice->exists() || $payload->id != $invoice->get_transaction_id() ) { |
186 | 186 | exit; |
187 | 187 | } |
188 | 188 | } |
189 | 189 | |
190 | 190 | /** |
191 | - * Process subscriptio IPNS. |
|
192 | - * |
|
193 | - * @return void |
|
194 | - */ |
|
195 | - public function maybe_process_old_ipn() { |
|
191 | + * Process subscriptio IPNS. |
|
192 | + * |
|
193 | + * @return void |
|
194 | + */ |
|
195 | + public function maybe_process_old_ipn() { |
|
196 | 196 | |
197 | 197 | $data = wp_kses_post_deep( wp_unslash( $_POST ) ); |
198 | 198 | |
@@ -234,11 +234,11 @@ discard block |
||
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
237 | - * Validates the old IPN signature. |
|
237 | + * Validates the old IPN signature. |
|
238 | 238 | * |
239 | 239 | * @param array $posted |
240 | - */ |
|
241 | - public function validate_old_ipn_signature( $posted ) { |
|
240 | + */ |
|
241 | + public function validate_old_ipn_signature( $posted ) { |
|
242 | 242 | |
243 | 243 | $signature = $this->get_option( 'signature_key' ); |
244 | 244 | if ( ! empty( $signature ) ) { |
@@ -257,9 +257,9 @@ discard block |
||
257 | 257 | } |
258 | 258 | |
259 | 259 | /** |
260 | - * Check Authorize.NET IPN validity. |
|
261 | - */ |
|
262 | - public function validate_ipn() { |
|
260 | + * Check Authorize.NET IPN validity. |
|
261 | + */ |
|
262 | + public function validate_ipn() { |
|
263 | 263 | |
264 | 264 | wpinv_error_log( 'Validating Authorize.NET IPN response' ); |
265 | 265 |
@@ -12,474 +12,474 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Paypal_Gateway_IPN_Handler { |
14 | 14 | |
15 | - /** |
|
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - protected $id = 'paypal'; |
|
21 | - |
|
22 | - /** |
|
23 | - * Payment method object. |
|
24 | - * |
|
25 | - * @var GetPaid_Paypal_Gateway |
|
26 | - */ |
|
27 | - protected $gateway; |
|
28 | - |
|
29 | - /** |
|
30 | - * Class constructor. |
|
31 | - * |
|
32 | - * @param GetPaid_Paypal_Gateway $gateway |
|
33 | - */ |
|
34 | - public function __construct( $gateway ) { |
|
35 | - $this->gateway = $gateway; |
|
36 | - $this->verify_ipn(); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Processes ipns and marks payments as complete. |
|
41 | - * |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function verify_ipn() { |
|
45 | - |
|
46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
47 | - |
|
48 | - // Validate the IPN. |
|
49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | - } |
|
52 | - |
|
53 | - // Process the IPN. |
|
54 | - $posted = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | - |
|
57 | - // Abort if it was not paid by our gateway. |
|
58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
60 | - wp_die( 'Invoice not paid via PayPal', 200 ); |
|
61 | - } |
|
62 | - |
|
63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | - |
|
66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
68 | - |
|
69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | - wpinv_error_log( 'Done processing IPN', false ); |
|
72 | - wp_die( 'Processed', 200 ); |
|
73 | - } |
|
74 | - |
|
75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
77 | - |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Retrieves IPN Invoice. |
|
82 | - * |
|
83 | - * @param array $posted |
|
84 | - * @return WPInv_Invoice |
|
85 | - */ |
|
86 | - protected function get_ipn_invoice( $posted ) { |
|
87 | - |
|
88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
89 | - |
|
90 | - if ( ! empty( $posted['custom'] ) ) { |
|
91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | - |
|
93 | - if ( $invoice->exists() ) { |
|
94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
95 | - return $invoice; |
|
96 | - } |
|
97 | - |
|
98 | - } |
|
99 | - |
|
100 | - wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
101 | - wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Check PayPal IPN validity. |
|
106 | - */ |
|
107 | - protected function validate_ipn() { |
|
108 | - |
|
109 | - wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
110 | - |
|
111 | - // Retrieve the associated invoice. |
|
112 | - $posted = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
113 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
114 | - |
|
115 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
116 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false ); |
|
117 | - } |
|
118 | - |
|
119 | - // Validate the IPN. |
|
120 | - $posted['cmd'] = '_notify-validate'; |
|
121 | - |
|
122 | - // Send back post vars to paypal. |
|
123 | - $params = array( |
|
124 | - 'body' => $posted, |
|
125 | - 'timeout' => 60, |
|
126 | - 'httpversion' => '1.1', |
|
127 | - 'compress' => false, |
|
128 | - 'decompress' => false, |
|
129 | - 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
130 | - ); |
|
131 | - |
|
132 | - // Post back to get a response. |
|
133 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
134 | - |
|
135 | - // Check to see if the request was valid. |
|
136 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
137 | - wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false ); |
|
138 | - return true; |
|
139 | - } |
|
140 | - |
|
141 | - if ( is_wp_error( $response ) ) { |
|
142 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
143 | - return false; |
|
144 | - } |
|
15 | + /** |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + protected $id = 'paypal'; |
|
21 | + |
|
22 | + /** |
|
23 | + * Payment method object. |
|
24 | + * |
|
25 | + * @var GetPaid_Paypal_Gateway |
|
26 | + */ |
|
27 | + protected $gateway; |
|
28 | + |
|
29 | + /** |
|
30 | + * Class constructor. |
|
31 | + * |
|
32 | + * @param GetPaid_Paypal_Gateway $gateway |
|
33 | + */ |
|
34 | + public function __construct( $gateway ) { |
|
35 | + $this->gateway = $gateway; |
|
36 | + $this->verify_ipn(); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Processes ipns and marks payments as complete. |
|
41 | + * |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function verify_ipn() { |
|
45 | + |
|
46 | + wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
47 | + |
|
48 | + // Validate the IPN. |
|
49 | + if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | + wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | + } |
|
52 | + |
|
53 | + // Process the IPN. |
|
54 | + $posted = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
55 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | + |
|
57 | + // Abort if it was not paid by our gateway. |
|
58 | + if ( $this->id != $invoice->get_gateway() ) { |
|
59 | + wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
60 | + wp_die( 'Invoice not paid via PayPal', 200 ); |
|
61 | + } |
|
62 | + |
|
63 | + $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | + $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | + |
|
66 | + wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
67 | + wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
68 | + |
|
69 | + if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | + call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | + wpinv_error_log( 'Done processing IPN', false ); |
|
72 | + wp_die( 'Processed', 200 ); |
|
73 | + } |
|
74 | + |
|
75 | + wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
76 | + wp_die( 'Unsupported IPN type', 200 ); |
|
77 | + |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Retrieves IPN Invoice. |
|
82 | + * |
|
83 | + * @param array $posted |
|
84 | + * @return WPInv_Invoice |
|
85 | + */ |
|
86 | + protected function get_ipn_invoice( $posted ) { |
|
87 | + |
|
88 | + wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
89 | + |
|
90 | + if ( ! empty( $posted['custom'] ) ) { |
|
91 | + $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | + |
|
93 | + if ( $invoice->exists() ) { |
|
94 | + wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
95 | + return $invoice; |
|
96 | + } |
|
97 | + |
|
98 | + } |
|
99 | + |
|
100 | + wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
101 | + wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Check PayPal IPN validity. |
|
106 | + */ |
|
107 | + protected function validate_ipn() { |
|
108 | + |
|
109 | + wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
110 | + |
|
111 | + // Retrieve the associated invoice. |
|
112 | + $posted = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
113 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
114 | + |
|
115 | + if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
116 | + wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false ); |
|
117 | + } |
|
118 | + |
|
119 | + // Validate the IPN. |
|
120 | + $posted['cmd'] = '_notify-validate'; |
|
121 | + |
|
122 | + // Send back post vars to paypal. |
|
123 | + $params = array( |
|
124 | + 'body' => $posted, |
|
125 | + 'timeout' => 60, |
|
126 | + 'httpversion' => '1.1', |
|
127 | + 'compress' => false, |
|
128 | + 'decompress' => false, |
|
129 | + 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
130 | + ); |
|
131 | + |
|
132 | + // Post back to get a response. |
|
133 | + $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
134 | + |
|
135 | + // Check to see if the request was valid. |
|
136 | + if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
137 | + wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false ); |
|
138 | + return true; |
|
139 | + } |
|
140 | + |
|
141 | + if ( is_wp_error( $response ) ) { |
|
142 | + wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
143 | + return false; |
|
144 | + } |
|
145 | 145 | |
146 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
147 | - return false; |
|
148 | - |
|
149 | - } |
|
146 | + wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
147 | + return false; |
|
148 | + |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Check currency from IPN matches the invoice. |
|
153 | - * |
|
154 | - * @param WPInv_Invoice $invoice Invoice object. |
|
155 | - * @param string $currency currency to validate. |
|
156 | - */ |
|
157 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
151 | + /** |
|
152 | + * Check currency from IPN matches the invoice. |
|
153 | + * |
|
154 | + * @param WPInv_Invoice $invoice Invoice object. |
|
155 | + * @param string $currency currency to validate. |
|
156 | + */ |
|
157 | + protected function validate_ipn_currency( $invoice, $currency ) { |
|
158 | 158 | |
159 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
159 | + if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
160 | 160 | |
161 | - /* translators: %s: currency code. */ |
|
162 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
161 | + /* translators: %s: currency code. */ |
|
162 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
163 | 163 | |
164 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
165 | - } |
|
164 | + wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
165 | + } |
|
166 | 166 | |
167 | - wpinv_error_log( $currency, 'Validated IPN Currency', false ); |
|
168 | - } |
|
167 | + wpinv_error_log( $currency, 'Validated IPN Currency', false ); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Check payment amount from IPN matches the invoice. |
|
172 | - * |
|
173 | - * @param WPInv_Invoice $invoice Invoice object. |
|
174 | - * @param float $amount amount to validate. |
|
175 | - */ |
|
176 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
177 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
170 | + /** |
|
171 | + * Check payment amount from IPN matches the invoice. |
|
172 | + * |
|
173 | + * @param WPInv_Invoice $invoice Invoice object. |
|
174 | + * @param float $amount amount to validate. |
|
175 | + */ |
|
176 | + protected function validate_ipn_amount( $invoice, $amount ) { |
|
177 | + if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
178 | 178 | |
179 | - /* translators: %s: Amount. */ |
|
180 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
179 | + /* translators: %s: Amount. */ |
|
180 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
181 | 181 | |
182 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
183 | - } |
|
182 | + wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
183 | + } |
|
184 | 184 | |
185 | - wpinv_error_log( $amount, 'Validated IPN Amount', false ); |
|
186 | - } |
|
185 | + wpinv_error_log( $amount, 'Validated IPN Amount', false ); |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Verify receiver email from PayPal. |
|
190 | - * |
|
191 | - * @param WPInv_Invoice $invoice Invoice object. |
|
192 | - * @param string $receiver_email Email to validate. |
|
193 | - */ |
|
194 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
195 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
188 | + /** |
|
189 | + * Verify receiver email from PayPal. |
|
190 | + * |
|
191 | + * @param WPInv_Invoice $invoice Invoice object. |
|
192 | + * @param string $receiver_email Email to validate. |
|
193 | + */ |
|
194 | + protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
195 | + $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
196 | 196 | |
197 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
198 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
197 | + if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
198 | + wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
199 | 199 | |
200 | - /* translators: %s: email address . */ |
|
201 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
200 | + /* translators: %s: email address . */ |
|
201 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
202 | 202 | |
203 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
204 | - } |
|
203 | + return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
204 | + } |
|
205 | 205 | |
206 | - wpinv_error_log( 'Validated PayPal Email', false ); |
|
207 | - } |
|
206 | + wpinv_error_log( 'Validated PayPal Email', false ); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Handles one time payments. |
|
211 | - * |
|
212 | - * @param WPInv_Invoice $invoice Invoice object. |
|
213 | - * @param array $posted Posted data. |
|
214 | - */ |
|
215 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
209 | + /** |
|
210 | + * Handles one time payments. |
|
211 | + * |
|
212 | + * @param WPInv_Invoice $invoice Invoice object. |
|
213 | + * @param array $posted Posted data. |
|
214 | + */ |
|
215 | + protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
216 | 216 | |
217 | - // Collect payment details |
|
218 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
219 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
217 | + // Collect payment details |
|
218 | + $payment_status = strtolower( $posted['payment_status'] ); |
|
219 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
220 | 220 | |
221 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
222 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
221 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
222 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
223 | 223 | |
224 | - // Update the transaction id. |
|
225 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
226 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
227 | - $invoice->save(); |
|
228 | - } |
|
224 | + // Update the transaction id. |
|
225 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
226 | + $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
227 | + $invoice->save(); |
|
228 | + } |
|
229 | 229 | |
230 | - $invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) ); |
|
230 | + $invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) ); |
|
231 | 231 | |
232 | - // Process a refund. |
|
233 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
232 | + // Process a refund. |
|
233 | + if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
234 | 234 | |
235 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
235 | + update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
236 | 236 | |
237 | - if ( ! $invoice->is_refunded() ) { |
|
238 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
239 | - } |
|
237 | + if ( ! $invoice->is_refunded() ) { |
|
238 | + $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
239 | + } |
|
240 | 240 | |
241 | - return wpinv_error_log( $posted['reason_code'], false ); |
|
242 | - } |
|
241 | + return wpinv_error_log( $posted['reason_code'], false ); |
|
242 | + } |
|
243 | 243 | |
244 | - // Process payments. |
|
245 | - if ( $payment_status == 'completed' ) { |
|
244 | + // Process payments. |
|
245 | + if ( $payment_status == 'completed' ) { |
|
246 | 246 | |
247 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
248 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
249 | - } |
|
247 | + if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
248 | + return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
249 | + } |
|
250 | 250 | |
251 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
251 | + $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
252 | 252 | |
253 | - $note = ''; |
|
253 | + $note = ''; |
|
254 | 254 | |
255 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
256 | - $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
257 | - } |
|
255 | + if ( ! empty( $posted['mc_fee'] ) ) { |
|
256 | + $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
257 | + } |
|
258 | 258 | |
259 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
260 | - $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
261 | - } |
|
259 | + if ( ! empty( $posted['payer_status'] ) ) { |
|
260 | + $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
261 | + } |
|
262 | 262 | |
263 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
264 | - return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
263 | + $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
264 | + return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
265 | 265 | |
266 | - } |
|
266 | + } |
|
267 | 267 | |
268 | - // Pending payments. |
|
269 | - if ( $payment_status == 'pending' ) { |
|
268 | + // Pending payments. |
|
269 | + if ( $payment_status == 'pending' ) { |
|
270 | 270 | |
271 | - /* translators: %s: pending reason. */ |
|
272 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
271 | + /* translators: %s: pending reason. */ |
|
272 | + $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
273 | 273 | |
274 | - return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
275 | - } |
|
274 | + return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
275 | + } |
|
276 | 276 | |
277 | - /* translators: %s: payment status. */ |
|
278 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
277 | + /* translators: %s: payment status. */ |
|
278 | + $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
279 | 279 | |
280 | - } |
|
280 | + } |
|
281 | 281 | |
282 | - /** |
|
283 | - * Handles one time payments. |
|
284 | - * |
|
285 | - * @param WPInv_Invoice $invoice Invoice object. |
|
286 | - * @param array $posted Posted data. |
|
287 | - */ |
|
288 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
289 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
290 | - } |
|
282 | + /** |
|
283 | + * Handles one time payments. |
|
284 | + * |
|
285 | + * @param WPInv_Invoice $invoice Invoice object. |
|
286 | + * @param array $posted Posted data. |
|
287 | + */ |
|
288 | + protected function ipn_txn_cart( $invoice, $posted ) { |
|
289 | + $this->ipn_txn_web_accept( $invoice, $posted ); |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * Handles subscription sign ups. |
|
294 | - * |
|
295 | - * @param WPInv_Invoice $invoice Invoice object. |
|
296 | - * @param array $posted Posted data. |
|
297 | - */ |
|
298 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
292 | + /** |
|
293 | + * Handles subscription sign ups. |
|
294 | + * |
|
295 | + * @param WPInv_Invoice $invoice Invoice object. |
|
296 | + * @param array $posted Posted data. |
|
297 | + */ |
|
298 | + protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
299 | 299 | |
300 | - wpinv_error_log( 'Processing subscription signup', false ); |
|
300 | + wpinv_error_log( 'Processing subscription signup', false ); |
|
301 | 301 | |
302 | - // Make sure the invoice has a subscription. |
|
303 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
302 | + // Make sure the invoice has a subscription. |
|
303 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
304 | 304 | |
305 | - if ( empty( $subscription ) ) { |
|
306 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
307 | - } |
|
305 | + if ( empty( $subscription ) ) { |
|
306 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
307 | + } |
|
308 | 308 | |
309 | - wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
309 | + wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
310 | 310 | |
311 | - // Validate the IPN. |
|
312 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
313 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
314 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
311 | + // Validate the IPN. |
|
312 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
313 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
314 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
315 | 315 | |
316 | - // Activate the subscription. |
|
317 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
318 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
319 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
320 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
321 | - $subscription->activate(); |
|
316 | + // Activate the subscription. |
|
317 | + $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
318 | + $subscription->set_date_created( current_time( 'mysql' ) ); |
|
319 | + $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
320 | + $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
321 | + $subscription->activate(); |
|
322 | 322 | |
323 | - // Set the transaction id. |
|
324 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
325 | - $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
326 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
327 | - } |
|
323 | + // Set the transaction id. |
|
324 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
325 | + $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
326 | + $invoice->set_transaction_id( $posted['txn_id'] ); |
|
327 | + } |
|
328 | 328 | |
329 | - // Update the payment status. |
|
330 | - $invoice->mark_paid(); |
|
329 | + // Update the payment status. |
|
330 | + $invoice->mark_paid(); |
|
331 | 331 | |
332 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
332 | + $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
333 | 333 | |
334 | - wpinv_error_log( 'Subscription started.', false ); |
|
335 | - } |
|
334 | + wpinv_error_log( 'Subscription started.', false ); |
|
335 | + } |
|
336 | 336 | |
337 | - /** |
|
338 | - * Handles subscription renewals. |
|
339 | - * |
|
340 | - * @param WPInv_Invoice $invoice Invoice object. |
|
341 | - * @param array $posted Posted data. |
|
342 | - */ |
|
343 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
337 | + /** |
|
338 | + * Handles subscription renewals. |
|
339 | + * |
|
340 | + * @param WPInv_Invoice $invoice Invoice object. |
|
341 | + * @param array $posted Posted data. |
|
342 | + */ |
|
343 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
344 | 344 | |
345 | - // Make sure the invoice has a subscription. |
|
346 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
345 | + // Make sure the invoice has a subscription. |
|
346 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
347 | 347 | |
348 | - if ( empty( $subscription ) ) { |
|
349 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
350 | - } |
|
348 | + if ( empty( $subscription ) ) { |
|
349 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
350 | + } |
|
351 | 351 | |
352 | - wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
352 | + wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
353 | 353 | |
354 | - // PayPal sends a subscr_payment for the first payment too. |
|
355 | - $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
|
356 | - $date_created = getpaid_format_date( $invoice->get_date_created() ); |
|
357 | - $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
|
358 | - $payment_date = getpaid_format_date( $posted['payment_date'] ); |
|
359 | - $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
|
360 | - $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
|
354 | + // PayPal sends a subscr_payment for the first payment too. |
|
355 | + $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
|
356 | + $date_created = getpaid_format_date( $invoice->get_date_created() ); |
|
357 | + $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
|
358 | + $payment_date = getpaid_format_date( $posted['payment_date'] ); |
|
359 | + $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
|
360 | + $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
|
361 | 361 | |
362 | - foreach( $dates as $date ) { |
|
362 | + foreach( $dates as $date ) { |
|
363 | 363 | |
364 | - if ( $date !== $today_date && $date !== $payment_date ) { |
|
365 | - continue; |
|
366 | - } |
|
364 | + if ( $date !== $today_date && $date !== $payment_date ) { |
|
365 | + continue; |
|
366 | + } |
|
367 | 367 | |
368 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
369 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
370 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
|
371 | - } |
|
368 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
369 | + $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
370 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
|
371 | + } |
|
372 | 372 | |
373 | - return $invoice->mark_paid(); |
|
374 | - |
|
375 | - } |
|
373 | + return $invoice->mark_paid(); |
|
374 | + |
|
375 | + } |
|
376 | 376 | |
377 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
378 | - |
|
379 | - // Abort if the payment is already recorded. |
|
380 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
381 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
382 | - } |
|
383 | - |
|
384 | - $args = array( |
|
385 | - 'transaction_id' => $posted['txn_id'], |
|
386 | - 'gateway' => $this->id, |
|
387 | - ); |
|
388 | - |
|
389 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
377 | + wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
378 | + |
|
379 | + // Abort if the payment is already recorded. |
|
380 | + if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
381 | + return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
382 | + } |
|
383 | + |
|
384 | + $args = array( |
|
385 | + 'transaction_id' => $posted['txn_id'], |
|
386 | + 'gateway' => $this->id, |
|
387 | + ); |
|
388 | + |
|
389 | + $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
390 | 390 | |
391 | - if ( empty( $invoice ) ) { |
|
392 | - return; |
|
393 | - } |
|
391 | + if ( empty( $invoice ) ) { |
|
392 | + return; |
|
393 | + } |
|
394 | 394 | |
395 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
396 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
395 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
396 | + $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
397 | 397 | |
398 | - $subscription->renew(); |
|
399 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
398 | + $subscription->renew(); |
|
399 | + wpinv_error_log( 'Subscription renewed.', false ); |
|
400 | 400 | |
401 | - } |
|
401 | + } |
|
402 | 402 | |
403 | - /** |
|
404 | - * Handles subscription cancelations. |
|
405 | - * |
|
406 | - * @param WPInv_Invoice $invoice Invoice object. |
|
407 | - */ |
|
408 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
403 | + /** |
|
404 | + * Handles subscription cancelations. |
|
405 | + * |
|
406 | + * @param WPInv_Invoice $invoice Invoice object. |
|
407 | + */ |
|
408 | + protected function ipn_txn_subscr_cancel( $invoice ) { |
|
409 | 409 | |
410 | - // Make sure the invoice has a subscription. |
|
411 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
412 | - |
|
413 | - if ( empty( $subscription ) ) { |
|
414 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
415 | - } |
|
416 | - |
|
417 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
418 | - $subscription->cancel(); |
|
419 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
410 | + // Make sure the invoice has a subscription. |
|
411 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
412 | + |
|
413 | + if ( empty( $subscription ) ) { |
|
414 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
415 | + } |
|
416 | + |
|
417 | + wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
418 | + $subscription->cancel(); |
|
419 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
420 | 420 | |
421 | - } |
|
421 | + } |
|
422 | 422 | |
423 | - /** |
|
424 | - * Handles subscription completions. |
|
425 | - * |
|
426 | - * @param WPInv_Invoice $invoice Invoice object. |
|
427 | - * @param array $posted Posted data. |
|
428 | - */ |
|
429 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
423 | + /** |
|
424 | + * Handles subscription completions. |
|
425 | + * |
|
426 | + * @param WPInv_Invoice $invoice Invoice object. |
|
427 | + * @param array $posted Posted data. |
|
428 | + */ |
|
429 | + protected function ipn_txn_subscr_eot( $invoice ) { |
|
430 | 430 | |
431 | - // Make sure the invoice has a subscription. |
|
432 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
431 | + // Make sure the invoice has a subscription. |
|
432 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
433 | 433 | |
434 | - if ( empty( $subscription ) ) { |
|
435 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
436 | - } |
|
434 | + if ( empty( $subscription ) ) { |
|
435 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
436 | + } |
|
437 | 437 | |
438 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
439 | - $subscription->complete(); |
|
440 | - wpinv_error_log( 'Subscription completed.', false ); |
|
438 | + wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
439 | + $subscription->complete(); |
|
440 | + wpinv_error_log( 'Subscription completed.', false ); |
|
441 | 441 | |
442 | - } |
|
442 | + } |
|
443 | 443 | |
444 | - /** |
|
445 | - * Handles subscription fails. |
|
446 | - * |
|
447 | - * @param WPInv_Invoice $invoice Invoice object. |
|
448 | - * @param array $posted Posted data. |
|
449 | - */ |
|
450 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
444 | + /** |
|
445 | + * Handles subscription fails. |
|
446 | + * |
|
447 | + * @param WPInv_Invoice $invoice Invoice object. |
|
448 | + * @param array $posted Posted data. |
|
449 | + */ |
|
450 | + protected function ipn_txn_subscr_failed( $invoice ) { |
|
451 | 451 | |
452 | - // Make sure the invoice has a subscription. |
|
453 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
452 | + // Make sure the invoice has a subscription. |
|
453 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
454 | 454 | |
455 | - if ( empty( $subscription ) ) { |
|
456 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
457 | - } |
|
455 | + if ( empty( $subscription ) ) { |
|
456 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
457 | + } |
|
458 | 458 | |
459 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
460 | - $subscription->failing(); |
|
461 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
459 | + wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
460 | + $subscription->failing(); |
|
461 | + wpinv_error_log( 'Subscription marked as failing.', false ); |
|
462 | 462 | |
463 | - } |
|
463 | + } |
|
464 | 464 | |
465 | - /** |
|
466 | - * Handles subscription suspensions. |
|
467 | - * |
|
468 | - * @param WPInv_Invoice $invoice Invoice object. |
|
469 | - * @param array $posted Posted data. |
|
470 | - */ |
|
471 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
465 | + /** |
|
466 | + * Handles subscription suspensions. |
|
467 | + * |
|
468 | + * @param WPInv_Invoice $invoice Invoice object. |
|
469 | + * @param array $posted Posted data. |
|
470 | + */ |
|
471 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
472 | 472 | |
473 | - // Make sure the invoice has a subscription. |
|
474 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
473 | + // Make sure the invoice has a subscription. |
|
474 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
475 | 475 | |
476 | - if ( empty( $subscription ) ) { |
|
477 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
478 | - } |
|
479 | - |
|
480 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
481 | - $subscription->cancel(); |
|
482 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
483 | - } |
|
476 | + if ( empty( $subscription ) ) { |
|
477 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
478 | + } |
|
479 | + |
|
480 | + wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
481 | + $subscription->cancel(); |
|
482 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
483 | + } |
|
484 | 484 | |
485 | 485 | } |
@@ -14,70 +14,70 @@ discard block |
||
14 | 14 | class WPInv_Ajax { |
15 | 15 | |
16 | 16 | /** |
17 | - * Hook in ajax handlers. |
|
18 | - */ |
|
19 | - public static function init() { |
|
20 | - add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 ); |
|
21 | - add_action( 'template_redirect', array( __CLASS__, 'do_wpinv_ajax' ), 0 ); |
|
22 | - self::add_ajax_events(); |
|
17 | + * Hook in ajax handlers. |
|
18 | + */ |
|
19 | + public static function init() { |
|
20 | + add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 ); |
|
21 | + add_action( 'template_redirect', array( __CLASS__, 'do_wpinv_ajax' ), 0 ); |
|
22 | + self::add_ajax_events(); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /** |
26 | - * Set GetPaid AJAX constant and headers. |
|
27 | - */ |
|
28 | - public static function define_ajax() { |
|
29 | - |
|
30 | - if ( ! empty( $_GET['wpinv-ajax'] ) ) { |
|
31 | - getpaid_maybe_define_constant( 'DOING_AJAX', true ); |
|
32 | - getpaid_maybe_define_constant( 'WPInv_DOING_AJAX', true ); |
|
33 | - if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) { |
|
34 | - /** @scrutinizer ignore-unhandled */ @ini_set( 'display_errors', 0 ); |
|
35 | - } |
|
36 | - $GLOBALS['wpdb']->hide_errors(); |
|
37 | - } |
|
26 | + * Set GetPaid AJAX constant and headers. |
|
27 | + */ |
|
28 | + public static function define_ajax() { |
|
29 | + |
|
30 | + if ( ! empty( $_GET['wpinv-ajax'] ) ) { |
|
31 | + getpaid_maybe_define_constant( 'DOING_AJAX', true ); |
|
32 | + getpaid_maybe_define_constant( 'WPInv_DOING_AJAX', true ); |
|
33 | + if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) { |
|
34 | + /** @scrutinizer ignore-unhandled */ @ini_set( 'display_errors', 0 ); |
|
35 | + } |
|
36 | + $GLOBALS['wpdb']->hide_errors(); |
|
37 | + } |
|
38 | 38 | |
39 | 39 | } |
40 | 40 | |
41 | 41 | /** |
42 | - * Send headers for GetPaid Ajax Requests. |
|
43 | - * |
|
44 | - * @since 1.0.18 |
|
45 | - */ |
|
46 | - private static function wpinv_ajax_headers() { |
|
47 | - if ( ! headers_sent() ) { |
|
48 | - send_origin_headers(); |
|
49 | - send_nosniff_header(); |
|
50 | - nocache_headers(); |
|
51 | - header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
52 | - header( 'X-Robots-Tag: noindex' ); |
|
53 | - status_header( 200 ); |
|
54 | - } |
|
42 | + * Send headers for GetPaid Ajax Requests. |
|
43 | + * |
|
44 | + * @since 1.0.18 |
|
45 | + */ |
|
46 | + private static function wpinv_ajax_headers() { |
|
47 | + if ( ! headers_sent() ) { |
|
48 | + send_origin_headers(); |
|
49 | + send_nosniff_header(); |
|
50 | + nocache_headers(); |
|
51 | + header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
52 | + header( 'X-Robots-Tag: noindex' ); |
|
53 | + status_header( 200 ); |
|
54 | + } |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
58 | - * Check for GetPaid Ajax request and fire action. |
|
59 | - */ |
|
60 | - public static function do_wpinv_ajax() { |
|
61 | - global $wp_query; |
|
58 | + * Check for GetPaid Ajax request and fire action. |
|
59 | + */ |
|
60 | + public static function do_wpinv_ajax() { |
|
61 | + global $wp_query; |
|
62 | 62 | |
63 | - if ( ! empty( $_GET['wpinv-ajax'] ) ) { |
|
64 | - $wp_query->set( 'wpinv-ajax', sanitize_text_field( wp_unslash( $_GET['wpinv-ajax'] ) ) ); |
|
65 | - } |
|
63 | + if ( ! empty( $_GET['wpinv-ajax'] ) ) { |
|
64 | + $wp_query->set( 'wpinv-ajax', sanitize_text_field( wp_unslash( $_GET['wpinv-ajax'] ) ) ); |
|
65 | + } |
|
66 | 66 | |
67 | - $action = $wp_query->get( 'wpinv-ajax' ); |
|
67 | + $action = $wp_query->get( 'wpinv-ajax' ); |
|
68 | 68 | |
69 | - if ( $action ) { |
|
70 | - self::wpinv_ajax_headers(); |
|
71 | - $action = sanitize_text_field( $action ); |
|
72 | - do_action( 'wpinv_ajax_' . $action ); |
|
73 | - wp_die(); |
|
74 | - } |
|
69 | + if ( $action ) { |
|
70 | + self::wpinv_ajax_headers(); |
|
71 | + $action = sanitize_text_field( $action ); |
|
72 | + do_action( 'wpinv_ajax_' . $action ); |
|
73 | + wp_die(); |
|
74 | + } |
|
75 | 75 | |
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
79 | - * Hook in ajax methods. |
|
80 | - */ |
|
79 | + * Hook in ajax methods. |
|
80 | + */ |
|
81 | 81 | public static function add_ajax_events() { |
82 | 82 | |
83 | 83 | // array( 'event' => is_frontend ) |
@@ -260,18 +260,18 @@ discard block |
||
260 | 260 | check_ajax_referer( 'getpaid_form_nonce' ); |
261 | 261 | |
262 | 262 | // Is the request set up correctly? |
263 | - if ( empty( $_GET['form'] ) && empty( $_GET['item'] ) && empty( $_GET['invoice'] ) ) { |
|
264 | - echo aui()->alert( |
|
265 | - array( |
|
266 | - 'type' => 'warning', |
|
267 | - 'content' => __( 'No payment form or item provided', 'invoicing' ), |
|
268 | - ) |
|
263 | + if ( empty( $_GET['form'] ) && empty( $_GET['item'] ) && empty( $_GET['invoice'] ) ) { |
|
264 | + echo aui()->alert( |
|
265 | + array( |
|
266 | + 'type' => 'warning', |
|
267 | + 'content' => __( 'No payment form or item provided', 'invoicing' ), |
|
268 | + ) |
|
269 | 269 | ); |
270 | 270 | exit; |
271 | 271 | } |
272 | 272 | |
273 | 273 | // Payment form or button? |
274 | - if ( ! empty( $_GET['form'] ) ) { |
|
274 | + if ( ! empty( $_GET['form'] ) ) { |
|
275 | 275 | $form = urldecode( $_GET['form'] ); |
276 | 276 | |
277 | 277 | if ( false !== strpos( $form, '|' ) ) { |
@@ -325,11 +325,11 @@ discard block |
||
325 | 325 | getpaid_display_payment_form( $form ); |
326 | 326 | } |
327 | 327 | |
328 | - } else if( ! empty( $_GET['invoice'] ) ) { |
|
329 | - getpaid_display_invoice_payment_form( urldecode( $_GET['invoice'] ) ); |
|
328 | + } else if( ! empty( $_GET['invoice'] ) ) { |
|
329 | + getpaid_display_invoice_payment_form( urldecode( $_GET['invoice'] ) ); |
|
330 | 330 | } else { |
331 | - $items = getpaid_convert_items_to_array( urldecode( $_GET['item'] ) ); |
|
332 | - getpaid_display_item_payment_form( $items ); |
|
331 | + $items = getpaid_convert_items_to_array( urldecode( $_GET['item'] ) ); |
|
332 | + getpaid_display_item_payment_form( $items ); |
|
333 | 333 | } |
334 | 334 | |
335 | 335 | exit; |
@@ -650,7 +650,7 @@ discard block |
||
650 | 650 | if ( is_wp_error( $error ) ) { |
651 | 651 | $alert = $error->get_error_message(); |
652 | 652 | wp_send_json_success( compact( 'alert' ) ); |
653 | - } |
|
653 | + } |
|
654 | 654 | |
655 | 655 | // Update totals. |
656 | 656 | $invoice->recalculate_total(); |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
10 | - exit; // Exit if accessed directly |
|
10 | + exit; // Exit if accessed directly |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | /** |
@@ -15,22 +15,22 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Meta_Box_Invoice_Shipping_Address { |
17 | 17 | |
18 | - /** |
|
19 | - * Output the metabox. |
|
20 | - * |
|
21 | - * @param WP_Post $post |
|
22 | - */ |
|
23 | - public static function output( $post ) { |
|
18 | + /** |
|
19 | + * Output the metabox. |
|
20 | + * |
|
21 | + * @param WP_Post $post |
|
22 | + */ |
|
23 | + public static function output( $post ) { |
|
24 | 24 | |
25 | - // Retrieve shipping address. |
|
26 | - $shipping_address = get_post_meta( $post->ID, 'shipping_address', true ); |
|
25 | + // Retrieve shipping address. |
|
26 | + $shipping_address = get_post_meta( $post->ID, 'shipping_address', true ); |
|
27 | 27 | |
28 | - // Abort if it is invalid. |
|
29 | - if ( ! is_array( $shipping_address ) ) { |
|
30 | - return; |
|
31 | - } |
|
28 | + // Abort if it is invalid. |
|
29 | + if ( ! is_array( $shipping_address ) ) { |
|
30 | + return; |
|
31 | + } |
|
32 | 32 | |
33 | - ?> |
|
33 | + ?> |
|
34 | 34 | |
35 | 35 | <div class="bsui"> |
36 | 36 | |
@@ -55,31 +55,31 @@ discard block |
||
55 | 55 | |
56 | 56 | <?php |
57 | 57 | |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Prepares a value. |
|
62 | - * |
|
63 | - * @param array $address |
|
64 | - * @param string $key |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public static function prepare_for_display( $address, $key ) { |
|
60 | + /** |
|
61 | + * Prepares a value. |
|
62 | + * |
|
63 | + * @param array $address |
|
64 | + * @param string $key |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public static function prepare_for_display( $address, $key ) { |
|
68 | 68 | |
69 | - // Prepare the value. |
|
70 | - $value = $address[ $key ]; |
|
69 | + // Prepare the value. |
|
70 | + $value = $address[ $key ]; |
|
71 | 71 | |
72 | - if ( $key == 'country' ) { |
|
73 | - $value = wpinv_country_name( $value ); |
|
74 | - } |
|
72 | + if ( $key == 'country' ) { |
|
73 | + $value = wpinv_country_name( $value ); |
|
74 | + } |
|
75 | 75 | |
76 | - if ( $key == 'state' ) { |
|
77 | - $country = isset( $address[ 'country' ] ) ? $address[ 'country' ] : wpinv_get_default_country(); |
|
78 | - $value = wpinv_state_name( $value, $country ); |
|
79 | - } |
|
76 | + if ( $key == 'state' ) { |
|
77 | + $country = isset( $address[ 'country' ] ) ? $address[ 'country' ] : wpinv_get_default_country(); |
|
78 | + $value = wpinv_state_name( $value, $country ); |
|
79 | + } |
|
80 | 80 | |
81 | - return esc_html( $value ); |
|
81 | + return esc_html( $value ); |
|
82 | 82 | |
83 | - } |
|
83 | + } |
|
84 | 84 | |
85 | 85 | } |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | if ( ! defined( 'ABSPATH' ) ) exit; |
7 | 7 | |
8 | 8 | if ( ! class_exists( 'WP_List_Table' ) ) { |
9 | - include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
9 | + include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
10 | 10 | } |
11 | 11 | |
12 | 12 | /** |
@@ -14,406 +14,406 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class WPInv_Subscriptions_List_Table extends WP_List_Table { |
16 | 16 | |
17 | - /** |
|
18 | - * URL of this page |
|
19 | - * |
|
20 | - * @var string |
|
21 | - * @since 1.0.19 |
|
22 | - */ |
|
23 | - public $base_url; |
|
24 | - |
|
25 | - /** |
|
26 | - * Query |
|
27 | - * |
|
28 | - * @var GetPaid_Subscriptions_Query |
|
29 | - * @since 1.0.19 |
|
30 | - */ |
|
31 | - public $query; |
|
32 | - |
|
33 | - /** |
|
34 | - * Total subscriptions |
|
35 | - * |
|
36 | - * @var string |
|
37 | - * @since 1.0.0 |
|
38 | - */ |
|
39 | - public $total_count; |
|
40 | - |
|
41 | - /** |
|
42 | - * Current status subscriptions |
|
43 | - * |
|
44 | - * @var string |
|
45 | - * @since 1.0.0 |
|
46 | - */ |
|
47 | - public $current_total_count; |
|
48 | - |
|
49 | - /** |
|
50 | - * Status counts |
|
51 | - * |
|
52 | - * @var array |
|
53 | - * @since 1.0.19 |
|
54 | - */ |
|
55 | - public $status_counts; |
|
56 | - |
|
57 | - /** |
|
58 | - * Number of results to show per page |
|
59 | - * |
|
60 | - * @var int |
|
61 | - * @since 1.0.0 |
|
62 | - */ |
|
63 | - public $per_page = 10; |
|
64 | - |
|
65 | - /** |
|
66 | - * Constructor function. |
|
67 | - */ |
|
68 | - public function __construct() { |
|
69 | - |
|
70 | - parent::__construct( |
|
71 | - array( |
|
72 | - 'singular' => 'subscription', |
|
73 | - 'plural' => 'subscriptions', |
|
74 | - ) |
|
75 | - ); |
|
76 | - |
|
77 | - $this->process_bulk_action(); |
|
78 | - |
|
79 | - $this->prepare_query(); |
|
80 | - |
|
81 | - $this->base_url = remove_query_arg( 'status' ); |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Prepares the display query |
|
87 | - */ |
|
88 | - public function prepare_query() { |
|
89 | - |
|
90 | - // Prepare query args. |
|
91 | - $query = array( |
|
92 | - 'number' => $this->per_page, |
|
93 | - 'paged' => $this->get_paged(), |
|
94 | - 'status' => ( isset( $_GET['status'] ) && array_key_exists( $_GET['status'], getpaid_get_subscription_statuses() ) ) ? $_GET['status'] : 'all', |
|
95 | - 'orderby' => ( isset( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id', |
|
96 | - 'order' => ( isset( $_GET['order'] ) ) ? $_GET['order'] : 'DESC', |
|
97 | - ); |
|
98 | - |
|
99 | - // Prepare class properties. |
|
100 | - $this->query = new GetPaid_Subscriptions_Query( $query ); |
|
101 | - $this->total_count = $this->query->get_total(); |
|
102 | - $this->current_total_count = $this->query->get_total(); |
|
103 | - $this->items = $this->query->get_results(); |
|
104 | - $this->status_counts = getpaid_get_subscription_status_counts( $query ); |
|
105 | - |
|
106 | - if ( 'all' != $query['status'] ) { |
|
107 | - unset( $query['status'] ); |
|
108 | - $this->total_count = getpaid_get_subscriptions( $query, 'count' ); |
|
109 | - } |
|
110 | - |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Gets the list of views available on this table. |
|
115 | - * |
|
116 | - * The format is an associative array: |
|
117 | - * - `'id' => 'link'` |
|
118 | - * |
|
119 | - * @since 1.0.0 |
|
120 | - * |
|
121 | - * @return array |
|
122 | - */ |
|
123 | - public function get_views() { |
|
124 | - |
|
125 | - $current = isset( $_GET['status'] ) ? $_GET['status'] : 'all'; |
|
126 | - $views = array( |
|
127 | - |
|
128 | - 'all' => sprintf( |
|
129 | - '<a href="%s" %s>%s <span class="count">(%d)</span></a>', |
|
130 | - esc_url( add_query_arg( 'status', false, $this->base_url ) ), |
|
131 | - $current === 'all' ? ' class="current"' : '', |
|
132 | - __('All','invoicing' ), |
|
133 | - $this->total_count |
|
134 | - ) |
|
135 | - |
|
136 | - ); |
|
137 | - |
|
138 | - foreach ( array_filter( $this->status_counts ) as $status => $count ) { |
|
139 | - |
|
140 | - $views[ $status ] = sprintf( |
|
141 | - '<a href="%s" %s>%s <span class="count">(%d)</span></a>', |
|
142 | - esc_url( add_query_arg( 'status', urlencode( $status ), $this->base_url ) ), |
|
143 | - $current === $status ? ' class="current"' : '', |
|
144 | - esc_html( getpaid_get_subscription_status_label( $status ) ), |
|
145 | - $count |
|
146 | - ); |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - return $views; |
|
151 | - |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Render most columns |
|
156 | - * |
|
157 | - * @access private |
|
158 | - * @since 1.0.0 |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function column_default( $item, $column_name ) { |
|
162 | - return apply_filters( "getpaid_subscriptions_table_column_$column_name", $item->$column_name ); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * This is how checkbox column renders. |
|
167 | - * |
|
168 | - * @param WPInv_Subscription $item |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - public function column_cb( $item ) { |
|
172 | - return sprintf( '<input type="checkbox" name="id[]" value="%s" />', esc_html( $item->get_id() ) ); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Status column |
|
177 | - * |
|
178 | - * @param WPInv_Subscription $item |
|
179 | - * @since 1.0.0 |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - public function column_status( $item ) { |
|
183 | - return $item->get_status_label_html(); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Subscription column |
|
188 | - * |
|
189 | - * @param WPInv_Subscription $item |
|
190 | - * @since 1.0.0 |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - public function column_subscription( $item ) { |
|
194 | - |
|
195 | - $username = __( '(Missing User)', 'invoicing' ); |
|
196 | - |
|
197 | - $user = get_userdata( $item->get_customer_id() ); |
|
198 | - if ( $user ) { |
|
199 | - |
|
200 | - $username = sprintf( |
|
201 | - '<a href="user-edit.php?user_id=%s">%s</a>', |
|
202 | - absint( $user->ID ), |
|
203 | - ! empty( $user->display_name ) ? esc_html( $user->display_name ) : sanitize_email( $user->user_email ) |
|
204 | - ); |
|
205 | - |
|
206 | - } |
|
207 | - |
|
208 | - // translators: $1: is opening link, $2: is subscription id number, $3: is closing link tag, $4: is user's name |
|
209 | - $column_content = sprintf( |
|
210 | - _x( '%1$s#%2$s%3$s for %4$s', 'Subscription title on admin table. (e.g.: #211 for John Doe)', 'invoicing' ), |
|
211 | - '<a href="' . esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $item->get_id() ) ) ) . '">', |
|
212 | - '<strong>' . esc_attr( $item->get_id() ) . '</strong>', '</a>', |
|
213 | - $username |
|
214 | - ); |
|
215 | - |
|
216 | - $row_actions = array(); |
|
217 | - |
|
218 | - // View subscription. |
|
219 | - $view_url = esc_url( add_query_arg( 'id', $item->get_id(), admin_url( 'admin.php?page=wpinv-subscriptions' ) )); |
|
220 | - $row_actions['view'] = '<a href="' . $view_url . '">' . __( 'View Subscription', 'invoicing' ) . '</a>'; |
|
221 | - |
|
222 | - // View invoice. |
|
223 | - $invoice = get_post( $item->get_parent_invoice_id() ); |
|
224 | - |
|
225 | - if ( ! empty( $invoice ) ) { |
|
226 | - $invoice_url = get_edit_post_link( $invoice ); |
|
227 | - $row_actions['invoice'] = '<a href="' . $invoice_url . '">' . __( 'View Invoice', 'invoicing' ) . '</a>'; |
|
228 | - } |
|
229 | - |
|
230 | - $delete_url = esc_url( |
|
231 | - wp_nonce_url( |
|
232 | - add_query_arg( |
|
233 | - array( |
|
234 | - 'getpaid-admin-action' => 'subscription_manual_delete', |
|
235 | - 'id' => $item->get_id(), |
|
236 | - ) |
|
237 | - ), |
|
238 | - 'getpaid-nonce', |
|
239 | - 'getpaid-nonce' |
|
240 | - ) |
|
241 | - ); |
|
242 | - $row_actions['delete'] = '<a class="text-danger" href="' . $delete_url . '">' . __( 'Delete Subscription', 'invoicing' ) . '</a>'; |
|
243 | - |
|
244 | - $row_actions = $this->row_actions( apply_filters( 'getpaid_subscription_table_row_actions', $row_actions, $item ) ); |
|
245 | - |
|
246 | - return "<strong>$column_content</strong>" . $this->column_amount( $item ) . $row_actions; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Renewal date column |
|
251 | - * |
|
252 | - * @param WPInv_Subscription $item |
|
253 | - * @since 1.0.0 |
|
254 | - * @return string |
|
255 | - */ |
|
256 | - public function column_renewal_date( $item ) { |
|
257 | - return getpaid_format_date_value( $item->get_expiration() ); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Start date column |
|
262 | - * |
|
263 | - * @param WPInv_Subscription $item |
|
264 | - * @since 1.0.0 |
|
265 | - * @return string |
|
266 | - */ |
|
267 | - public function column_start_date( $item ) { |
|
268 | - return getpaid_format_date_value( $item->get_date_created() ); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Amount column |
|
273 | - * |
|
274 | - * @param WPInv_Subscription $item |
|
275 | - * @since 1.0.19 |
|
276 | - * @return string |
|
277 | - */ |
|
278 | - public static function column_amount( $item ) { |
|
279 | - $amount = getpaid_get_formatted_subscription_amount( $item ); |
|
280 | - return "<span class='text-muted form-text mt-2 mb-2'>$amount</span>"; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Billing Times column |
|
285 | - * |
|
286 | - * @param WPInv_Subscription $item |
|
287 | - * @since 1.0.0 |
|
288 | - * @return string |
|
289 | - */ |
|
290 | - public function column_renewals( $item ) { |
|
291 | - $max_bills = $item->get_bill_times(); |
|
292 | - return $item->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "∞" : $max_bills ); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Product ID column |
|
297 | - * |
|
298 | - * @param WPInv_Subscription $item |
|
299 | - * @since 1.0.0 |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - public function column_item( $item ) { |
|
303 | - $subscription_group = getpaid_get_invoice_subscription_group( $item->get_parent_invoice_id(), $item->get_id() ); |
|
304 | - |
|
305 | - if ( empty( $subscription_group ) ) { |
|
306 | - return $this->generate_item_markup( $item->get_product_id() ); |
|
307 | - } |
|
308 | - |
|
309 | - $markup = array_map( array( $this, 'generate_item_markup' ), array_keys( $subscription_group['items'] ) ); |
|
310 | - return implode( ' | ', $markup ); |
|
311 | - |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Generates the items markup. |
|
316 | - * |
|
317 | - * @param int $item_id |
|
318 | - * @since 1.0.0 |
|
319 | - * @return string |
|
320 | - */ |
|
321 | - public static function generate_item_markup( $item_id ) { |
|
322 | - $item = get_post( $item_id ); |
|
323 | - |
|
324 | - if ( ! empty( $item ) ) { |
|
325 | - $link = get_edit_post_link( $item ); |
|
326 | - $link = esc_url( $link ); |
|
327 | - $name = esc_html( get_the_title( $item ) ); |
|
328 | - return wpinv_current_user_can_manage_invoicing() ? "<a href='$link'>$name</a>" : $name; |
|
329 | - } else { |
|
330 | - return sprintf( __( 'Item #%s', 'invoicing' ), $item_id ); |
|
331 | - } |
|
332 | - |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Retrieve the current page number |
|
337 | - * |
|
338 | - * @return int |
|
339 | - */ |
|
340 | - public function get_paged() { |
|
341 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Setup the final data for the table |
|
346 | - * |
|
347 | - */ |
|
348 | - public function prepare_items() { |
|
349 | - |
|
350 | - $columns = $this->get_columns(); |
|
351 | - $hidden = array(); |
|
352 | - $sortable = $this->get_sortable_columns(); |
|
353 | - |
|
354 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
355 | - |
|
356 | - $this->set_pagination_args( |
|
357 | - array( |
|
358 | - 'total_items' => $this->current_total_count, |
|
359 | - 'per_page' => $this->per_page, |
|
360 | - 'total_pages' => ceil( $this->current_total_count / $this->per_page ) |
|
361 | - ) |
|
362 | - ); |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Table columns |
|
367 | - * |
|
368 | - * @return array |
|
369 | - */ |
|
370 | - public function get_columns(){ |
|
371 | - $columns = array( |
|
372 | - 'cb' => '<input type="checkbox" />', |
|
373 | - 'subscription' => __( 'Subscription', 'invoicing' ), |
|
374 | - 'start_date' => __( 'Start Date', 'invoicing' ), |
|
375 | - 'renewal_date' => __( 'Next Payment', 'invoicing' ), |
|
376 | - 'renewals' => __( 'Payments', 'invoicing' ), |
|
377 | - 'item' => __( 'Items', 'invoicing' ), |
|
378 | - 'status' => __( 'Status', 'invoicing' ), |
|
379 | - ); |
|
380 | - |
|
381 | - return apply_filters( 'manage_getpaid_subscriptions_table_columns', $columns ); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Sortable table columns. |
|
386 | - * |
|
387 | - * @return array |
|
388 | - */ |
|
389 | - public function get_sortable_columns() { |
|
390 | - $sortable = array( |
|
391 | - 'subscription' => array( 'id', true ), |
|
392 | - 'start_date' => array( 'created', true ), |
|
393 | - 'renewal_date' => array( 'expiration', true ), |
|
394 | - 'renewals' => array( 'bill_times', true ), |
|
395 | - 'item' => array( 'product_id', true ), |
|
396 | - 'status' => array( 'status', true ), |
|
397 | - ); |
|
398 | - |
|
399 | - return apply_filters( 'manage_getpaid_subscriptions_sortable_table_columns', $sortable ); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Whether the table has items to display or not |
|
404 | - * |
|
405 | - * @return bool |
|
406 | - */ |
|
407 | - public function has_items() { |
|
408 | - return ! empty( $this->current_total_count ); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Processes bulk actions. |
|
413 | - * |
|
414 | - */ |
|
415 | - public function process_bulk_action() { |
|
416 | - |
|
417 | - } |
|
17 | + /** |
|
18 | + * URL of this page |
|
19 | + * |
|
20 | + * @var string |
|
21 | + * @since 1.0.19 |
|
22 | + */ |
|
23 | + public $base_url; |
|
24 | + |
|
25 | + /** |
|
26 | + * Query |
|
27 | + * |
|
28 | + * @var GetPaid_Subscriptions_Query |
|
29 | + * @since 1.0.19 |
|
30 | + */ |
|
31 | + public $query; |
|
32 | + |
|
33 | + /** |
|
34 | + * Total subscriptions |
|
35 | + * |
|
36 | + * @var string |
|
37 | + * @since 1.0.0 |
|
38 | + */ |
|
39 | + public $total_count; |
|
40 | + |
|
41 | + /** |
|
42 | + * Current status subscriptions |
|
43 | + * |
|
44 | + * @var string |
|
45 | + * @since 1.0.0 |
|
46 | + */ |
|
47 | + public $current_total_count; |
|
48 | + |
|
49 | + /** |
|
50 | + * Status counts |
|
51 | + * |
|
52 | + * @var array |
|
53 | + * @since 1.0.19 |
|
54 | + */ |
|
55 | + public $status_counts; |
|
56 | + |
|
57 | + /** |
|
58 | + * Number of results to show per page |
|
59 | + * |
|
60 | + * @var int |
|
61 | + * @since 1.0.0 |
|
62 | + */ |
|
63 | + public $per_page = 10; |
|
64 | + |
|
65 | + /** |
|
66 | + * Constructor function. |
|
67 | + */ |
|
68 | + public function __construct() { |
|
69 | + |
|
70 | + parent::__construct( |
|
71 | + array( |
|
72 | + 'singular' => 'subscription', |
|
73 | + 'plural' => 'subscriptions', |
|
74 | + ) |
|
75 | + ); |
|
76 | + |
|
77 | + $this->process_bulk_action(); |
|
78 | + |
|
79 | + $this->prepare_query(); |
|
80 | + |
|
81 | + $this->base_url = remove_query_arg( 'status' ); |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Prepares the display query |
|
87 | + */ |
|
88 | + public function prepare_query() { |
|
89 | + |
|
90 | + // Prepare query args. |
|
91 | + $query = array( |
|
92 | + 'number' => $this->per_page, |
|
93 | + 'paged' => $this->get_paged(), |
|
94 | + 'status' => ( isset( $_GET['status'] ) && array_key_exists( $_GET['status'], getpaid_get_subscription_statuses() ) ) ? $_GET['status'] : 'all', |
|
95 | + 'orderby' => ( isset( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id', |
|
96 | + 'order' => ( isset( $_GET['order'] ) ) ? $_GET['order'] : 'DESC', |
|
97 | + ); |
|
98 | + |
|
99 | + // Prepare class properties. |
|
100 | + $this->query = new GetPaid_Subscriptions_Query( $query ); |
|
101 | + $this->total_count = $this->query->get_total(); |
|
102 | + $this->current_total_count = $this->query->get_total(); |
|
103 | + $this->items = $this->query->get_results(); |
|
104 | + $this->status_counts = getpaid_get_subscription_status_counts( $query ); |
|
105 | + |
|
106 | + if ( 'all' != $query['status'] ) { |
|
107 | + unset( $query['status'] ); |
|
108 | + $this->total_count = getpaid_get_subscriptions( $query, 'count' ); |
|
109 | + } |
|
110 | + |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Gets the list of views available on this table. |
|
115 | + * |
|
116 | + * The format is an associative array: |
|
117 | + * - `'id' => 'link'` |
|
118 | + * |
|
119 | + * @since 1.0.0 |
|
120 | + * |
|
121 | + * @return array |
|
122 | + */ |
|
123 | + public function get_views() { |
|
124 | + |
|
125 | + $current = isset( $_GET['status'] ) ? $_GET['status'] : 'all'; |
|
126 | + $views = array( |
|
127 | + |
|
128 | + 'all' => sprintf( |
|
129 | + '<a href="%s" %s>%s <span class="count">(%d)</span></a>', |
|
130 | + esc_url( add_query_arg( 'status', false, $this->base_url ) ), |
|
131 | + $current === 'all' ? ' class="current"' : '', |
|
132 | + __('All','invoicing' ), |
|
133 | + $this->total_count |
|
134 | + ) |
|
135 | + |
|
136 | + ); |
|
137 | + |
|
138 | + foreach ( array_filter( $this->status_counts ) as $status => $count ) { |
|
139 | + |
|
140 | + $views[ $status ] = sprintf( |
|
141 | + '<a href="%s" %s>%s <span class="count">(%d)</span></a>', |
|
142 | + esc_url( add_query_arg( 'status', urlencode( $status ), $this->base_url ) ), |
|
143 | + $current === $status ? ' class="current"' : '', |
|
144 | + esc_html( getpaid_get_subscription_status_label( $status ) ), |
|
145 | + $count |
|
146 | + ); |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + return $views; |
|
151 | + |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Render most columns |
|
156 | + * |
|
157 | + * @access private |
|
158 | + * @since 1.0.0 |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function column_default( $item, $column_name ) { |
|
162 | + return apply_filters( "getpaid_subscriptions_table_column_$column_name", $item->$column_name ); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * This is how checkbox column renders. |
|
167 | + * |
|
168 | + * @param WPInv_Subscription $item |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + public function column_cb( $item ) { |
|
172 | + return sprintf( '<input type="checkbox" name="id[]" value="%s" />', esc_html( $item->get_id() ) ); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Status column |
|
177 | + * |
|
178 | + * @param WPInv_Subscription $item |
|
179 | + * @since 1.0.0 |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + public function column_status( $item ) { |
|
183 | + return $item->get_status_label_html(); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Subscription column |
|
188 | + * |
|
189 | + * @param WPInv_Subscription $item |
|
190 | + * @since 1.0.0 |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + public function column_subscription( $item ) { |
|
194 | + |
|
195 | + $username = __( '(Missing User)', 'invoicing' ); |
|
196 | + |
|
197 | + $user = get_userdata( $item->get_customer_id() ); |
|
198 | + if ( $user ) { |
|
199 | + |
|
200 | + $username = sprintf( |
|
201 | + '<a href="user-edit.php?user_id=%s">%s</a>', |
|
202 | + absint( $user->ID ), |
|
203 | + ! empty( $user->display_name ) ? esc_html( $user->display_name ) : sanitize_email( $user->user_email ) |
|
204 | + ); |
|
205 | + |
|
206 | + } |
|
207 | + |
|
208 | + // translators: $1: is opening link, $2: is subscription id number, $3: is closing link tag, $4: is user's name |
|
209 | + $column_content = sprintf( |
|
210 | + _x( '%1$s#%2$s%3$s for %4$s', 'Subscription title on admin table. (e.g.: #211 for John Doe)', 'invoicing' ), |
|
211 | + '<a href="' . esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $item->get_id() ) ) ) . '">', |
|
212 | + '<strong>' . esc_attr( $item->get_id() ) . '</strong>', '</a>', |
|
213 | + $username |
|
214 | + ); |
|
215 | + |
|
216 | + $row_actions = array(); |
|
217 | + |
|
218 | + // View subscription. |
|
219 | + $view_url = esc_url( add_query_arg( 'id', $item->get_id(), admin_url( 'admin.php?page=wpinv-subscriptions' ) )); |
|
220 | + $row_actions['view'] = '<a href="' . $view_url . '">' . __( 'View Subscription', 'invoicing' ) . '</a>'; |
|
221 | + |
|
222 | + // View invoice. |
|
223 | + $invoice = get_post( $item->get_parent_invoice_id() ); |
|
224 | + |
|
225 | + if ( ! empty( $invoice ) ) { |
|
226 | + $invoice_url = get_edit_post_link( $invoice ); |
|
227 | + $row_actions['invoice'] = '<a href="' . $invoice_url . '">' . __( 'View Invoice', 'invoicing' ) . '</a>'; |
|
228 | + } |
|
229 | + |
|
230 | + $delete_url = esc_url( |
|
231 | + wp_nonce_url( |
|
232 | + add_query_arg( |
|
233 | + array( |
|
234 | + 'getpaid-admin-action' => 'subscription_manual_delete', |
|
235 | + 'id' => $item->get_id(), |
|
236 | + ) |
|
237 | + ), |
|
238 | + 'getpaid-nonce', |
|
239 | + 'getpaid-nonce' |
|
240 | + ) |
|
241 | + ); |
|
242 | + $row_actions['delete'] = '<a class="text-danger" href="' . $delete_url . '">' . __( 'Delete Subscription', 'invoicing' ) . '</a>'; |
|
243 | + |
|
244 | + $row_actions = $this->row_actions( apply_filters( 'getpaid_subscription_table_row_actions', $row_actions, $item ) ); |
|
245 | + |
|
246 | + return "<strong>$column_content</strong>" . $this->column_amount( $item ) . $row_actions; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Renewal date column |
|
251 | + * |
|
252 | + * @param WPInv_Subscription $item |
|
253 | + * @since 1.0.0 |
|
254 | + * @return string |
|
255 | + */ |
|
256 | + public function column_renewal_date( $item ) { |
|
257 | + return getpaid_format_date_value( $item->get_expiration() ); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Start date column |
|
262 | + * |
|
263 | + * @param WPInv_Subscription $item |
|
264 | + * @since 1.0.0 |
|
265 | + * @return string |
|
266 | + */ |
|
267 | + public function column_start_date( $item ) { |
|
268 | + return getpaid_format_date_value( $item->get_date_created() ); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Amount column |
|
273 | + * |
|
274 | + * @param WPInv_Subscription $item |
|
275 | + * @since 1.0.19 |
|
276 | + * @return string |
|
277 | + */ |
|
278 | + public static function column_amount( $item ) { |
|
279 | + $amount = getpaid_get_formatted_subscription_amount( $item ); |
|
280 | + return "<span class='text-muted form-text mt-2 mb-2'>$amount</span>"; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Billing Times column |
|
285 | + * |
|
286 | + * @param WPInv_Subscription $item |
|
287 | + * @since 1.0.0 |
|
288 | + * @return string |
|
289 | + */ |
|
290 | + public function column_renewals( $item ) { |
|
291 | + $max_bills = $item->get_bill_times(); |
|
292 | + return $item->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "∞" : $max_bills ); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Product ID column |
|
297 | + * |
|
298 | + * @param WPInv_Subscription $item |
|
299 | + * @since 1.0.0 |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + public function column_item( $item ) { |
|
303 | + $subscription_group = getpaid_get_invoice_subscription_group( $item->get_parent_invoice_id(), $item->get_id() ); |
|
304 | + |
|
305 | + if ( empty( $subscription_group ) ) { |
|
306 | + return $this->generate_item_markup( $item->get_product_id() ); |
|
307 | + } |
|
308 | + |
|
309 | + $markup = array_map( array( $this, 'generate_item_markup' ), array_keys( $subscription_group['items'] ) ); |
|
310 | + return implode( ' | ', $markup ); |
|
311 | + |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Generates the items markup. |
|
316 | + * |
|
317 | + * @param int $item_id |
|
318 | + * @since 1.0.0 |
|
319 | + * @return string |
|
320 | + */ |
|
321 | + public static function generate_item_markup( $item_id ) { |
|
322 | + $item = get_post( $item_id ); |
|
323 | + |
|
324 | + if ( ! empty( $item ) ) { |
|
325 | + $link = get_edit_post_link( $item ); |
|
326 | + $link = esc_url( $link ); |
|
327 | + $name = esc_html( get_the_title( $item ) ); |
|
328 | + return wpinv_current_user_can_manage_invoicing() ? "<a href='$link'>$name</a>" : $name; |
|
329 | + } else { |
|
330 | + return sprintf( __( 'Item #%s', 'invoicing' ), $item_id ); |
|
331 | + } |
|
332 | + |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Retrieve the current page number |
|
337 | + * |
|
338 | + * @return int |
|
339 | + */ |
|
340 | + public function get_paged() { |
|
341 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Setup the final data for the table |
|
346 | + * |
|
347 | + */ |
|
348 | + public function prepare_items() { |
|
349 | + |
|
350 | + $columns = $this->get_columns(); |
|
351 | + $hidden = array(); |
|
352 | + $sortable = $this->get_sortable_columns(); |
|
353 | + |
|
354 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
355 | + |
|
356 | + $this->set_pagination_args( |
|
357 | + array( |
|
358 | + 'total_items' => $this->current_total_count, |
|
359 | + 'per_page' => $this->per_page, |
|
360 | + 'total_pages' => ceil( $this->current_total_count / $this->per_page ) |
|
361 | + ) |
|
362 | + ); |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Table columns |
|
367 | + * |
|
368 | + * @return array |
|
369 | + */ |
|
370 | + public function get_columns(){ |
|
371 | + $columns = array( |
|
372 | + 'cb' => '<input type="checkbox" />', |
|
373 | + 'subscription' => __( 'Subscription', 'invoicing' ), |
|
374 | + 'start_date' => __( 'Start Date', 'invoicing' ), |
|
375 | + 'renewal_date' => __( 'Next Payment', 'invoicing' ), |
|
376 | + 'renewals' => __( 'Payments', 'invoicing' ), |
|
377 | + 'item' => __( 'Items', 'invoicing' ), |
|
378 | + 'status' => __( 'Status', 'invoicing' ), |
|
379 | + ); |
|
380 | + |
|
381 | + return apply_filters( 'manage_getpaid_subscriptions_table_columns', $columns ); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Sortable table columns. |
|
386 | + * |
|
387 | + * @return array |
|
388 | + */ |
|
389 | + public function get_sortable_columns() { |
|
390 | + $sortable = array( |
|
391 | + 'subscription' => array( 'id', true ), |
|
392 | + 'start_date' => array( 'created', true ), |
|
393 | + 'renewal_date' => array( 'expiration', true ), |
|
394 | + 'renewals' => array( 'bill_times', true ), |
|
395 | + 'item' => array( 'product_id', true ), |
|
396 | + 'status' => array( 'status', true ), |
|
397 | + ); |
|
398 | + |
|
399 | + return apply_filters( 'manage_getpaid_subscriptions_sortable_table_columns', $sortable ); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Whether the table has items to display or not |
|
404 | + * |
|
405 | + * @return bool |
|
406 | + */ |
|
407 | + public function has_items() { |
|
408 | + return ! empty( $this->current_total_count ); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Processes bulk actions. |
|
413 | + * |
|
414 | + */ |
|
415 | + public function process_bulk_action() { |
|
416 | + |
|
417 | + } |
|
418 | 418 | |
419 | 419 | } |