@@ -11,58 +11,58 @@ discard block |
||
11 | 11 | * @var string $country The current user's country |
12 | 12 | */ |
13 | 13 | |
14 | -defined( 'ABSPATH' ) || exit; |
|
14 | +defined('ABSPATH') || exit; |
|
15 | 15 | |
16 | 16 | |
17 | -$field_type = sanitize_key( $field_type ); |
|
17 | +$field_type = sanitize_key($field_type); |
|
18 | 18 | |
19 | 19 | echo "<div class='row $field_type'>"; |
20 | 20 | |
21 | -foreach ( $fields as $address_field ) { |
|
21 | +foreach ($fields as $address_field) { |
|
22 | 22 | |
23 | 23 | // Skip if it is hidden. |
24 | - if ( empty( $address_field['visible'] ) ) { |
|
24 | + if (empty($address_field['visible'])) { |
|
25 | 25 | continue; |
26 | 26 | } |
27 | 27 | |
28 | - do_action( 'getpaid_payment_form_address_field_before_' . $address_field['name'], $field_type, $address_field ); |
|
28 | + do_action('getpaid_payment_form_address_field_before_' . $address_field['name'], $field_type, $address_field); |
|
29 | 29 | |
30 | 30 | // Prepare variables. |
31 | - if ( ! empty( $form->invoice ) ) { |
|
31 | + if (!empty($form->invoice)) { |
|
32 | 32 | $user_id = $form->invoice->get_user_id(); |
33 | 33 | } |
34 | 34 | |
35 | - if ( empty( $user_id ) && is_user_logged_in() ) { |
|
35 | + if (empty($user_id) && is_user_logged_in()) { |
|
36 | 36 | $user_id = get_current_user_id(); |
37 | 37 | } |
38 | 38 | |
39 | 39 | $field_name = $address_field['name']; |
40 | 40 | $field_name = "{$field_type}[$field_name]"; |
41 | - $wrap_class = getpaid_get_form_element_grid_class( $address_field ); |
|
42 | - $wrap_class = esc_attr( "$wrap_class getpaid-address-field-wrapper" ); |
|
43 | - $placeholder = empty( $address_field['placeholder'] ) ? '' : esc_attr( $address_field['placeholder'] ); |
|
44 | - $description = empty( $address_field['description'] ) ? '' : wp_kses_post( $address_field['description'] ); |
|
45 | - $value = ! empty( $user_id ) ? get_user_meta( $user_id, '_' . $address_field['name'], true ) : ''; |
|
46 | - $label = empty( $address_field['label'] ) ? '' : wp_kses_post( $address_field['label'] ); |
|
47 | - |
|
48 | - if ( ! empty( $address_field['required'] ) ) { |
|
41 | + $wrap_class = getpaid_get_form_element_grid_class($address_field); |
|
42 | + $wrap_class = esc_attr("$wrap_class getpaid-address-field-wrapper"); |
|
43 | + $placeholder = empty($address_field['placeholder']) ? '' : esc_attr($address_field['placeholder']); |
|
44 | + $description = empty($address_field['description']) ? '' : wp_kses_post($address_field['description']); |
|
45 | + $value = !empty($user_id) ? get_user_meta($user_id, '_' . $address_field['name'], true) : ''; |
|
46 | + $label = empty($address_field['label']) ? '' : wp_kses_post($address_field['label']); |
|
47 | + |
|
48 | + if (!empty($address_field['required'])) { |
|
49 | 49 | $label .= "<span class='text-danger'> *</span>"; |
50 | 50 | } |
51 | 51 | |
52 | 52 | // Display the country. |
53 | - if ( 'wpinv_country' == $address_field['name'] ) { |
|
53 | + if ('wpinv_country' == $address_field['name']) { |
|
54 | 54 | |
55 | 55 | echo "<div class='form-group $wrap_class getpaid-address-field-wrapper__country'"; |
56 | 56 | |
57 | 57 | echo aui()->select( |
58 | 58 | array( |
59 | 59 | 'options' => wpinv_get_country_list(), |
60 | - 'name' => esc_attr( $field_name ), |
|
61 | - 'id' => sanitize_html_class( $field_name ) . $uniqid, |
|
62 | - 'value' => sanitize_text_field( $country ), |
|
60 | + 'name' => esc_attr($field_name), |
|
61 | + 'id' => sanitize_html_class($field_name) . $uniqid, |
|
62 | + 'value' => sanitize_text_field($country), |
|
63 | 63 | 'placeholder' => $placeholder, |
64 | - 'required' => ! empty( $address_field['required'] ), |
|
65 | - 'label' => wp_kses_post( $label ), |
|
64 | + 'required' => !empty($address_field['required']), |
|
65 | + 'label' => wp_kses_post($label), |
|
66 | 66 | 'label_type' => 'vertical', |
67 | 67 | 'help_text' => $description, |
68 | 68 | 'class' => 'getpaid-address-field wpinv_country', |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | ) |
76 | 76 | ); |
77 | 77 | |
78 | - if ( wpinv_should_validate_vat_number() ) { |
|
78 | + if (wpinv_should_validate_vat_number()) { |
|
79 | 79 | |
80 | 80 | echo aui()->input( |
81 | 81 | array( |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | 'id' => "shipping-toggle$uniqid", |
85 | 85 | 'wrap_class' => "getpaid-address-field-wrapper__address-confirm mt-1 d-none", |
86 | 86 | 'required' => false, |
87 | - 'label' => __( 'I certify that I live in the country selected above', 'invoicing' ) . "<span class='text-danger'> *</span>", |
|
87 | + 'label' => __('I certify that I live in the country selected above', 'invoicing') . "<span class='text-danger'> *</span>", |
|
88 | 88 | 'value' => 1, |
89 | 89 | 'checked' => true, |
90 | 90 | 'class' => 'w-auto', |
@@ -98,27 +98,27 @@ discard block |
||
98 | 98 | } |
99 | 99 | |
100 | 100 | // Display the state. |
101 | - else if ( 'wpinv_state' == $address_field['name'] ) { |
|
101 | + else if ('wpinv_state' == $address_field['name']) { |
|
102 | 102 | |
103 | - if ( empty( $value ) ) { |
|
103 | + if (empty($value)) { |
|
104 | 104 | $value = wpinv_get_default_state(); |
105 | 105 | } |
106 | 106 | |
107 | - echo getpaid_get_states_select_markup ( |
|
107 | + echo getpaid_get_states_select_markup( |
|
108 | 108 | $country, |
109 | 109 | $value, |
110 | 110 | $placeholder, |
111 | 111 | $label, |
112 | 112 | $description, |
113 | - ! empty( $address_field['required'] ), |
|
113 | + !empty($address_field['required']), |
|
114 | 114 | $wrap_class, |
115 | 115 | $field_name |
116 | 116 | ); |
117 | 117 | |
118 | 118 | } else { |
119 | 119 | |
120 | - $key = str_replace( 'wpinv_', '', $address_field['name'] ); |
|
121 | - $key = esc_attr( str_replace( '_', '-', $key ) ); |
|
120 | + $key = str_replace('wpinv_', '', $address_field['name']); |
|
121 | + $key = esc_attr(str_replace('_', '-', $key)); |
|
122 | 122 | $autocomplete = ''; |
123 | 123 | $replacements = array( |
124 | 124 | 'zip' => 'postal-code', |
@@ -131,37 +131,37 @@ discard block |
||
131 | 131 | ); |
132 | 132 | |
133 | 133 | |
134 | - if ( isset( $replacements[ $key ] ) ) { |
|
134 | + if (isset($replacements[$key])) { |
|
135 | 135 | $autocomplete = array( |
136 | - 'autocomplete' => "$field_type {$replacements[ $key ]}", |
|
136 | + 'autocomplete' => "$field_type {$replacements[$key]}", |
|
137 | 137 | ); |
138 | 138 | } |
139 | 139 | |
140 | 140 | $append = ''; |
141 | 141 | |
142 | - if ( 'billing' === $field_type && wpinv_should_validate_vat_number() && 'vat-number' === $key ) { |
|
143 | - $valid = esc_attr__( 'Valid', 'invoicing' ); |
|
144 | - $invalid = esc_attr__( 'Invalid', 'invoicing' ); |
|
145 | - $validate = esc_attr__( 'Validate', 'invoicing' ); |
|
142 | + if ('billing' === $field_type && wpinv_should_validate_vat_number() && 'vat-number' === $key) { |
|
143 | + $valid = esc_attr__('Valid', 'invoicing'); |
|
144 | + $invalid = esc_attr__('Invalid', 'invoicing'); |
|
145 | + $validate = esc_attr__('Validate', 'invoicing'); |
|
146 | 146 | $append = "<span class='btn btn-primary getpaid-vat-number-validate' data-valid='$valid' data-invalid='$invalid' data-validate='$validate'>$validate</span>"; |
147 | 147 | } |
148 | 148 | |
149 | - if ( 'billing' === $field_type ) { |
|
150 | - $description .= '<div class="getpaid-error-' . sanitize_html_class( $field_name ) . ' getpaid-custom-payment-form-errors alert alert-danger d-none"></div>'; |
|
149 | + if ('billing' === $field_type) { |
|
150 | + $description .= '<div class="getpaid-error-' . sanitize_html_class($field_name) . ' getpaid-custom-payment-form-errors alert alert-danger d-none"></div>'; |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | echo aui()->input( |
154 | 154 | array( |
155 | - 'name' => esc_attr( $field_name ), |
|
156 | - 'id' => sanitize_html_class( $field_name ) . $uniqid, |
|
157 | - 'required' => ! empty( $address_field['required'] ), |
|
155 | + 'name' => esc_attr($field_name), |
|
156 | + 'id' => sanitize_html_class($field_name) . $uniqid, |
|
157 | + 'required' => !empty($address_field['required']), |
|
158 | 158 | 'placeholder' => $placeholder, |
159 | - 'label' => wp_kses_post( $label ), |
|
159 | + 'label' => wp_kses_post($label), |
|
160 | 160 | 'label_type' => 'vertical', |
161 | 161 | 'help_text' => $description, |
162 | 162 | 'type' => 'text', |
163 | - 'value' => sanitize_text_field( $value ), |
|
164 | - 'class' => 'getpaid-address-field ' . esc_attr( $address_field['name'] ), |
|
163 | + 'value' => sanitize_text_field($value), |
|
164 | + 'class' => 'getpaid-address-field ' . esc_attr($address_field['name']), |
|
165 | 165 | 'wrap_class' => "$wrap_class getpaid-address-field-wrapper__$key", |
166 | 166 | 'label_class' => 'getpaid-address-field-label getpaid-address-field-label__' . $key, |
167 | 167 | 'extra_attributes' => $autocomplete, |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | |
172 | 172 | } |
173 | 173 | |
174 | - do_action( 'getpaid_payment_form_address_field_after_' . $address_field['name'], $field_type, $address_field ); |
|
174 | + do_action('getpaid_payment_form_address_field_after_' . $address_field['name'], $field_type, $address_field); |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | echo "</div>"; |
@@ -12,228 +12,228 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Payment_Form_Submission_Taxes { |
14 | 14 | |
15 | - /** |
|
16 | - * Submission taxes. |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $taxes = array(); |
|
20 | - |
|
21 | - /** |
|
22 | - * Whether or not we should skip the taxes. |
|
23 | - * @var bool |
|
24 | - */ |
|
25 | - protected $skip_taxes = false; |
|
15 | + /** |
|
16 | + * Submission taxes. |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $taxes = array(); |
|
20 | + |
|
21 | + /** |
|
22 | + * Whether or not we should skip the taxes. |
|
23 | + * @var bool |
|
24 | + */ |
|
25 | + protected $skip_taxes = false; |
|
26 | 26 | |
27 | 27 | /** |
28 | - * Class constructor |
|
29 | - * |
|
30 | - * @param GetPaid_Payment_Form_Submission $submission |
|
31 | - */ |
|
32 | - public function __construct( $submission ) { |
|
33 | - |
|
34 | - // Validate VAT number. |
|
35 | - $this->validate_vat( $submission ); |
|
36 | - |
|
37 | - if ( $this->skip_taxes ) { |
|
38 | - return; |
|
39 | - } |
|
40 | - |
|
41 | - foreach ( $submission->get_items() as $item ) { |
|
42 | - $this->process_item_tax( $item, $submission ); |
|
43 | - } |
|
44 | - |
|
45 | - // Process any existing invoice taxes. |
|
46 | - if ( $submission->has_invoice() ) { |
|
47 | - $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
48 | - } |
|
49 | - |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Maybe process tax. |
|
54 | - * |
|
55 | - * @since 1.0.19 |
|
56 | - * @param GetPaid_Form_Item $item |
|
57 | - * @param GetPaid_Payment_Form_Submission $submission |
|
58 | - */ |
|
59 | - public function process_item_tax( $item, $submission ) { |
|
60 | - |
|
61 | - $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
62 | - $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
63 | - $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
64 | - $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
65 | - |
|
66 | - foreach ( $taxes as $name => $amount ) { |
|
67 | - $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
68 | - $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
69 | - |
|
70 | - $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
71 | - |
|
72 | - if ( ! isset( $this->taxes[ $name ] ) ) { |
|
73 | - $this->taxes[ $name ] = $tax; |
|
74 | - continue; |
|
75 | - } |
|
76 | - |
|
77 | - $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
78 | - $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
79 | - |
|
80 | - } |
|
81 | - |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Checks if the submission has a digital item. |
|
86 | - * |
|
87 | - * @param GetPaid_Payment_Form_Submission $submission |
|
88 | - * @since 1.0.19 |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - public function has_digital_item( $submission ) { |
|
92 | - |
|
93 | - foreach ( $submission->get_items() as $item ) { |
|
94 | - |
|
95 | - if ( 'digital' == $item->get_vat_rule() ) { |
|
96 | - return true; |
|
97 | - } |
|
98 | - |
|
99 | - } |
|
100 | - |
|
101 | - return false; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Checks if this is an eu store. |
|
106 | - * |
|
107 | - * @since 1.0.19 |
|
108 | - * @return bool |
|
109 | - */ |
|
110 | - public static function is_eu_store() { |
|
111 | - return self::is_eu_country( wpinv_get_default_country() ); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Checks if this is an eu country. |
|
116 | - * |
|
117 | - * @param string $country |
|
118 | - * @since 1.0.19 |
|
119 | - * @return bool |
|
120 | - */ |
|
121 | - public static function is_eu_country( $country ) { |
|
122 | - return getpaid_is_eu_state( $country ); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Checks if this is an eu purchase. |
|
127 | - * |
|
128 | - * @param string $customer_country |
|
129 | - * @since 1.0.19 |
|
130 | - * @return bool |
|
131 | - */ |
|
132 | - public static function is_eu_transaction( $customer_country ) { |
|
133 | - return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Retrieves the vat number. |
|
138 | - * |
|
139 | - * @param GetPaid_Payment_Form_Submission $submission |
|
140 | - * @since 1.0.19 |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - public function get_vat_number( $submission ) { |
|
144 | - |
|
145 | - // Retrieve from the posted number. |
|
146 | - $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
147 | - if ( ! is_null( $vat_number ) ) { |
|
148 | - return wpinv_clean( $vat_number ); |
|
149 | - } |
|
150 | - |
|
151 | - return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Retrieves the company. |
|
156 | - * |
|
157 | - * @param GetPaid_Payment_Form_Submission $submission |
|
158 | - * @since 1.0.19 |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function get_company( $submission ) { |
|
162 | - |
|
163 | - // Retrieve from the posted data. |
|
164 | - $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
165 | - if ( ! empty( $company ) ) { |
|
166 | - return wpinv_clean( $company ); |
|
167 | - } |
|
168 | - |
|
169 | - // Retrieve from the invoice. |
|
170 | - return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Checks if we require a VAT number. |
|
175 | - * |
|
176 | - * @param bool $ip_in_eu Whether the customer IP is from the EU |
|
177 | - * @param bool $country_in_eu Whether the customer country is from the EU |
|
178 | - * @since 1.0.19 |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
182 | - |
|
183 | - $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
184 | - $prevent_b2c = ! empty( $prevent_b2c ); |
|
185 | - $is_eu = $ip_in_eu || $country_in_eu; |
|
186 | - |
|
187 | - return $prevent_b2c && $is_eu; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Validate VAT data. |
|
192 | - * |
|
193 | - * @param GetPaid_Payment_Form_Submission $submission |
|
194 | - * @since 1.0.19 |
|
195 | - */ |
|
196 | - public function validate_vat( $submission ) { |
|
197 | - |
|
198 | - $in_eu = $this->is_eu_transaction( $submission->country ); |
|
199 | - |
|
200 | - // Abort if we are not validating vat numbers. |
|
201 | - if ( ! $in_eu ) { |
|
28 | + * Class constructor |
|
29 | + * |
|
30 | + * @param GetPaid_Payment_Form_Submission $submission |
|
31 | + */ |
|
32 | + public function __construct( $submission ) { |
|
33 | + |
|
34 | + // Validate VAT number. |
|
35 | + $this->validate_vat( $submission ); |
|
36 | + |
|
37 | + if ( $this->skip_taxes ) { |
|
202 | 38 | return; |
203 | - } |
|
39 | + } |
|
40 | + |
|
41 | + foreach ( $submission->get_items() as $item ) { |
|
42 | + $this->process_item_tax( $item, $submission ); |
|
43 | + } |
|
44 | + |
|
45 | + // Process any existing invoice taxes. |
|
46 | + if ( $submission->has_invoice() ) { |
|
47 | + $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
48 | + } |
|
49 | + |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Maybe process tax. |
|
54 | + * |
|
55 | + * @since 1.0.19 |
|
56 | + * @param GetPaid_Form_Item $item |
|
57 | + * @param GetPaid_Payment_Form_Submission $submission |
|
58 | + */ |
|
59 | + public function process_item_tax( $item, $submission ) { |
|
60 | + |
|
61 | + $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
62 | + $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
63 | + $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
64 | + $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
65 | + |
|
66 | + foreach ( $taxes as $name => $amount ) { |
|
67 | + $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
68 | + $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
69 | + |
|
70 | + $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
71 | + |
|
72 | + if ( ! isset( $this->taxes[ $name ] ) ) { |
|
73 | + $this->taxes[ $name ] = $tax; |
|
74 | + continue; |
|
75 | + } |
|
76 | + |
|
77 | + $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
78 | + $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
79 | + |
|
80 | + } |
|
81 | + |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Checks if the submission has a digital item. |
|
86 | + * |
|
87 | + * @param GetPaid_Payment_Form_Submission $submission |
|
88 | + * @since 1.0.19 |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + public function has_digital_item( $submission ) { |
|
92 | + |
|
93 | + foreach ( $submission->get_items() as $item ) { |
|
94 | + |
|
95 | + if ( 'digital' == $item->get_vat_rule() ) { |
|
96 | + return true; |
|
97 | + } |
|
98 | + |
|
99 | + } |
|
204 | 100 | |
205 | - // Prepare variables. |
|
206 | - $vat_number = $this->get_vat_number( $submission ); |
|
207 | - $ip_country = getpaid_get_ip_country(); |
|
101 | + return false; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Checks if this is an eu store. |
|
106 | + * |
|
107 | + * @since 1.0.19 |
|
108 | + * @return bool |
|
109 | + */ |
|
110 | + public static function is_eu_store() { |
|
111 | + return self::is_eu_country( wpinv_get_default_country() ); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Checks if this is an eu country. |
|
116 | + * |
|
117 | + * @param string $country |
|
118 | + * @since 1.0.19 |
|
119 | + * @return bool |
|
120 | + */ |
|
121 | + public static function is_eu_country( $country ) { |
|
122 | + return getpaid_is_eu_state( $country ); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Checks if this is an eu purchase. |
|
127 | + * |
|
128 | + * @param string $customer_country |
|
129 | + * @since 1.0.19 |
|
130 | + * @return bool |
|
131 | + */ |
|
132 | + public static function is_eu_transaction( $customer_country ) { |
|
133 | + return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Retrieves the vat number. |
|
138 | + * |
|
139 | + * @param GetPaid_Payment_Form_Submission $submission |
|
140 | + * @since 1.0.19 |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + public function get_vat_number( $submission ) { |
|
144 | + |
|
145 | + // Retrieve from the posted number. |
|
146 | + $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
147 | + if ( ! is_null( $vat_number ) ) { |
|
148 | + return wpinv_clean( $vat_number ); |
|
149 | + } |
|
150 | + |
|
151 | + return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Retrieves the company. |
|
156 | + * |
|
157 | + * @param GetPaid_Payment_Form_Submission $submission |
|
158 | + * @since 1.0.19 |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function get_company( $submission ) { |
|
162 | + |
|
163 | + // Retrieve from the posted data. |
|
164 | + $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
165 | + if ( ! empty( $company ) ) { |
|
166 | + return wpinv_clean( $company ); |
|
167 | + } |
|
168 | + |
|
169 | + // Retrieve from the invoice. |
|
170 | + return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Checks if we require a VAT number. |
|
175 | + * |
|
176 | + * @param bool $ip_in_eu Whether the customer IP is from the EU |
|
177 | + * @param bool $country_in_eu Whether the customer country is from the EU |
|
178 | + * @since 1.0.19 |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
182 | + |
|
183 | + $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
184 | + $prevent_b2c = ! empty( $prevent_b2c ); |
|
185 | + $is_eu = $ip_in_eu || $country_in_eu; |
|
186 | + |
|
187 | + return $prevent_b2c && $is_eu; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Validate VAT data. |
|
192 | + * |
|
193 | + * @param GetPaid_Payment_Form_Submission $submission |
|
194 | + * @since 1.0.19 |
|
195 | + */ |
|
196 | + public function validate_vat( $submission ) { |
|
197 | + |
|
198 | + $in_eu = $this->is_eu_transaction( $submission->country ); |
|
199 | + |
|
200 | + // Abort if we are not validating vat numbers. |
|
201 | + if ( ! $in_eu ) { |
|
202 | + return; |
|
203 | + } |
|
204 | + |
|
205 | + // Prepare variables. |
|
206 | + $vat_number = $this->get_vat_number( $submission ); |
|
207 | + $ip_country = getpaid_get_ip_country(); |
|
208 | 208 | $is_eu = $this->is_eu_country( $submission->country ); |
209 | 209 | $is_ip_eu = $this->is_eu_country( $ip_country ); |
210 | 210 | |
211 | - // Maybe abort early for initial fetches. |
|
212 | - if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
213 | - return; |
|
214 | - } |
|
211 | + // Maybe abort early for initial fetches. |
|
212 | + if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
213 | + return; |
|
214 | + } |
|
215 | 215 | |
216 | - // If we're preventing business to consumer purchases, |
|
217 | - if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
216 | + // If we're preventing business to consumer purchases, |
|
217 | + if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
218 | 218 | |
219 | - // Ensure that a vat number has been specified. |
|
220 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) ); |
|
219 | + // Ensure that a vat number has been specified. |
|
220 | + throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) ); |
|
221 | 221 | |
222 | - } |
|
222 | + } |
|
223 | 223 | |
224 | - if ( empty( $vat_number ) ) { |
|
225 | - return; |
|
226 | - } |
|
224 | + if ( empty( $vat_number ) ) { |
|
225 | + return; |
|
226 | + } |
|
227 | 227 | |
228 | - if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
229 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
230 | - } |
|
228 | + if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
229 | + throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
230 | + } |
|
231 | 231 | |
232 | - if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
233 | - return; |
|
234 | - } |
|
232 | + if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
233 | + return; |
|
234 | + } |
|
235 | 235 | |
236 | - $this->skip_taxes = true; |
|
237 | - } |
|
236 | + $this->skip_taxes = true; |
|
237 | + } |
|
238 | 238 | |
239 | 239 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Payment form submission taxes class |
@@ -29,22 +29,22 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @param GetPaid_Payment_Form_Submission $submission |
31 | 31 | */ |
32 | - public function __construct( $submission ) { |
|
32 | + public function __construct($submission) { |
|
33 | 33 | |
34 | 34 | // Validate VAT number. |
35 | - $this->validate_vat( $submission ); |
|
35 | + $this->validate_vat($submission); |
|
36 | 36 | |
37 | - if ( $this->skip_taxes ) { |
|
37 | + if ($this->skip_taxes) { |
|
38 | 38 | return; |
39 | 39 | } |
40 | 40 | |
41 | - foreach ( $submission->get_items() as $item ) { |
|
42 | - $this->process_item_tax( $item, $submission ); |
|
41 | + foreach ($submission->get_items() as $item) { |
|
42 | + $this->process_item_tax($item, $submission); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | // Process any existing invoice taxes. |
46 | - if ( $submission->has_invoice() ) { |
|
47 | - $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
46 | + if ($submission->has_invoice()) { |
|
47 | + $this->taxes = array_replace($submission->get_invoice()->get_taxes(), $this->taxes); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | } |
@@ -56,26 +56,26 @@ discard block |
||
56 | 56 | * @param GetPaid_Form_Item $item |
57 | 57 | * @param GetPaid_Payment_Form_Submission $submission |
58 | 58 | */ |
59 | - public function process_item_tax( $item, $submission ) { |
|
59 | + public function process_item_tax($item, $submission) { |
|
60 | 60 | |
61 | - $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
62 | - $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
63 | - $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
64 | - $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
61 | + $rates = getpaid_get_item_tax_rates($item, $submission->country, $submission->state); |
|
62 | + $rates = getpaid_filter_item_tax_rates($item, $rates); |
|
63 | + $taxes = getpaid_calculate_item_taxes(getpaid_get_taxable_amount($item, false), $rates); |
|
64 | + $r_taxes = getpaid_calculate_item_taxes(getpaid_get_taxable_amount($item, true), $rates); |
|
65 | 65 | |
66 | - foreach ( $taxes as $name => $amount ) { |
|
67 | - $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
68 | - $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
66 | + foreach ($taxes as $name => $amount) { |
|
67 | + $recurring = isset($r_taxes[$name]) ? $r_taxes[$name] : 0; |
|
68 | + $tax = getpaid_prepare_item_tax($item, $name, $amount, $recurring); |
|
69 | 69 | |
70 | - $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
70 | + $item->item_tax += wpinv_sanitize_amount($tax['initial_tax']); |
|
71 | 71 | |
72 | - if ( ! isset( $this->taxes[ $name ] ) ) { |
|
73 | - $this->taxes[ $name ] = $tax; |
|
72 | + if (!isset($this->taxes[$name])) { |
|
73 | + $this->taxes[$name] = $tax; |
|
74 | 74 | continue; |
75 | 75 | } |
76 | 76 | |
77 | - $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
78 | - $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
77 | + $this->taxes[$name]['initial_tax'] += $tax['initial_tax']; |
|
78 | + $this->taxes[$name]['recurring_tax'] += $tax['recurring_tax']; |
|
79 | 79 | |
80 | 80 | } |
81 | 81 | |
@@ -88,11 +88,11 @@ discard block |
||
88 | 88 | * @since 1.0.19 |
89 | 89 | * @return bool |
90 | 90 | */ |
91 | - public function has_digital_item( $submission ) { |
|
91 | + public function has_digital_item($submission) { |
|
92 | 92 | |
93 | - foreach ( $submission->get_items() as $item ) { |
|
93 | + foreach ($submission->get_items() as $item) { |
|
94 | 94 | |
95 | - if ( 'digital' == $item->get_vat_rule() ) { |
|
95 | + if ('digital' == $item->get_vat_rule()) { |
|
96 | 96 | return true; |
97 | 97 | } |
98 | 98 | |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | * @return bool |
109 | 109 | */ |
110 | 110 | public static function is_eu_store() { |
111 | - return self::is_eu_country( wpinv_get_default_country() ); |
|
111 | + return self::is_eu_country(wpinv_get_default_country()); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
@@ -118,8 +118,8 @@ discard block |
||
118 | 118 | * @since 1.0.19 |
119 | 119 | * @return bool |
120 | 120 | */ |
121 | - public static function is_eu_country( $country ) { |
|
122 | - return getpaid_is_eu_state( $country ); |
|
121 | + public static function is_eu_country($country) { |
|
122 | + return getpaid_is_eu_state($country); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -129,8 +129,8 @@ discard block |
||
129 | 129 | * @since 1.0.19 |
130 | 130 | * @return bool |
131 | 131 | */ |
132 | - public static function is_eu_transaction( $customer_country ) { |
|
133 | - return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
132 | + public static function is_eu_transaction($customer_country) { |
|
133 | + return self::is_eu_country($customer_country) && self::is_eu_store(); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -140,12 +140,12 @@ discard block |
||
140 | 140 | * @since 1.0.19 |
141 | 141 | * @return string |
142 | 142 | */ |
143 | - public function get_vat_number( $submission ) { |
|
143 | + public function get_vat_number($submission) { |
|
144 | 144 | |
145 | 145 | // Retrieve from the posted number. |
146 | - $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
147 | - if ( ! is_null( $vat_number ) ) { |
|
148 | - return wpinv_clean( $vat_number ); |
|
146 | + $vat_number = $submission->get_field('wpinv_vat_number', 'billing'); |
|
147 | + if (!is_null($vat_number)) { |
|
148 | + return wpinv_clean($vat_number); |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
@@ -158,12 +158,12 @@ discard block |
||
158 | 158 | * @since 1.0.19 |
159 | 159 | * @return string |
160 | 160 | */ |
161 | - public function get_company( $submission ) { |
|
161 | + public function get_company($submission) { |
|
162 | 162 | |
163 | 163 | // Retrieve from the posted data. |
164 | - $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
165 | - if ( ! empty( $company ) ) { |
|
166 | - return wpinv_clean( $company ); |
|
164 | + $company = $submission->get_field('wpinv_company', 'billing'); |
|
165 | + if (!empty($company)) { |
|
166 | + return wpinv_clean($company); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | // Retrieve from the invoice. |
@@ -178,10 +178,10 @@ discard block |
||
178 | 178 | * @since 1.0.19 |
179 | 179 | * @return string |
180 | 180 | */ |
181 | - public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
181 | + public function requires_vat($ip_in_eu, $country_in_eu) { |
|
182 | 182 | |
183 | - $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
184 | - $prevent_b2c = ! empty( $prevent_b2c ); |
|
183 | + $prevent_b2c = wpinv_get_option('vat_prevent_b2c_purchase'); |
|
184 | + $prevent_b2c = !empty($prevent_b2c); |
|
185 | 185 | $is_eu = $ip_in_eu || $country_in_eu; |
186 | 186 | |
187 | 187 | return $prevent_b2c && $is_eu; |
@@ -193,43 +193,43 @@ discard block |
||
193 | 193 | * @param GetPaid_Payment_Form_Submission $submission |
194 | 194 | * @since 1.0.19 |
195 | 195 | */ |
196 | - public function validate_vat( $submission ) { |
|
196 | + public function validate_vat($submission) { |
|
197 | 197 | |
198 | - $in_eu = $this->is_eu_transaction( $submission->country ); |
|
198 | + $in_eu = $this->is_eu_transaction($submission->country); |
|
199 | 199 | |
200 | 200 | // Abort if we are not validating vat numbers. |
201 | - if ( ! $in_eu ) { |
|
201 | + if (!$in_eu) { |
|
202 | 202 | return; |
203 | 203 | } |
204 | 204 | |
205 | 205 | // Prepare variables. |
206 | - $vat_number = $this->get_vat_number( $submission ); |
|
206 | + $vat_number = $this->get_vat_number($submission); |
|
207 | 207 | $ip_country = getpaid_get_ip_country(); |
208 | - $is_eu = $this->is_eu_country( $submission->country ); |
|
209 | - $is_ip_eu = $this->is_eu_country( $ip_country ); |
|
208 | + $is_eu = $this->is_eu_country($submission->country); |
|
209 | + $is_ip_eu = $this->is_eu_country($ip_country); |
|
210 | 210 | |
211 | 211 | // Maybe abort early for initial fetches. |
212 | - if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
212 | + if ($submission->is_initial_fetch() && empty($vat_number)) { |
|
213 | 213 | return; |
214 | 214 | } |
215 | 215 | |
216 | 216 | // If we're preventing business to consumer purchases, |
217 | - if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
217 | + if ($this->requires_vat($is_ip_eu, $is_eu) && empty($vat_number)) { |
|
218 | 218 | |
219 | 219 | // Ensure that a vat number has been specified. |
220 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) ); |
|
220 | + throw new GetPaid_Payment_Exception('.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __('Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing')); |
|
221 | 221 | |
222 | 222 | } |
223 | 223 | |
224 | - if ( empty( $vat_number ) ) { |
|
224 | + if (empty($vat_number)) { |
|
225 | 225 | return; |
226 | 226 | } |
227 | 227 | |
228 | - if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
229 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
228 | + if (wpinv_should_validate_vat_number() && !wpinv_validate_vat_number($vat_number, $submission->country)) { |
|
229 | + throw new GetPaid_Payment_Exception('.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __('Your VAT number is invalid', 'invoicing')); |
|
230 | 230 | } |
231 | 231 | |
232 | - if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
232 | + if (wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option('vat_same_country_rule', 'vat_too')) { |
|
233 | 233 | return; |
234 | 234 | } |
235 | 235 |