@@ -12,180 +12,180 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Checkout { |
14 | 14 | |
15 | - /** |
|
16 | - * @var GetPaid_Payment_Form_Submission |
|
17 | - */ |
|
18 | - protected $payment_form_submission; |
|
19 | - |
|
20 | - /** |
|
21 | - * Class constructor. |
|
22 | - * |
|
23 | - * @param GetPaid_Payment_Form_Submission $submission |
|
24 | - */ |
|
25 | - public function __construct( $submission ) { |
|
26 | - $this->payment_form_submission = $submission; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Processes the checkout. |
|
31 | - * |
|
32 | - */ |
|
33 | - public function process_checkout() { |
|
34 | - |
|
35 | - // Validate the submission. |
|
36 | - $this->validate_submission(); |
|
37 | - |
|
38 | - // Prepare the invoice. |
|
39 | - $items = $this->get_submission_items(); |
|
40 | - $invoice = $this->get_submission_invoice(); |
|
41 | - $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | - $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | - |
|
44 | - $this->prepare_billing_info( $invoice ); |
|
45 | - |
|
46 | - $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | - |
|
48 | - // Save the invoice. |
|
49 | - $invoice->set_is_viewed( true ); |
|
50 | - $invoice->recalculate_total(); |
|
15 | + /** |
|
16 | + * @var GetPaid_Payment_Form_Submission |
|
17 | + */ |
|
18 | + protected $payment_form_submission; |
|
19 | + |
|
20 | + /** |
|
21 | + * Class constructor. |
|
22 | + * |
|
23 | + * @param GetPaid_Payment_Form_Submission $submission |
|
24 | + */ |
|
25 | + public function __construct( $submission ) { |
|
26 | + $this->payment_form_submission = $submission; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Processes the checkout. |
|
31 | + * |
|
32 | + */ |
|
33 | + public function process_checkout() { |
|
34 | + |
|
35 | + // Validate the submission. |
|
36 | + $this->validate_submission(); |
|
37 | + |
|
38 | + // Prepare the invoice. |
|
39 | + $items = $this->get_submission_items(); |
|
40 | + $invoice = $this->get_submission_invoice(); |
|
41 | + $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | + $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | + |
|
44 | + $this->prepare_billing_info( $invoice ); |
|
45 | + |
|
46 | + $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | + |
|
48 | + // Save the invoice. |
|
49 | + $invoice->set_is_viewed( true ); |
|
50 | + $invoice->recalculate_total(); |
|
51 | 51 | $invoice->save(); |
52 | 52 | |
53 | - do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
53 | + do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
54 | 54 | |
55 | - // Send to the gateway. |
|
56 | - $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | - } |
|
55 | + // Send to the gateway. |
|
56 | + $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Validates the submission. |
|
61 | - * |
|
62 | - */ |
|
63 | - protected function validate_submission() { |
|
59 | + /** |
|
60 | + * Validates the submission. |
|
61 | + * |
|
62 | + */ |
|
63 | + protected function validate_submission() { |
|
64 | 64 | |
65 | - $submission = $this->payment_form_submission; |
|
66 | - $data = $submission->get_data(); |
|
65 | + $submission = $this->payment_form_submission; |
|
66 | + $data = $submission->get_data(); |
|
67 | 67 | |
68 | - // Do we have an error? |
|
68 | + // Do we have an error? |
|
69 | 69 | if ( ! empty( $submission->last_error ) ) { |
70 | - wp_send_json_error( $submission->last_error ); |
|
70 | + wp_send_json_error( $submission->last_error ); |
|
71 | 71 | } |
72 | 72 | |
73 | - // We need a billing email. |
|
73 | + // We need a billing email. |
|
74 | 74 | if ( ! $submission->has_billing_email() ) { |
75 | 75 | wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
76 | - } |
|
76 | + } |
|
77 | 77 | |
78 | - // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | - if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | - wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | - } |
|
78 | + // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | + if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | + wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | + } |
|
82 | 82 | |
83 | - // Ensure the gateway is active. |
|
84 | - if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | - wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | - } |
|
83 | + // Ensure the gateway is active. |
|
84 | + if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | + wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | + } |
|
87 | 87 | |
88 | - // Clear any existing errors. |
|
89 | - wpinv_clear_errors(); |
|
88 | + // Clear any existing errors. |
|
89 | + wpinv_clear_errors(); |
|
90 | 90 | |
91 | - // Allow themes and plugins to hook to errors |
|
92 | - do_action( 'getpaid_checkout_error_checks', $submission ); |
|
91 | + // Allow themes and plugins to hook to errors |
|
92 | + do_action( 'getpaid_checkout_error_checks', $submission ); |
|
93 | 93 | |
94 | - // Do we have any errors? |
|
94 | + // Do we have any errors? |
|
95 | 95 | if ( wpinv_get_errors() ) { |
96 | 96 | wp_send_json_error( getpaid_get_errors_html() ); |
97 | - } |
|
97 | + } |
|
98 | 98 | |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Retrieves submission items. |
|
103 | - * |
|
104 | - * @return GetPaid_Form_Item[] |
|
105 | - */ |
|
106 | - protected function get_submission_items() { |
|
101 | + /** |
|
102 | + * Retrieves submission items. |
|
103 | + * |
|
104 | + * @return GetPaid_Form_Item[] |
|
105 | + */ |
|
106 | + protected function get_submission_items() { |
|
107 | 107 | |
108 | - $items = $this->payment_form_submission->get_items(); |
|
108 | + $items = $this->payment_form_submission->get_items(); |
|
109 | 109 | |
110 | 110 | // Ensure that we have items. |
111 | 111 | if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
112 | 112 | wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
113 | - } |
|
114 | - |
|
115 | - return $items; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Retrieves submission invoice. |
|
120 | - * |
|
121 | - * @return WPInv_Invoice |
|
122 | - */ |
|
123 | - protected function get_submission_invoice() { |
|
124 | - $submission = $this->payment_form_submission; |
|
125 | - |
|
126 | - if ( ! $submission->has_invoice() ) { |
|
127 | - $invoice = new WPInv_Invoice(); |
|
128 | - $invoice->set_created_via( 'payment_form' ); |
|
129 | - return $invoice; |
|
130 | 113 | } |
131 | 114 | |
132 | - $invoice = $submission->get_invoice(); |
|
115 | + return $items; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Retrieves submission invoice. |
|
120 | + * |
|
121 | + * @return WPInv_Invoice |
|
122 | + */ |
|
123 | + protected function get_submission_invoice() { |
|
124 | + $submission = $this->payment_form_submission; |
|
125 | + |
|
126 | + if ( ! $submission->has_invoice() ) { |
|
127 | + $invoice = new WPInv_Invoice(); |
|
128 | + $invoice->set_created_via( 'payment_form' ); |
|
129 | + return $invoice; |
|
130 | + } |
|
131 | + |
|
132 | + $invoice = $submission->get_invoice(); |
|
133 | 133 | |
134 | - // Make sure that it is neither paid or refunded. |
|
135 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | - wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | - } |
|
134 | + // Make sure that it is neither paid or refunded. |
|
135 | + if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | + wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | + } |
|
138 | 138 | |
139 | - return $invoice; |
|
140 | - } |
|
139 | + return $invoice; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Processes the submission invoice. |
|
144 | - * |
|
145 | - * @param WPInv_Invoice $invoice |
|
146 | - * @param GetPaid_Form_Item[] $items |
|
147 | - * @return WPInv_Invoice |
|
148 | - */ |
|
149 | - protected function process_submission_invoice( $invoice, $items ) { |
|
142 | + /** |
|
143 | + * Processes the submission invoice. |
|
144 | + * |
|
145 | + * @param WPInv_Invoice $invoice |
|
146 | + * @param GetPaid_Form_Item[] $items |
|
147 | + * @return WPInv_Invoice |
|
148 | + */ |
|
149 | + protected function process_submission_invoice( $invoice, $items ) { |
|
150 | 150 | |
151 | - $submission = $this->payment_form_submission; |
|
151 | + $submission = $this->payment_form_submission; |
|
152 | 152 | |
153 | - // Set-up the invoice details. |
|
154 | - $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | - $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | - $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
153 | + // Set-up the invoice details. |
|
154 | + $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | + $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | + $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
157 | 157 | $invoice->set_items( $items ); |
158 | 158 | $invoice->set_fees( $submission->get_fees() ); |
159 | 159 | $invoice->set_taxes( $submission->get_taxes() ); |
160 | - $invoice->set_discounts( $submission->get_discounts() ); |
|
161 | - $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
162 | - $invoice->set_currency( $submission->get_currency() ); |
|
160 | + $invoice->set_discounts( $submission->get_discounts() ); |
|
161 | + $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
162 | + $invoice->set_currency( $submission->get_currency() ); |
|
163 | 163 | |
164 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
165 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
164 | + $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
165 | + $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
166 | 166 | |
167 | - if ( $submission->has_discount_code() ) { |
|
167 | + if ( $submission->has_discount_code() ) { |
|
168 | 168 | $invoice->set_discount_code( $submission->get_discount_code() ); |
169 | - } |
|
170 | - |
|
171 | - getpaid_maybe_add_default_address( $invoice ); |
|
172 | - return $invoice; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Retrieves the submission's customer. |
|
177 | - * |
|
178 | - * @return int The customer id. |
|
179 | - */ |
|
180 | - protected function get_submission_customer() { |
|
181 | - $submission = $this->payment_form_submission; |
|
182 | - |
|
183 | - // If this is an existing invoice... |
|
184 | - if ( $submission->has_invoice() ) { |
|
185 | - return $submission->get_invoice()->get_user_id(); |
|
186 | - } |
|
187 | - |
|
188 | - // (Maybe) create the user. |
|
169 | + } |
|
170 | + |
|
171 | + getpaid_maybe_add_default_address( $invoice ); |
|
172 | + return $invoice; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Retrieves the submission's customer. |
|
177 | + * |
|
178 | + * @return int The customer id. |
|
179 | + */ |
|
180 | + protected function get_submission_customer() { |
|
181 | + $submission = $this->payment_form_submission; |
|
182 | + |
|
183 | + // If this is an existing invoice... |
|
184 | + if ( $submission->has_invoice() ) { |
|
185 | + return $submission->get_invoice()->get_user_id(); |
|
186 | + } |
|
187 | + |
|
188 | + // (Maybe) create the user. |
|
189 | 189 | $user = get_current_user_id(); |
190 | 190 | |
191 | 191 | if ( empty( $user ) ) { |
@@ -195,11 +195,11 @@ discard block |
||
195 | 195 | if ( empty( $user ) ) { |
196 | 196 | $user = wpinv_create_user( $submission->get_billing_email() ); |
197 | 197 | |
198 | - // (Maybe) send new user notification. |
|
199 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
200 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
201 | - wp_send_new_user_notifications( $user, 'user' ); |
|
202 | - } |
|
198 | + // (Maybe) send new user notification. |
|
199 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
200 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
201 | + wp_send_new_user_notifications( $user, 'user' ); |
|
202 | + } |
|
203 | 203 | |
204 | 204 | } |
205 | 205 | |
@@ -209,49 +209,49 @@ discard block |
||
209 | 209 | |
210 | 210 | if ( is_numeric( $user ) ) { |
211 | 211 | return $user; |
212 | - } |
|
212 | + } |
|
213 | 213 | |
214 | - return $user->ID; |
|
214 | + return $user->ID; |
|
215 | 215 | |
216 | - } |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
218 | + /** |
|
219 | 219 | * Prepares submission data for saving to the database. |
220 | 220 | * |
221 | - * @return array |
|
221 | + * @return array |
|
222 | 222 | */ |
223 | 223 | public function prepare_submission_data_for_saving() { |
224 | 224 | |
225 | - $submission = $this->payment_form_submission; |
|
225 | + $submission = $this->payment_form_submission; |
|
226 | 226 | |
227 | - // Prepared submission details. |
|
227 | + // Prepared submission details. |
|
228 | 228 | $prepared = array( |
229 | - 'all' => array(), |
|
230 | - 'meta' => array(), |
|
231 | - ); |
|
229 | + 'all' => array(), |
|
230 | + 'meta' => array(), |
|
231 | + ); |
|
232 | 232 | |
233 | 233 | // Raw submission details. |
234 | - $data = $submission->get_data(); |
|
234 | + $data = $submission->get_data(); |
|
235 | 235 | |
236 | - // Loop through the submitted details. |
|
236 | + // Loop through the submitted details. |
|
237 | 237 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
238 | 238 | |
239 | - // Skip premade fields. |
|
239 | + // Skip premade fields. |
|
240 | 240 | if ( ! empty( $field['premade'] ) ) { |
241 | 241 | continue; |
242 | 242 | } |
243 | 243 | |
244 | - // Ensure address is provided. |
|
245 | - if ( $field['type'] == 'address' ) { |
|
244 | + // Ensure address is provided. |
|
245 | + if ( $field['type'] == 'address' ) { |
|
246 | 246 | $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
247 | 247 | |
248 | - foreach ( $field['fields'] as $address_field ) { |
|
248 | + foreach ( $field['fields'] as $address_field ) { |
|
249 | 249 | |
250 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { |
|
251 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
252 | - } |
|
250 | + if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { |
|
251 | + wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
252 | + } |
|
253 | 253 | |
254 | - } |
|
254 | + } |
|
255 | 255 | |
256 | 256 | } |
257 | 257 | |
@@ -263,31 +263,31 @@ discard block |
||
263 | 263 | // Handle misc fields. |
264 | 264 | if ( isset( $data[ $field['id'] ] ) ) { |
265 | 265 | |
266 | - // Uploads. |
|
267 | - if ( $field['type'] == 'file_upload' ) { |
|
268 | - $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
266 | + // Uploads. |
|
267 | + if ( $field['type'] == 'file_upload' ) { |
|
268 | + $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
269 | 269 | |
270 | - if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
271 | - wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
272 | - } |
|
270 | + if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
271 | + wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
272 | + } |
|
273 | 273 | |
274 | - $value = array(); |
|
274 | + $value = array(); |
|
275 | 275 | |
276 | - foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
277 | - $value[] = sprintf( |
|
278 | - '<a href="%s" target="_blank">%s</a>', |
|
279 | - esc_url_raw( $url ), |
|
280 | - esc_html( $name ) |
|
281 | - ); |
|
282 | - } |
|
276 | + foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
277 | + $value[] = sprintf( |
|
278 | + '<a href="%s" target="_blank">%s</a>', |
|
279 | + esc_url_raw( $url ), |
|
280 | + esc_html( $name ) |
|
281 | + ); |
|
282 | + } |
|
283 | 283 | |
284 | - $value = implode( ' | ', $value ); |
|
284 | + $value = implode( ' | ', $value ); |
|
285 | 285 | |
286 | - } else if ( $field['type'] == 'checkbox' ) { |
|
287 | - $value = isset( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
288 | - } else { |
|
289 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
290 | - } |
|
286 | + } else if ( $field['type'] == 'checkbox' ) { |
|
287 | + $value = isset( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
288 | + } else { |
|
289 | + $value = wp_kses_post( $data[ $field['id'] ] ); |
|
290 | + } |
|
291 | 291 | |
292 | 292 | $label = $field['id']; |
293 | 293 | |
@@ -295,189 +295,189 @@ discard block |
||
295 | 295 | $label = $field['label']; |
296 | 296 | } |
297 | 297 | |
298 | - if ( ! empty( $field['add_meta'] ) ) { |
|
299 | - $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
300 | - } |
|
301 | - $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
298 | + if ( ! empty( $field['add_meta'] ) ) { |
|
299 | + $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
300 | + } |
|
301 | + $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
302 | 302 | |
303 | 303 | } |
304 | 304 | |
305 | - } |
|
305 | + } |
|
306 | 306 | |
307 | - return $prepared; |
|
307 | + return $prepared; |
|
308 | 308 | |
309 | - } |
|
309 | + } |
|
310 | 310 | |
311 | - /** |
|
311 | + /** |
|
312 | 312 | * Retrieves address details. |
313 | 313 | * |
314 | - * @return array |
|
315 | - * @param WPInv_Invoice $invoice |
|
316 | - * @param string $type |
|
314 | + * @return array |
|
315 | + * @param WPInv_Invoice $invoice |
|
316 | + * @param string $type |
|
317 | 317 | */ |
318 | 318 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
319 | 319 | |
320 | - $data = $this->payment_form_submission->get_data(); |
|
321 | - $type = sanitize_key( $type ); |
|
322 | - $address = array(); |
|
323 | - $prepared = array(); |
|
320 | + $data = $this->payment_form_submission->get_data(); |
|
321 | + $type = sanitize_key( $type ); |
|
322 | + $address = array(); |
|
323 | + $prepared = array(); |
|
324 | 324 | |
325 | - if ( ! empty( $data[ $type ] ) ) { |
|
326 | - $address = $data[ $type ]; |
|
327 | - } |
|
325 | + if ( ! empty( $data[ $type ] ) ) { |
|
326 | + $address = $data[ $type ]; |
|
327 | + } |
|
328 | 328 | |
329 | - // Clean address details. |
|
330 | - foreach ( $address as $key => $value ) { |
|
331 | - $key = sanitize_key( $key ); |
|
332 | - $key = str_replace( 'wpinv_', '', $key ); |
|
333 | - $value = wpinv_clean( $value ); |
|
334 | - $prepared[ $key] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
335 | - } |
|
329 | + // Clean address details. |
|
330 | + foreach ( $address as $key => $value ) { |
|
331 | + $key = sanitize_key( $key ); |
|
332 | + $key = str_replace( 'wpinv_', '', $key ); |
|
333 | + $value = wpinv_clean( $value ); |
|
334 | + $prepared[ $key] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
335 | + } |
|
336 | 336 | |
337 | - // Filter address details. |
|
338 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
337 | + // Filter address details. |
|
338 | + $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
339 | 339 | |
340 | - // Remove non-whitelisted values. |
|
341 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
340 | + // Remove non-whitelisted values. |
|
341 | + return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
342 | 342 | |
343 | - } |
|
343 | + } |
|
344 | 344 | |
345 | - /** |
|
345 | + /** |
|
346 | 346 | * Prepares the billing details. |
347 | 347 | * |
348 | - * @return array |
|
349 | - * @param WPInv_Invoice $invoice |
|
348 | + * @return array |
|
349 | + * @param WPInv_Invoice $invoice |
|
350 | 350 | */ |
351 | 351 | protected function prepare_billing_info( &$invoice ) { |
352 | 352 | |
353 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
353 | + $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
354 | 354 | |
355 | - // Update the invoice with the billing details. |
|
356 | - $invoice->set_props( $billing_address ); |
|
355 | + // Update the invoice with the billing details. |
|
356 | + $invoice->set_props( $billing_address ); |
|
357 | 357 | |
358 | - } |
|
358 | + } |
|
359 | 359 | |
360 | - /** |
|
360 | + /** |
|
361 | 361 | * Prepares the shipping details. |
362 | 362 | * |
363 | - * @return array |
|
364 | - * @param WPInv_Invoice $invoice |
|
363 | + * @return array |
|
364 | + * @param WPInv_Invoice $invoice |
|
365 | 365 | */ |
366 | 366 | protected function prepare_shipping_info( $invoice ) { |
367 | 367 | |
368 | - $data = $this->payment_form_submission->get_data(); |
|
368 | + $data = $this->payment_form_submission->get_data(); |
|
369 | 369 | |
370 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
371 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
372 | - } |
|
370 | + if ( empty( $data['same-shipping-address'] ) ) { |
|
371 | + return $this->prepare_address_details( $invoice, 'shipping' ); |
|
372 | + } |
|
373 | 373 | |
374 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
374 | + return $this->prepare_address_details( $invoice, 'billing' ); |
|
375 | 375 | |
376 | - } |
|
376 | + } |
|
377 | 377 | |
378 | - /** |
|
379 | - * Confirms the submission is valid and send users to the gateway. |
|
380 | - * |
|
381 | - * @param WPInv_Invoice $invoice |
|
382 | - * @param array $prepared_payment_form_data |
|
383 | - * @param array $shipping |
|
384 | - */ |
|
385 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
378 | + /** |
|
379 | + * Confirms the submission is valid and send users to the gateway. |
|
380 | + * |
|
381 | + * @param WPInv_Invoice $invoice |
|
382 | + * @param array $prepared_payment_form_data |
|
383 | + * @param array $shipping |
|
384 | + */ |
|
385 | + protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
386 | 386 | |
387 | - // Ensure the invoice exists. |
|
387 | + // Ensure the invoice exists. |
|
388 | 388 | if ( ! $invoice->exists() ) { |
389 | 389 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
390 | 390 | } |
391 | 391 | |
392 | - // Save payment form data. |
|
393 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
392 | + // Save payment form data. |
|
393 | + $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
394 | 394 | delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
395 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
396 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
395 | + delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
396 | + if ( ! empty( $prepared_payment_form_data ) ) { |
|
397 | 397 | |
398 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
399 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
400 | - } |
|
398 | + if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
399 | + update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
400 | + } |
|
401 | 401 | |
402 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
403 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
404 | - } |
|
402 | + if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
403 | + update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
404 | + } |
|
405 | 405 | |
406 | - } |
|
406 | + } |
|
407 | 407 | |
408 | - // Save payment form data. |
|
408 | + // Save payment form data. |
|
409 | 409 | if ( ! empty( $shipping ) ) { |
410 | 410 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
411 | - } |
|
411 | + } |
|
412 | 412 | |
413 | - // Backwards compatibility. |
|
413 | + // Backwards compatibility. |
|
414 | 414 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
415 | 415 | |
416 | - $this->process_payment( $invoice ); |
|
416 | + $this->process_payment( $invoice ); |
|
417 | 417 | |
418 | 418 | // If we are here, there was an error. |
419 | - wpinv_send_back_to_checkout( $invoice ); |
|
419 | + wpinv_send_back_to_checkout( $invoice ); |
|
420 | 420 | |
421 | - } |
|
421 | + } |
|
422 | 422 | |
423 | - /** |
|
424 | - * Processes the actual payment. |
|
425 | - * |
|
426 | - * @param WPInv_Invoice $invoice |
|
427 | - */ |
|
428 | - protected function process_payment( $invoice ) { |
|
423 | + /** |
|
424 | + * Processes the actual payment. |
|
425 | + * |
|
426 | + * @param WPInv_Invoice $invoice |
|
427 | + */ |
|
428 | + protected function process_payment( $invoice ) { |
|
429 | 429 | |
430 | - // Clear any checkout errors. |
|
431 | - wpinv_clear_errors(); |
|
430 | + // Clear any checkout errors. |
|
431 | + wpinv_clear_errors(); |
|
432 | 432 | |
433 | - // No need to send free invoices to the gateway. |
|
434 | - if ( $invoice->is_free() ) { |
|
435 | - $this->process_free_payment( $invoice ); |
|
436 | - } |
|
433 | + // No need to send free invoices to the gateway. |
|
434 | + if ( $invoice->is_free() ) { |
|
435 | + $this->process_free_payment( $invoice ); |
|
436 | + } |
|
437 | 437 | |
438 | - $submission = $this->payment_form_submission; |
|
438 | + $submission = $this->payment_form_submission; |
|
439 | 439 | |
440 | - // Fires before sending to the gateway. |
|
441 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
440 | + // Fires before sending to the gateway. |
|
441 | + do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
442 | 442 | |
443 | - // Allow the sumission data to be modified before it is sent to the gateway. |
|
444 | - $submission_data = $submission->get_data(); |
|
445 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
446 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
443 | + // Allow the sumission data to be modified before it is sent to the gateway. |
|
444 | + $submission_data = $submission->get_data(); |
|
445 | + $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
446 | + $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
447 | 447 | |
448 | - // Validate the currency. |
|
449 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
450 | - wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) ); |
|
451 | - } |
|
448 | + // Validate the currency. |
|
449 | + if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
450 | + wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) ); |
|
451 | + } |
|
452 | 452 | |
453 | - // Check to see if we have any errors. |
|
454 | - if ( wpinv_get_errors() ) { |
|
455 | - wpinv_send_back_to_checkout( $invoice ); |
|
456 | - } |
|
453 | + // Check to see if we have any errors. |
|
454 | + if ( wpinv_get_errors() ) { |
|
455 | + wpinv_send_back_to_checkout( $invoice ); |
|
456 | + } |
|
457 | 457 | |
458 | - // Send info to the gateway for payment processing |
|
459 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
458 | + // Send info to the gateway for payment processing |
|
459 | + do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
460 | 460 | |
461 | - // Backwards compatibility. |
|
462 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
461 | + // Backwards compatibility. |
|
462 | + wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
463 | 463 | |
464 | - } |
|
464 | + } |
|
465 | 465 | |
466 | - /** |
|
467 | - * Marks the invoice as paid in case the checkout is free. |
|
468 | - * |
|
469 | - * @param WPInv_Invoice $invoice |
|
470 | - */ |
|
471 | - protected function process_free_payment( $invoice ) { |
|
466 | + /** |
|
467 | + * Marks the invoice as paid in case the checkout is free. |
|
468 | + * |
|
469 | + * @param WPInv_Invoice $invoice |
|
470 | + */ |
|
471 | + protected function process_free_payment( $invoice ) { |
|
472 | 472 | |
473 | - $invoice->set_gateway( 'none' ); |
|
474 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
475 | - $invoice->mark_paid(); |
|
476 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
473 | + $invoice->set_gateway( 'none' ); |
|
474 | + $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
475 | + $invoice->mark_paid(); |
|
476 | + wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
477 | 477 | |
478 | - } |
|
478 | + } |
|
479 | 479 | |
480 | - /** |
|
480 | + /** |
|
481 | 481 | * Sends a redrect response to payment details. |
482 | 482 | * |
483 | 483 | */ |
@@ -19,16 +19,16 @@ discard block |
||
19 | 19 | |
20 | 20 | // Define constants. |
21 | 21 | if ( ! defined( 'WPINV_PLUGIN_FILE' ) ) { |
22 | - define( 'WPINV_PLUGIN_FILE', __FILE__ ); |
|
22 | + define( 'WPINV_PLUGIN_FILE', __FILE__ ); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | if ( ! defined( 'WPINV_VERSION' ) ) { |
26 | - define( 'WPINV_VERSION', '2.5.5' ); |
|
26 | + define( 'WPINV_VERSION', '2.5.5' ); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | // Include the main Invoicing class. |
30 | 30 | if ( ! class_exists( 'WPInv_Plugin', false ) ) { |
31 | - require_once plugin_dir_path( WPINV_PLUGIN_FILE ) . 'includes/class-wpinv.php'; |
|
31 | + require_once plugin_dir_path( WPINV_PLUGIN_FILE ) . 'includes/class-wpinv.php'; |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | $GLOBALS['invoicing'] = new WPInv_Plugin(); |
44 | 44 | } |
45 | 45 | |
46 | - return $GLOBALS['invoicing']; |
|
46 | + return $GLOBALS['invoicing']; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -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,55 +10,55 @@ discard block |
||
10 | 10 | class GetPaid_Payment_Form extends GetPaid_Data { |
11 | 11 | |
12 | 12 | /** |
13 | - * Which data store to load. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
13 | + * Which data store to load. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | 17 | protected $data_store_name = 'payment_form'; |
18 | 18 | |
19 | 19 | /** |
20 | - * This is the name of this object type. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $object_type = 'payment_form'; |
|
20 | + * This is the name of this object type. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $object_type = 'payment_form'; |
|
25 | 25 | |
26 | 26 | /** |
27 | - * Form Data array. This is the core form data exposed in APIs. |
|
28 | - * |
|
29 | - * @since 1.0.19 |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $data = array( |
|
33 | - 'status' => 'draft', |
|
34 | - 'version' => '', |
|
35 | - 'date_created' => null, |
|
27 | + * Form Data array. This is the core form data exposed in APIs. |
|
28 | + * |
|
29 | + * @since 1.0.19 |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $data = array( |
|
33 | + 'status' => 'draft', |
|
34 | + 'version' => '', |
|
35 | + 'date_created' => null, |
|
36 | 36 | 'date_modified' => null, |
37 | 37 | 'name' => '', |
38 | 38 | 'author' => 1, |
39 | 39 | 'elements' => null, |
40 | - 'items' => null, |
|
41 | - 'earned' => 0, |
|
42 | - 'refunded' => 0, |
|
43 | - 'cancelled' => 0, |
|
44 | - 'failed' => 0, |
|
45 | - ); |
|
46 | - |
|
47 | - /** |
|
48 | - * Stores meta in cache for future reads. |
|
49 | - * |
|
50 | - * A group must be set to to enable caching. |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $cache_group = 'getpaid_forms'; |
|
55 | - |
|
56 | - /** |
|
57 | - * Stores a reference to the invoice if the form is for an invoice.. |
|
58 | - * |
|
59 | - * @var WPInv_Invoice |
|
60 | - */ |
|
61 | - public $invoice = 0; |
|
40 | + 'items' => null, |
|
41 | + 'earned' => 0, |
|
42 | + 'refunded' => 0, |
|
43 | + 'cancelled' => 0, |
|
44 | + 'failed' => 0, |
|
45 | + ); |
|
46 | + |
|
47 | + /** |
|
48 | + * Stores meta in cache for future reads. |
|
49 | + * |
|
50 | + * A group must be set to to enable caching. |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $cache_group = 'getpaid_forms'; |
|
55 | + |
|
56 | + /** |
|
57 | + * Stores a reference to the invoice if the form is for an invoice.. |
|
58 | + * |
|
59 | + * @var WPInv_Invoice |
|
60 | + */ |
|
61 | + public $invoice = 0; |
|
62 | 62 | |
63 | 63 | /** |
64 | 64 | * Stores a reference to the original WP_Post object |
@@ -68,35 +68,35 @@ discard block |
||
68 | 68 | protected $post = null; |
69 | 69 | |
70 | 70 | /** |
71 | - * Get the form if ID is passed, otherwise the form is new and empty. |
|
72 | - * |
|
73 | - * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
|
74 | - */ |
|
75 | - public function __construct( $form = 0 ) { |
|
76 | - parent::__construct( $form ); |
|
71 | + * Get the form if ID is passed, otherwise the form is new and empty. |
|
72 | + * |
|
73 | + * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
|
74 | + */ |
|
75 | + public function __construct( $form = 0 ) { |
|
76 | + parent::__construct( $form ); |
|
77 | 77 | |
78 | - if ( is_numeric( $form ) && $form > 0 ) { |
|
79 | - $this->set_id( $form ); |
|
80 | - } elseif ( $form instanceof self ) { |
|
78 | + if ( is_numeric( $form ) && $form > 0 ) { |
|
79 | + $this->set_id( $form ); |
|
80 | + } elseif ( $form instanceof self ) { |
|
81 | 81 | |
82 | - $this->set_id( $form->get_id() ); |
|
83 | - $this->invoice = $form->invoice; |
|
82 | + $this->set_id( $form->get_id() ); |
|
83 | + $this->invoice = $form->invoice; |
|
84 | 84 | |
85 | - } elseif ( ! empty( $form->ID ) ) { |
|
86 | - $this->set_id( $form->ID ); |
|
87 | - } else { |
|
88 | - $this->set_object_read( true ); |
|
89 | - } |
|
85 | + } elseif ( ! empty( $form->ID ) ) { |
|
86 | + $this->set_id( $form->ID ); |
|
87 | + } else { |
|
88 | + $this->set_object_read( true ); |
|
89 | + } |
|
90 | 90 | |
91 | 91 | // Load the datastore. |
92 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
92 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
93 | 93 | |
94 | - if ( $this->get_id() > 0 ) { |
|
94 | + if ( $this->get_id() > 0 ) { |
|
95 | 95 | $this->post = get_post( $this->get_id() ); |
96 | - $this->data_store->read( $this ); |
|
96 | + $this->data_store->read( $this ); |
|
97 | 97 | } |
98 | 98 | |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | 101 | /* |
102 | 102 | |-------------------------------------------------------------------------- |
@@ -114,358 +114,358 @@ discard block |
||
114 | 114 | */ |
115 | 115 | |
116 | 116 | /** |
117 | - * Get plugin version when the form was created. |
|
118 | - * |
|
119 | - * @since 1.0.19 |
|
120 | - * @param string $context View or edit context. |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public function get_version( $context = 'view' ) { |
|
124 | - return $this->get_prop( 'version', $context ); |
|
117 | + * Get plugin version when the form was created. |
|
118 | + * |
|
119 | + * @since 1.0.19 |
|
120 | + * @param string $context View or edit context. |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public function get_version( $context = 'view' ) { |
|
124 | + return $this->get_prop( 'version', $context ); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
128 | - * Get date when the form was created. |
|
129 | - * |
|
130 | - * @since 1.0.19 |
|
131 | - * @param string $context View or edit context. |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - public function get_date_created( $context = 'view' ) { |
|
135 | - return $this->get_prop( 'date_created', $context ); |
|
128 | + * Get date when the form was created. |
|
129 | + * |
|
130 | + * @since 1.0.19 |
|
131 | + * @param string $context View or edit context. |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + public function get_date_created( $context = 'view' ) { |
|
135 | + return $this->get_prop( 'date_created', $context ); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
139 | - * Get GMT date when the form was created. |
|
140 | - * |
|
141 | - * @since 1.0.19 |
|
142 | - * @param string $context View or edit context. |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function get_date_created_gmt( $context = 'view' ) { |
|
139 | + * Get GMT date when the form was created. |
|
140 | + * |
|
141 | + * @since 1.0.19 |
|
142 | + * @param string $context View or edit context. |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function get_date_created_gmt( $context = 'view' ) { |
|
146 | 146 | $date = $this->get_date_created( $context ); |
147 | 147 | |
148 | 148 | if ( $date ) { |
149 | 149 | $date = get_gmt_from_date( $date ); |
150 | 150 | } |
151 | - return $date; |
|
151 | + return $date; |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | /** |
155 | - * Get date when the form was last modified. |
|
156 | - * |
|
157 | - * @since 1.0.19 |
|
158 | - * @param string $context View or edit context. |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function get_date_modified( $context = 'view' ) { |
|
162 | - return $this->get_prop( 'date_modified', $context ); |
|
155 | + * Get date when the form was last modified. |
|
156 | + * |
|
157 | + * @since 1.0.19 |
|
158 | + * @param string $context View or edit context. |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function get_date_modified( $context = 'view' ) { |
|
162 | + return $this->get_prop( 'date_modified', $context ); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | /** |
166 | - * Get GMT date when the form was last modified. |
|
167 | - * |
|
168 | - * @since 1.0.19 |
|
169 | - * @param string $context View or edit context. |
|
170 | - * @return string |
|
171 | - */ |
|
172 | - public function get_date_modified_gmt( $context = 'view' ) { |
|
166 | + * Get GMT date when the form was last modified. |
|
167 | + * |
|
168 | + * @since 1.0.19 |
|
169 | + * @param string $context View or edit context. |
|
170 | + * @return string |
|
171 | + */ |
|
172 | + public function get_date_modified_gmt( $context = 'view' ) { |
|
173 | 173 | $date = $this->get_date_modified( $context ); |
174 | 174 | |
175 | 175 | if ( $date ) { |
176 | 176 | $date = get_gmt_from_date( $date ); |
177 | 177 | } |
178 | - return $date; |
|
178 | + return $date; |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
182 | - * Get the form name. |
|
183 | - * |
|
184 | - * @since 1.0.19 |
|
185 | - * @param string $context View or edit context. |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - public function get_name( $context = 'view' ) { |
|
189 | - return $this->get_prop( 'name', $context ); |
|
182 | + * Get the form name. |
|
183 | + * |
|
184 | + * @since 1.0.19 |
|
185 | + * @param string $context View or edit context. |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + public function get_name( $context = 'view' ) { |
|
189 | + return $this->get_prop( 'name', $context ); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
193 | - * Alias of self::get_name(). |
|
194 | - * |
|
195 | - * @since 1.0.19 |
|
196 | - * @param string $context View or edit context. |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function get_title( $context = 'view' ) { |
|
200 | - return $this->get_name( $context ); |
|
201 | - } |
|
193 | + * Alias of self::get_name(). |
|
194 | + * |
|
195 | + * @since 1.0.19 |
|
196 | + * @param string $context View or edit context. |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function get_title( $context = 'view' ) { |
|
200 | + return $this->get_name( $context ); |
|
201 | + } |
|
202 | 202 | |
203 | 203 | /** |
204 | - * Get the owner of the form. |
|
205 | - * |
|
206 | - * @since 1.0.19 |
|
207 | - * @param string $context View or edit context. |
|
208 | - * @return int |
|
209 | - */ |
|
210 | - public function get_author( $context = 'view' ) { |
|
211 | - return (int) $this->get_prop( 'author', $context ); |
|
204 | + * Get the owner of the form. |
|
205 | + * |
|
206 | + * @since 1.0.19 |
|
207 | + * @param string $context View or edit context. |
|
208 | + * @return int |
|
209 | + */ |
|
210 | + public function get_author( $context = 'view' ) { |
|
211 | + return (int) $this->get_prop( 'author', $context ); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | /** |
215 | - * Get the elements that make up the form. |
|
216 | - * |
|
217 | - * @since 1.0.19 |
|
218 | - * @param string $context View or edit context. |
|
219 | - * @return array |
|
220 | - */ |
|
221 | - public function get_elements( $context = 'view' ) { |
|
222 | - $elements = $this->get_prop( 'elements', $context ); |
|
215 | + * Get the elements that make up the form. |
|
216 | + * |
|
217 | + * @since 1.0.19 |
|
218 | + * @param string $context View or edit context. |
|
219 | + * @return array |
|
220 | + */ |
|
221 | + public function get_elements( $context = 'view' ) { |
|
222 | + $elements = $this->get_prop( 'elements', $context ); |
|
223 | 223 | |
224 | - if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
224 | + if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
225 | 225 | return wpinv_get_data( 'sample-payment-form' ); |
226 | - } |
|
226 | + } |
|
227 | 227 | |
228 | - // Ensure that all required elements exist. |
|
229 | - $_elements = array(); |
|
230 | - foreach ( $elements as $element ) { |
|
228 | + // Ensure that all required elements exist. |
|
229 | + $_elements = array(); |
|
230 | + foreach ( $elements as $element ) { |
|
231 | 231 | |
232 | - if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) { |
|
232 | + if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) { |
|
233 | 233 | |
234 | - $_elements[] = array( |
|
235 | - 'text' => __( 'Select Payment Method', 'invoicing' ), |
|
236 | - 'id' => 'gtscicd', |
|
237 | - 'name' => 'gtscicd', |
|
238 | - 'type' => 'gateway_select', |
|
239 | - 'premade' => true |
|
234 | + $_elements[] = array( |
|
235 | + 'text' => __( 'Select Payment Method', 'invoicing' ), |
|
236 | + 'id' => 'gtscicd', |
|
237 | + 'name' => 'gtscicd', |
|
238 | + 'type' => 'gateway_select', |
|
239 | + 'premade' => true |
|
240 | 240 | |
241 | - ); |
|
241 | + ); |
|
242 | 242 | |
243 | - } |
|
243 | + } |
|
244 | 244 | |
245 | - $_elements[] = $element; |
|
245 | + $_elements[] = $element; |
|
246 | 246 | |
247 | - } |
|
247 | + } |
|
248 | 248 | |
249 | 249 | return $_elements; |
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Get the items sold via the form. |
|
254 | - * |
|
255 | - * @since 1.0.19 |
|
256 | - * @param string $context View or edit context. |
|
257 | - * @param string $return objects or arrays. |
|
258 | - * @return GetPaid_Form_Item[] |
|
259 | - */ |
|
260 | - public function get_items( $context = 'view', $return = 'objects' ) { |
|
261 | - $items = $this->get_prop( 'items', $context ); |
|
262 | - |
|
263 | - if ( empty( $items ) || ! is_array( $items ) ) { |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Get the items sold via the form. |
|
254 | + * |
|
255 | + * @since 1.0.19 |
|
256 | + * @param string $context View or edit context. |
|
257 | + * @param string $return objects or arrays. |
|
258 | + * @return GetPaid_Form_Item[] |
|
259 | + */ |
|
260 | + public function get_items( $context = 'view', $return = 'objects' ) { |
|
261 | + $items = $this->get_prop( 'items', $context ); |
|
262 | + |
|
263 | + if ( empty( $items ) || ! is_array( $items ) ) { |
|
264 | 264 | $items = wpinv_get_data( 'sample-payment-form-items' ); |
265 | - } |
|
265 | + } |
|
266 | 266 | |
267 | - // Convert the items. |
|
268 | - $prepared = array(); |
|
267 | + // Convert the items. |
|
268 | + $prepared = array(); |
|
269 | 269 | |
270 | - foreach ( $items as $key => $value ) { |
|
270 | + foreach ( $items as $key => $value ) { |
|
271 | 271 | |
272 | - // Form items. |
|
273 | - if ( $value instanceof GetPaid_Form_Item ) { |
|
272 | + // Form items. |
|
273 | + if ( $value instanceof GetPaid_Form_Item ) { |
|
274 | 274 | |
275 | - if ( $value->can_purchase() ) { |
|
276 | - $prepared[] = $value; |
|
277 | - } |
|
275 | + if ( $value->can_purchase() ) { |
|
276 | + $prepared[] = $value; |
|
277 | + } |
|
278 | 278 | |
279 | - continue; |
|
279 | + continue; |
|
280 | 280 | |
281 | - } |
|
281 | + } |
|
282 | 282 | |
283 | - // $item_id => $quantity (buy buttons) |
|
284 | - if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
285 | - $item = new GetPaid_Form_Item( $key ); |
|
283 | + // $item_id => $quantity (buy buttons) |
|
284 | + if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
285 | + $item = new GetPaid_Form_Item( $key ); |
|
286 | 286 | |
287 | - if ( $item->can_purchase() ) { |
|
287 | + if ( $item->can_purchase() ) { |
|
288 | 288 | |
289 | - $value = (float) $value; |
|
290 | - $item->set_quantity( $value ); |
|
291 | - if ( 0 == $value ) { |
|
292 | - $item->set_quantity( 1 ); |
|
293 | - $item->set_allow_quantities( true ); |
|
294 | - } |
|
289 | + $value = (float) $value; |
|
290 | + $item->set_quantity( $value ); |
|
291 | + if ( 0 == $value ) { |
|
292 | + $item->set_quantity( 1 ); |
|
293 | + $item->set_allow_quantities( true ); |
|
294 | + } |
|
295 | 295 | |
296 | - $prepared[] = $item; |
|
297 | - } |
|
296 | + $prepared[] = $item; |
|
297 | + } |
|
298 | 298 | |
299 | - continue; |
|
300 | - } |
|
299 | + continue; |
|
300 | + } |
|
301 | 301 | |
302 | - // Items saved via payment forms editor. |
|
303 | - if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
302 | + // Items saved via payment forms editor. |
|
303 | + if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
304 | 304 | |
305 | - $item = new GetPaid_Form_Item( $value['id'] ); |
|
305 | + $item = new GetPaid_Form_Item( $value['id'] ); |
|
306 | 306 | |
307 | - if ( ! $item->can_purchase() ) { |
|
308 | - continue; |
|
309 | - } |
|
307 | + if ( ! $item->can_purchase() ) { |
|
308 | + continue; |
|
309 | + } |
|
310 | 310 | |
311 | - // Sub-total (Cart items). |
|
312 | - if ( isset( $value['subtotal'] ) ) { |
|
313 | - $item->set_price( $value['subtotal'] ); |
|
314 | - } |
|
311 | + // Sub-total (Cart items). |
|
312 | + if ( isset( $value['subtotal'] ) ) { |
|
313 | + $item->set_price( $value['subtotal'] ); |
|
314 | + } |
|
315 | 315 | |
316 | - if ( isset( $value['quantity'] ) ) { |
|
317 | - $item->set_quantity( $value['quantity'] ); |
|
318 | - } |
|
316 | + if ( isset( $value['quantity'] ) ) { |
|
317 | + $item->set_quantity( $value['quantity'] ); |
|
318 | + } |
|
319 | 319 | |
320 | - if ( isset( $value['allow_quantities'] ) ) { |
|
321 | - $item->set_allow_quantities( $value['allow_quantities'] ); |
|
322 | - } |
|
320 | + if ( isset( $value['allow_quantities'] ) ) { |
|
321 | + $item->set_allow_quantities( $value['allow_quantities'] ); |
|
322 | + } |
|
323 | 323 | |
324 | - if ( isset( $value['required'] ) ) { |
|
325 | - $item->set_is_required( $value['required'] ); |
|
326 | - } |
|
324 | + if ( isset( $value['required'] ) ) { |
|
325 | + $item->set_is_required( $value['required'] ); |
|
326 | + } |
|
327 | 327 | |
328 | - if ( isset( $value['description'] ) ) { |
|
329 | - $item->set_custom_description( $value['description'] ); |
|
330 | - } |
|
328 | + if ( isset( $value['description'] ) ) { |
|
329 | + $item->set_custom_description( $value['description'] ); |
|
330 | + } |
|
331 | 331 | |
332 | - $prepared[] = $item; |
|
333 | - continue; |
|
332 | + $prepared[] = $item; |
|
333 | + continue; |
|
334 | 334 | |
335 | - } |
|
335 | + } |
|
336 | 336 | |
337 | - // $item_id => array( 'price' => 10 ) (item variations) |
|
338 | - if ( is_numeric( $key ) && is_array( $value ) ) { |
|
339 | - $item = new GetPaid_Form_Item( $key ); |
|
337 | + // $item_id => array( 'price' => 10 ) (item variations) |
|
338 | + if ( is_numeric( $key ) && is_array( $value ) ) { |
|
339 | + $item = new GetPaid_Form_Item( $key ); |
|
340 | 340 | |
341 | - if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) { |
|
342 | - $item->set_price( $value['price'] ); |
|
343 | - } |
|
344 | - |
|
345 | - if ( $item->can_purchase() ) { |
|
346 | - $prepared[] = $item; |
|
347 | - } |
|
348 | - |
|
349 | - continue; |
|
350 | - } |
|
351 | - |
|
352 | - } |
|
353 | - |
|
354 | - if ( 'objects' == $return && 'view' == $context ) { |
|
355 | - return $prepared; |
|
356 | - } |
|
357 | - |
|
358 | - $items = array(); |
|
359 | - foreach ( $prepared as $item ) { |
|
360 | - $items[] = $item->prepare_data_for_use(); |
|
361 | - } |
|
362 | - |
|
363 | - return $items; |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Get a single item belonging to the form. |
|
368 | - * |
|
369 | - * @since 1.0.19 |
|
370 | - * @param int $item_id The item id to return. |
|
371 | - * @return GetPaid_Form_Item|bool |
|
372 | - */ |
|
373 | - public function get_item( $item_id ) { |
|
374 | - |
|
375 | - if ( empty( $item_id ) || ! is_numeric( $item_id ) ) { |
|
376 | - return false; |
|
377 | - } |
|
378 | - |
|
379 | - foreach( $this->get_items() as $item ) { |
|
380 | - if ( $item->get_id() == (int) $item_id ) { |
|
381 | - return $item; |
|
382 | - } |
|
383 | - } |
|
384 | - |
|
385 | - return false; |
|
386 | - |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Gets a single element. |
|
391 | - * |
|
392 | - * @since 1.0.19 |
|
393 | - * @param string $element_type The element type to return. |
|
394 | - * @return array|bool |
|
395 | - */ |
|
396 | - public function get_element_type( $element_type ) { |
|
397 | - |
|
398 | - if ( empty( $element_type ) || ! is_scalar( $element_type ) ) { |
|
399 | - return false; |
|
400 | - } |
|
401 | - |
|
402 | - foreach ( $this->get_prop( 'elements' ) as $element ) { |
|
403 | - |
|
404 | - if ( $element['type'] == $element_type ) { |
|
405 | - return $element; |
|
406 | - } |
|
407 | - |
|
408 | - } |
|
409 | - |
|
410 | - return false; |
|
411 | - |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Get the total amount earned via this form. |
|
416 | - * |
|
417 | - * @since 1.0.19 |
|
418 | - * @param string $context View or edit context. |
|
419 | - * @return float |
|
420 | - */ |
|
421 | - public function get_earned( $context = 'view' ) { |
|
422 | - return $this->get_prop( 'earned', $context ); |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * Get the total amount refunded via this form. |
|
427 | - * |
|
428 | - * @since 1.0.19 |
|
429 | - * @param string $context View or edit context. |
|
430 | - * @return float |
|
431 | - */ |
|
432 | - public function get_refunded( $context = 'view' ) { |
|
433 | - return $this->get_prop( 'refunded', $context ); |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * Get the total amount cancelled via this form. |
|
438 | - * |
|
439 | - * @since 1.0.19 |
|
440 | - * @param string $context View or edit context. |
|
441 | - * @return float |
|
442 | - */ |
|
443 | - public function get_cancelled( $context = 'view' ) { |
|
444 | - return $this->get_prop( 'cancelled', $context ); |
|
445 | - } |
|
446 | - |
|
447 | - /** |
|
448 | - * Get the total amount failed via this form. |
|
449 | - * |
|
450 | - * @since 1.0.19 |
|
451 | - * @param string $context View or edit context. |
|
452 | - * @return float |
|
453 | - */ |
|
454 | - public function get_failed( $context = 'view' ) { |
|
455 | - return $this->get_prop( 'failed', $context ); |
|
456 | - } |
|
457 | - |
|
458 | - /** |
|
459 | - * Get the currency. |
|
460 | - * |
|
461 | - * @since 1.0.19 |
|
462 | - * @param string $context View or edit context. |
|
463 | - * @return string |
|
464 | - */ |
|
465 | - public function get_currency() { |
|
466 | - $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency(); |
|
467 | - return apply_filters( 'getpaid-payment-form-currency', $currency, $this ); |
|
468 | - } |
|
341 | + if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) { |
|
342 | + $item->set_price( $value['price'] ); |
|
343 | + } |
|
344 | + |
|
345 | + if ( $item->can_purchase() ) { |
|
346 | + $prepared[] = $item; |
|
347 | + } |
|
348 | + |
|
349 | + continue; |
|
350 | + } |
|
351 | + |
|
352 | + } |
|
353 | + |
|
354 | + if ( 'objects' == $return && 'view' == $context ) { |
|
355 | + return $prepared; |
|
356 | + } |
|
357 | + |
|
358 | + $items = array(); |
|
359 | + foreach ( $prepared as $item ) { |
|
360 | + $items[] = $item->prepare_data_for_use(); |
|
361 | + } |
|
362 | + |
|
363 | + return $items; |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Get a single item belonging to the form. |
|
368 | + * |
|
369 | + * @since 1.0.19 |
|
370 | + * @param int $item_id The item id to return. |
|
371 | + * @return GetPaid_Form_Item|bool |
|
372 | + */ |
|
373 | + public function get_item( $item_id ) { |
|
374 | + |
|
375 | + if ( empty( $item_id ) || ! is_numeric( $item_id ) ) { |
|
376 | + return false; |
|
377 | + } |
|
378 | + |
|
379 | + foreach( $this->get_items() as $item ) { |
|
380 | + if ( $item->get_id() == (int) $item_id ) { |
|
381 | + return $item; |
|
382 | + } |
|
383 | + } |
|
384 | + |
|
385 | + return false; |
|
386 | + |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Gets a single element. |
|
391 | + * |
|
392 | + * @since 1.0.19 |
|
393 | + * @param string $element_type The element type to return. |
|
394 | + * @return array|bool |
|
395 | + */ |
|
396 | + public function get_element_type( $element_type ) { |
|
397 | + |
|
398 | + if ( empty( $element_type ) || ! is_scalar( $element_type ) ) { |
|
399 | + return false; |
|
400 | + } |
|
401 | + |
|
402 | + foreach ( $this->get_prop( 'elements' ) as $element ) { |
|
403 | + |
|
404 | + if ( $element['type'] == $element_type ) { |
|
405 | + return $element; |
|
406 | + } |
|
407 | + |
|
408 | + } |
|
409 | + |
|
410 | + return false; |
|
411 | + |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Get the total amount earned via this form. |
|
416 | + * |
|
417 | + * @since 1.0.19 |
|
418 | + * @param string $context View or edit context. |
|
419 | + * @return float |
|
420 | + */ |
|
421 | + public function get_earned( $context = 'view' ) { |
|
422 | + return $this->get_prop( 'earned', $context ); |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * Get the total amount refunded via this form. |
|
427 | + * |
|
428 | + * @since 1.0.19 |
|
429 | + * @param string $context View or edit context. |
|
430 | + * @return float |
|
431 | + */ |
|
432 | + public function get_refunded( $context = 'view' ) { |
|
433 | + return $this->get_prop( 'refunded', $context ); |
|
434 | + } |
|
435 | + |
|
436 | + /** |
|
437 | + * Get the total amount cancelled via this form. |
|
438 | + * |
|
439 | + * @since 1.0.19 |
|
440 | + * @param string $context View or edit context. |
|
441 | + * @return float |
|
442 | + */ |
|
443 | + public function get_cancelled( $context = 'view' ) { |
|
444 | + return $this->get_prop( 'cancelled', $context ); |
|
445 | + } |
|
446 | + |
|
447 | + /** |
|
448 | + * Get the total amount failed via this form. |
|
449 | + * |
|
450 | + * @since 1.0.19 |
|
451 | + * @param string $context View or edit context. |
|
452 | + * @return float |
|
453 | + */ |
|
454 | + public function get_failed( $context = 'view' ) { |
|
455 | + return $this->get_prop( 'failed', $context ); |
|
456 | + } |
|
457 | + |
|
458 | + /** |
|
459 | + * Get the currency. |
|
460 | + * |
|
461 | + * @since 1.0.19 |
|
462 | + * @param string $context View or edit context. |
|
463 | + * @return string |
|
464 | + */ |
|
465 | + public function get_currency() { |
|
466 | + $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency(); |
|
467 | + return apply_filters( 'getpaid-payment-form-currency', $currency, $this ); |
|
468 | + } |
|
469 | 469 | |
470 | 470 | /* |
471 | 471 | |-------------------------------------------------------------------------- |
@@ -478,22 +478,22 @@ discard block |
||
478 | 478 | */ |
479 | 479 | |
480 | 480 | /** |
481 | - * Set plugin version when the item was created. |
|
482 | - * |
|
483 | - * @since 1.0.19 |
|
484 | - */ |
|
485 | - public function set_version( $value ) { |
|
486 | - $this->set_prop( 'version', $value ); |
|
481 | + * Set plugin version when the item was created. |
|
482 | + * |
|
483 | + * @since 1.0.19 |
|
484 | + */ |
|
485 | + public function set_version( $value ) { |
|
486 | + $this->set_prop( 'version', $value ); |
|
487 | 487 | } |
488 | 488 | |
489 | 489 | /** |
490 | - * Set date when the item was created. |
|
491 | - * |
|
492 | - * @since 1.0.19 |
|
493 | - * @param string $value Value to set. |
|
490 | + * Set date when the item was created. |
|
491 | + * |
|
492 | + * @since 1.0.19 |
|
493 | + * @param string $value Value to set. |
|
494 | 494 | * @return bool Whether or not the date was set. |
495 | - */ |
|
496 | - public function set_date_created( $value ) { |
|
495 | + */ |
|
496 | + public function set_date_created( $value ) { |
|
497 | 497 | $date = strtotime( $value ); |
498 | 498 | |
499 | 499 | if ( $date ) { |
@@ -506,13 +506,13 @@ discard block |
||
506 | 506 | } |
507 | 507 | |
508 | 508 | /** |
509 | - * Set date when the item was last modified. |
|
510 | - * |
|
511 | - * @since 1.0.19 |
|
512 | - * @param string $value Value to set. |
|
509 | + * Set date when the item was last modified. |
|
510 | + * |
|
511 | + * @since 1.0.19 |
|
512 | + * @param string $value Value to set. |
|
513 | 513 | * @return bool Whether or not the date was set. |
514 | - */ |
|
515 | - public function set_date_modified( $value ) { |
|
514 | + */ |
|
515 | + public function set_date_modified( $value ) { |
|
516 | 516 | $date = strtotime( $value ); |
517 | 517 | |
518 | 518 | if ( $date ) { |
@@ -525,165 +525,165 @@ discard block |
||
525 | 525 | } |
526 | 526 | |
527 | 527 | /** |
528 | - * Set the item name. |
|
529 | - * |
|
530 | - * @since 1.0.19 |
|
531 | - * @param string $value New name. |
|
532 | - */ |
|
533 | - public function set_name( $value ) { |
|
534 | - $this->set_prop( 'name', sanitize_text_field( $value ) ); |
|
535 | - } |
|
536 | - |
|
537 | - /** |
|
538 | - * Alias of self::set_name(). |
|
539 | - * |
|
540 | - * @since 1.0.19 |
|
541 | - * @param string $value New name. |
|
542 | - */ |
|
543 | - public function set_title( $value ) { |
|
544 | - $this->set_name( $value ); |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Set the owner of the item. |
|
549 | - * |
|
550 | - * @since 1.0.19 |
|
551 | - * @param int $value New author. |
|
552 | - */ |
|
553 | - public function set_author( $value ) { |
|
554 | - $this->set_prop( 'author', (int) $value ); |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * Set the form elements. |
|
559 | - * |
|
560 | - * @since 1.0.19 |
|
561 | - * @sinve 2.3.4 Array values sanitized. |
|
562 | - * @param array $value Form elements. |
|
563 | - */ |
|
564 | - public function set_elements( $value ) { |
|
565 | - if ( is_array( $value ) ) { |
|
566 | - $this->set_prop( 'elements', wp_kses_post_deep( $value ) ); |
|
567 | - } |
|
568 | - } |
|
569 | - |
|
570 | - /** |
|
571 | - * Sanitize array values. |
|
572 | - * |
|
573 | - * @param $value |
|
574 | - * |
|
575 | - * @return mixed |
|
576 | - */ |
|
577 | - public function sanitize_array_values($value){ |
|
578 | - |
|
579 | - // sanitize |
|
580 | - if(!empty($value )){ |
|
581 | - |
|
582 | - foreach($value as $key => $val_arr){ |
|
583 | - |
|
584 | - if(is_array($val_arr)){ |
|
585 | - // check if we have sub array items. |
|
586 | - $sub_arr = array(); |
|
587 | - foreach($val_arr as $key2 => $val2){ |
|
588 | - if(is_array($val2)){ |
|
589 | - $sub_arr[$key2] = $this->sanitize_array_values($val2); |
|
590 | - unset($val_arr[$key][$key2]); |
|
591 | - } |
|
592 | - } |
|
593 | - |
|
594 | - // we allow some html in description so we sanitize it separately. |
|
595 | - $help_text = !empty($val_arr['description']) ? wp_kses_post($val_arr['description']) : ''; |
|
596 | - |
|
597 | - // sanitize array elements |
|
598 | - $value[$key] = array_map( 'sanitize_text_field', $val_arr ); |
|
599 | - |
|
600 | - // add back the description if set |
|
601 | - if(isset($val_arr['description'])){ $value[$key]['description'] = $help_text;} |
|
602 | - |
|
603 | - // add back sub array items after its been sanitized. |
|
604 | - if ( ! empty( $sub_arr ) ) { |
|
605 | - $value[$key] = array_merge($value[$key],$sub_arr); |
|
606 | - } |
|
607 | - } |
|
608 | - |
|
609 | - } |
|
610 | - |
|
611 | - } |
|
612 | - |
|
613 | - return $value; |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Set the form items. |
|
618 | - * |
|
619 | - * @since 1.0.19 |
|
620 | - * @param array $value Form elements. |
|
621 | - */ |
|
622 | - public function set_items( $value ) { |
|
623 | - if ( is_array( $value ) ) { |
|
624 | - $this->set_prop( 'items', $value ); |
|
625 | - } |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Set the total amount earned via this form. |
|
630 | - * |
|
631 | - * @since 1.0.19 |
|
632 | - * @param float $value Amount earned. |
|
633 | - */ |
|
634 | - public function set_earned( $value ) { |
|
635 | - $value = max( (float) $value, 0 ); |
|
636 | - $this->set_prop( 'earned', $value ); |
|
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * Set the total amount refunded via this form. |
|
641 | - * |
|
642 | - * @since 1.0.19 |
|
643 | - * @param float $value Amount refunded. |
|
644 | - */ |
|
645 | - public function set_refunded( $value ) { |
|
646 | - $value = max( (float) $value, 0 ); |
|
647 | - $this->set_prop( 'refunded', $value ); |
|
648 | - } |
|
649 | - |
|
650 | - /** |
|
651 | - * Set the total amount cancelled via this form. |
|
652 | - * |
|
653 | - * @since 1.0.19 |
|
654 | - * @param float $value Amount cancelled. |
|
655 | - */ |
|
656 | - public function set_cancelled( $value ) { |
|
657 | - $value = max( (float) $value, 0 ); |
|
658 | - $this->set_prop( 'cancelled', $value ); |
|
659 | - } |
|
660 | - |
|
661 | - /** |
|
662 | - * Set the total amount failed via this form. |
|
663 | - * |
|
664 | - * @since 1.0.19 |
|
665 | - * @param float $value Amount cancelled. |
|
666 | - */ |
|
667 | - public function set_failed( $value ) { |
|
668 | - $value = max( (float) $value, 0 ); |
|
669 | - $this->set_prop( 'failed', $value ); |
|
670 | - } |
|
528 | + * Set the item name. |
|
529 | + * |
|
530 | + * @since 1.0.19 |
|
531 | + * @param string $value New name. |
|
532 | + */ |
|
533 | + public function set_name( $value ) { |
|
534 | + $this->set_prop( 'name', sanitize_text_field( $value ) ); |
|
535 | + } |
|
536 | + |
|
537 | + /** |
|
538 | + * Alias of self::set_name(). |
|
539 | + * |
|
540 | + * @since 1.0.19 |
|
541 | + * @param string $value New name. |
|
542 | + */ |
|
543 | + public function set_title( $value ) { |
|
544 | + $this->set_name( $value ); |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Set the owner of the item. |
|
549 | + * |
|
550 | + * @since 1.0.19 |
|
551 | + * @param int $value New author. |
|
552 | + */ |
|
553 | + public function set_author( $value ) { |
|
554 | + $this->set_prop( 'author', (int) $value ); |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Set the form elements. |
|
559 | + * |
|
560 | + * @since 1.0.19 |
|
561 | + * @sinve 2.3.4 Array values sanitized. |
|
562 | + * @param array $value Form elements. |
|
563 | + */ |
|
564 | + public function set_elements( $value ) { |
|
565 | + if ( is_array( $value ) ) { |
|
566 | + $this->set_prop( 'elements', wp_kses_post_deep( $value ) ); |
|
567 | + } |
|
568 | + } |
|
569 | + |
|
570 | + /** |
|
571 | + * Sanitize array values. |
|
572 | + * |
|
573 | + * @param $value |
|
574 | + * |
|
575 | + * @return mixed |
|
576 | + */ |
|
577 | + public function sanitize_array_values($value){ |
|
578 | + |
|
579 | + // sanitize |
|
580 | + if(!empty($value )){ |
|
581 | + |
|
582 | + foreach($value as $key => $val_arr){ |
|
583 | + |
|
584 | + if(is_array($val_arr)){ |
|
585 | + // check if we have sub array items. |
|
586 | + $sub_arr = array(); |
|
587 | + foreach($val_arr as $key2 => $val2){ |
|
588 | + if(is_array($val2)){ |
|
589 | + $sub_arr[$key2] = $this->sanitize_array_values($val2); |
|
590 | + unset($val_arr[$key][$key2]); |
|
591 | + } |
|
592 | + } |
|
593 | + |
|
594 | + // we allow some html in description so we sanitize it separately. |
|
595 | + $help_text = !empty($val_arr['description']) ? wp_kses_post($val_arr['description']) : ''; |
|
596 | + |
|
597 | + // sanitize array elements |
|
598 | + $value[$key] = array_map( 'sanitize_text_field', $val_arr ); |
|
599 | + |
|
600 | + // add back the description if set |
|
601 | + if(isset($val_arr['description'])){ $value[$key]['description'] = $help_text;} |
|
602 | + |
|
603 | + // add back sub array items after its been sanitized. |
|
604 | + if ( ! empty( $sub_arr ) ) { |
|
605 | + $value[$key] = array_merge($value[$key],$sub_arr); |
|
606 | + } |
|
607 | + } |
|
608 | + |
|
609 | + } |
|
610 | + |
|
611 | + } |
|
612 | + |
|
613 | + return $value; |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Set the form items. |
|
618 | + * |
|
619 | + * @since 1.0.19 |
|
620 | + * @param array $value Form elements. |
|
621 | + */ |
|
622 | + public function set_items( $value ) { |
|
623 | + if ( is_array( $value ) ) { |
|
624 | + $this->set_prop( 'items', $value ); |
|
625 | + } |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Set the total amount earned via this form. |
|
630 | + * |
|
631 | + * @since 1.0.19 |
|
632 | + * @param float $value Amount earned. |
|
633 | + */ |
|
634 | + public function set_earned( $value ) { |
|
635 | + $value = max( (float) $value, 0 ); |
|
636 | + $this->set_prop( 'earned', $value ); |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * Set the total amount refunded via this form. |
|
641 | + * |
|
642 | + * @since 1.0.19 |
|
643 | + * @param float $value Amount refunded. |
|
644 | + */ |
|
645 | + public function set_refunded( $value ) { |
|
646 | + $value = max( (float) $value, 0 ); |
|
647 | + $this->set_prop( 'refunded', $value ); |
|
648 | + } |
|
649 | + |
|
650 | + /** |
|
651 | + * Set the total amount cancelled via this form. |
|
652 | + * |
|
653 | + * @since 1.0.19 |
|
654 | + * @param float $value Amount cancelled. |
|
655 | + */ |
|
656 | + public function set_cancelled( $value ) { |
|
657 | + $value = max( (float) $value, 0 ); |
|
658 | + $this->set_prop( 'cancelled', $value ); |
|
659 | + } |
|
660 | + |
|
661 | + /** |
|
662 | + * Set the total amount failed via this form. |
|
663 | + * |
|
664 | + * @since 1.0.19 |
|
665 | + * @param float $value Amount cancelled. |
|
666 | + */ |
|
667 | + public function set_failed( $value ) { |
|
668 | + $value = max( (float) $value, 0 ); |
|
669 | + $this->set_prop( 'failed', $value ); |
|
670 | + } |
|
671 | 671 | |
672 | 672 | /** |
673 | 673 | * Create an item. For backwards compatibilty. |
674 | 674 | * |
675 | 675 | * @deprecated |
676 | - * @return int item id |
|
676 | + * @return int item id |
|
677 | 677 | */ |
678 | 678 | public function create( $data = array() ) { |
679 | 679 | |
680 | - // Set the properties. |
|
681 | - if ( is_array( $data ) ) { |
|
682 | - $this->set_props( $data ); |
|
683 | - } |
|
680 | + // Set the properties. |
|
681 | + if ( is_array( $data ) ) { |
|
682 | + $this->set_props( $data ); |
|
683 | + } |
|
684 | 684 | |
685 | - // Save the item. |
|
686 | - return $this->save(); |
|
685 | + // Save the item. |
|
686 | + return $this->save(); |
|
687 | 687 | |
688 | 688 | } |
689 | 689 | |
@@ -691,7 +691,7 @@ discard block |
||
691 | 691 | * Updates an item. For backwards compatibilty. |
692 | 692 | * |
693 | 693 | * @deprecated |
694 | - * @return int item id |
|
694 | + * @return int item id |
|
695 | 695 | */ |
696 | 696 | public function update( $data = array() ) { |
697 | 697 | return $this->create( $data ); |
@@ -707,22 +707,22 @@ discard block |
||
707 | 707 | */ |
708 | 708 | |
709 | 709 | /** |
710 | - * Checks whether this is the default payment form. |
|
711 | - * |
|
712 | - * @since 1.0.19 |
|
713 | - * @return bool |
|
714 | - */ |
|
710 | + * Checks whether this is the default payment form. |
|
711 | + * |
|
712 | + * @since 1.0.19 |
|
713 | + * @return bool |
|
714 | + */ |
|
715 | 715 | public function is_default() { |
716 | 716 | $is_default = $this->get_id() == wpinv_get_default_payment_form(); |
717 | 717 | return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->get_id(), $this ); |
718 | - } |
|
718 | + } |
|
719 | 719 | |
720 | 720 | /** |
721 | - * Checks whether the form is active. |
|
722 | - * |
|
723 | - * @since 1.0.19 |
|
724 | - * @return bool |
|
725 | - */ |
|
721 | + * Checks whether the form is active. |
|
722 | + * |
|
723 | + * @since 1.0.19 |
|
724 | + * @return bool |
|
725 | + */ |
|
726 | 726 | public function is_active() { |
727 | 727 | $is_active = 0 !== (int) $this->get_id(); |
728 | 728 | |
@@ -731,76 +731,76 @@ discard block |
||
731 | 731 | } |
732 | 732 | |
733 | 733 | return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this ); |
734 | - } |
|
735 | - |
|
736 | - /** |
|
737 | - * Checks whether the form has a given item. |
|
738 | - * |
|
739 | - * @since 1.0.19 |
|
740 | - * @return bool |
|
741 | - */ |
|
734 | + } |
|
735 | + |
|
736 | + /** |
|
737 | + * Checks whether the form has a given item. |
|
738 | + * |
|
739 | + * @since 1.0.19 |
|
740 | + * @return bool |
|
741 | + */ |
|
742 | 742 | public function has_item( $item_id ) { |
743 | 743 | return false !== $this->get_item( $item_id ); |
744 | - } |
|
745 | - |
|
746 | - /** |
|
747 | - * Checks whether the form has a given element. |
|
748 | - * |
|
749 | - * @since 1.0.19 |
|
750 | - * @return bool |
|
751 | - */ |
|
744 | + } |
|
745 | + |
|
746 | + /** |
|
747 | + * Checks whether the form has a given element. |
|
748 | + * |
|
749 | + * @since 1.0.19 |
|
750 | + * @return bool |
|
751 | + */ |
|
752 | 752 | public function has_element_type( $element_type ) { |
753 | 753 | return false !== $this->get_element_type( $element_type ); |
754 | - } |
|
755 | - |
|
756 | - /** |
|
757 | - * Checks whether this form is recurring or not. |
|
758 | - * |
|
759 | - * @since 1.0.19 |
|
760 | - * @return bool |
|
761 | - */ |
|
754 | + } |
|
755 | + |
|
756 | + /** |
|
757 | + * Checks whether this form is recurring or not. |
|
758 | + * |
|
759 | + * @since 1.0.19 |
|
760 | + * @return bool |
|
761 | + */ |
|
762 | 762 | public function is_recurring() { |
763 | 763 | |
764 | - if ( ! empty( $this->invoice ) ) { |
|
765 | - return $this->invoice->is_recurring(); |
|
766 | - } |
|
764 | + if ( ! empty( $this->invoice ) ) { |
|
765 | + return $this->invoice->is_recurring(); |
|
766 | + } |
|
767 | 767 | |
768 | - foreach ( $this->get_items() as $item ) { |
|
768 | + foreach ( $this->get_items() as $item ) { |
|
769 | 769 | |
770 | - if ( $item->is_recurring() ) { |
|
771 | - return true; |
|
772 | - } |
|
770 | + if ( $item->is_recurring() ) { |
|
771 | + return true; |
|
772 | + } |
|
773 | 773 | |
774 | - } |
|
774 | + } |
|
775 | 775 | |
776 | 776 | return false; |
777 | - } |
|
777 | + } |
|
778 | 778 | |
779 | - /** |
|
780 | - * Retrieves the form's html. |
|
781 | - * |
|
782 | - * @since 1.0.19 |
|
783 | - */ |
|
779 | + /** |
|
780 | + * Retrieves the form's html. |
|
781 | + * |
|
782 | + * @since 1.0.19 |
|
783 | + */ |
|
784 | 784 | public function get_html( $extra_markup = '' ) { |
785 | 785 | |
786 | - // Return the HTML. |
|
787 | - return wpinv_get_template_html( |
|
788 | - 'payment-forms/form.php', |
|
789 | - array( |
|
790 | - 'form' => $this, |
|
791 | - 'extra_markup' => $extra_markup, |
|
792 | - ) |
|
793 | - ); |
|
794 | - |
|
795 | - } |
|
796 | - |
|
797 | - /** |
|
798 | - * Displays the payment form. |
|
799 | - * |
|
800 | - * @since 1.0.19 |
|
801 | - */ |
|
786 | + // Return the HTML. |
|
787 | + return wpinv_get_template_html( |
|
788 | + 'payment-forms/form.php', |
|
789 | + array( |
|
790 | + 'form' => $this, |
|
791 | + 'extra_markup' => $extra_markup, |
|
792 | + ) |
|
793 | + ); |
|
794 | + |
|
795 | + } |
|
796 | + |
|
797 | + /** |
|
798 | + * Displays the payment form. |
|
799 | + * |
|
800 | + * @since 1.0.19 |
|
801 | + */ |
|
802 | 802 | public function display( $extra_markup = '' ) { |
803 | - echo $this->get_html( $extra_markup ); |
|
803 | + echo $this->get_html( $extra_markup ); |
|
804 | 804 | } |
805 | 805 | |
806 | 806 | } |
@@ -13,96 +13,96 @@ discard block |
||
13 | 13 | class GetPaid_Paypal_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 = 'paypal'; |
21 | 21 | |
22 | 22 | /** |
23 | - * An array of features that this gateway supports. |
|
24 | - * |
|
25 | - * @var array |
|
26 | - */ |
|
23 | + * An array of features that this gateway supports. |
|
24 | + * |
|
25 | + * @var array |
|
26 | + */ |
|
27 | 27 | protected $supports = array( 'subscription', 'sandbox', 'single_subscription_group' ); |
28 | 28 | |
29 | 29 | /** |
30 | - * Payment method order. |
|
31 | - * |
|
32 | - * @var int |
|
33 | - */ |
|
30 | + * Payment method order. |
|
31 | + * |
|
32 | + * @var int |
|
33 | + */ |
|
34 | 34 | public $order = 1; |
35 | 35 | |
36 | 36 | /** |
37 | - * Stores line items to send to PayPal. |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
37 | + * Stores line items to send to PayPal. |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | 41 | protected $line_items = array(); |
42 | 42 | |
43 | 43 | /** |
44 | - * Endpoint for requests from PayPal. |
|
45 | - * |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $notify_url; |
|
49 | - |
|
50 | - /** |
|
51 | - * Endpoint for requests to PayPal. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
44 | + * Endpoint for requests from PayPal. |
|
45 | + * |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $notify_url; |
|
49 | + |
|
50 | + /** |
|
51 | + * Endpoint for requests to PayPal. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | 55 | protected $endpoint; |
56 | 56 | |
57 | 57 | /** |
58 | - * Currencies this gateway is allowed for. |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' ); |
|
58 | + * Currencies this gateway is allowed for. |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' ); |
|
63 | 63 | |
64 | 64 | /** |
65 | - * URL to view a transaction. |
|
66 | - * |
|
67 | - * @var string |
|
68 | - */ |
|
65 | + * URL to view a transaction. |
|
66 | + * |
|
67 | + * @var string |
|
68 | + */ |
|
69 | 69 | public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s'; |
70 | 70 | |
71 | 71 | /** |
72 | - * URL to view a subscription. |
|
73 | - * |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
72 | + * URL to view a subscription. |
|
73 | + * |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
77 | 77 | |
78 | 78 | /** |
79 | - * Class constructor. |
|
80 | - */ |
|
81 | - public function __construct() { |
|
79 | + * Class constructor. |
|
80 | + */ |
|
81 | + public function __construct() { |
|
82 | 82 | |
83 | 83 | $this->title = __( 'PayPal Standard', 'invoicing' ); |
84 | 84 | $this->method_title = __( 'PayPal Standard', 'invoicing' ); |
85 | 85 | $this->checkout_button_text = __( 'Proceed to PayPal', 'invoicing' ); |
86 | 86 | $this->notify_url = wpinv_get_ipn_url( $this->id ); |
87 | 87 | |
88 | - add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 ); |
|
88 | + add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 ); |
|
89 | 89 | add_filter( 'getpaid_paypal_sandbox_notice', array( $this, 'sandbox_notice' ) ); |
90 | - add_filter( 'getpaid_get_paypal_connect_url', array( $this, 'maybe_get_connect_url' ), 10, 2 ); |
|
91 | - add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) ); |
|
90 | + add_filter( 'getpaid_get_paypal_connect_url', array( $this, 'maybe_get_connect_url' ), 10, 2 ); |
|
91 | + add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) ); |
|
92 | 92 | |
93 | 93 | parent::__construct(); |
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
97 | - * Process Payment. |
|
98 | - * |
|
99 | - * |
|
100 | - * @param WPInv_Invoice $invoice Invoice. |
|
101 | - * @param array $submission_data Posted checkout fields. |
|
102 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
103 | - * @return array |
|
104 | - */ |
|
105 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
97 | + * Process Payment. |
|
98 | + * |
|
99 | + * |
|
100 | + * @param WPInv_Invoice $invoice Invoice. |
|
101 | + * @param array $submission_data Posted checkout fields. |
|
102 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
103 | + * @return array |
|
104 | + */ |
|
105 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
106 | 106 | |
107 | 107 | // Get redirect url. |
108 | 108 | $paypal_redirect = $this->get_request_url( $invoice ); |
@@ -125,15 +125,15 @@ discard block |
||
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
128 | - * Get the PayPal request URL for an invoice. |
|
129 | - * |
|
130 | - * @param WPInv_Invoice $invoice Invoice object. |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - public function get_request_url( $invoice ) { |
|
128 | + * Get the PayPal request URL for an invoice. |
|
129 | + * |
|
130 | + * @param WPInv_Invoice $invoice Invoice object. |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + public function get_request_url( $invoice ) { |
|
134 | 134 | |
135 | 135 | // Endpoint for this request |
136 | - $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?'; |
|
136 | + $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?'; |
|
137 | 137 | |
138 | 138 | // Retrieve paypal args. |
139 | 139 | $paypal_args = map_deep( $this->get_paypal_args( $invoice ), 'urlencode' ); |
@@ -146,45 +146,45 @@ discard block |
||
146 | 146 | |
147 | 147 | return add_query_arg( $paypal_args, $this->endpoint ); |
148 | 148 | |
149 | - } |
|
149 | + } |
|
150 | 150 | |
151 | 151 | /** |
152 | - * Get PayPal Args for passing to PP. |
|
153 | - * |
|
154 | - * @param WPInv_Invoice $invoice Invoice object. |
|
155 | - * @return array |
|
156 | - */ |
|
157 | - protected function get_paypal_args( $invoice ) { |
|
152 | + * Get PayPal Args for passing to PP. |
|
153 | + * |
|
154 | + * @param WPInv_Invoice $invoice Invoice object. |
|
155 | + * @return array |
|
156 | + */ |
|
157 | + protected function get_paypal_args( $invoice ) { |
|
158 | 158 | |
159 | 159 | // Whether or not to send the line items as one item. |
160 | - $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice ); |
|
161 | - |
|
162 | - if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) { |
|
163 | - $force_one_line_item = true; |
|
164 | - } |
|
165 | - |
|
166 | - $paypal_args = apply_filters( |
|
167 | - 'getpaid_paypal_args', |
|
168 | - array_merge( |
|
169 | - $this->get_transaction_args( $invoice ), |
|
170 | - $this->get_line_item_args( $invoice, $force_one_line_item ) |
|
171 | - ), |
|
172 | - $invoice |
|
173 | - ); |
|
174 | - |
|
175 | - return $this->fix_request_length( $invoice, $paypal_args ); |
|
160 | + $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice ); |
|
161 | + |
|
162 | + if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) { |
|
163 | + $force_one_line_item = true; |
|
164 | + } |
|
165 | + |
|
166 | + $paypal_args = apply_filters( |
|
167 | + 'getpaid_paypal_args', |
|
168 | + array_merge( |
|
169 | + $this->get_transaction_args( $invoice ), |
|
170 | + $this->get_line_item_args( $invoice, $force_one_line_item ) |
|
171 | + ), |
|
172 | + $invoice |
|
173 | + ); |
|
174 | + |
|
175 | + return $this->fix_request_length( $invoice, $paypal_args ); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | /** |
179 | - * Get transaction args for paypal request. |
|
180 | - * |
|
181 | - * @param WPInv_Invoice $invoice Invoice object. |
|
182 | - * @return array |
|
183 | - */ |
|
184 | - protected function get_transaction_args( $invoice ) { |
|
185 | - |
|
186 | - $email = $this->is_sandbox( $invoice ) ? wpinv_get_option( 'paypal_sandbox_email', wpinv_get_option( 'paypal_email', '' ) ) : wpinv_get_option( 'paypal_email', '' ); |
|
187 | - return array( |
|
179 | + * Get transaction args for paypal request. |
|
180 | + * |
|
181 | + * @param WPInv_Invoice $invoice Invoice object. |
|
182 | + * @return array |
|
183 | + */ |
|
184 | + protected function get_transaction_args( $invoice ) { |
|
185 | + |
|
186 | + $email = $this->is_sandbox( $invoice ) ? wpinv_get_option( 'paypal_sandbox_email', wpinv_get_option( 'paypal_email', '' ) ) : wpinv_get_option( 'paypal_email', '' ); |
|
187 | + return array( |
|
188 | 188 | 'cmd' => '_cart', |
189 | 189 | 'business' => $email, |
190 | 190 | 'no_shipping' => '1', |
@@ -209,16 +209,16 @@ discard block |
||
209 | 209 | } |
210 | 210 | |
211 | 211 | /** |
212 | - * Get line item args for paypal request. |
|
213 | - * |
|
214 | - * @param WPInv_Invoice $invoice Invoice object. |
|
215 | - * @param bool $force_one_line_item Create only one item for this invoice. |
|
216 | - * @return array |
|
217 | - */ |
|
218 | - protected function get_line_item_args( $invoice, $force_one_line_item = false ) { |
|
212 | + * Get line item args for paypal request. |
|
213 | + * |
|
214 | + * @param WPInv_Invoice $invoice Invoice object. |
|
215 | + * @param bool $force_one_line_item Create only one item for this invoice. |
|
216 | + * @return array |
|
217 | + */ |
|
218 | + protected function get_line_item_args( $invoice, $force_one_line_item = false ) { |
|
219 | 219 | |
220 | 220 | // Maybe send invoice as a single item. |
221 | - if ( $force_one_line_item ) { |
|
221 | + if ( $force_one_line_item ) { |
|
222 | 222 | return $this->get_line_item_args_single_item( $invoice ); |
223 | 223 | } |
224 | 224 | |
@@ -238,129 +238,129 @@ discard block |
||
238 | 238 | $line_item_args['discount_amount_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_discount(), 2 ); |
239 | 239 | } |
240 | 240 | |
241 | - return array_merge( $line_item_args, $this->get_line_items() ); |
|
241 | + return array_merge( $line_item_args, $this->get_line_items() ); |
|
242 | 242 | |
243 | 243 | } |
244 | 244 | |
245 | 245 | /** |
246 | - * Get line item args for paypal request as a single line item. |
|
247 | - * |
|
248 | - * @param WPInv_Invoice $invoice Invoice object. |
|
249 | - * @return array |
|
250 | - */ |
|
251 | - protected function get_line_item_args_single_item( $invoice ) { |
|
252 | - $this->delete_line_items(); |
|
246 | + * Get line item args for paypal request as a single line item. |
|
247 | + * |
|
248 | + * @param WPInv_Invoice $invoice Invoice object. |
|
249 | + * @return array |
|
250 | + */ |
|
251 | + protected function get_line_item_args_single_item( $invoice ) { |
|
252 | + $this->delete_line_items(); |
|
253 | 253 | |
254 | 254 | $item_name = sprintf( __( 'Invoice #%s', 'invoicing' ), $invoice->get_number() ); |
255 | - $this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() ); |
|
255 | + $this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() ); |
|
256 | 256 | |
257 | - return $this->get_line_items(); |
|
257 | + return $this->get_line_items(); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | /** |
261 | - * Return all line items. |
|
262 | - */ |
|
263 | - protected function get_line_items() { |
|
264 | - return $this->line_items; |
|
265 | - } |
|
261 | + * Return all line items. |
|
262 | + */ |
|
263 | + protected function get_line_items() { |
|
264 | + return $this->line_items; |
|
265 | + } |
|
266 | 266 | |
267 | 267 | /** |
268 | - * Remove all line items. |
|
269 | - */ |
|
270 | - protected function delete_line_items() { |
|
271 | - $this->line_items = array(); |
|
268 | + * Remove all line items. |
|
269 | + */ |
|
270 | + protected function delete_line_items() { |
|
271 | + $this->line_items = array(); |
|
272 | 272 | } |
273 | 273 | |
274 | 274 | /** |
275 | - * Prepare line items to send to paypal. |
|
276 | - * |
|
277 | - * @param WPInv_Invoice $invoice Invoice object. |
|
278 | - */ |
|
279 | - protected function prepare_line_items( $invoice ) { |
|
280 | - $this->delete_line_items(); |
|
281 | - |
|
282 | - // Items. |
|
283 | - foreach ( $invoice->get_items() as $item ) { |
|
284 | - $amount = $item->get_price(); |
|
285 | - $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity(); |
|
286 | - $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() ); |
|
275 | + * Prepare line items to send to paypal. |
|
276 | + * |
|
277 | + * @param WPInv_Invoice $invoice Invoice object. |
|
278 | + */ |
|
279 | + protected function prepare_line_items( $invoice ) { |
|
280 | + $this->delete_line_items(); |
|
281 | + |
|
282 | + // Items. |
|
283 | + foreach ( $invoice->get_items() as $item ) { |
|
284 | + $amount = $item->get_price(); |
|
285 | + $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity(); |
|
286 | + $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() ); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | // Fees. |
290 | - foreach ( $invoice->get_fees() as $fee => $data ) { |
|
290 | + foreach ( $invoice->get_fees() as $fee => $data ) { |
|
291 | 291 | $this->add_line_item( $fee, 1, wpinv_sanitize_amount( $data['initial_fee'] ) ); |
292 | 292 | } |
293 | 293 | |
294 | 294 | } |
295 | 295 | |
296 | 296 | /** |
297 | - * Add PayPal Line Item. |
|
298 | - * |
|
299 | - * @param string $item_name Item name. |
|
300 | - * @param float $quantity Item quantity. |
|
301 | - * @param float $amount Amount. |
|
302 | - * @param string $item_number Item number. |
|
303 | - */ |
|
304 | - protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) { |
|
305 | - $index = ( count( $this->line_items ) / 4 ) + 1; |
|
306 | - |
|
307 | - $item = apply_filters( |
|
308 | - 'getpaid_paypal_line_item', |
|
309 | - array( |
|
310 | - 'item_name' => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ), |
|
311 | - 'quantity' => (float) $quantity, |
|
312 | - 'amount' => wpinv_sanitize_amount( (float) $amount, 2 ), |
|
313 | - 'item_number' => $item_number, |
|
314 | - ), |
|
315 | - $item_name, |
|
316 | - $quantity, |
|
317 | - $amount, |
|
318 | - $item_number |
|
319 | - ); |
|
320 | - |
|
321 | - $this->line_items[ 'item_name_' . $index ] = getpaid_limit_length( $item['item_name'], 127 ); |
|
297 | + * Add PayPal Line Item. |
|
298 | + * |
|
299 | + * @param string $item_name Item name. |
|
300 | + * @param float $quantity Item quantity. |
|
301 | + * @param float $amount Amount. |
|
302 | + * @param string $item_number Item number. |
|
303 | + */ |
|
304 | + protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) { |
|
305 | + $index = ( count( $this->line_items ) / 4 ) + 1; |
|
306 | + |
|
307 | + $item = apply_filters( |
|
308 | + 'getpaid_paypal_line_item', |
|
309 | + array( |
|
310 | + 'item_name' => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ), |
|
311 | + 'quantity' => (float) $quantity, |
|
312 | + 'amount' => wpinv_sanitize_amount( (float) $amount, 2 ), |
|
313 | + 'item_number' => $item_number, |
|
314 | + ), |
|
315 | + $item_name, |
|
316 | + $quantity, |
|
317 | + $amount, |
|
318 | + $item_number |
|
319 | + ); |
|
320 | + |
|
321 | + $this->line_items[ 'item_name_' . $index ] = getpaid_limit_length( $item['item_name'], 127 ); |
|
322 | 322 | $this->line_items[ 'quantity_' . $index ] = $item['quantity']; |
323 | 323 | |
324 | 324 | // The price or amount of the product, service, or contribution, not including shipping, handling, or tax. |
325 | - $this->line_items[ 'amount_' . $index ] = $item['amount'] * $item['quantity']; |
|
326 | - $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 ); |
|
325 | + $this->line_items[ 'amount_' . $index ] = $item['amount'] * $item['quantity']; |
|
326 | + $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 ); |
|
327 | 327 | } |
328 | 328 | |
329 | 329 | /** |
330 | - * If the default request with line items is too long, generate a new one with only one line item. |
|
331 | - * |
|
332 | - * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer. |
|
333 | - * |
|
334 | - * @param WPInv_Invoice $invoice Invoice to be sent to Paypal. |
|
335 | - * @param array $paypal_args Arguments sent to Paypal in the request. |
|
336 | - * @return array |
|
337 | - */ |
|
338 | - protected function fix_request_length( $invoice, $paypal_args ) { |
|
339 | - $max_paypal_length = 2083; |
|
340 | - $query_candidate = http_build_query( $paypal_args, '', '&' ); |
|
341 | - |
|
342 | - if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) { |
|
343 | - return $paypal_args; |
|
344 | - } |
|
345 | - |
|
346 | - return apply_filters( |
|
347 | - 'getpaid_paypal_args', |
|
348 | - array_merge( |
|
349 | - $this->get_transaction_args( $invoice ), |
|
350 | - $this->get_line_item_args( $invoice, true ) |
|
351 | - ), |
|
352 | - $invoice |
|
353 | - ); |
|
330 | + * If the default request with line items is too long, generate a new one with only one line item. |
|
331 | + * |
|
332 | + * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer. |
|
333 | + * |
|
334 | + * @param WPInv_Invoice $invoice Invoice to be sent to Paypal. |
|
335 | + * @param array $paypal_args Arguments sent to Paypal in the request. |
|
336 | + * @return array |
|
337 | + */ |
|
338 | + protected function fix_request_length( $invoice, $paypal_args ) { |
|
339 | + $max_paypal_length = 2083; |
|
340 | + $query_candidate = http_build_query( $paypal_args, '', '&' ); |
|
341 | + |
|
342 | + if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) { |
|
343 | + return $paypal_args; |
|
344 | + } |
|
345 | + |
|
346 | + return apply_filters( |
|
347 | + 'getpaid_paypal_args', |
|
348 | + array_merge( |
|
349 | + $this->get_transaction_args( $invoice ), |
|
350 | + $this->get_line_item_args( $invoice, true ) |
|
351 | + ), |
|
352 | + $invoice |
|
353 | + ); |
|
354 | 354 | |
355 | 355 | } |
356 | 356 | |
357 | 357 | /** |
358 | - * Processes recurring invoices. |
|
359 | - * |
|
360 | - * @param array $paypal_args PayPal args. |
|
361 | - * @param WPInv_Invoice $invoice Invoice object. |
|
362 | - */ |
|
363 | - public function process_subscription( $paypal_args, $invoice ) { |
|
358 | + * Processes recurring invoices. |
|
359 | + * |
|
360 | + * @param array $paypal_args PayPal args. |
|
361 | + * @param WPInv_Invoice $invoice Invoice object. |
|
362 | + */ |
|
363 | + public function process_subscription( $paypal_args, $invoice ) { |
|
364 | 364 | |
365 | 365 | // Make sure this is a subscription. |
366 | 366 | if ( ! $invoice->is_recurring() || ! $subscription = getpaid_get_invoice_subscription( $invoice ) ) { |
@@ -381,21 +381,21 @@ discard block |
||
381 | 381 | $recurring_amount = (float) wpinv_sanitize_amount( $invoice->get_recurring_total(), 2 ); |
382 | 382 | $subscription_item = $invoice->get_recurring( true ); |
383 | 383 | |
384 | - // Convert 365 days to 1 year. |
|
385 | - if ( 'D' == $period && 365 == $interval ) { |
|
386 | - $period = 'Y'; |
|
387 | - $interval = 1; |
|
388 | - } |
|
384 | + // Convert 365 days to 1 year. |
|
385 | + if ( 'D' == $period && 365 == $interval ) { |
|
386 | + $period = 'Y'; |
|
387 | + $interval = 1; |
|
388 | + } |
|
389 | 389 | |
390 | 390 | if ( $subscription_item->has_free_trial() ) { |
391 | 391 | |
392 | 392 | $paypal_args['a1'] = 0 == $initial_amount ? 0 : $initial_amount; |
393 | 393 | |
394 | - // Trial period length. |
|
395 | - $paypal_args['p1'] = $subscription_item->get_trial_interval(); |
|
394 | + // Trial period length. |
|
395 | + $paypal_args['p1'] = $subscription_item->get_trial_interval(); |
|
396 | 396 | |
397 | - // Trial period. |
|
398 | - $paypal_args['t1'] = $subscription_item->get_trial_period(); |
|
397 | + // Trial period. |
|
398 | + $paypal_args['t1'] = $subscription_item->get_trial_period(); |
|
399 | 399 | |
400 | 400 | } else if ( $initial_amount != $recurring_amount ) { |
401 | 401 | |
@@ -418,40 +418,40 @@ discard block |
||
418 | 418 | } |
419 | 419 | |
420 | 420 | // We have a recurring payment |
421 | - if ( ! isset( $param_number ) || 1 == $param_number ) { |
|
421 | + if ( ! isset( $param_number ) || 1 == $param_number ) { |
|
422 | 422 | |
423 | - // Subscription price |
|
424 | - $paypal_args['a3'] = $recurring_amount; |
|
423 | + // Subscription price |
|
424 | + $paypal_args['a3'] = $recurring_amount; |
|
425 | 425 | |
426 | - // Subscription duration |
|
427 | - $paypal_args['p3'] = $interval; |
|
426 | + // Subscription duration |
|
427 | + $paypal_args['p3'] = $interval; |
|
428 | 428 | |
429 | - // Subscription period |
|
430 | - $paypal_args['t3'] = $period; |
|
429 | + // Subscription period |
|
430 | + $paypal_args['t3'] = $period; |
|
431 | 431 | |
432 | 432 | } |
433 | 433 | |
434 | 434 | // Recurring payments |
435 | - if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) { |
|
435 | + if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) { |
|
436 | 436 | |
437 | - // Non-recurring payments |
|
438 | - $paypal_args['src'] = 0; |
|
437 | + // Non-recurring payments |
|
438 | + $paypal_args['src'] = 0; |
|
439 | 439 | |
440 | - } else { |
|
440 | + } else { |
|
441 | 441 | |
442 | - $paypal_args['src'] = 1; |
|
442 | + $paypal_args['src'] = 1; |
|
443 | 443 | |
444 | - if ( $bill_times > 0 ) { |
|
444 | + if ( $bill_times > 0 ) { |
|
445 | 445 | |
446 | - // An initial period is being used to charge a sign-up fee |
|
447 | - if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) { |
|
448 | - $bill_times--; |
|
449 | - } |
|
446 | + // An initial period is being used to charge a sign-up fee |
|
447 | + if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) { |
|
448 | + $bill_times--; |
|
449 | + } |
|
450 | 450 | |
451 | 451 | // Make sure it's not over the max of 52 |
452 | 452 | $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 ); |
453 | 453 | |
454 | - } |
|
454 | + } |
|
455 | 455 | } |
456 | 456 | |
457 | 457 | // Force return URL so that order description & instructions display |
@@ -467,19 +467,19 @@ discard block |
||
467 | 467 | } |
468 | 468 | |
469 | 469 | return apply_filters( |
470 | - 'getpaid_paypal_subscription_args', |
|
471 | - $paypal_args, |
|
472 | - $invoice |
|
470 | + 'getpaid_paypal_subscription_args', |
|
471 | + $paypal_args, |
|
472 | + $invoice |
|
473 | 473 | ); |
474 | 474 | |
475 | 475 | } |
476 | 476 | |
477 | 477 | /** |
478 | - * Processes ipns and marks payments as complete. |
|
479 | - * |
|
480 | - * @return void |
|
481 | - */ |
|
482 | - public function verify_ipn() { |
|
478 | + * Processes ipns and marks payments as complete. |
|
479 | + * |
|
480 | + * @return void |
|
481 | + */ |
|
482 | + public function verify_ipn() { |
|
483 | 483 | new GetPaid_Paypal_Gateway_IPN_Handler( $this ); |
484 | 484 | } |
485 | 485 | |
@@ -489,19 +489,19 @@ discard block |
||
489 | 489 | public function sandbox_notice() { |
490 | 490 | |
491 | 491 | return sprintf( |
492 | - __( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %sPayPal Sandbox Testing Guide%s for more details.', 'invoicing' ), |
|
493 | - '<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">', |
|
494 | - '</a>' |
|
495 | - ); |
|
492 | + __( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %sPayPal Sandbox Testing Guide%s for more details.', 'invoicing' ), |
|
493 | + '<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">', |
|
494 | + '</a>' |
|
495 | + ); |
|
496 | 496 | |
497 | 497 | } |
498 | 498 | |
499 | - /** |
|
500 | - * Filters the gateway settings. |
|
501 | - * |
|
502 | - * @param array $admin_settings |
|
503 | - */ |
|
504 | - public function admin_settings( $admin_settings ) { |
|
499 | + /** |
|
500 | + * Filters the gateway settings. |
|
501 | + * |
|
502 | + * @param array $admin_settings |
|
503 | + */ |
|
504 | + public function admin_settings( $admin_settings ) { |
|
505 | 505 | |
506 | 506 | $currencies = sprintf( |
507 | 507 | __( 'Supported Currencies: %s', 'invoicing' ), |
@@ -511,39 +511,39 @@ discard block |
||
511 | 511 | $admin_settings['paypal_active']['desc'] .= " ($currencies)"; |
512 | 512 | $admin_settings['paypal_desc']['std'] = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' ); |
513 | 513 | |
514 | - // Access tokens. |
|
515 | - $live_email = wpinv_get_option( 'paypal_email' ); |
|
516 | - $sandbox_email = wpinv_get_option( 'paypal_sandbox_email' ); |
|
517 | - |
|
518 | - $admin_settings['paypal_connect'] = array( |
|
519 | - 'type' => 'raw_html', |
|
520 | - 'id' => 'paypal_connect', |
|
521 | - 'name' => __( 'Connect to PayPal', 'invoicing' ), |
|
522 | - 'desc' => sprintf( |
|
523 | - '<div class="wpinv-paypal-connect-live"><a class="button button-primary" href="%s">%s</a></div><div class="wpinv-paypal-connect-sandbox"><a class="button button-primary" href="%s">%s</a></div>%s', |
|
524 | - esc_url( self::get_connect_url( false ) ), |
|
525 | - __( 'Connect to PayPal', 'invoicing' ), |
|
526 | - esc_url( self::get_connect_url( true ) ), |
|
527 | - __( 'Connect to PayPal Sandox', 'invoicing' ), |
|
528 | - $this->get_js() |
|
529 | - ), |
|
530 | - ); |
|
514 | + // Access tokens. |
|
515 | + $live_email = wpinv_get_option( 'paypal_email' ); |
|
516 | + $sandbox_email = wpinv_get_option( 'paypal_sandbox_email' ); |
|
517 | + |
|
518 | + $admin_settings['paypal_connect'] = array( |
|
519 | + 'type' => 'raw_html', |
|
520 | + 'id' => 'paypal_connect', |
|
521 | + 'name' => __( 'Connect to PayPal', 'invoicing' ), |
|
522 | + 'desc' => sprintf( |
|
523 | + '<div class="wpinv-paypal-connect-live"><a class="button button-primary" href="%s">%s</a></div><div class="wpinv-paypal-connect-sandbox"><a class="button button-primary" href="%s">%s</a></div>%s', |
|
524 | + esc_url( self::get_connect_url( false ) ), |
|
525 | + __( 'Connect to PayPal', 'invoicing' ), |
|
526 | + esc_url( self::get_connect_url( true ) ), |
|
527 | + __( 'Connect to PayPal Sandox', 'invoicing' ), |
|
528 | + $this->get_js() |
|
529 | + ), |
|
530 | + ); |
|
531 | 531 | |
532 | 532 | $admin_settings['paypal_email'] = array( |
533 | 533 | 'type' => 'text', |
534 | - 'class' => 'live-auth-data', |
|
534 | + 'class' => 'live-auth-data', |
|
535 | 535 | 'id' => 'paypal_email', |
536 | 536 | 'name' => __( 'Live Email Address', 'invoicing' ), |
537 | 537 | 'desc' => __( 'The email address of your PayPal account.', 'invoicing' ), |
538 | 538 | ); |
539 | 539 | |
540 | - $admin_settings['paypal_sandbox_email'] = array( |
|
540 | + $admin_settings['paypal_sandbox_email'] = array( |
|
541 | 541 | 'type' => 'text', |
542 | - 'class' => 'sandbox-auth-data', |
|
542 | + 'class' => 'sandbox-auth-data', |
|
543 | 543 | 'id' => 'paypal_sandbox_email', |
544 | 544 | 'name' => __( 'Sandbox Email Address', 'invoicing' ), |
545 | 545 | 'desc' => __( 'The email address of your sandbox PayPal account.', 'invoicing' ), |
546 | - 'std' => wpinv_get_option( 'paypal_email', '' ), |
|
546 | + 'std' => wpinv_get_option( 'paypal_email', '' ), |
|
547 | 547 | ); |
548 | 548 | |
549 | 549 | $admin_settings['paypal_ipn_url'] = array( |
@@ -555,29 +555,29 @@ discard block |
||
555 | 555 | 'readonly' => true, |
556 | 556 | ); |
557 | 557 | |
558 | - return $admin_settings; |
|
559 | - } |
|
558 | + return $admin_settings; |
|
559 | + } |
|
560 | 560 | |
561 | - /** |
|
562 | - * Retrieves the PayPal connect URL when using the setup wizzard. |
|
563 | - * |
|
564 | - * |
|
561 | + /** |
|
562 | + * Retrieves the PayPal connect URL when using the setup wizzard. |
|
563 | + * |
|
564 | + * |
|
565 | 565 | * @param array $data |
566 | 566 | * @return string |
567 | - */ |
|
568 | - public static function maybe_get_connect_url( $url = '', $data ) { |
|
569 | - return self::get_connect_url( false, urldecode( $data['redirect'] ) ); |
|
570 | - } |
|
571 | - |
|
572 | - /** |
|
573 | - * Retrieves the PayPal connect URL. |
|
574 | - * |
|
575 | - * |
|
567 | + */ |
|
568 | + public static function maybe_get_connect_url( $url = '', $data ) { |
|
569 | + return self::get_connect_url( false, urldecode( $data['redirect'] ) ); |
|
570 | + } |
|
571 | + |
|
572 | + /** |
|
573 | + * Retrieves the PayPal connect URL. |
|
574 | + * |
|
575 | + * |
|
576 | 576 | * @param bool $is_sandbox |
577 | - * @param string $redirect |
|
577 | + * @param string $redirect |
|
578 | 578 | * @return string |
579 | - */ |
|
580 | - public static function get_connect_url( $is_sandbox, $redirect = '' ) { |
|
579 | + */ |
|
580 | + public static function get_connect_url( $is_sandbox, $redirect = '' ) { |
|
581 | 581 | |
582 | 582 | $redirect_url = add_query_arg( |
583 | 583 | array( |
@@ -587,7 +587,7 @@ discard block |
||
587 | 587 | 'tab' => 'gateways', |
588 | 588 | 'section' => 'paypal', |
589 | 589 | 'getpaid-nonce' => wp_create_nonce( 'getpaid-nonce' ), |
590 | - 'redirect' => urlencode( $redirect ), |
|
590 | + 'redirect' => urlencode( $redirect ), |
|
591 | 591 | ), |
592 | 592 | admin_url( 'admin.php' ) |
593 | 593 | ); |
@@ -602,12 +602,12 @@ discard block |
||
602 | 602 | |
603 | 603 | } |
604 | 604 | |
605 | - /** |
|
606 | - * Generates settings page js. |
|
607 | - * |
|
605 | + /** |
|
606 | + * Generates settings page js. |
|
607 | + * |
|
608 | 608 | * @return void |
609 | - */ |
|
610 | - public static function get_js() { |
|
609 | + */ |
|
610 | + public static function get_js() { |
|
611 | 611 | ob_start(); |
612 | 612 | ?> |
613 | 613 | <script> |
@@ -643,72 +643,72 @@ discard block |
||
643 | 643 | return ob_get_clean(); |
644 | 644 | } |
645 | 645 | |
646 | - /** |
|
647 | - * Connects to PayPal. |
|
648 | - * |
|
649 | - * @param array $data Connection data. |
|
650 | - * @return void |
|
651 | - */ |
|
652 | - public function connect_paypal( $data ) { |
|
653 | - |
|
654 | - $sandbox = $this->is_sandbox(); |
|
655 | - $data = wp_unslash( $data ); |
|
656 | - $access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] ); |
|
657 | - |
|
658 | - if ( isset( $data['live_mode'] ) ) { |
|
659 | - $sandbox = empty( $data['live_mode'] ); |
|
660 | - } |
|
661 | - |
|
662 | - wpinv_update_option( 'paypal_sandbox', (int) $sandbox ); |
|
663 | - wpinv_update_option( 'paypal_active', 1 ); |
|
664 | - |
|
665 | - if ( ! empty( $data['error_description'] ) ) { |
|
666 | - getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) ); |
|
667 | - } else { |
|
668 | - |
|
669 | - // Retrieve the user info. |
|
670 | - $user_info = wp_remote_get( |
|
671 | - ! $sandbox ? 'https://api-m.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1' : 'https://api-m.sandbox.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1', |
|
672 | - array( |
|
673 | - |
|
674 | - 'headers' => array( |
|
675 | - 'Authorization' => 'Bearer ' . $access_token, |
|
676 | - 'Content-type' => 'application/json', |
|
677 | - ) |
|
678 | - |
|
679 | - ) |
|
680 | - ); |
|
681 | - |
|
682 | - if ( is_wp_error( $user_info ) ) { |
|
683 | - getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) ); |
|
684 | - } else { |
|
685 | - |
|
686 | - // Create application. |
|
687 | - $user_info = json_decode( wp_remote_retrieve_body( $user_info ) ); |
|
688 | - |
|
689 | - if ( $sandbox ) { |
|
690 | - wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) ); |
|
691 | - wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) ); |
|
692 | - set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] ); |
|
693 | - getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) ); |
|
694 | - } else { |
|
695 | - wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) ); |
|
696 | - wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) ); |
|
697 | - set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] ); |
|
698 | - getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) ); |
|
699 | - } |
|
700 | - |
|
701 | - } |
|
702 | - |
|
703 | - } |
|
704 | - |
|
705 | - $redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways§ion=paypal' ) : urldecode( $data['redirect'] ); |
|
706 | - |
|
707 | - if ( isset( $data['step'] ) ) { |
|
708 | - $redirect = add_query_arg( 'step', $data['step'], $redirect ); |
|
709 | - } |
|
710 | - wp_redirect( $redirect ); |
|
711 | - exit; |
|
712 | - } |
|
646 | + /** |
|
647 | + * Connects to PayPal. |
|
648 | + * |
|
649 | + * @param array $data Connection data. |
|
650 | + * @return void |
|
651 | + */ |
|
652 | + public function connect_paypal( $data ) { |
|
653 | + |
|
654 | + $sandbox = $this->is_sandbox(); |
|
655 | + $data = wp_unslash( $data ); |
|
656 | + $access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] ); |
|
657 | + |
|
658 | + if ( isset( $data['live_mode'] ) ) { |
|
659 | + $sandbox = empty( $data['live_mode'] ); |
|
660 | + } |
|
661 | + |
|
662 | + wpinv_update_option( 'paypal_sandbox', (int) $sandbox ); |
|
663 | + wpinv_update_option( 'paypal_active', 1 ); |
|
664 | + |
|
665 | + if ( ! empty( $data['error_description'] ) ) { |
|
666 | + getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) ); |
|
667 | + } else { |
|
668 | + |
|
669 | + // Retrieve the user info. |
|
670 | + $user_info = wp_remote_get( |
|
671 | + ! $sandbox ? 'https://api-m.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1' : 'https://api-m.sandbox.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1', |
|
672 | + array( |
|
673 | + |
|
674 | + 'headers' => array( |
|
675 | + 'Authorization' => 'Bearer ' . $access_token, |
|
676 | + 'Content-type' => 'application/json', |
|
677 | + ) |
|
678 | + |
|
679 | + ) |
|
680 | + ); |
|
681 | + |
|
682 | + if ( is_wp_error( $user_info ) ) { |
|
683 | + getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) ); |
|
684 | + } else { |
|
685 | + |
|
686 | + // Create application. |
|
687 | + $user_info = json_decode( wp_remote_retrieve_body( $user_info ) ); |
|
688 | + |
|
689 | + if ( $sandbox ) { |
|
690 | + wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) ); |
|
691 | + wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) ); |
|
692 | + set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] ); |
|
693 | + getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) ); |
|
694 | + } else { |
|
695 | + wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) ); |
|
696 | + wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) ); |
|
697 | + set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] ); |
|
698 | + getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) ); |
|
699 | + } |
|
700 | + |
|
701 | + } |
|
702 | + |
|
703 | + } |
|
704 | + |
|
705 | + $redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways§ion=paypal' ) : urldecode( $data['redirect'] ); |
|
706 | + |
|
707 | + if ( isset( $data['step'] ) ) { |
|
708 | + $redirect = add_query_arg( 'step', $data['step'], $redirect ); |
|
709 | + } |
|
710 | + wp_redirect( $redirect ); |
|
711 | + exit; |
|
712 | + } |
|
713 | 713 | |
714 | 714 | } |