Passed
Push — master ( 6ac4b2...b2da5b )
by Brian
05:40
created
includes/admin/meta-boxes/class-getpaid-meta-box-discount-details.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  */
9 9
 
10 10
 if ( ! defined( 'ABSPATH' ) ) {
11
-	exit; // Exit if accessed directly
11
+    exit; // Exit if accessed directly
12 12
 }
13 13
 
14 14
 /**
@@ -17,10 +17,10 @@  discard block
 block discarded – undo
17 17
 class GetPaid_Meta_Box_Discount_Details {
18 18
 
19 19
     /**
20
-	 * Output the metabox.
21
-	 *
22
-	 * @param WP_Post $post
23
-	 */
20
+     * Output the metabox.
21
+     *
22
+     * @param WP_Post $post
23
+     */
24 24
     public static function output( $post ) {
25 25
 
26 26
         // Prepare the discount.
@@ -368,34 +368,34 @@  discard block
 block discarded – undo
368 368
     }
369 369
 
370 370
     /**
371
-	 * Save meta box data.
372
-	 *
373
-	 * @param int $post_id
374
-	 */
375
-	public static function save( $post_id ) {
371
+     * Save meta box data.
372
+     *
373
+     * @param int $post_id
374
+     */
375
+    public static function save( $post_id ) {
376 376
 
377 377
         // Prepare the discount.
378 378
         $discount = new WPInv_Discount( $post_id );
379 379
 
380 380
         // Load new data.
381 381
         $discount->set_props(
382
-			array(
383
-				'code'                 => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null,
384
-				'amount'               => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null,
385
-				'start'                => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null,
386
-				'expiration'           => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null,
387
-				'is_single_use'        => isset( $_POST['wpinv_discount_single_use'] ),
382
+            array(
383
+                'code'                 => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null,
384
+                'amount'               => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null,
385
+                'start'                => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null,
386
+                'expiration'           => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null,
387
+                'is_single_use'        => isset( $_POST['wpinv_discount_single_use'] ),
388 388
                 'type'                 => isset( $_POST['wpinv_discount_type'] ) ? wpinv_clean( $_POST['wpinv_discount_type'] ) : null,
389
-				'is_recurring'         => isset( $_POST['wpinv_discount_recurring'] ),
390
-				'items'                => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(),
391
-				'excluded_items'       => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(),
392
-				'max_uses'             => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null,
393
-				'min_total'            => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null,
394
-				'max_total'            => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null,
395
-			)
389
+                'is_recurring'         => isset( $_POST['wpinv_discount_recurring'] ),
390
+                'items'                => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(),
391
+                'excluded_items'       => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(),
392
+                'max_uses'             => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null,
393
+                'min_total'            => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null,
394
+                'max_total'            => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null,
395
+            )
396 396
         );
397 397
 
398
-		$discount->save();
399
-		do_action( 'getpaid_discount_metabox_save', $post_id, $discount );
400
-	}
398
+        $discount->save();
399
+        do_action( 'getpaid_discount_metabox_save', $post_id, $discount );
400
+    }
401 401
 }
Please login to merge, or discard this patch.
includes/admin/class-getpaid-metaboxes.php 1 patch
Indentation   +240 added lines, -240 removed lines patch added patch discarded remove patch
@@ -12,276 +12,276 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Metaboxes {
14 14
 
15
-	/**
16
-	 * Only save metaboxes once.
17
-	 *
18
-	 * @var boolean
19
-	 */
20
-	private static $saved_meta_boxes = false;
21
-
22 15
     /**
23
-	 * Hook in methods.
24
-	 */
25
-	public static function init() {
26
-
27
-		// Register metaboxes.
28
-		add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::add_meta_boxes', 5, 2 );
29
-
30
-		// Remove metaboxes.
31
-		add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::remove_meta_boxes', 30 );
32
-
33
-		// Rename metaboxes.
34
-		add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::rename_meta_boxes', 45 );
35
-
36
-		// Save metaboxes.
37
-		add_action( 'save_post', 'GetPaid_Metaboxes::save_meta_boxes', 1, 2 );
38
-	}
39
-
40
-	/**
41
-	 * Register core metaboxes.
42
-	 */
43
-	public static function add_meta_boxes( $post_type, $post ) {
16
+     * Only save metaboxes once.
17
+     *
18
+     * @var boolean
19
+     */
20
+    private static $saved_meta_boxes = false;
44 21
 
45
-		// For invoices...
46
-		self::add_invoice_meta_boxes( $post_type, $post );
47
-
48
-		// For payment forms.
49
-		self::add_payment_form_meta_boxes( $post_type );
50
-
51
-		// For invoice items.
52
-		self::add_item_meta_boxes( $post_type );
53
-
54
-		// For invoice discounts.
55
-		if ( $post_type == 'wpi_discount' ) {
56
-			add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' );
57
-		}
58
-
59
-	}
22
+    /**
23
+     * Hook in methods.
24
+     */
25
+    public static function init() {
60 26
 
61
-	/**
62
-	 * Register core metaboxes.
63
-	 */
64
-	protected static function add_payment_form_meta_boxes( $post_type ) {
27
+        // Register metaboxes.
28
+        add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::add_meta_boxes', 5, 2 );
65 29
 
66
-		// For payment forms.
67
-		if ( $post_type == 'wpi_payment_form' ) {
30
+        // Remove metaboxes.
31
+        add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::remove_meta_boxes', 30 );
68 32
 
69
-			// Design payment form.
70
-			add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' );
33
+        // Rename metaboxes.
34
+        add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::rename_meta_boxes', 45 );
71 35
 
72
-			// Payment form information.
73
-			add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' );
36
+        // Save metaboxes.
37
+        add_action( 'save_post', 'GetPaid_Metaboxes::save_meta_boxes', 1, 2 );
38
+    }
74 39
 
75
-		}
40
+    /**
41
+     * Register core metaboxes.
42
+     */
43
+    public static function add_meta_boxes( $post_type, $post ) {
76 44
 
77
-	}
45
+        // For invoices...
46
+        self::add_invoice_meta_boxes( $post_type, $post );
78 47
 
79
-	/**
80
-	 * Register core metaboxes.
81
-	 */
82
-	protected static function add_item_meta_boxes( $post_type ) {
48
+        // For payment forms.
49
+        self::add_payment_form_meta_boxes( $post_type );
83 50
 
84
-		if ( $post_type == 'wpi_item' ) {
51
+        // For invoice items.
52
+        self::add_item_meta_boxes( $post_type );
85 53
 
86
-			// Item details.
87
-			add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' );
54
+        // For invoice discounts.
55
+        if ( $post_type == 'wpi_discount' ) {
56
+            add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' );
57
+        }
88 58
 
89
-			// If taxes are enabled, register the tax metabox.
90
-			if ( wpinv_use_taxes() ) {
91
-				add_meta_box( 'wpinv_item_vat', __( 'Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' );
92
-			}
59
+    }
93 60
 
94
-			// Item info.
95
-			add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' );
61
+    /**
62
+     * Register core metaboxes.
63
+     */
64
+    protected static function add_payment_form_meta_boxes( $post_type ) {
96 65
 
97
-		}
66
+        // For payment forms.
67
+        if ( $post_type == 'wpi_payment_form' ) {
98 68
 
99
-	}
69
+            // Design payment form.
70
+            add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' );
100 71
 
101
-	/**
102
-	 * Register invoice metaboxes.
103
-	 */
104
-	protected static function add_invoice_meta_boxes( $post_type, $post ) {
72
+            // Payment form information.
73
+            add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' );
105 74
 
106
-		// For invoices...
107
-		if ( getpaid_is_invoice_post_type( $post_type ) ) {
108
-			$invoice = new WPInv_Invoice( $post );
75
+        }
109 76
 
110
-			// Resend invoice.
111
-			if ( ! $invoice->is_draft() ) {
77
+    }
112 78
 
113
-				add_meta_box(
114
-					'wpinv-mb-resend-invoice',
115
-					sprintf(
116
-						__( 'Resend %s', 'invoicing' ),
117
-						ucfirst( $invoice->get_invoice_quote_type() )
118
-					),
119
-					'GetPaid_Meta_Box_Resend_Invoice::output',
120
-					$post_type,
121
-					'side',
122
-					'low'
123
-				);
79
+    /**
80
+     * Register core metaboxes.
81
+     */
82
+    protected static function add_item_meta_boxes( $post_type ) {
124 83
 
125
-			}
84
+        if ( $post_type == 'wpi_item' ) {
126 85
 
127
-			// Subscriptions.
128
-			$subscriptions = getpaid_get_invoice_subscriptions( $invoice );
129
-			if ( ! empty( $subscriptions ) ) {
86
+            // Item details.
87
+            add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' );
130 88
 
131
-				if ( is_array( $subscriptions ) ) {
132
-					add_meta_box( 'wpinv-mb-subscriptions', __( 'Related Subscriptions', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_related', $post_type, 'advanced' );
133
-				} else {
134
-					add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscription Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output', $post_type, 'advanced' );
135
-				}
89
+            // If taxes are enabled, register the tax metabox.
90
+            if ( wpinv_use_taxes() ) {
91
+                add_meta_box( 'wpinv_item_vat', __( 'Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' );
92
+            }
136 93
 
137
-				if ( getpaid_count_subscription_invoices( $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id() ) > 1 ) {
138
-					add_meta_box( 'wpinv-mb-subscription-invoices', __( 'Related Payments', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_invoices', $post_type, 'advanced' );
139
-				}
94
+            // Item info.
95
+            add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' );
140 96
 
141
-			}
97
+        }
142 98
 
143
-			// Invoice details.
144
-			add_meta_box(
145
-				'wpinv-details',
146
-				sprintf(
147
-					__( '%s Details', 'invoicing' ),
148
-					ucfirst( $invoice->get_invoice_quote_type() )
149
-				),
150
-				'GetPaid_Meta_Box_Invoice_Details::output',
151
-				$post_type,
152
-				'side'
153
-			);
154
-
155
-			// Payment details.
156
-			if ( ! $invoice->is_draft() ) {
157
-				add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Payment_Meta::output', $post_type, 'side', 'default' );
158
-			}
99
+    }
159 100
 
160
-			// Billing details.
161
-			add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Address::output', $post_type, 'normal', 'high' );
101
+    /**
102
+     * Register invoice metaboxes.
103
+     */
104
+    protected static function add_invoice_meta_boxes( $post_type, $post ) {
105
+
106
+        // For invoices...
107
+        if ( getpaid_is_invoice_post_type( $post_type ) ) {
108
+            $invoice = new WPInv_Invoice( $post );
109
+
110
+            // Resend invoice.
111
+            if ( ! $invoice->is_draft() ) {
112
+
113
+                add_meta_box(
114
+                    'wpinv-mb-resend-invoice',
115
+                    sprintf(
116
+                        __( 'Resend %s', 'invoicing' ),
117
+                        ucfirst( $invoice->get_invoice_quote_type() )
118
+                    ),
119
+                    'GetPaid_Meta_Box_Resend_Invoice::output',
120
+                    $post_type,
121
+                    'side',
122
+                    'low'
123
+                );
124
+
125
+            }
126
+
127
+            // Subscriptions.
128
+            $subscriptions = getpaid_get_invoice_subscriptions( $invoice );
129
+            if ( ! empty( $subscriptions ) ) {
130
+
131
+                if ( is_array( $subscriptions ) ) {
132
+                    add_meta_box( 'wpinv-mb-subscriptions', __( 'Related Subscriptions', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_related', $post_type, 'advanced' );
133
+                } else {
134
+                    add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscription Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output', $post_type, 'advanced' );
135
+                }
136
+
137
+                if ( getpaid_count_subscription_invoices( $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id() ) > 1 ) {
138
+                    add_meta_box( 'wpinv-mb-subscription-invoices', __( 'Related Payments', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_invoices', $post_type, 'advanced' );
139
+                }
140
+
141
+            }
142
+
143
+            // Invoice details.
144
+            add_meta_box(
145
+                'wpinv-details',
146
+                sprintf(
147
+                    __( '%s Details', 'invoicing' ),
148
+                    ucfirst( $invoice->get_invoice_quote_type() )
149
+                ),
150
+                'GetPaid_Meta_Box_Invoice_Details::output',
151
+                $post_type,
152
+                'side'
153
+            );
154
+
155
+            // Payment details.
156
+            if ( ! $invoice->is_draft() ) {
157
+                add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Payment_Meta::output', $post_type, 'side', 'default' );
158
+            }
159
+
160
+            // Billing details.
161
+            add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Address::output', $post_type, 'normal', 'high' );
162 162
 			
163
-			// Invoice items.
164
-			add_meta_box(
165
-				'wpinv-items',
166
-				sprintf(
167
-					__( '%s Items', 'invoicing' ),
168
-					ucfirst( $invoice->get_invoice_quote_type() )
169
-				),
170
-				'GetPaid_Meta_Box_Invoice_Items::output',
171
-				$post_type,
172
-				'normal',
173
-				'high'
174
-			);
163
+            // Invoice items.
164
+            add_meta_box(
165
+                'wpinv-items',
166
+                sprintf(
167
+                    __( '%s Items', 'invoicing' ),
168
+                    ucfirst( $invoice->get_invoice_quote_type() )
169
+                ),
170
+                'GetPaid_Meta_Box_Invoice_Items::output',
171
+                $post_type,
172
+                'normal',
173
+                'high'
174
+            );
175 175
 			
176
-			// Invoice notes.
177
-			add_meta_box(
178
-				'wpinv-notes',
179
-				sprintf(
180
-					__( '%s Notes', 'invoicing' ),
181
-					ucfirst( $invoice->get_invoice_quote_type() )
182
-				),
183
-				'WPInv_Meta_Box_Notes::output',
184
-				$post_type,
185
-				'side',
186
-				'low'
187
-			);
188
-
189
-			// Shipping Address.
190
-			if ( get_post_meta( $invoice->get_id(), 'shipping_address', true ) ) {
191
-				add_meta_box( 'wpinv-invoice-shipping-details', __( 'Shipping Address', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Shipping_Address::output', $post_type, 'side', 'high' );
192
-			}
193
-
194
-			// Payment form information.
195
-			if ( get_post_meta( $invoice->get_id(), 'payment_form_data', true ) ) {
196
-				add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', $post_type, 'side', 'high' );
197
-			}
198
-
199
-		}
200
-
201
-	}
202
-
203
-	/**
204
-	 * Remove some metaboxes.
205
-	 */
206
-	public static function remove_meta_boxes() {
207
-		remove_meta_box( 'wpseo_meta', 'wpi_invoice', 'normal' );
208
-	}
209
-
210
-	/**
211
-	 * Rename other metaboxes.
212
-	 */
213
-	public static function rename_meta_boxes() {
176
+            // Invoice notes.
177
+            add_meta_box(
178
+                'wpinv-notes',
179
+                sprintf(
180
+                    __( '%s Notes', 'invoicing' ),
181
+                    ucfirst( $invoice->get_invoice_quote_type() )
182
+                ),
183
+                'WPInv_Meta_Box_Notes::output',
184
+                $post_type,
185
+                'side',
186
+                'low'
187
+            );
188
+
189
+            // Shipping Address.
190
+            if ( get_post_meta( $invoice->get_id(), 'shipping_address', true ) ) {
191
+                add_meta_box( 'wpinv-invoice-shipping-details', __( 'Shipping Address', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Shipping_Address::output', $post_type, 'side', 'high' );
192
+            }
193
+
194
+            // Payment form information.
195
+            if ( get_post_meta( $invoice->get_id(), 'payment_form_data', true ) ) {
196
+                add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', $post_type, 'side', 'high' );
197
+            }
198
+
199
+        }
200
+
201
+    }
202
+
203
+    /**
204
+     * Remove some metaboxes.
205
+     */
206
+    public static function remove_meta_boxes() {
207
+        remove_meta_box( 'wpseo_meta', 'wpi_invoice', 'normal' );
208
+    }
209
+
210
+    /**
211
+     * Rename other metaboxes.
212
+     */
213
+    public static function rename_meta_boxes() {
214 214
 		
215
-	}
216
-
217
-	/**
218
-	 * Check if we're saving, then trigger an action based on the post type.
219
-	 *
220
-	 * @param  int    $post_id Post ID.
221
-	 * @param  object $post Post object.
222
-	 */
223
-	public static function save_meta_boxes( $post_id, $post ) {
224
-		$post_id = absint( $post_id );
225
-		$data    = wp_kses_post_deep( wp_unslash( $_POST ) );
226
-
227
-		// Do not save for ajax requests.
228
-		if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
229
-			return;
230
-		}
231
-
232
-		// $post_id and $post are required
233
-		if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) {
234
-			return;
235
-		}
236
-
237
-		// Dont' save meta boxes for revisions or autosaves.
238
-		if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
239
-			return;
240
-		}
241
-
242
-		// Check the nonce.
243
-		if ( empty( $data['getpaid_meta_nonce'] ) || ! wp_verify_nonce( $data['getpaid_meta_nonce'], 'getpaid_meta_nonce' ) ) {
244
-			return;
245
-		}
246
-
247
-		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
248
-		if ( empty( $data['post_ID'] ) || absint( $data['post_ID'] ) !== $post_id ) {
249
-			return;
250
-		}
251
-
252
-		// Check user has permission to edit.
253
-		if ( ! current_user_can( 'edit_post', $post_id ) ) {
254
-			return;
255
-		}
256
-
257
-		if ( getpaid_is_invoice_post_type( $post->post_type ) ) {
258
-
259
-			// We need this save event to run once to avoid potential endless loops.
260
-			self::$saved_meta_boxes = true;
261
-
262
-			return GetPaid_Meta_Box_Invoice_Address::save( $post_id );
263
-
264
-		}
265
-
266
-		// Ensure this is our post type.
267
-		$post_types_map = array(
268
-			'wpi_item'         => 'GetPaid_Meta_Box_Item_Details',
269
-			'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form',
270
-			'wpi_discount'     => 'GetPaid_Meta_Box_Discount_Details',
271
-		);
272
-
273
-		// Is this our post type?
274
-		if ( ! isset( $post_types_map[ $post->post_type ] ) ) {
275
-			return;
276
-		}
277
-
278
-		// We need this save event to run once to avoid potential endless loops.
279
-		self::$saved_meta_boxes = true;
215
+    }
216
+
217
+    /**
218
+     * Check if we're saving, then trigger an action based on the post type.
219
+     *
220
+     * @param  int    $post_id Post ID.
221
+     * @param  object $post Post object.
222
+     */
223
+    public static function save_meta_boxes( $post_id, $post ) {
224
+        $post_id = absint( $post_id );
225
+        $data    = wp_kses_post_deep( wp_unslash( $_POST ) );
226
+
227
+        // Do not save for ajax requests.
228
+        if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
229
+            return;
230
+        }
231
+
232
+        // $post_id and $post are required
233
+        if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) {
234
+            return;
235
+        }
236
+
237
+        // Dont' save meta boxes for revisions or autosaves.
238
+        if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
239
+            return;
240
+        }
241
+
242
+        // Check the nonce.
243
+        if ( empty( $data['getpaid_meta_nonce'] ) || ! wp_verify_nonce( $data['getpaid_meta_nonce'], 'getpaid_meta_nonce' ) ) {
244
+            return;
245
+        }
246
+
247
+        // Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
248
+        if ( empty( $data['post_ID'] ) || absint( $data['post_ID'] ) !== $post_id ) {
249
+            return;
250
+        }
251
+
252
+        // Check user has permission to edit.
253
+        if ( ! current_user_can( 'edit_post', $post_id ) ) {
254
+            return;
255
+        }
256
+
257
+        if ( getpaid_is_invoice_post_type( $post->post_type ) ) {
258
+
259
+            // We need this save event to run once to avoid potential endless loops.
260
+            self::$saved_meta_boxes = true;
261
+
262
+            return GetPaid_Meta_Box_Invoice_Address::save( $post_id );
263
+
264
+        }
265
+
266
+        // Ensure this is our post type.
267
+        $post_types_map = array(
268
+            'wpi_item'         => 'GetPaid_Meta_Box_Item_Details',
269
+            'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form',
270
+            'wpi_discount'     => 'GetPaid_Meta_Box_Discount_Details',
271
+        );
272
+
273
+        // Is this our post type?
274
+        if ( ! isset( $post_types_map[ $post->post_type ] ) ) {
275
+            return;
276
+        }
277
+
278
+        // We need this save event to run once to avoid potential endless loops.
279
+        self::$saved_meta_boxes = true;
280 280
 		
281
-		// Save the post.
282
-		$class = $post_types_map[ $post->post_type ];
283
-		$class::save( $post_id, $_POST, $post );
281
+        // Save the post.
282
+        $class = $post_types_map[ $post->post_type ];
283
+        $class::save( $post_id, $_POST, $post );
284 284
 
285
-	}
285
+    }
286 286
 
287 287
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission.php 1 patch
Indentation   +818 added lines, -818 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 if ( ! defined( 'ABSPATH' ) ) {
3
-	exit;
3
+    exit;
4 4
 }
5 5
 
6 6
 /**
@@ -10,198 +10,198 @@  discard block
 block discarded – undo
10 10
 class GetPaid_Payment_Form_Submission {
11 11
 
12 12
     /**
13
-	 * Submission ID
14
-	 *
15
-	 * @var string
16
-	 */
17
-	public $id = null;
18
-
19
-	/**
20
-	 * The raw submission data.
21
-	 *
22
-	 * @var array
23
-	 */
24
-	protected $data = null;
25
-
26
-	/**
27
-	 * Submission totals
28
-	 *
29
-	 * @var array
30
-	 */
31
-	protected $totals = array(
32
-
33
-		'subtotal'      => array(
34
-			'initial'   => 0,
35
-			'recurring' => 0,
36
-		),
37
-
38
-		'discount'      => array(
39
-			'initial'   => 0,
40
-			'recurring' => 0,
41
-		),
42
-
43
-		'fees'          => array(
44
-			'initial'   => 0,
45
-			'recurring' => 0,
46
-		),
47
-
48
-		'taxes'         => array(
49
-			'initial'   => 0,
50
-			'recurring' => 0,
51
-		),
52
-
53
-	);
54
-
55
-	/**
56
-	 * Sets the associated payment form.
57
-	 *
58
-	 * @var GetPaid_Payment_Form
59
-	 */
13
+     * Submission ID
14
+     *
15
+     * @var string
16
+     */
17
+    public $id = null;
18
+
19
+    /**
20
+     * The raw submission data.
21
+     *
22
+     * @var array
23
+     */
24
+    protected $data = null;
25
+
26
+    /**
27
+     * Submission totals
28
+     *
29
+     * @var array
30
+     */
31
+    protected $totals = array(
32
+
33
+        'subtotal'      => array(
34
+            'initial'   => 0,
35
+            'recurring' => 0,
36
+        ),
37
+
38
+        'discount'      => array(
39
+            'initial'   => 0,
40
+            'recurring' => 0,
41
+        ),
42
+
43
+        'fees'          => array(
44
+            'initial'   => 0,
45
+            'recurring' => 0,
46
+        ),
47
+
48
+        'taxes'         => array(
49
+            'initial'   => 0,
50
+            'recurring' => 0,
51
+        ),
52
+
53
+    );
54
+
55
+    /**
56
+     * Sets the associated payment form.
57
+     *
58
+     * @var GetPaid_Payment_Form
59
+     */
60 60
     protected $payment_form = null;
61 61
 
62 62
     /**
63
-	 * The country for the submission.
64
-	 *
65
-	 * @var string
66
-	 */
67
-	public $country = null;
68
-
69
-    /**
70
-	 * The state for the submission.
71
-	 *
72
-	 * @since 1.0.19
73
-	 * @var string
74
-	 */
75
-	public $state = null;
76
-
77
-	/**
78
-	 * The invoice associated with the submission.
79
-	 *
80
-	 * @var WPInv_Invoice
81
-	 */
82
-	protected $invoice = null;
83
-
84
-	/**
85
-	 * The recurring item for the submission.
86
-	 *
87
-	 * @var int
88
-	 */
89
-	public $has_recurring = 0;
90
-
91
-	/**
92
-	 * An array of fees for the submission.
93
-	 *
94
-	 * @var array
95
-	 */
96
-	protected $fees = array();
97
-
98
-	/**
99
-	 * An array of discounts for the submission.
100
-	 *
101
-	 * @var array
102
-	 */
103
-	protected $discounts = array();
104
-
105
-	/**
106
-	 * An array of taxes for the submission.
107
-	 *
108
-	 * @var array
109
-	 */
110
-	protected $taxes = array();
111
-
112
-	/**
113
-	 * An array of items for the submission.
114
-	 *
115
-	 * @var GetPaid_Form_Item[]
116
-	 */
117
-	protected $items = array();
118
-
119
-	/**
120
-	 * The last error.
121
-	 *
122
-	 * @var string
123
-	 */
124
-	public $last_error = null;
125
-
126
-	/**
127
-	 * The last error code.
128
-	 *
129
-	 * @var string
130
-	 */
131
-	public $last_error_code = null;
132
-
133
-    /**
134
-	 * Class constructor.
135
-	 *
136
-	 */
137
-	public function __construct() {
138
-
139
-		// Set the state and country to the default state and country.
140
-		$this->country = wpinv_default_billing_country();
141
-		$this->state   = wpinv_get_default_state();
142
-
143
-		// Do we have an actual submission?
144
-		if ( isset( $_POST['getpaid_payment_form_submission'] ) ) {
145
-			$this->load_data( $_POST );
146
-		}
147
-
148
-	}
149
-
150
-	/**
151
-	 * Loads submission data.
152
-	 *
153
-	 * @param array $data
154
-	 */
155
-	public function load_data( $data ) {
156
-
157
-		// Remove slashes from the submitted data...
158
-		$data       = wp_kses_post_deep( wp_unslash( $data ) );
159
-
160
-		// Allow plugins to filter the data.
161
-		$data       = apply_filters( 'getpaid_submission_data', $data, $this );
162
-
163
-		// Cache it...
164
-		$this->data = $data;
165
-
166
-		// Then generate a unique id from the data.
167
-		$this->id   = md5( wp_json_encode( $data ) );
168
-
169
-		// Finally, process the submission.
170
-		try {
171
-
172
-			// Each process is passed an instance of the class (with reference)
173
-			// and should throw an Exception whenever it encounters one.
174
-			$processors = apply_filters(
175
-				'getpaid_payment_form_submission_processors',
176
-				array(
177
-					array( $this, 'process_payment_form' ),
178
-					array( $this, 'process_invoice' ),
179
-					array( $this, 'process_fees' ),
180
-					array( $this, 'process_items' ),
181
-					array( $this, 'process_discount' ),
182
-					array( $this, 'process_taxes' ),
183
-				),
184
-				$this		
185
-			);
186
-
187
-			foreach ( $processors as $processor ) {
188
-				call_user_func_array( $processor, array( &$this ) );
189
-			}
190
-
191
-		} catch( GetPaid_Payment_Exception $e ) {
192
-			$this->last_error      = $e->getMessage();
193
-			$this->last_error_code = $e->getErrorCode();
194
-		} catch ( Exception $e ) {
195
-			$this->last_error      = $e->getMessage();
196
-			$this->last_error_code = $e->getCode();
197
-		}
198
-
199
-		// Fired when we are done processing a submission.
200
-		do_action_ref_array( 'getpaid_process_submission', array( &$this ) );
201
-
202
-	}
203
-
204
-	/*
63
+     * The country for the submission.
64
+     *
65
+     * @var string
66
+     */
67
+    public $country = null;
68
+
69
+    /**
70
+     * The state for the submission.
71
+     *
72
+     * @since 1.0.19
73
+     * @var string
74
+     */
75
+    public $state = null;
76
+
77
+    /**
78
+     * The invoice associated with the submission.
79
+     *
80
+     * @var WPInv_Invoice
81
+     */
82
+    protected $invoice = null;
83
+
84
+    /**
85
+     * The recurring item for the submission.
86
+     *
87
+     * @var int
88
+     */
89
+    public $has_recurring = 0;
90
+
91
+    /**
92
+     * An array of fees for the submission.
93
+     *
94
+     * @var array
95
+     */
96
+    protected $fees = array();
97
+
98
+    /**
99
+     * An array of discounts for the submission.
100
+     *
101
+     * @var array
102
+     */
103
+    protected $discounts = array();
104
+
105
+    /**
106
+     * An array of taxes for the submission.
107
+     *
108
+     * @var array
109
+     */
110
+    protected $taxes = array();
111
+
112
+    /**
113
+     * An array of items for the submission.
114
+     *
115
+     * @var GetPaid_Form_Item[]
116
+     */
117
+    protected $items = array();
118
+
119
+    /**
120
+     * The last error.
121
+     *
122
+     * @var string
123
+     */
124
+    public $last_error = null;
125
+
126
+    /**
127
+     * The last error code.
128
+     *
129
+     * @var string
130
+     */
131
+    public $last_error_code = null;
132
+
133
+    /**
134
+     * Class constructor.
135
+     *
136
+     */
137
+    public function __construct() {
138
+
139
+        // Set the state and country to the default state and country.
140
+        $this->country = wpinv_default_billing_country();
141
+        $this->state   = wpinv_get_default_state();
142
+
143
+        // Do we have an actual submission?
144
+        if ( isset( $_POST['getpaid_payment_form_submission'] ) ) {
145
+            $this->load_data( $_POST );
146
+        }
147
+
148
+    }
149
+
150
+    /**
151
+     * Loads submission data.
152
+     *
153
+     * @param array $data
154
+     */
155
+    public function load_data( $data ) {
156
+
157
+        // Remove slashes from the submitted data...
158
+        $data       = wp_kses_post_deep( wp_unslash( $data ) );
159
+
160
+        // Allow plugins to filter the data.
161
+        $data       = apply_filters( 'getpaid_submission_data', $data, $this );
162
+
163
+        // Cache it...
164
+        $this->data = $data;
165
+
166
+        // Then generate a unique id from the data.
167
+        $this->id   = md5( wp_json_encode( $data ) );
168
+
169
+        // Finally, process the submission.
170
+        try {
171
+
172
+            // Each process is passed an instance of the class (with reference)
173
+            // and should throw an Exception whenever it encounters one.
174
+            $processors = apply_filters(
175
+                'getpaid_payment_form_submission_processors',
176
+                array(
177
+                    array( $this, 'process_payment_form' ),
178
+                    array( $this, 'process_invoice' ),
179
+                    array( $this, 'process_fees' ),
180
+                    array( $this, 'process_items' ),
181
+                    array( $this, 'process_discount' ),
182
+                    array( $this, 'process_taxes' ),
183
+                ),
184
+                $this		
185
+            );
186
+
187
+            foreach ( $processors as $processor ) {
188
+                call_user_func_array( $processor, array( &$this ) );
189
+            }
190
+
191
+        } catch( GetPaid_Payment_Exception $e ) {
192
+            $this->last_error      = $e->getMessage();
193
+            $this->last_error_code = $e->getErrorCode();
194
+        } catch ( Exception $e ) {
195
+            $this->last_error      = $e->getMessage();
196
+            $this->last_error_code = $e->getCode();
197
+        }
198
+
199
+        // Fired when we are done processing a submission.
200
+        do_action_ref_array( 'getpaid_process_submission', array( &$this ) );
201
+
202
+    }
203
+
204
+    /*
205 205
 	|--------------------------------------------------------------------------
206 206
 	| Payment Forms.
207 207
 	|--------------------------------------------------------------------------
@@ -210,39 +210,39 @@  discard block
 block discarded – undo
210 210
 	| submission has an active payment form etc.
211 211
     */
212 212
 
213
-	/**
214
-	 * Prepares the submission's payment form.
215
-	 *
216
-	 * @since 1.0.19
217
-	 */
218
-	public function process_payment_form() {
213
+    /**
214
+     * Prepares the submission's payment form.
215
+     *
216
+     * @since 1.0.19
217
+     */
218
+    public function process_payment_form() {
219 219
 
220
-		// Every submission needs an active payment form.
221
-		if ( empty( $this->data['form_id'] ) ) {
222
-			throw new Exception( __( 'Missing payment form', 'invoicing' ) );
223
-		}
220
+        // Every submission needs an active payment form.
221
+        if ( empty( $this->data['form_id'] ) ) {
222
+            throw new Exception( __( 'Missing payment form', 'invoicing' ) );
223
+        }
224 224
 
225
-		// Fetch the payment form.
226
-		$this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] );
225
+        // Fetch the payment form.
226
+        $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] );
227 227
 
228
-		if ( ! $this->payment_form->is_active() ) {
229
-			throw new Exception( __( 'Payment form not active', 'invoicing' ) );
230
-		}
228
+        if ( ! $this->payment_form->is_active() ) {
229
+            throw new Exception( __( 'Payment form not active', 'invoicing' ) );
230
+        }
231 231
 
232
-		do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) );
233
-	}
232
+        do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) );
233
+    }
234 234
 
235 235
     /**
236
-	 * Returns the payment form.
237
-	 *
238
-	 * @since 1.0.19
239
-	 * @return GetPaid_Payment_Form
240
-	 */
241
-	public function get_payment_form() {
242
-		return $this->payment_form;
243
-	}
236
+     * Returns the payment form.
237
+     *
238
+     * @since 1.0.19
239
+     * @return GetPaid_Payment_Form
240
+     */
241
+    public function get_payment_form() {
242
+        return $this->payment_form;
243
+    }
244 244
 
245
-	/*
245
+    /*
246 246
 	|--------------------------------------------------------------------------
247 247
 	| Invoices.
248 248
 	|--------------------------------------------------------------------------
@@ -251,84 +251,84 @@  discard block
 block discarded – undo
251 251
 	| might be for an existing invoice.
252 252
 	*/
253 253
 
254
-	/**
255
-	 * Prepares the submission's invoice.
256
-	 *
257
-	 * @since 1.0.19
258
-	 */
259
-	public function process_invoice() {
254
+    /**
255
+     * Prepares the submission's invoice.
256
+     *
257
+     * @since 1.0.19
258
+     */
259
+    public function process_invoice() {
260 260
 
261
-		// Abort if there is no invoice.
262
-		if ( empty( $this->data['invoice_id'] ) ) {
263
-			return;
264
-		}
261
+        // Abort if there is no invoice.
262
+        if ( empty( $this->data['invoice_id'] ) ) {
263
+            return;
264
+        }
265 265
 
266
-		// If the submission is for an existing invoice, ensure that it exists
267
-		// and that it is not paid for.
268
-		$invoice = wpinv_get_invoice( $this->data['invoice_id'] );
266
+        // If the submission is for an existing invoice, ensure that it exists
267
+        // and that it is not paid for.
268
+        $invoice = wpinv_get_invoice( $this->data['invoice_id'] );
269 269
 
270 270
         if ( empty( $invoice ) ) {
271
-			throw new Exception( __( 'Invalid invoice', 'invoicing' ) );
272
-		}
271
+            throw new Exception( __( 'Invalid invoice', 'invoicing' ) );
272
+        }
273 273
 
274
-		if ( $invoice->is_paid() ) {
275
-			throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) );
276
-		}
274
+        if ( $invoice->is_paid() ) {
275
+            throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) );
276
+        }
277 277
 
278
-		$this->payment_form->invoice = $invoice;
279
-		if ( ! $this->payment_form->is_default() ) {
278
+        $this->payment_form->invoice = $invoice;
279
+        if ( ! $this->payment_form->is_default() ) {
280 280
 
281
-			$items    = array();
282
-			$item_ids = array();
281
+            $items    = array();
282
+            $item_ids = array();
283 283
 	
284
-			foreach ( $invoice->get_items() as $item ) {
285
-				if ( ! in_array( $item->get_id(), $item_ids ) ) {
286
-					$item_ids[] = $item->get_id();
287
-					$items[]    = $item;
288
-				}
289
-			}
284
+            foreach ( $invoice->get_items() as $item ) {
285
+                if ( ! in_array( $item->get_id(), $item_ids ) ) {
286
+                    $item_ids[] = $item->get_id();
287
+                    $items[]    = $item;
288
+                }
289
+            }
290 290
 	
291
-			foreach ( $this->payment_form->get_items() as $item ) {
292
-				if ( ! in_array( $item->get_id(), $item_ids ) ) {
293
-					$item_ids[] = $item->get_id();
294
-					$items[]    = $item;
295
-				}
296
-			}
291
+            foreach ( $this->payment_form->get_items() as $item ) {
292
+                if ( ! in_array( $item->get_id(), $item_ids ) ) {
293
+                    $item_ids[] = $item->get_id();
294
+                    $items[]    = $item;
295
+                }
296
+            }
297 297
 	
298
-			$this->payment_form->set_items( $items );
298
+            $this->payment_form->set_items( $items );
299 299
 	
300
-		} else {
301
-			$this->payment_form->set_items( $invoice->get_items() );
302
-		}
303
-
304
-		$this->country = $invoice->get_country();
305
-		$this->state   = $invoice->get_state();
306
-		$this->invoice = $invoice;
307
-
308
-		do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) );
309
-	}
310
-
311
-	/**
312
-	 * Returns the associated invoice.
313
-	 *
314
-	 * @since 1.0.19
315
-	 * @return WPInv_Invoice
316
-	 */
317
-	public function get_invoice() {
318
-		return $this->invoice;
319
-	}
320
-
321
-	/**
322
-	 * Checks whether there is an invoice associated with this submission.
323
-	 *
324
-	 * @since 1.0.19
325
-	 * @return bool
326
-	 */
327
-	public function has_invoice() {
328
-		return ! empty( $this->invoice );
329
-	}
330
-
331
-	/*
300
+        } else {
301
+            $this->payment_form->set_items( $invoice->get_items() );
302
+        }
303
+
304
+        $this->country = $invoice->get_country();
305
+        $this->state   = $invoice->get_state();
306
+        $this->invoice = $invoice;
307
+
308
+        do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) );
309
+    }
310
+
311
+    /**
312
+     * Returns the associated invoice.
313
+     *
314
+     * @since 1.0.19
315
+     * @return WPInv_Invoice
316
+     */
317
+    public function get_invoice() {
318
+        return $this->invoice;
319
+    }
320
+
321
+    /**
322
+     * Checks whether there is an invoice associated with this submission.
323
+     *
324
+     * @since 1.0.19
325
+     * @return bool
326
+     */
327
+    public function has_invoice() {
328
+        return ! empty( $this->invoice );
329
+    }
330
+
331
+    /*
332 332
 	|--------------------------------------------------------------------------
333 333
 	| Items.
334 334
 	|--------------------------------------------------------------------------
@@ -337,129 +337,129 @@  discard block
 block discarded – undo
337 337
 	| recurring item. But can have an unlimited number of non-recurring items.
338 338
 	*/
339 339
 
340
-	/**
341
-	 * Prepares the submission's items.
342
-	 *
343
-	 * @since 1.0.19
344
-	 */
345
-	public function process_items() {
346
-
347
-		$processor = new GetPaid_Payment_Form_Submission_Items( $this );
348
-
349
-		foreach ( $processor->items as $item ) {
350
-			$this->add_item( $item );
351
-		}
352
-
353
-		do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) );
354
-	}
355
-
356
-	/**
357
-	 * Adds an item to the submission.
358
-	 *
359
-	 * @since 1.0.19
360
-	 * @param GetPaid_Form_Item $item
361
-	 */
362
-	public function add_item( $item ) {
363
-
364
-		// Make sure that it is available for purchase.
365
-		if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) {
366
-			return;
367
-		}
368
-
369
-		// Each submission can only contain one recurring item.
370
-		if ( $item->is_recurring() ) {
371
-			$this->has_recurring = $item->get_id();
372
-		}
373
-
374
-		// Update the items and totals.
375
-		$this->items[ $item->get_id() ]         = $item;
376
-		$this->totals['subtotal']['initial']   += $item->get_sub_total();
377
-		$this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total();
378
-
379
-	}
380
-
381
-	/**
382
-	 * Removes a specific item.
383
-	 * 
384
-	 * You should not call this method after the discounts and taxes
385
-	 * have been calculated.
386
-	 *
387
-	 * @since 1.0.19
388
-	 */
389
-	public function remove_item( $item_id ) {
390
-
391
-		if ( isset( $this->items[ $item_id ] ) ) {
392
-			$this->totals['subtotal']['initial']   -= $this->items[ $item_id ]->get_sub_total();
393
-			$this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total();
394
-
395
-			if ( $this->items[ $item_id ]->is_recurring() ) {
396
-				$this->has_recurring = 0;
397
-			}
398
-
399
-			unset( $this->items[ $item_id ] );
400
-		}
401
-
402
-	}
403
-
404
-	/**
405
-	 * Returns the subtotal.
406
-	 *
407
-	 * @since 1.0.19
408
-	 */
409
-	public function get_subtotal() {
410
-
411
-		if ( wpinv_prices_include_tax() ) {
412
-			return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial'];
413
-		}
414
-
415
-		return $this->totals['subtotal']['initial'];
416
-	}
417
-
418
-	/**
419
-	 * Returns the recurring subtotal.
420
-	 *
421
-	 * @since 1.0.19
422
-	 */
423
-	public function get_recurring_subtotal() {
424
-
425
-		if ( wpinv_prices_include_tax() ) {
426
-			return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring'];
427
-		}
428
-
429
-		return $this->totals['subtotal']['recurring'];
430
-	}
431
-
432
-	/**
433
-	 * Returns all items.
434
-	 *
435
-	 * @since 1.0.19
436
-	 * @return GetPaid_Form_Item[]
437
-	 */
438
-	public function get_items() {
439
-		return $this->items;
440
-	}
441
-
442
-	/**
443
-	 * Checks if there's a single subscription group in the submission.
444
-	 *
445
-	 * @since 2.3.0
446
-	 * @return bool
447
-	 */
448
-	public function has_subscription_group() {
449
-		return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) );
450
-	}
451
-
452
-	/**
453
-	 * Checks if there are multipe subscription groups in the submission.
454
-	 *
455
-	 * @since 2.3.0
456
-	 * @return bool
457
-	 */
458
-	public function has_multiple_subscription_groups() {
459
-		return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) );
460
-	}
461
-
462
-	/*
340
+    /**
341
+     * Prepares the submission's items.
342
+     *
343
+     * @since 1.0.19
344
+     */
345
+    public function process_items() {
346
+
347
+        $processor = new GetPaid_Payment_Form_Submission_Items( $this );
348
+
349
+        foreach ( $processor->items as $item ) {
350
+            $this->add_item( $item );
351
+        }
352
+
353
+        do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) );
354
+    }
355
+
356
+    /**
357
+     * Adds an item to the submission.
358
+     *
359
+     * @since 1.0.19
360
+     * @param GetPaid_Form_Item $item
361
+     */
362
+    public function add_item( $item ) {
363
+
364
+        // Make sure that it is available for purchase.
365
+        if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) {
366
+            return;
367
+        }
368
+
369
+        // Each submission can only contain one recurring item.
370
+        if ( $item->is_recurring() ) {
371
+            $this->has_recurring = $item->get_id();
372
+        }
373
+
374
+        // Update the items and totals.
375
+        $this->items[ $item->get_id() ]         = $item;
376
+        $this->totals['subtotal']['initial']   += $item->get_sub_total();
377
+        $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total();
378
+
379
+    }
380
+
381
+    /**
382
+     * Removes a specific item.
383
+     * 
384
+     * You should not call this method after the discounts and taxes
385
+     * have been calculated.
386
+     *
387
+     * @since 1.0.19
388
+     */
389
+    public function remove_item( $item_id ) {
390
+
391
+        if ( isset( $this->items[ $item_id ] ) ) {
392
+            $this->totals['subtotal']['initial']   -= $this->items[ $item_id ]->get_sub_total();
393
+            $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total();
394
+
395
+            if ( $this->items[ $item_id ]->is_recurring() ) {
396
+                $this->has_recurring = 0;
397
+            }
398
+
399
+            unset( $this->items[ $item_id ] );
400
+        }
401
+
402
+    }
403
+
404
+    /**
405
+     * Returns the subtotal.
406
+     *
407
+     * @since 1.0.19
408
+     */
409
+    public function get_subtotal() {
410
+
411
+        if ( wpinv_prices_include_tax() ) {
412
+            return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial'];
413
+        }
414
+
415
+        return $this->totals['subtotal']['initial'];
416
+    }
417
+
418
+    /**
419
+     * Returns the recurring subtotal.
420
+     *
421
+     * @since 1.0.19
422
+     */
423
+    public function get_recurring_subtotal() {
424
+
425
+        if ( wpinv_prices_include_tax() ) {
426
+            return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring'];
427
+        }
428
+
429
+        return $this->totals['subtotal']['recurring'];
430
+    }
431
+
432
+    /**
433
+     * Returns all items.
434
+     *
435
+     * @since 1.0.19
436
+     * @return GetPaid_Form_Item[]
437
+     */
438
+    public function get_items() {
439
+        return $this->items;
440
+    }
441
+
442
+    /**
443
+     * Checks if there's a single subscription group in the submission.
444
+     *
445
+     * @since 2.3.0
446
+     * @return bool
447
+     */
448
+    public function has_subscription_group() {
449
+        return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) );
450
+    }
451
+
452
+    /**
453
+     * Checks if there are multipe subscription groups in the submission.
454
+     *
455
+     * @since 2.3.0
456
+     * @return bool
457
+     */
458
+    public function has_multiple_subscription_groups() {
459
+        return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) );
460
+    }
461
+
462
+    /*
463 463
 	|--------------------------------------------------------------------------
464 464
 	| Taxes
465 465
 	|--------------------------------------------------------------------------
@@ -468,128 +468,128 @@  discard block
 block discarded – undo
468 468
 	| or only one-time.
469 469
     */
470 470
 
471
-	/**
472
-	 * Prepares the submission's taxes.
473
-	 *
474
-	 * @since 1.0.19
475
-	 */
476
-	public function process_taxes() {
477
-
478
-		// Abort if we're not using taxes.
479
-		if ( ! $this->use_taxes() ) {
480
-			return;
481
-		}
482
-
483
-		// If a custom country && state has been passed in, use it to calculate taxes.
484
-		$country = $this->get_field( 'wpinv_country', 'billing' );
485
-		if ( ! empty( $country ) ) {
486
-			$this->country = $country;
487
-		}
488
-
489
-		$state = $this->get_field( 'wpinv_state', 'billing' );
490
-		if ( ! empty( $state ) ) {
491
-			$this->state = $state;
492
-		}
493
-
494
-		// Confirm if the provided country and the ip country are similar.
495
-		$address_confirmed = $this->get_field( 'confirm-address' );
496
-		if ( isset( $_POST['billing']['country'] ) && wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) {
497
-			throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) );
498
-		}
499
-
500
-		// Abort if the country is not taxable.
501
-		if ( ! wpinv_is_country_taxable( $this->country ) ) {
502
-			return;
503
-		}
504
-
505
-		$processor = new GetPaid_Payment_Form_Submission_Taxes( $this );
506
-
507
-		foreach ( $processor->taxes as $tax ) {
508
-			$this->add_tax( $tax );
509
-		}
510
-
511
-		do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) );
512
-	}
513
-
514
-	/**
515
-	 * Adds a tax to the submission.
516
-	 *
517
-	 * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required.
518
-	 * @since 1.0.19
519
-	 */
520
-	public function add_tax( $tax ) {
521
-
522
-		if ( wpinv_round_tax_per_tax_rate() ) {
523
-			$tax['initial_tax']   = wpinv_round_amount( $tax['initial_tax'] );
524
-			$tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] );
525
-		}
526
-
527
-		$this->taxes[ $tax['name'] ]         = $tax;
528
-		$this->totals['taxes']['initial']   += wpinv_sanitize_amount( $tax['initial_tax'] );
529
-		$this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] );
530
-
531
-	}
532
-
533
-	/**
534
-	 * Removes a specific tax.
535
-	 *
536
-	 * @since 1.0.19
537
-	 */
538
-	public function remove_tax( $tax_name ) {
539
-
540
-		if ( isset( $this->taxes[ $tax_name ] ) ) {
541
-			$this->totals['taxes']['initial']   -= $this->taxes[ $tax_name ]['initial_tax'];
542
-			$this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax'];
543
-			unset( $this->taxes[ $tax_name ] );
544
-		}
545
-
546
-	}
547
-
548
-	/**
549
-	 * Whether or not we'll use taxes for the submission.
550
-	 *
551
-	 * @since 1.0.19
552
-	 */
553
-	public function use_taxes() {
554
-
555
-		$use_taxes = wpinv_use_taxes();
556
-
557
-		if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) {
558
-			$use_taxes = false;
559
-		}
560
-
561
-		return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this );
562
-
563
-	}
564
-
565
-	/**
566
-	 * Returns the tax.
567
-	 *
568
-	 * @since 1.0.19
569
-	 */
570
-	public function get_tax() {
571
-		return $this->totals['taxes']['initial'];
572
-	}
573
-
574
-	/**
575
-	 * Returns the recurring tax.
576
-	 *
577
-	 * @since 1.0.19
578
-	 */
579
-	public function get_recurring_tax() {
580
-		return $this->totals['taxes']['recurring'];
581
-	}
582
-
583
-	/**
584
-	 * Returns all taxes.
585
-	 *
586
-	 * @since 1.0.19
587
-	 */
588
-	public function get_taxes() {
589
-		return $this->taxes;
590
-	}
591
-
592
-	/*
471
+    /**
472
+     * Prepares the submission's taxes.
473
+     *
474
+     * @since 1.0.19
475
+     */
476
+    public function process_taxes() {
477
+
478
+        // Abort if we're not using taxes.
479
+        if ( ! $this->use_taxes() ) {
480
+            return;
481
+        }
482
+
483
+        // If a custom country && state has been passed in, use it to calculate taxes.
484
+        $country = $this->get_field( 'wpinv_country', 'billing' );
485
+        if ( ! empty( $country ) ) {
486
+            $this->country = $country;
487
+        }
488
+
489
+        $state = $this->get_field( 'wpinv_state', 'billing' );
490
+        if ( ! empty( $state ) ) {
491
+            $this->state = $state;
492
+        }
493
+
494
+        // Confirm if the provided country and the ip country are similar.
495
+        $address_confirmed = $this->get_field( 'confirm-address' );
496
+        if ( isset( $_POST['billing']['country'] ) && wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) {
497
+            throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) );
498
+        }
499
+
500
+        // Abort if the country is not taxable.
501
+        if ( ! wpinv_is_country_taxable( $this->country ) ) {
502
+            return;
503
+        }
504
+
505
+        $processor = new GetPaid_Payment_Form_Submission_Taxes( $this );
506
+
507
+        foreach ( $processor->taxes as $tax ) {
508
+            $this->add_tax( $tax );
509
+        }
510
+
511
+        do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) );
512
+    }
513
+
514
+    /**
515
+     * Adds a tax to the submission.
516
+     *
517
+     * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required.
518
+     * @since 1.0.19
519
+     */
520
+    public function add_tax( $tax ) {
521
+
522
+        if ( wpinv_round_tax_per_tax_rate() ) {
523
+            $tax['initial_tax']   = wpinv_round_amount( $tax['initial_tax'] );
524
+            $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] );
525
+        }
526
+
527
+        $this->taxes[ $tax['name'] ]         = $tax;
528
+        $this->totals['taxes']['initial']   += wpinv_sanitize_amount( $tax['initial_tax'] );
529
+        $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] );
530
+
531
+    }
532
+
533
+    /**
534
+     * Removes a specific tax.
535
+     *
536
+     * @since 1.0.19
537
+     */
538
+    public function remove_tax( $tax_name ) {
539
+
540
+        if ( isset( $this->taxes[ $tax_name ] ) ) {
541
+            $this->totals['taxes']['initial']   -= $this->taxes[ $tax_name ]['initial_tax'];
542
+            $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax'];
543
+            unset( $this->taxes[ $tax_name ] );
544
+        }
545
+
546
+    }
547
+
548
+    /**
549
+     * Whether or not we'll use taxes for the submission.
550
+     *
551
+     * @since 1.0.19
552
+     */
553
+    public function use_taxes() {
554
+
555
+        $use_taxes = wpinv_use_taxes();
556
+
557
+        if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) {
558
+            $use_taxes = false;
559
+        }
560
+
561
+        return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this );
562
+
563
+    }
564
+
565
+    /**
566
+     * Returns the tax.
567
+     *
568
+     * @since 1.0.19
569
+     */
570
+    public function get_tax() {
571
+        return $this->totals['taxes']['initial'];
572
+    }
573
+
574
+    /**
575
+     * Returns the recurring tax.
576
+     *
577
+     * @since 1.0.19
578
+     */
579
+    public function get_recurring_tax() {
580
+        return $this->totals['taxes']['recurring'];
581
+    }
582
+
583
+    /**
584
+     * Returns all taxes.
585
+     *
586
+     * @since 1.0.19
587
+     */
588
+    public function get_taxes() {
589
+        return $this->taxes;
590
+    }
591
+
592
+    /*
593 593
 	|--------------------------------------------------------------------------
594 594
 	| Discounts
595 595
 	|--------------------------------------------------------------------------
@@ -598,99 +598,99 @@  discard block
 block discarded – undo
598 598
 	| or only one-time. They also do not have to come from a discount code.
599 599
     */
600 600
 
601
-	/**
602
-	 * Prepares the submission's discount.
603
-	 *
604
-	 * @since 1.0.19
605
-	 */
606
-	public function process_discount() {
607
-
608
-		$initial_total    = $this->get_subtotal() + $this->get_fee() + $this->get_tax();
609
-		$recurring_total  = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax();
610
-		$processor        = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total );
611
-
612
-		foreach ( $processor->discounts as $discount ) {
613
-			$this->add_discount( $discount );
614
-		}
615
-
616
-		do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) );
617
-	}
618
-
619
-	/**
620
-	 * Adds a discount to the submission.
621
-	 *
622
-	 * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code.
623
-	 * @since 1.0.19
624
-	 */
625
-	public function add_discount( $discount ) {
626
-		$this->discounts[ $discount['name'] ]   = $discount;
627
-		$this->totals['discount']['initial']   += wpinv_sanitize_amount( $discount['initial_discount'] );
628
-		$this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] );
629
-	}
630
-
631
-	/**
632
-	 * Removes a discount from the submission.
633
-	 *
634
-	 * @since 1.0.19
635
-	 */
636
-	public function remove_discount( $name ) {
637
-
638
-		if ( isset( $this->discounts[ $name ] ) ) {
639
-			$this->totals['discount']['initial']   -= $this->discounts[ $name ]['initial_discount'];
640
-			$this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount'];
641
-			unset( $this->discounts[ $name ] );
642
-		}
643
-
644
-	}
645
-
646
-	/**
647
-	 * Checks whether there is a discount code associated with this submission.
648
-	 *
649
-	 * @since 1.0.19
650
-	 * @return bool
651
-	 */
652
-	public function has_discount_code() {
653
-		return ! empty( $this->discounts['discount_code'] );
654
-	}
655
-
656
-	/**
657
-	 * Returns the discount code.
658
-	 *
659
-	 * @since 1.0.19
660
-	 * @return string
661
-	 */
662
-	public function get_discount_code() {
663
-		return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : '';
664
-	}
665
-
666
-	/**
667
-	 * Returns the discount.
668
-	 *
669
-	 * @since 1.0.19
670
-	 */
671
-	public function get_discount() {
672
-		return $this->totals['discount']['initial'];
673
-	}
674
-
675
-	/**
676
-	 * Returns the recurring discount.
677
-	 *
678
-	 * @since 1.0.19
679
-	 */
680
-	public function get_recurring_discount() {
681
-		return $this->totals['discount']['recurring'];
682
-	}
683
-
684
-	/**
685
-	 * Returns all discounts.
686
-	 *
687
-	 * @since 1.0.19
688
-	 */
689
-	public function get_discounts() {
690
-		return $this->discounts;
691
-	}
692
-
693
-	/*
601
+    /**
602
+     * Prepares the submission's discount.
603
+     *
604
+     * @since 1.0.19
605
+     */
606
+    public function process_discount() {
607
+
608
+        $initial_total    = $this->get_subtotal() + $this->get_fee() + $this->get_tax();
609
+        $recurring_total  = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax();
610
+        $processor        = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total );
611
+
612
+        foreach ( $processor->discounts as $discount ) {
613
+            $this->add_discount( $discount );
614
+        }
615
+
616
+        do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) );
617
+    }
618
+
619
+    /**
620
+     * Adds a discount to the submission.
621
+     *
622
+     * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code.
623
+     * @since 1.0.19
624
+     */
625
+    public function add_discount( $discount ) {
626
+        $this->discounts[ $discount['name'] ]   = $discount;
627
+        $this->totals['discount']['initial']   += wpinv_sanitize_amount( $discount['initial_discount'] );
628
+        $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] );
629
+    }
630
+
631
+    /**
632
+     * Removes a discount from the submission.
633
+     *
634
+     * @since 1.0.19
635
+     */
636
+    public function remove_discount( $name ) {
637
+
638
+        if ( isset( $this->discounts[ $name ] ) ) {
639
+            $this->totals['discount']['initial']   -= $this->discounts[ $name ]['initial_discount'];
640
+            $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount'];
641
+            unset( $this->discounts[ $name ] );
642
+        }
643
+
644
+    }
645
+
646
+    /**
647
+     * Checks whether there is a discount code associated with this submission.
648
+     *
649
+     * @since 1.0.19
650
+     * @return bool
651
+     */
652
+    public function has_discount_code() {
653
+        return ! empty( $this->discounts['discount_code'] );
654
+    }
655
+
656
+    /**
657
+     * Returns the discount code.
658
+     *
659
+     * @since 1.0.19
660
+     * @return string
661
+     */
662
+    public function get_discount_code() {
663
+        return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : '';
664
+    }
665
+
666
+    /**
667
+     * Returns the discount.
668
+     *
669
+     * @since 1.0.19
670
+     */
671
+    public function get_discount() {
672
+        return $this->totals['discount']['initial'];
673
+    }
674
+
675
+    /**
676
+     * Returns the recurring discount.
677
+     *
678
+     * @since 1.0.19
679
+     */
680
+    public function get_recurring_discount() {
681
+        return $this->totals['discount']['recurring'];
682
+    }
683
+
684
+    /**
685
+     * Returns all discounts.
686
+     *
687
+     * @since 1.0.19
688
+     */
689
+    public function get_discounts() {
690
+        return $this->discounts;
691
+    }
692
+
693
+    /*
694 694
 	|--------------------------------------------------------------------------
695 695
 	| Fees
696 696
 	|--------------------------------------------------------------------------
@@ -700,89 +700,89 @@  discard block
 block discarded – undo
700 700
 	| fees.
701 701
     */
702 702
 
703
-	/**
704
-	 * Prepares the submission's fees.
705
-	 *
706
-	 * @since 1.0.19
707
-	 */
708
-	public function process_fees() {
709
-
710
-		$fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this );
711
-
712
-		foreach ( $fees_processor->fees as $fee ) {
713
-			$this->add_fee( $fee );
714
-		}
715
-
716
-		do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) );
717
-	}
718
-
719
-	/**
720
-	 * Adds a fee to the submission.
721
-	 *
722
-	 * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required.
723
-	 * @since 1.0.19
724
-	 */
725
-	public function add_fee( $fee ) {
726
-
727
-		$this->fees[ $fee['name'] ]         = $fee;
728
-		$this->totals['fees']['initial']   += wpinv_sanitize_amount( $fee['initial_fee'] );
729
-		$this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] );
730
-
731
-	}
732
-
733
-	/**
734
-	 * Removes a fee from the submission.
735
-	 *
736
-	 * @since 1.0.19
737
-	 */
738
-	public function remove_fee( $name ) {
739
-
740
-		if ( isset( $this->fees[ $name ] ) ) {
741
-			$this->totals['fees']['initial']   -= $this->fees[ $name ]['initial_fee'];
742
-			$this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee'];
743
-			unset( $this->fees[ $name ] );
744
-		}
745
-
746
-	}
747
-
748
-	/**
749
-	 * Returns the fees.
750
-	 *
751
-	 * @since 1.0.19
752
-	 */
753
-	public function get_fee() {
754
-		return $this->totals['fees']['initial'];
755
-	}
756
-
757
-	/**
758
-	 * Returns the recurring fees.
759
-	 *
760
-	 * @since 1.0.19
761
-	 */
762
-	public function get_recurring_fee() {
763
-		return $this->totals['fees']['recurring'];
764
-	}
765
-
766
-	/**
767
-	 * Returns all fees.
768
-	 *
769
-	 * @since 1.0.19
770
-	 */
771
-	public function get_fees() {
772
-		return $this->fees;
773
-	}
774
-
775
-	/**
776
-	 * Checks if there are any fees for the form.
777
-	 *
778
-	 * @return bool
779
-	 * @since 1.0.19
780
-	 */
781
-	public function has_fees() {
782
-		return count( $this->fees ) !== 0;
783
-	}
784
-
785
-	/*
703
+    /**
704
+     * Prepares the submission's fees.
705
+     *
706
+     * @since 1.0.19
707
+     */
708
+    public function process_fees() {
709
+
710
+        $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this );
711
+
712
+        foreach ( $fees_processor->fees as $fee ) {
713
+            $this->add_fee( $fee );
714
+        }
715
+
716
+        do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) );
717
+    }
718
+
719
+    /**
720
+     * Adds a fee to the submission.
721
+     *
722
+     * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required.
723
+     * @since 1.0.19
724
+     */
725
+    public function add_fee( $fee ) {
726
+
727
+        $this->fees[ $fee['name'] ]         = $fee;
728
+        $this->totals['fees']['initial']   += wpinv_sanitize_amount( $fee['initial_fee'] );
729
+        $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] );
730
+
731
+    }
732
+
733
+    /**
734
+     * Removes a fee from the submission.
735
+     *
736
+     * @since 1.0.19
737
+     */
738
+    public function remove_fee( $name ) {
739
+
740
+        if ( isset( $this->fees[ $name ] ) ) {
741
+            $this->totals['fees']['initial']   -= $this->fees[ $name ]['initial_fee'];
742
+            $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee'];
743
+            unset( $this->fees[ $name ] );
744
+        }
745
+
746
+    }
747
+
748
+    /**
749
+     * Returns the fees.
750
+     *
751
+     * @since 1.0.19
752
+     */
753
+    public function get_fee() {
754
+        return $this->totals['fees']['initial'];
755
+    }
756
+
757
+    /**
758
+     * Returns the recurring fees.
759
+     *
760
+     * @since 1.0.19
761
+     */
762
+    public function get_recurring_fee() {
763
+        return $this->totals['fees']['recurring'];
764
+    }
765
+
766
+    /**
767
+     * Returns all fees.
768
+     *
769
+     * @since 1.0.19
770
+     */
771
+    public function get_fees() {
772
+        return $this->fees;
773
+    }
774
+
775
+    /**
776
+     * Checks if there are any fees for the form.
777
+     *
778
+     * @return bool
779
+     * @since 1.0.19
780
+     */
781
+    public function has_fees() {
782
+        return count( $this->fees ) !== 0;
783
+    }
784
+
785
+    /*
786 786
 	|--------------------------------------------------------------------------
787 787
 	| MISC
788 788
 	|--------------------------------------------------------------------------
@@ -790,119 +790,119 @@  discard block
 block discarded – undo
790 790
 	| Extra submission functions.
791 791
     */
792 792
 
793
-	/**
794
-	 * Checks if this is the initial fetch.
795
-	 *
796
-	 * @return bool
797
-	 * @since 1.0.19
798
-	 */
799
-	public function is_initial_fetch() {
800
-		return empty( $this->data['initial_state'] );
801
-	}
802
-
803
-	/**
804
-	 * Returns the total amount to collect for this submission.
805
-	 *
806
-	 * @since 1.0.19
807
-	 */
808
-	public function get_total() {
809
-		$total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount();
810
-		return max( $total, 0 );
811
-	}
812
-
813
-	/**
814
-	 * Returns the recurring total amount to collect for this submission.
815
-	 *
816
-	 * @since 1.0.19
817
-	 */
818
-	public function get_recurring_total() {
819
-		$total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount();
820
-		return max( $total, 0 );
821
-	}
822
-
823
-	/**
824
-	 * Whether payment details should be collected for this submission.
825
-	 *
826
-	 * @since 1.0.19
827
-	 */
828
-	public function should_collect_payment_details() {
829
-		$initial   = $this->get_total();
830
-		$recurring = $this->get_recurring_total();
831
-
832
-		if ( $this->has_recurring == 0 ) {
833
-			$recurring = 0;
834
-		}
835
-
836
-		$collect = $initial > 0 || $recurring > 0;
837
-		return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this  );
838
-	}
839
-
840
-	/**
841
-	 * Returns the billing email of the user.
842
-	 *
843
-	 * @since 1.0.19
844
-	 */
845
-	public function get_billing_email() {
846
-		return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this  );
847
-	}
848
-
849
-	/**
850
-	 * Checks if the submitter has a billing email.
851
-	 *
852
-	 * @since 1.0.19
853
-	 */
854
-	public function has_billing_email() {
855
-		$billing_email = $this->get_billing_email();
856
-		return ! empty( $billing_email ) && is_email( $billing_email );
857
-	}
858
-
859
-	/**
860
-	 * Returns the appropriate currency for the submission.
861
-	 *
862
-	 * @since 1.0.19
863
-	 * @return string
864
-	 */
865
-	public function get_currency() {
866
-		return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency();
867
-    }
868
-
869
-    /**
870
-	 * Returns the raw submission data.
871
-	 *
872
-	 * @since 1.0.19
873
-	 * @return array
874
-	 */
875
-	public function get_data() {
876
-		return $this->data;
877
-	}
878
-
879
-	/**
880
-	 * Returns a field from the submission data
881
-	 *
882
-	 * @param string $field
883
-	 * @since 1.0.19
884
-	 * @return mixed|null
885
-	 */
886
-	public function get_field( $field, $sub_array_key = null ) {
887
-		return getpaid_get_array_field( $this->data, $field, $sub_array_key );
888
-	}
889
-
890
-	/**
891
-	 * Checks if a required field is set.
892
-	 *
893
-	 * @since 1.0.19
894
-	 */
895
-	public function is_required_field_set( $field ) {
896
-		return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] );
897
-	}
898
-
899
-	/**
900
-	 * Formats an amount
901
-	 *
902
-	 * @since 1.0.19
903
-	 */
904
-	public function format_amount( $amount ) {
905
-		return wpinv_price( $amount, $this->get_currency() );
906
-	}
793
+    /**
794
+     * Checks if this is the initial fetch.
795
+     *
796
+     * @return bool
797
+     * @since 1.0.19
798
+     */
799
+    public function is_initial_fetch() {
800
+        return empty( $this->data['initial_state'] );
801
+    }
802
+
803
+    /**
804
+     * Returns the total amount to collect for this submission.
805
+     *
806
+     * @since 1.0.19
807
+     */
808
+    public function get_total() {
809
+        $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount();
810
+        return max( $total, 0 );
811
+    }
812
+
813
+    /**
814
+     * Returns the recurring total amount to collect for this submission.
815
+     *
816
+     * @since 1.0.19
817
+     */
818
+    public function get_recurring_total() {
819
+        $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount();
820
+        return max( $total, 0 );
821
+    }
822
+
823
+    /**
824
+     * Whether payment details should be collected for this submission.
825
+     *
826
+     * @since 1.0.19
827
+     */
828
+    public function should_collect_payment_details() {
829
+        $initial   = $this->get_total();
830
+        $recurring = $this->get_recurring_total();
831
+
832
+        if ( $this->has_recurring == 0 ) {
833
+            $recurring = 0;
834
+        }
835
+
836
+        $collect = $initial > 0 || $recurring > 0;
837
+        return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this  );
838
+    }
839
+
840
+    /**
841
+     * Returns the billing email of the user.
842
+     *
843
+     * @since 1.0.19
844
+     */
845
+    public function get_billing_email() {
846
+        return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this  );
847
+    }
848
+
849
+    /**
850
+     * Checks if the submitter has a billing email.
851
+     *
852
+     * @since 1.0.19
853
+     */
854
+    public function has_billing_email() {
855
+        $billing_email = $this->get_billing_email();
856
+        return ! empty( $billing_email ) && is_email( $billing_email );
857
+    }
858
+
859
+    /**
860
+     * Returns the appropriate currency for the submission.
861
+     *
862
+     * @since 1.0.19
863
+     * @return string
864
+     */
865
+    public function get_currency() {
866
+        return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency();
867
+    }
868
+
869
+    /**
870
+     * Returns the raw submission data.
871
+     *
872
+     * @since 1.0.19
873
+     * @return array
874
+     */
875
+    public function get_data() {
876
+        return $this->data;
877
+    }
878
+
879
+    /**
880
+     * Returns a field from the submission data
881
+     *
882
+     * @param string $field
883
+     * @since 1.0.19
884
+     * @return mixed|null
885
+     */
886
+    public function get_field( $field, $sub_array_key = null ) {
887
+        return getpaid_get_array_field( $this->data, $field, $sub_array_key );
888
+    }
889
+
890
+    /**
891
+     * Checks if a required field is set.
892
+     *
893
+     * @since 1.0.19
894
+     */
895
+    public function is_required_field_set( $field ) {
896
+        return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] );
897
+    }
898
+
899
+    /**
900
+     * Formats an amount
901
+     *
902
+     * @since 1.0.19
903
+     */
904
+    public function format_amount( $amount ) {
905
+        return wpinv_price( $amount, $this->get_currency() );
906
+    }
907 907
 
908 908
 }
Please login to merge, or discard this patch.
includes/gateways/class-getpaid-worldpay-gateway.php 1 patch
Indentation   +94 added lines, -94 removed lines patch added patch discarded remove patch
@@ -13,65 +13,65 @@  discard block
 block discarded – undo
13 13
 class GetPaid_Worldpay_Gateway extends GetPaid_Payment_Gateway {
14 14
 
15 15
     /**
16
-	 * Payment method id.
17
-	 *
18
-	 * @var string
19
-	 */
16
+     * Payment method id.
17
+     *
18
+     * @var string
19
+     */
20 20
     public $id = 'worldpay';
21 21
 
22 22
     /**
23
-	 * Payment method order.
24
-	 *
25
-	 * @var int
26
-	 */
23
+     * Payment method order.
24
+     *
25
+     * @var int
26
+     */
27 27
     public $order = 5;
28 28
 
29 29
     /**
30
-	 * Endpoint for requests from Worldpay.
31
-	 *
32
-	 * @var string
33
-	 */
34
-	protected $notify_url;
35
-
36
-	/**
37
-	 * Endpoint for requests to Worldpay.
38
-	 *
39
-	 * @var string
40
-	 */
30
+     * Endpoint for requests from Worldpay.
31
+     *
32
+     * @var string
33
+     */
34
+    protected $notify_url;
35
+
36
+    /**
37
+     * Endpoint for requests to Worldpay.
38
+     *
39
+     * @var string
40
+     */
41 41
     protected $endpoint;
42 42
 
43 43
     /**
44
-	 * An array of features that this gateway supports.
45
-	 *
46
-	 * @var array
47
-	 */
44
+     * An array of features that this gateway supports.
45
+     *
46
+     * @var array
47
+     */
48 48
     protected $supports = array( 'sandbox' );
49 49
     
50 50
     /**
51
-	 * Currencies this gateway is allowed for.
52
-	 *
53
-	 * @var array
54
-	 */
55
-	public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' );
51
+     * Currencies this gateway is allowed for.
52
+     *
53
+     * @var array
54
+     */
55
+    public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' );
56 56
 
57 57
     /**
58
-	 * URL to view a transaction.
59
-	 *
60
-	 * @var string
61
-	 */
58
+     * URL to view a transaction.
59
+     *
60
+     * @var string
61
+     */
62 62
     public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s';
63 63
 
64 64
     /**
65
-	 * URL to view a subscription.
66
-	 *
67
-	 * @var string
68
-	 */
69
-	public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s';
65
+     * URL to view a subscription.
66
+     *
67
+     * @var string
68
+     */
69
+    public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s';
70 70
 
71 71
     /**
72
-	 * Class constructor.
73
-	 */
74
-	public function __construct() {
72
+     * Class constructor.
73
+     */
74
+    public function __construct() {
75 75
 
76 76
         $this->method_title         = __( 'Worldpay', 'invoicing' );
77 77
         $this->title                = __( 'Worldpay - Credit Card / Debit Card', 'invoicing' );
@@ -85,15 +85,15 @@  discard block
 block discarded – undo
85 85
     }
86 86
 
87 87
     /**
88
-	 * Process Payment.
89
-	 *
90
-	 *
91
-	 * @param WPInv_Invoice $invoice Invoice.
92
-	 * @param array $submission_data Posted checkout fields.
93
-	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
94
-	 * @return array
95
-	 */
96
-	public function process_payment( $invoice, $submission_data, $submission ) {
88
+     * Process Payment.
89
+     *
90
+     *
91
+     * @param WPInv_Invoice $invoice Invoice.
92
+     * @param array $submission_data Posted checkout fields.
93
+     * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
94
+     * @return array
95
+     */
96
+    public function process_payment( $invoice, $submission_data, $submission ) {
97 97
 
98 98
         // Get redirect url.
99 99
         $worldpay_redirect = esc_url( $this->get_request_url( $invoice ) );
@@ -128,31 +128,31 @@  discard block
 block discarded – undo
128 128
     }
129 129
 
130 130
     /**
131
-	 * Get the Worldpay request URL for an invoice.
132
-	 *
133
-	 * @param  WPInv_Invoice $invoice Invoice object.
134
-	 * @return string
135
-	 */
136
-	public function get_request_url( $invoice ) {
131
+     * Get the Worldpay request URL for an invoice.
132
+     *
133
+     * @param  WPInv_Invoice $invoice Invoice object.
134
+     * @return string
135
+     */
136
+    public function get_request_url( $invoice ) {
137 137
 
138 138
         // Endpoint for this request
139
-		$this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase';
139
+        $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase';
140 140
 
141 141
         return $this->endpoint;
142 142
 
143
-	}
143
+    }
144 144
 
145 145
     /**
146
-	 * Get Worldpay Args for passing to Worldpay.
147
-	 *
148
-	 * @param  WPInv_Invoice $invoice Invoice object.
149
-	 * @return array
150
-	 */
151
-	protected function get_worldpay_args( $invoice ) {
152
-
153
-		return apply_filters(
154
-			'getpaid_worldpay_args',
155
-			array(
146
+     * Get Worldpay Args for passing to Worldpay.
147
+     *
148
+     * @param  WPInv_Invoice $invoice Invoice object.
149
+     * @return array
150
+     */
151
+    protected function get_worldpay_args( $invoice ) {
152
+
153
+        return apply_filters(
154
+            'getpaid_worldpay_args',
155
+            array(
156 156
                 'amount'         => wpinv_sanitize_amount( $invoice->get_total() ), // mandatory
157 157
                 'cartId'         => wpinv_clean( $invoice->get_number() ), // mandatory reference for the item purchased
158 158
                 'currency'       => wpinv_clean( $invoice->get_currency() ), // mandatory
@@ -177,18 +177,18 @@  discard block
 block discarded – undo
177 177
                 'countryString'  => wpinv_clean( wpinv_country_name( $invoice->get_country() ) ),
178 178
                 'compName'       => wpinv_clean( $invoice->get_company() ),
179 179
             ),
180
-			$invoice
181
-		);
180
+            $invoice
181
+        );
182 182
 
183 183
     }
184 184
 
185 185
     /**
186
-	 * Secures worldpay args with an md5 hash.
187
-	 *
188
-	 * @param  array $args Gateway args.
189
-	 * @return array
190
-	 */
191
-	public function hash_args( $args ) {
186
+     * Secures worldpay args with an md5 hash.
187
+     *
188
+     * @param  array $args Gateway args.
189
+     * @return array
190
+     */
191
+    public function hash_args( $args ) {
192 192
 
193 193
         $md5_secret = $this->get_option( 'md5_secret' );
194 194
 
@@ -204,16 +204,16 @@  discard block
 block discarded – undo
204 204
     }
205 205
 
206 206
     /**
207
-	 * Processes ipns and marks payments as complete.
208
-	 *
209
-	 * @return void
210
-	 */
211
-	public function verify_ipn() {
207
+     * Processes ipns and marks payments as complete.
208
+     *
209
+     * @return void
210
+     */
211
+    public function verify_ipn() {
212 212
 
213 213
         // Validate the IPN.
214 214
         if ( empty( $_POST ) || ! $this->validate_ipn() ) {
215
-		    wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) );
216
-		}
215
+            wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) );
216
+        }
217 217
 
218 218
         // Process the IPN.
219 219
         $posted  = wp_kses_post_deep( wp_unslash( $_POST ) );
@@ -229,8 +229,8 @@  discard block
 block discarded – undo
229 229
                 $invoice->set_transaction_id( wpinv_clean( $posted['transId'] ) );
230 230
             }
231 231
 
232
-             // Update the ip address.
233
-             if ( ! empty( $posted['ipAddress'] ) ) {
232
+                // Update the ip address.
233
+                if ( ! empty( $posted['ipAddress'] ) ) {
234 234
                 $invoice->set_ip( wpinv_clean( $posted['ipAddress'] ) );
235 235
             }
236 236
 
@@ -257,9 +257,9 @@  discard block
 block discarded – undo
257 257
     }
258 258
 
259 259
     /**
260
-	 * Check Worldpay IPN validity.
261
-	 */
262
-	public function validate_ipn() {
260
+     * Check Worldpay IPN validity.
261
+     */
262
+    public function validate_ipn() {
263 263
 
264 264
         wpinv_error_log( 'Validating Worldpay IPN response' );
265 265
 
@@ -305,11 +305,11 @@  discard block
 block discarded – undo
305 305
     }
306 306
 
307 307
     /**
308
-	 * Filters the gateway settings.
309
-	 * 
310
-	 * @param array $admin_settings
311
-	 */
312
-	public function admin_settings( $admin_settings ) {
308
+     * Filters the gateway settings.
309
+     * 
310
+     * @param array $admin_settings
311
+     */
312
+    public function admin_settings( $admin_settings ) {
313 313
 
314 314
         $currencies = sprintf(
315 315
             __( 'Supported Currencies: %s', 'invoicing' ),
@@ -350,7 +350,7 @@  discard block
 block discarded – undo
350 350
             'readonly' => true
351 351
         );
352 352
 
353
-		return $admin_settings;
354
-	}
353
+        return $admin_settings;
354
+    }
355 355
 
356 356
 }
Please login to merge, or discard this patch.
includes/gateways/class-getpaid-authorize-net-legacy-gateway.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -15,31 +15,31 @@  discard block
 block discarded – undo
15 15
 abstract class GetPaid_Authorize_Net_Legacy_Gateway extends GetPaid_Payment_Gateway {
16 16
 
17 17
     /**
18
-	 * Class constructor.
19
-	 */
20
-	public function __construct() {
18
+     * Class constructor.
19
+     */
20
+    public function __construct() {
21 21
         parent::__construct();
22 22
     }
23 23
 
24 24
     /**
25
-	 * Returns the API URL.
26
-	 *
27
-	 *
28
-	 * @param WPInv_Invoice $invoice Invoice.
29
-	 * @return string
30
-	 */
31
-	public function get_api_url( $invoice ) {
25
+     * Returns the API URL.
26
+     *
27
+     *
28
+     * @param WPInv_Invoice $invoice Invoice.
29
+     * @return string
30
+     */
31
+    public function get_api_url( $invoice ) {
32 32
         return $this->is_sandbox( $invoice ) ? 'https://apitest.authorize.net/xml/v1/request.api' : 'https://api.authorize.net/xml/v1/request.api';
33 33
     }
34 34
 
35 35
     /**
36
-	 * Communicates with authorize.net
37
-	 *
38
-	 *
39
-	 * @param array $post Data to post.
36
+     * Communicates with authorize.net
37
+     *
38
+     *
39
+     * @param array $post Data to post.
40 40
      * @param WPInv_Invoice $invoice Invoice.
41
-	 * @return stdClass|WP_Error
42
-	 */
41
+     * @return stdClass|WP_Error
42
+     */
43 43
     public function post( $post, $invoice ){
44 44
 
45 45
         $url      = $this->get_api_url( $invoice );
@@ -89,12 +89,12 @@  discard block
 block discarded – undo
89 89
     }
90 90
 
91 91
     /**
92
-	 * Returns the API authentication params.
93
-	 *
94
-	 *
95
-	 * @return array
96
-	 */
97
-	public function get_auth_params() {
92
+     * Returns the API authentication params.
93
+     *
94
+     *
95
+     * @return array
96
+     */
97
+    public function get_auth_params() {
98 98
 
99 99
         return array(
100 100
             'name'           => $this->get_option( 'login_id' ),
@@ -104,13 +104,13 @@  discard block
 block discarded – undo
104 104
     }
105 105
 
106 106
     /**
107
-	 * Cancels a subscription remotely
108
-	 *
109
-	 *
110
-	 * @param WPInv_Subscription $subscription Subscription.
107
+     * Cancels a subscription remotely
108
+     *
109
+     *
110
+     * @param WPInv_Subscription $subscription Subscription.
111 111
      * @param WPInv_Invoice $invoice Invoice.
112
-	 */
113
-	public function cancel_subscription( $subscription, $invoice ) {
112
+     */
113
+    public function cancel_subscription( $subscription, $invoice ) {
114 114
 
115 115
         // Backwards compatibility. New version do not use authorize.net subscriptions.
116 116
         $this->post(
@@ -126,17 +126,17 @@  discard block
 block discarded – undo
126 126
     }
127 127
 
128 128
     /**
129
-	 * Processes ipns.
130
-	 *
131
-	 * @return void
132
-	 */
133
-	public function verify_ipn() {
129
+     * Processes ipns.
130
+     *
131
+     * @return void
132
+     */
133
+    public function verify_ipn() {
134 134
 
135 135
         $this->maybe_process_old_ipn();
136 136
 
137 137
         // Validate the IPN.
138 138
         if ( empty( $_POST ) || ! $this->validate_ipn() ) {
139
-		    wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 500 ) );
139
+            wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 500 ) );
140 140
         }
141 141
 
142 142
         // Event type.
@@ -175,24 +175,24 @@  discard block
 block discarded – undo
175 175
     }
176 176
 
177 177
     /**
178
-	 * Validates IPN invoices.
179
-	 *
178
+     * Validates IPN invoices.
179
+     *
180 180
      * @param WPInv_Invoice $invoice
181 181
      * @param object $payload
182
-	 * @return void
183
-	 */
184
-	public function validate_ipn_invoice( $invoice, $payload ) {
182
+     * @return void
183
+     */
184
+    public function validate_ipn_invoice( $invoice, $payload ) {
185 185
         if ( ! $invoice->exists() || $payload->id != $invoice->get_transaction_id() ) {
186 186
             exit;
187 187
         }
188 188
     }
189 189
 
190 190
     /**
191
-	 * Process subscriptio IPNS.
192
-	 *
193
-	 * @return void
194
-	 */
195
-	public function maybe_process_old_ipn() {
191
+     * Process subscriptio IPNS.
192
+     *
193
+     * @return void
194
+     */
195
+    public function maybe_process_old_ipn() {
196 196
 
197 197
         $data = wp_kses_post_deep( wp_unslash( $_POST ) );
198 198
 
@@ -234,11 +234,11 @@  discard block
 block discarded – undo
234 234
     }
235 235
 
236 236
     /**
237
-	 * Validates the old IPN signature.
237
+     * Validates the old IPN signature.
238 238
      *
239 239
      * @param array $posted
240
-	 */
241
-	public function validate_old_ipn_signature( $posted ) {
240
+     */
241
+    public function validate_old_ipn_signature( $posted ) {
242 242
 
243 243
         $signature = $this->get_option( 'signature_key' );
244 244
         if ( ! empty( $signature ) ) {
@@ -257,9 +257,9 @@  discard block
 block discarded – undo
257 257
     }
258 258
 
259 259
     /**
260
-	 * Check Authorize.NET IPN validity.
261
-	 */
262
-	public function validate_ipn() {
260
+     * Check Authorize.NET IPN validity.
261
+     */
262
+    public function validate_ipn() {
263 263
 
264 264
         wpinv_error_log( 'Validating Authorize.NET IPN response' );
265 265
 
Please login to merge, or discard this patch.
includes/gateways/class-getpaid-paypal-gateway-ipn-handler.php 1 patch
Indentation   +390 added lines, -390 removed lines patch added patch discarded remove patch
@@ -12,474 +12,474 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Paypal_Gateway_IPN_Handler {
14 14
 
15
-	/**
16
-	 * Payment method id.
17
-	 *
18
-	 * @var string
19
-	 */
20
-	protected $id = 'paypal';
21
-
22
-	/**
23
-	 * Payment method object.
24
-	 *
25
-	 * @var GetPaid_Paypal_Gateway
26
-	 */
27
-	protected $gateway;
28
-
29
-	/**
30
-	 * Class constructor.
31
-	 *
32
-	 * @param GetPaid_Paypal_Gateway $gateway
33
-	 */
34
-	public function __construct( $gateway ) {
35
-		$this->gateway = $gateway;
36
-		$this->verify_ipn();
37
-	}
38
-
39
-	/**
40
-	 * Processes ipns and marks payments as complete.
41
-	 *
42
-	 * @return void
43
-	 */
44
-	public function verify_ipn() {
45
-
46
-		wpinv_error_log( 'GetPaid PayPal IPN Handler', false );
47
-
48
-		// Validate the IPN.
49
-		if ( empty( $_POST ) || ! $this->validate_ipn() ) {
50
-			wp_die( 'PayPal IPN Request Failure', 500 );
51
-		}
52
-
53
-		// Process the IPN.
54
-		$posted  = wp_kses_post_deep( wp_unslash( $_POST ) );
55
-		$invoice = $this->get_ipn_invoice( $posted );
56
-
57
-		// Abort if it was not paid by our gateway.
58
-		if ( $this->id != $invoice->get_gateway() ) {
59
-			wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false );
60
-			wp_die( 'Invoice not paid via PayPal', 200 );
61
-		}
62
-
63
-		$posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : '';
64
-		$posted['txn_type']       = sanitize_key( strtolower( $posted['txn_type'] ) );
65
-
66
-		wpinv_error_log( 'Payment status:' . $posted['payment_status'], false );
67
-		wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false );
68
-
69
-		if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) {
70
-			call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted );
71
-			wpinv_error_log( 'Done processing IPN', false );
72
-			wp_die( 'Processed', 200 );
73
-		}
74
-
75
-		wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false );
76
-		wp_die( 'Unsupported IPN type', 200 );
77
-
78
-	}
79
-
80
-	/**
81
-	 * Retrieves IPN Invoice.
82
-	 *
83
-	 * @param array $posted
84
-	 * @return WPInv_Invoice
85
-	 */
86
-	protected function get_ipn_invoice( $posted ) {
87
-
88
-		wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false );
89
-
90
-		if ( ! empty( $posted['custom'] ) ) {
91
-			$invoice = new WPInv_Invoice( $posted['custom'] );
92
-
93
-			if ( $invoice->exists() ) {
94
-				wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false );
95
-				return $invoice;
96
-			}
97
-
98
-		}
99
-
100
-		wpinv_error_log( 'Could not retrieve the associated invoice.', false );
101
-		wp_die( 'Could not retrieve the associated invoice.', 200 );
102
-	}
103
-
104
-	/**
105
-	 * Check PayPal IPN validity.
106
-	 */
107
-	protected function validate_ipn() {
108
-
109
-		wpinv_error_log( 'Validating PayPal IPN response', false );
110
-
111
-		// Retrieve the associated invoice.
112
-		$posted  = wp_kses_post_deep( wp_unslash( $_POST ) );
113
-		$invoice = $this->get_ipn_invoice( $posted );
114
-
115
-		if ( $this->gateway->is_sandbox( $invoice ) ) {
116
-			wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false );
117
-		}
118
-
119
-		// Validate the IPN.
120
-		$posted['cmd'] = '_notify-validate';
121
-
122
-		// Send back post vars to paypal.
123
-		$params = array(
124
-			'body'        => $posted,
125
-			'timeout'     => 60,
126
-			'httpversion' => '1.1',
127
-			'compress'    => false,
128
-			'decompress'  => false,
129
-			'user-agent'  => 'GetPaid/' . WPINV_VERSION,
130
-		);
131
-
132
-		// Post back to get a response.
133
-		$response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params );
134
-
135
-		// Check to see if the request was valid.
136
-		if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) {
137
-			wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false );
138
-			return true;
139
-		}
140
-
141
-		if ( is_wp_error( $response ) ) {
142
-			wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' );
143
-			return false;
144
-		}
15
+    /**
16
+     * Payment method id.
17
+     *
18
+     * @var string
19
+     */
20
+    protected $id = 'paypal';
21
+
22
+    /**
23
+     * Payment method object.
24
+     *
25
+     * @var GetPaid_Paypal_Gateway
26
+     */
27
+    protected $gateway;
28
+
29
+    /**
30
+     * Class constructor.
31
+     *
32
+     * @param GetPaid_Paypal_Gateway $gateway
33
+     */
34
+    public function __construct( $gateway ) {
35
+        $this->gateway = $gateway;
36
+        $this->verify_ipn();
37
+    }
38
+
39
+    /**
40
+     * Processes ipns and marks payments as complete.
41
+     *
42
+     * @return void
43
+     */
44
+    public function verify_ipn() {
45
+
46
+        wpinv_error_log( 'GetPaid PayPal IPN Handler', false );
47
+
48
+        // Validate the IPN.
49
+        if ( empty( $_POST ) || ! $this->validate_ipn() ) {
50
+            wp_die( 'PayPal IPN Request Failure', 500 );
51
+        }
52
+
53
+        // Process the IPN.
54
+        $posted  = wp_kses_post_deep( wp_unslash( $_POST ) );
55
+        $invoice = $this->get_ipn_invoice( $posted );
56
+
57
+        // Abort if it was not paid by our gateway.
58
+        if ( $this->id != $invoice->get_gateway() ) {
59
+            wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false );
60
+            wp_die( 'Invoice not paid via PayPal', 200 );
61
+        }
62
+
63
+        $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : '';
64
+        $posted['txn_type']       = sanitize_key( strtolower( $posted['txn_type'] ) );
65
+
66
+        wpinv_error_log( 'Payment status:' . $posted['payment_status'], false );
67
+        wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false );
68
+
69
+        if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) {
70
+            call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted );
71
+            wpinv_error_log( 'Done processing IPN', false );
72
+            wp_die( 'Processed', 200 );
73
+        }
74
+
75
+        wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false );
76
+        wp_die( 'Unsupported IPN type', 200 );
77
+
78
+    }
79
+
80
+    /**
81
+     * Retrieves IPN Invoice.
82
+     *
83
+     * @param array $posted
84
+     * @return WPInv_Invoice
85
+     */
86
+    protected function get_ipn_invoice( $posted ) {
87
+
88
+        wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false );
89
+
90
+        if ( ! empty( $posted['custom'] ) ) {
91
+            $invoice = new WPInv_Invoice( $posted['custom'] );
92
+
93
+            if ( $invoice->exists() ) {
94
+                wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false );
95
+                return $invoice;
96
+            }
97
+
98
+        }
99
+
100
+        wpinv_error_log( 'Could not retrieve the associated invoice.', false );
101
+        wp_die( 'Could not retrieve the associated invoice.', 200 );
102
+    }
103
+
104
+    /**
105
+     * Check PayPal IPN validity.
106
+     */
107
+    protected function validate_ipn() {
108
+
109
+        wpinv_error_log( 'Validating PayPal IPN response', false );
110
+
111
+        // Retrieve the associated invoice.
112
+        $posted  = wp_kses_post_deep( wp_unslash( $_POST ) );
113
+        $invoice = $this->get_ipn_invoice( $posted );
114
+
115
+        if ( $this->gateway->is_sandbox( $invoice ) ) {
116
+            wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false );
117
+        }
118
+
119
+        // Validate the IPN.
120
+        $posted['cmd'] = '_notify-validate';
121
+
122
+        // Send back post vars to paypal.
123
+        $params = array(
124
+            'body'        => $posted,
125
+            'timeout'     => 60,
126
+            'httpversion' => '1.1',
127
+            'compress'    => false,
128
+            'decompress'  => false,
129
+            'user-agent'  => 'GetPaid/' . WPINV_VERSION,
130
+        );
131
+
132
+        // Post back to get a response.
133
+        $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params );
134
+
135
+        // Check to see if the request was valid.
136
+        if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) {
137
+            wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false );
138
+            return true;
139
+        }
140
+
141
+        if ( is_wp_error( $response ) ) {
142
+            wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' );
143
+            return false;
144
+        }
145 145
 
146
-		wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' );
147
-		return false;
148
-
149
-	}
146
+        wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' );
147
+        return false;
148
+
149
+    }
150 150
 
151
-	/**
152
-	 * Check currency from IPN matches the invoice.
153
-	 *
154
-	 * @param WPInv_Invoice $invoice          Invoice object.
155
-	 * @param string   $currency currency to validate.
156
-	 */
157
-	protected function validate_ipn_currency( $invoice, $currency ) {
151
+    /**
152
+     * Check currency from IPN matches the invoice.
153
+     *
154
+     * @param WPInv_Invoice $invoice          Invoice object.
155
+     * @param string   $currency currency to validate.
156
+     */
157
+    protected function validate_ipn_currency( $invoice, $currency ) {
158 158
 
159
-		if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) {
159
+        if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) {
160 160
 
161
-			/* translators: %s: currency code. */
162
-			$invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) );
161
+            /* translators: %s: currency code. */
162
+            $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) );
163 163
 
164
-			wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true );
165
-		}
164
+            wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true );
165
+        }
166 166
 
167
-		wpinv_error_log( $currency, 'Validated IPN Currency', false );
168
-	}
167
+        wpinv_error_log( $currency, 'Validated IPN Currency', false );
168
+    }
169 169
 
170
-	/**
171
-	 * Check payment amount from IPN matches the invoice.
172
-	 *
173
-	 * @param WPInv_Invoice $invoice          Invoice object.
174
-	 * @param float   $amount amount to validate.
175
-	 */
176
-	protected function validate_ipn_amount( $invoice, $amount ) {
177
-		if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) {
170
+    /**
171
+     * Check payment amount from IPN matches the invoice.
172
+     *
173
+     * @param WPInv_Invoice $invoice          Invoice object.
174
+     * @param float   $amount amount to validate.
175
+     */
176
+    protected function validate_ipn_amount( $invoice, $amount ) {
177
+        if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) {
178 178
 
179
-			/* translators: %s: Amount. */
180
-			$invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) );
179
+            /* translators: %s: Amount. */
180
+            $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) );
181 181
 
182
-			wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true );
183
-		}
182
+            wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true );
183
+        }
184 184
 
185
-		wpinv_error_log( $amount, 'Validated IPN Amount', false );
186
-	}
185
+        wpinv_error_log( $amount, 'Validated IPN Amount', false );
186
+    }
187 187
 
188
-	/**
189
-	 * Verify receiver email from PayPal.
190
-	 *
191
-	 * @param WPInv_Invoice $invoice          Invoice object.
192
-	 * @param string   $receiver_email Email to validate.
193
-	 */
194
-	protected function validate_ipn_receiver_email( $invoice, $receiver_email ) {
195
-		$paypal_email = wpinv_get_option( 'paypal_email' );
188
+    /**
189
+     * Verify receiver email from PayPal.
190
+     *
191
+     * @param WPInv_Invoice $invoice          Invoice object.
192
+     * @param string   $receiver_email Email to validate.
193
+     */
194
+    protected function validate_ipn_receiver_email( $invoice, $receiver_email ) {
195
+        $paypal_email = wpinv_get_option( 'paypal_email' );
196 196
 
197
-		if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) {
198
-			wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" );
197
+        if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) {
198
+            wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" );
199 199
 
200
-			/* translators: %s: email address . */
201
-			$invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) );
200
+            /* translators: %s: email address . */
201
+            $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) );
202 202
 
203
-			return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true );
204
-		}
203
+            return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true );
204
+        }
205 205
 
206
-		wpinv_error_log( 'Validated PayPal Email', false );
207
-	}
206
+        wpinv_error_log( 'Validated PayPal Email', false );
207
+    }
208 208
 
209
-	/**
210
-	 * Handles one time payments.
211
-	 *
212
-	 * @param WPInv_Invoice $invoice  Invoice object.
213
-	 * @param array    $posted Posted data.
214
-	 */
215
-	protected function ipn_txn_web_accept( $invoice, $posted ) {
209
+    /**
210
+     * Handles one time payments.
211
+     *
212
+     * @param WPInv_Invoice $invoice  Invoice object.
213
+     * @param array    $posted Posted data.
214
+     */
215
+    protected function ipn_txn_web_accept( $invoice, $posted ) {
216 216
 
217
-		// Collect payment details
218
-		$payment_status = strtolower( $posted['payment_status'] );
219
-		$business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] );
217
+        // Collect payment details
218
+        $payment_status = strtolower( $posted['payment_status'] );
219
+        $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] );
220 220
 
221
-		$this->validate_ipn_receiver_email( $invoice, $business_email );
222
-		$this->validate_ipn_currency( $invoice, $posted['mc_currency'] );
221
+        $this->validate_ipn_receiver_email( $invoice, $business_email );
222
+        $this->validate_ipn_currency( $invoice, $posted['mc_currency'] );
223 223
 
224
-		// Update the transaction id.
225
-		if ( ! empty( $posted['txn_id'] ) ) {
226
-			$invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) );
227
-			$invoice->save();
228
-		}
224
+        // Update the transaction id.
225
+        if ( ! empty( $posted['txn_id'] ) ) {
226
+            $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) );
227
+            $invoice->save();
228
+        }
229 229
 
230
-		$invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) );
230
+        $invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) );
231 231
 
232
-		// Process a refund.
233
-		if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) {
232
+        // Process a refund.
233
+        if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) {
234 234
 
235
-			update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 );
235
+            update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 );
236 236
 
237
-			if ( ! $invoice->is_refunded() ) {
238
-				$invoice->update_status( 'wpi-refunded', $posted['reason_code'] );
239
-			}
237
+            if ( ! $invoice->is_refunded() ) {
238
+                $invoice->update_status( 'wpi-refunded', $posted['reason_code'] );
239
+            }
240 240
 
241
-			return wpinv_error_log( $posted['reason_code'], false );
242
-		}
241
+            return wpinv_error_log( $posted['reason_code'], false );
242
+        }
243 243
 
244
-		// Process payments.
245
-		if ( $payment_status == 'completed' ) {
244
+        // Process payments.
245
+        if ( $payment_status == 'completed' ) {
246 246
 
247
-			if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) {
248
-				return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false );
249
-			}
247
+            if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) {
248
+                return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false );
249
+            }
250 250
 
251
-			$this->validate_ipn_amount( $invoice, $posted['mc_gross'] );
251
+            $this->validate_ipn_amount( $invoice, $posted['mc_gross'] );
252 252
 
253
-			$note = '';
253
+            $note = '';
254 254
 
255
-			if ( ! empty( $posted['mc_fee'] ) ) {
256
-				$note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) );
257
-			}
255
+            if ( ! empty( $posted['mc_fee'] ) ) {
256
+                $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) );
257
+            }
258 258
 
259
-			if ( ! empty( $posted['payer_status'] ) ) {
260
-				$note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) );
261
-			}
259
+            if ( ! empty( $posted['payer_status'] ) ) {
260
+                $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) );
261
+            }
262 262
 
263
-			$invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) );
264
-			return wpinv_error_log( 'Invoice marked as paid.', false );
263
+            $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) );
264
+            return wpinv_error_log( 'Invoice marked as paid.', false );
265 265
 
266
-		}
266
+        }
267 267
 
268
-		// Pending payments.
269
-		if ( $payment_status == 'pending' ) {
268
+        // Pending payments.
269
+        if ( $payment_status == 'pending' ) {
270 270
 
271
-			/* translators: %s: pending reason. */
272
-			$invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) );
271
+            /* translators: %s: pending reason. */
272
+            $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) );
273 273
 
274
-			return wpinv_error_log( 'Invoice marked as "payment held".', false );
275
-		}
274
+            return wpinv_error_log( 'Invoice marked as "payment held".', false );
275
+        }
276 276
 
277
-		/* translators: %s: payment status. */
278
-		$invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) );
277
+        /* translators: %s: payment status. */
278
+        $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) );
279 279
 
280
-	}
280
+    }
281 281
 
282
-	/**
283
-	 * Handles one time payments.
284
-	 *
285
-	 * @param WPInv_Invoice $invoice  Invoice object.
286
-	 * @param array    $posted Posted data.
287
-	 */
288
-	protected function ipn_txn_cart( $invoice, $posted ) {
289
-		$this->ipn_txn_web_accept( $invoice, $posted );
290
-	}
282
+    /**
283
+     * Handles one time payments.
284
+     *
285
+     * @param WPInv_Invoice $invoice  Invoice object.
286
+     * @param array    $posted Posted data.
287
+     */
288
+    protected function ipn_txn_cart( $invoice, $posted ) {
289
+        $this->ipn_txn_web_accept( $invoice, $posted );
290
+    }
291 291
 
292
-	/**
293
-	 * Handles subscription sign ups.
294
-	 *
295
-	 * @param WPInv_Invoice $invoice  Invoice object.
296
-	 * @param array    $posted Posted data.
297
-	 */
298
-	protected function ipn_txn_subscr_signup( $invoice, $posted ) {
292
+    /**
293
+     * Handles subscription sign ups.
294
+     *
295
+     * @param WPInv_Invoice $invoice  Invoice object.
296
+     * @param array    $posted Posted data.
297
+     */
298
+    protected function ipn_txn_subscr_signup( $invoice, $posted ) {
299 299
 
300
-		wpinv_error_log( 'Processing subscription signup', false );
300
+        wpinv_error_log( 'Processing subscription signup', false );
301 301
 
302
-		// Make sure the invoice has a subscription.
303
-		$subscription = getpaid_get_invoice_subscription( $invoice );
302
+        // Make sure the invoice has a subscription.
303
+        $subscription = getpaid_get_invoice_subscription( $invoice );
304 304
 
305
-		if ( empty( $subscription ) ) {
306
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
307
-		}
305
+        if ( empty( $subscription ) ) {
306
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
307
+        }
308 308
 
309
-		wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
309
+        wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
310 310
 
311
-		// Validate the IPN.
312
-		$business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] );
313
-		$this->validate_ipn_receiver_email( $invoice, $business_email );
314
-		$this->validate_ipn_currency( $invoice, $posted['mc_currency'] );
311
+        // Validate the IPN.
312
+        $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] );
313
+        $this->validate_ipn_receiver_email( $invoice, $business_email );
314
+        $this->validate_ipn_currency( $invoice, $posted['mc_currency'] );
315 315
 
316
-		// Activate the subscription.
317
-		$duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() );
318
-		$subscription->set_date_created( current_time( 'mysql' ) );
319
-		$subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) );
320
-		$subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) );
321
-		$subscription->activate();
316
+        // Activate the subscription.
317
+        $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() );
318
+        $subscription->set_date_created( current_time( 'mysql' ) );
319
+        $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) );
320
+        $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) );
321
+        $subscription->activate();
322 322
 
323
-		// Set the transaction id.
324
-		if ( ! empty( $posted['txn_id'] ) ) {
325
-			$invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
326
-			$invoice->set_transaction_id( $posted['txn_id'] );
327
-		}
323
+        // Set the transaction id.
324
+        if ( ! empty( $posted['txn_id'] ) ) {
325
+            $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
326
+            $invoice->set_transaction_id( $posted['txn_id'] );
327
+        }
328 328
 
329
-		// Update the payment status.
330
-		$invoice->mark_paid();
329
+        // Update the payment status.
330
+        $invoice->mark_paid();
331 331
 
332
-		$invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
332
+        $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
333 333
 
334
-		wpinv_error_log( 'Subscription started.', false );
335
-	}
334
+        wpinv_error_log( 'Subscription started.', false );
335
+    }
336 336
 
337
-	/**
338
-	 * Handles subscription renewals.
339
-	 *
340
-	 * @param WPInv_Invoice $invoice  Invoice object.
341
-	 * @param array    $posted Posted data.
342
-	 */
343
-	protected function ipn_txn_subscr_payment( $invoice, $posted ) {
337
+    /**
338
+     * Handles subscription renewals.
339
+     *
340
+     * @param WPInv_Invoice $invoice  Invoice object.
341
+     * @param array    $posted Posted data.
342
+     */
343
+    protected function ipn_txn_subscr_payment( $invoice, $posted ) {
344 344
 
345
-		// Make sure the invoice has a subscription.
346
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
345
+        // Make sure the invoice has a subscription.
346
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
347 347
 
348
-		if ( empty( $subscription ) ) {
349
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
350
-		}
348
+        if ( empty( $subscription ) ) {
349
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
350
+        }
351 351
 
352
-		wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
352
+        wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
353 353
 
354
-		// PayPal sends a subscr_payment for the first payment too.
355
-		$date_completed = getpaid_format_date( $invoice->get_date_completed() );
356
-		$date_created   = getpaid_format_date( $invoice->get_date_created() );
357
-		$today_date     = getpaid_format_date( current_time( 'mysql' ) );
358
-		$payment_date   = getpaid_format_date( $posted['payment_date'] );
359
-		$subscribe_date = getpaid_format_date( $subscription->get_date_created() );
360
-		$dates          = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) );
354
+        // PayPal sends a subscr_payment for the first payment too.
355
+        $date_completed = getpaid_format_date( $invoice->get_date_completed() );
356
+        $date_created   = getpaid_format_date( $invoice->get_date_created() );
357
+        $today_date     = getpaid_format_date( current_time( 'mysql' ) );
358
+        $payment_date   = getpaid_format_date( $posted['payment_date'] );
359
+        $subscribe_date = getpaid_format_date( $subscription->get_date_created() );
360
+        $dates          = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) );
361 361
 
362
-		foreach( $dates as $date ) {
362
+        foreach( $dates as $date ) {
363 363
 
364
-			if ( $date !== $today_date && $date !== $payment_date ) {
365
-				continue;
366
-			}
364
+            if ( $date !== $today_date && $date !== $payment_date ) {
365
+                continue;
366
+            }
367 367
 
368
-			if ( ! empty( $posted['txn_id'] ) ) {
369
-				$invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) );	
370
-				$invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true );
371
-			}
368
+            if ( ! empty( $posted['txn_id'] ) ) {
369
+                $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) );	
370
+                $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true );
371
+            }
372 372
 
373
-			return $invoice->mark_paid();
374
-
375
-		}
373
+            return $invoice->mark_paid();
374
+
375
+        }
376 376
 
377
-		wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false );
378
-
379
-		// Abort if the payment is already recorded.
380
-		if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) {
381
-			return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false );
382
-		}
383
-
384
-		$args = array(
385
-			'transaction_id' => $posted['txn_id'],
386
-			'gateway'        => $this->id,
387
-		);
388
-
389
-		$invoice = wpinv_get_invoice( $subscription->add_payment( $args ) );
377
+        wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false );
378
+
379
+        // Abort if the payment is already recorded.
380
+        if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) {
381
+            return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false );
382
+        }
383
+
384
+        $args = array(
385
+            'transaction_id' => $posted['txn_id'],
386
+            'gateway'        => $this->id,
387
+        );
388
+
389
+        $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) );
390 390
 
391
-		if ( empty( $invoice ) ) {
392
-			return;
393
-		}
391
+        if ( empty( $invoice ) ) {
392
+            return;
393
+        }
394 394
 
395
-		$invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
396
-		$invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
395
+        $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
396
+        $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
397 397
 
398
-		$subscription->renew();
399
-		wpinv_error_log( 'Subscription renewed.', false );
398
+        $subscription->renew();
399
+        wpinv_error_log( 'Subscription renewed.', false );
400 400
 
401
-	}
401
+    }
402 402
 
403
-	/**
404
-	 * Handles subscription cancelations.
405
-	 *
406
-	 * @param WPInv_Invoice $invoice  Invoice object.
407
-	 */
408
-	protected function ipn_txn_subscr_cancel( $invoice ) {
403
+    /**
404
+     * Handles subscription cancelations.
405
+     *
406
+     * @param WPInv_Invoice $invoice  Invoice object.
407
+     */
408
+    protected function ipn_txn_subscr_cancel( $invoice ) {
409 409
 
410
-		// Make sure the invoice has a subscription.
411
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
412
-
413
-		if ( empty( $subscription ) ) {
414
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
415
-		}
416
-
417
-		wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false );
418
-		$subscription->cancel();
419
-		wpinv_error_log( 'Subscription cancelled.', false );
410
+        // Make sure the invoice has a subscription.
411
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
412
+
413
+        if ( empty( $subscription ) ) {
414
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
415
+        }
416
+
417
+        wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false );
418
+        $subscription->cancel();
419
+        wpinv_error_log( 'Subscription cancelled.', false );
420 420
 
421
-	}
421
+    }
422 422
 
423
-	/**
424
-	 * Handles subscription completions.
425
-	 *
426
-	 * @param WPInv_Invoice $invoice  Invoice object.
427
-	 * @param array    $posted Posted data.
428
-	 */
429
-	protected function ipn_txn_subscr_eot( $invoice ) {
423
+    /**
424
+     * Handles subscription completions.
425
+     *
426
+     * @param WPInv_Invoice $invoice  Invoice object.
427
+     * @param array    $posted Posted data.
428
+     */
429
+    protected function ipn_txn_subscr_eot( $invoice ) {
430 430
 
431
-		// Make sure the invoice has a subscription.
432
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
431
+        // Make sure the invoice has a subscription.
432
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
433 433
 
434
-		if ( empty( $subscription ) ) {
435
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
436
-		}
434
+        if ( empty( $subscription ) ) {
435
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
436
+        }
437 437
 
438
-		wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false );
439
-		$subscription->complete();
440
-		wpinv_error_log( 'Subscription completed.', false );
438
+        wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false );
439
+        $subscription->complete();
440
+        wpinv_error_log( 'Subscription completed.', false );
441 441
 
442
-	}
442
+    }
443 443
 
444
-	/**
445
-	 * Handles subscription fails.
446
-	 *
447
-	 * @param WPInv_Invoice $invoice  Invoice object.
448
-	 * @param array    $posted Posted data.
449
-	 */
450
-	protected function ipn_txn_subscr_failed( $invoice ) {
444
+    /**
445
+     * Handles subscription fails.
446
+     *
447
+     * @param WPInv_Invoice $invoice  Invoice object.
448
+     * @param array    $posted Posted data.
449
+     */
450
+    protected function ipn_txn_subscr_failed( $invoice ) {
451 451
 
452
-		// Make sure the invoice has a subscription.
453
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
452
+        // Make sure the invoice has a subscription.
453
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
454 454
 
455
-		if ( empty( $subscription ) ) {
456
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
457
-		}
455
+        if ( empty( $subscription ) ) {
456
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
457
+        }
458 458
 
459
-		wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false );
460
-		$subscription->failing();
461
-		wpinv_error_log( 'Subscription marked as failing.', false );
459
+        wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false );
460
+        $subscription->failing();
461
+        wpinv_error_log( 'Subscription marked as failing.', false );
462 462
 
463
-	}
463
+    }
464 464
 
465
-	/**
466
-	 * Handles subscription suspensions.
467
-	 *
468
-	 * @param WPInv_Invoice $invoice  Invoice object.
469
-	 * @param array    $posted Posted data.
470
-	 */
471
-	protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) {
465
+    /**
466
+     * Handles subscription suspensions.
467
+     *
468
+     * @param WPInv_Invoice $invoice  Invoice object.
469
+     * @param array    $posted Posted data.
470
+     */
471
+    protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) {
472 472
 
473
-		// Make sure the invoice has a subscription.
474
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
473
+        // Make sure the invoice has a subscription.
474
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
475 475
 
476
-		if ( empty( $subscription ) ) {
477
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
478
-		}
479
-
480
-		wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false );
481
-		$subscription->cancel();
482
-		wpinv_error_log( 'Subscription cancelled.', false );
483
-	}
476
+        if ( empty( $subscription ) ) {
477
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
478
+        }
479
+
480
+        wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false );
481
+        $subscription->cancel();
482
+        wpinv_error_log( 'Subscription cancelled.', false );
483
+    }
484 484
 
485 485
 }
Please login to merge, or discard this patch.