Passed
Push — master ( 1c746e...f4e669 )
by Brian
04:44
created
includes/subscription-functions.php 2 patches
Indentation   +290 added lines, -290 removed lines patch added patch discarded remove patch
@@ -50,7 +50,7 @@  discard block
 block discarded – undo
50 50
  */
51 51
 function getpaid_get_invoice_subscription_group( $invoice_id, $subscription_id ) {
52 52
     $subscription_groups = getpaid_get_invoice_subscription_groups( $invoice_id );
53
-	$matching_group      = wp_list_filter( $subscription_groups, compact( 'subscription_id' ) );
53
+    $matching_group      = wp_list_filter( $subscription_groups, compact( 'subscription_id' ) );
54 54
     return reset( $matching_group );
55 55
 }
56 56
 
@@ -63,11 +63,11 @@  discard block
 block discarded – undo
63 63
  */
64 64
 function getpaid_get_subscription( $subscription ) {
65 65
 
66
-	if ( ! is_a( $subscription, 'WPInv_Subscription' ) ) {
67
-		$subscription = new WPInv_Subscription( $subscription );
68
-	}
66
+    if ( ! is_a( $subscription, 'WPInv_Subscription' ) ) {
67
+        $subscription = new WPInv_Subscription( $subscription );
68
+    }
69 69
 
70
-	return $subscription->exists() ? $subscription : false;
70
+    return $subscription->exists() ? $subscription : false;
71 71
 }
72 72
 
73 73
 /**
@@ -81,28 +81,28 @@  discard block
 block discarded – undo
81 81
  */
82 82
 function getpaid_get_subscriptions( $args = array(), $return = 'results' ) {
83 83
 
84
-	// Do not retrieve all fields if we just want the count.
85
-	if ( 'count' == $return ) {
86
-		$args['fields'] = 'id';
87
-		$args['number'] = 1;
88
-	}
84
+    // Do not retrieve all fields if we just want the count.
85
+    if ( 'count' == $return ) {
86
+        $args['fields'] = 'id';
87
+        $args['number'] = 1;
88
+    }
89 89
 
90
-	// Do not count all matches if we just want the results.
91
-	if ( 'results' == $return ) {
92
-		$args['count_total'] = false;
93
-	}
90
+    // Do not count all matches if we just want the results.
91
+    if ( 'results' == $return ) {
92
+        $args['count_total'] = false;
93
+    }
94 94
 
95
-	$query = new GetPaid_Subscriptions_Query( $args );
95
+    $query = new GetPaid_Subscriptions_Query( $args );
96 96
 
97
-	if ( 'results' == $return ) {
98
-		return $query->get_results();
99
-	}
97
+    if ( 'results' == $return ) {
98
+        return $query->get_results();
99
+    }
100 100
 
101
-	if ( 'count' == $return ) {
102
-		return $query->get_total();
103
-	}
101
+    if ( 'count' == $return ) {
102
+        return $query->get_total();
103
+    }
104 104
 
105
-	return $query;
105
+    return $query;
106 106
 }
107 107
 
108 108
 /**
@@ -112,18 +112,18 @@  discard block
 block discarded – undo
112 112
  */
113 113
 function getpaid_get_subscription_statuses() {
114 114
 
115
-	return apply_filters(
116
-		'getpaid_get_subscription_statuses',
117
-		array(
118
-			'pending'    => __( 'Pending', 'invoicing' ),
119
-			'trialling'  => __( 'Trialing', 'invoicing' ),
120
-			'active'     => __( 'Active', 'invoicing' ),
121
-			'failing'    => __( 'Failing', 'invoicing' ),
122
-			'expired'    => __( 'Expired', 'invoicing' ),
123
-			'completed'  => __( 'Complete', 'invoicing' ),
124
-			'cancelled'  => __( 'Cancelled', 'invoicing' ),
125
-		)
126
-	);
115
+    return apply_filters(
116
+        'getpaid_get_subscription_statuses',
117
+        array(
118
+            'pending'    => __( 'Pending', 'invoicing' ),
119
+            'trialling'  => __( 'Trialing', 'invoicing' ),
120
+            'active'     => __( 'Active', 'invoicing' ),
121
+            'failing'    => __( 'Failing', 'invoicing' ),
122
+            'expired'    => __( 'Expired', 'invoicing' ),
123
+            'completed'  => __( 'Complete', 'invoicing' ),
124
+            'cancelled'  => __( 'Cancelled', 'invoicing' ),
125
+        )
126
+    );
127 127
 
128 128
 }
129 129
 
@@ -133,8 +133,8 @@  discard block
 block discarded – undo
133 133
  * @return string
134 134
  */
135 135
 function getpaid_get_subscription_status_label( $status ) {
136
-	$statuses = getpaid_get_subscription_statuses();
137
-	return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) );
136
+    $statuses = getpaid_get_subscription_statuses();
137
+    return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) );
138 138
 }
139 139
 
140 140
 /**
@@ -144,18 +144,18 @@  discard block
 block discarded – undo
144 144
  */
145 145
 function getpaid_get_subscription_status_classes() {
146 146
 
147
-	return apply_filters(
148
-		'getpaid_get_subscription_status_classes',
149
-		array(
150
-			'pending'    => 'badge-dark',
151
-			'trialling'  => 'badge-info',
152
-			'active'     => 'badge-success',
153
-			'failing'    => 'badge-warning',
154
-			'expired'    => 'badge-danger',
155
-			'completed'  => 'badge-primary',
156
-			'cancelled'  => 'badge-secondary',
157
-		)
158
-	);
147
+    return apply_filters(
148
+        'getpaid_get_subscription_status_classes',
149
+        array(
150
+            'pending'    => 'badge-dark',
151
+            'trialling'  => 'badge-info',
152
+            'active'     => 'badge-success',
153
+            'failing'    => 'badge-warning',
154
+            'expired'    => 'badge-danger',
155
+            'completed'  => 'badge-primary',
156
+            'cancelled'  => 'badge-secondary',
157
+        )
158
+    );
159 159
 
160 160
 }
161 161
 
@@ -166,15 +166,15 @@  discard block
 block discarded – undo
166 166
  */
167 167
 function getpaid_get_subscription_status_counts( $args = array() ) {
168 168
 
169
-	$statuses = array_keys( getpaid_get_subscription_statuses() );
170
-	$counts   = array();
169
+    $statuses = array_keys( getpaid_get_subscription_statuses() );
170
+    $counts   = array();
171 171
 
172
-	foreach ( $statuses as $status ) {
173
-		$_args             = wp_parse_args( "status=$status", $args );
174
-		$counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' );
175
-	}
172
+    foreach ( $statuses as $status ) {
173
+        $_args             = wp_parse_args( "status=$status", $args );
174
+        $counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' );
175
+    }
176 176
 
177
-	return $counts;
177
+    return $counts;
178 178
 
179 179
 }
180 180
 
@@ -185,32 +185,32 @@  discard block
 block discarded – undo
185 185
  */
186 186
 function getpaid_get_subscription_periods() {
187 187
 
188
-	return apply_filters(
189
-		'getpaid_get_subscription_periods',
190
-		array(
188
+    return apply_filters(
189
+        'getpaid_get_subscription_periods',
190
+        array(
191 191
 
192
-			'day'   => array(
193
-				'singular' => __( '%s day', 'invoicing' ),
194
-				'plural'   => __( '%d days', 'invoicing' ),
195
-			),
192
+            'day'   => array(
193
+                'singular' => __( '%s day', 'invoicing' ),
194
+                'plural'   => __( '%d days', 'invoicing' ),
195
+            ),
196 196
 
197
-			'week'   => array(
198
-				'singular' => __( '%s week', 'invoicing' ),
199
-				'plural'   => __( '%d weeks', 'invoicing' ),
200
-			),
197
+            'week'   => array(
198
+                'singular' => __( '%s week', 'invoicing' ),
199
+                'plural'   => __( '%d weeks', 'invoicing' ),
200
+            ),
201 201
 
202
-			'month'   => array(
203
-				'singular' => __( '%s month', 'invoicing' ),
204
-				'plural'   => __( '%d months', 'invoicing' ),
205
-			),
202
+            'month'   => array(
203
+                'singular' => __( '%s month', 'invoicing' ),
204
+                'plural'   => __( '%d months', 'invoicing' ),
205
+            ),
206 206
 
207
-			'year'   => array(
208
-				'singular' => __( '%s year', 'invoicing' ),
209
-				'plural'   => __( '%d years', 'invoicing' ),
210
-			),
207
+            'year'   => array(
208
+                'singular' => __( '%s year', 'invoicing' ),
209
+                'plural'   => __( '%d years', 'invoicing' ),
210
+            ),
211 211
 
212
-		)
213
-	);
212
+        )
213
+    );
214 214
 
215 215
 }
216 216
 
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
  * @return int
222 222
  */
223 223
 function getpaid_get_subscription_trial_period_interval( $trial_period ) {
224
-	return (int) preg_replace( '/[^0-9]/', '', $trial_period );
224
+    return (int) preg_replace( '/[^0-9]/', '', $trial_period );
225 225
 }
226 226
 
227 227
 /**
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
  * @return string
232 232
  */
233 233
 function getpaid_get_subscription_trial_period_period( $trial_period ) {
234
-	return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) );
234
+    return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) );
235 235
 }
236 236
 
237 237
 /**
@@ -242,8 +242,8 @@  discard block
 block discarded – undo
242 242
  * @return string
243 243
  */
244 244
 function getpaid_get_subscription_period_label( $period, $interval = 1, $singular_prefix = '1' ) {
245
-	$label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label(  $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix );
246
-	return strtolower( sanitize_text_field( $label ) );
245
+    $label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label(  $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix );
246
+    return strtolower( sanitize_text_field( $label ) );
247 247
 }
248 248
 
249 249
 /**
@@ -254,22 +254,22 @@  discard block
 block discarded – undo
254 254
  */
255 255
 function getpaid_get_singular_subscription_period_label( $period, $singular_prefix = '1' ) {
256 256
 
257
-	$periods = getpaid_get_subscription_periods();
258
-	$period  = strtolower( $period );
257
+    $periods = getpaid_get_subscription_periods();
258
+    $period  = strtolower( $period );
259 259
 
260
-	if ( isset( $periods[ $period ] ) ) {
261
-		return sprintf( $periods[ $period ]['singular'], $singular_prefix );
262
-	}
260
+    if ( isset( $periods[ $period ] ) ) {
261
+        return sprintf( $periods[ $period ]['singular'], $singular_prefix );
262
+    }
263 263
 
264
-	// Backwards compatibility.
265
-	foreach ( $periods as $key => $data ) {
266
-		if ( strpos( $key, $period ) === 0 ) {
267
-			return sprintf( $data['singular'], $singular_prefix );
268
-		}
269
-	}
264
+    // Backwards compatibility.
265
+    foreach ( $periods as $key => $data ) {
266
+        if ( strpos( $key, $period ) === 0 ) {
267
+            return sprintf( $data['singular'], $singular_prefix );
268
+        }
269
+    }
270 270
 
271
-	// Invalid string.
272
-	return '';
271
+    // Invalid string.
272
+    return '';
273 273
 }
274 274
 
275 275
 /**
@@ -281,22 +281,22 @@  discard block
 block discarded – undo
281 281
  */
282 282
 function getpaid_get_plural_subscription_period_label( $period, $interval ) {
283 283
 
284
-	$periods = getpaid_get_subscription_periods();
285
-	$period  = strtolower( $period );
284
+    $periods = getpaid_get_subscription_periods();
285
+    $period  = strtolower( $period );
286 286
 
287
-	if ( isset( $periods[ $period ] ) ) {
288
-		return sprintf( $periods[ $period ]['plural'], $interval );
289
-	}
287
+    if ( isset( $periods[ $period ] ) ) {
288
+        return sprintf( $periods[ $period ]['plural'], $interval );
289
+    }
290 290
 
291
-	// Backwards compatibility.
292
-	foreach ( $periods as $key => $data ) {
293
-		if ( strpos( $key, $period ) === 0 ) {
294
-			return sprintf( $data['plural'], $interval );
295
-		}
296
-	}
291
+    // Backwards compatibility.
292
+    foreach ( $periods as $key => $data ) {
293
+        if ( strpos( $key, $period ) === 0 ) {
294
+            return sprintf( $data['plural'], $interval );
295
+        }
296
+    }
297 297
 
298
-	// Invalid string.
299
-	return '';
298
+    // Invalid string.
299
+    return '';
300 300
 }
301 301
 
302 302
 /**
@@ -307,101 +307,101 @@  discard block
 block discarded – undo
307 307
  */
308 308
 function getpaid_get_formatted_subscription_amount( $subscription ) {
309 309
 
310
-	$initial    = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() );
311
-	$recurring  = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() );
312
-	$period     = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' );
313
-	$bill_times = $subscription->get_bill_times();
310
+    $initial    = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() );
311
+    $recurring  = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() );
312
+    $period     = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' );
313
+    $bill_times = $subscription->get_bill_times();
314 314
 
315
-	if ( ! empty( $bill_times ) ) {
316
-		$bill_times = $subscription->get_frequency() * $bill_times;
317
-		$bill_times = getpaid_get_subscription_period_label( $subscription->get_period(), $bill_times );
318
-	}
315
+    if ( ! empty( $bill_times ) ) {
316
+        $bill_times = $subscription->get_frequency() * $bill_times;
317
+        $bill_times = getpaid_get_subscription_period_label( $subscription->get_period(), $bill_times );
318
+    }
319 319
 
320
-	// Trial periods.
321
-	if ( $subscription->has_trial_period() ) {
320
+    // Trial periods.
321
+    if ( $subscription->has_trial_period() ) {
322 322
 
323
-		$trial_period   = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() );
324
-		$trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() );
323
+        $trial_period   = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() );
324
+        $trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() );
325 325
 
326
-		if ( empty( $bill_times ) ) {
326
+        if ( empty( $bill_times ) ) {
327 327
 
328
-			return sprintf(
328
+            return sprintf(
329 329
 
330
-				// translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period
331
-				_x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ),
332
-				$initial,
333
-				getpaid_get_subscription_period_label( $trial_period, $trial_interval ),
334
-				$recurring,
335
-				$period
330
+                // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period
331
+                _x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ),
332
+                $initial,
333
+                getpaid_get_subscription_period_label( $trial_period, $trial_interval ),
334
+                $recurring,
335
+                $period
336 336
 	
337
-			);
337
+            );
338 338
 
339
-		}
339
+        }
340 340
 
341
-		return sprintf(
341
+        return sprintf(
342 342
 
343
-			// translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period, $5: is the bill times
344
-			_x( '%1$s trial for %2$s then %3$s / %4$s for %5$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year for 4 years)', 'invoicing' ),
345
-			$initial,
346
-			getpaid_get_subscription_period_label( $trial_period, $trial_interval ),
347
-			$recurring,
348
-			$period,
349
-			$bill_times
350
-		);
343
+            // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period, $5: is the bill times
344
+            _x( '%1$s trial for %2$s then %3$s / %4$s for %5$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year for 4 years)', 'invoicing' ),
345
+            $initial,
346
+            getpaid_get_subscription_period_label( $trial_period, $trial_interval ),
347
+            $recurring,
348
+            $period,
349
+            $bill_times
350
+        );
351 351
 
352
-	}
352
+    }
353 353
 
354
-	if ( $initial != $recurring ) {
354
+    if ( $initial != $recurring ) {
355 355
 
356
-		if ( empty( $bill_times ) ) {
356
+        if ( empty( $bill_times ) ) {
357 357
 
358
-			return sprintf(
358
+            return sprintf(
359 359
 
360
-				// translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period
361
-				_x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ),
362
-				$initial,
363
-				$recurring,
364
-				$period
360
+                // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period
361
+                _x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ),
362
+                $initial,
363
+                $recurring,
364
+                $period
365 365
 	
366
-			);
366
+            );
367 367
 
368
-		}
368
+        }
369 369
 
370
-		return sprintf(
370
+        return sprintf(
371 371
 
372
-			// translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period, $4: is the bill times
373
-			_x( 'Initial payment of %1$s which renews at %2$s / %3$s for %4$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year for 5 years)', 'invoicing' ),
374
-			$initial,
375
-			$recurring,
376
-			$period,
377
-			$bill_times
372
+            // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period, $4: is the bill times
373
+            _x( 'Initial payment of %1$s which renews at %2$s / %3$s for %4$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year for 5 years)', 'invoicing' ),
374
+            $initial,
375
+            $recurring,
376
+            $period,
377
+            $bill_times
378 378
 
379
-		);
379
+        );
380 380
 
381
-	}
381
+    }
382 382
 
383
-	if ( empty( $bill_times ) ) {
383
+    if ( empty( $bill_times ) ) {
384 384
 
385
-		return sprintf(
385
+        return sprintf(
386 386
 
387
-			// translators: $1: is the recurring amount, $2: is the recurring period
388
-			_x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ),
389
-			$initial,
390
-			$period
387
+            // translators: $1: is the recurring amount, $2: is the recurring period
388
+            _x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ),
389
+            $initial,
390
+            $period
391 391
 	
392
-		);
392
+        );
393 393
 
394
-	}
394
+    }
395 395
 
396
-	return sprintf(
396
+    return sprintf(
397 397
 
398
-		// translators: $1: is the bill times, $2: is the recurring amount, $3: is the recurring period
399
-		_x( '%2$s / %3$s for %1$s', 'Subscription amount. (e.g.: $120 / year for 5 years)', 'invoicing' ),
400
-		$bill_times,
401
-		$initial,
402
-		$period
398
+        // translators: $1: is the bill times, $2: is the recurring amount, $3: is the recurring period
399
+        _x( '%2$s / %3$s for %1$s', 'Subscription amount. (e.g.: $120 / year for 5 years)', 'invoicing' ),
400
+        $bill_times,
401
+        $initial,
402
+        $period
403 403
 
404
-	);
404
+    );
405 405
 
406 406
 }
407 407
 
@@ -412,7 +412,7 @@  discard block
 block discarded – undo
412 412
  * @return WPInv_Subscription|false
413 413
  */
414 414
 function getpaid_get_invoice_subscription( $invoice ) {
415
-	return getpaid_subscriptions()->get_invoice_subscription( $invoice );
415
+    return getpaid_subscriptions()->get_invoice_subscription( $invoice );
416 416
 }
417 417
 
418 418
 /**
@@ -421,10 +421,10 @@  discard block
 block discarded – undo
421 421
  * @param WPInv_Invoice $invoice
422 422
  */
423 423
 function getpaid_activate_invoice_subscription( $invoice ) {
424
-	$subscription = getpaid_get_invoice_subscription( $invoice );
425
-	if ( is_a( $subscription, 'WPInv_Subscription' ) ) {
426
-		$subscription->activate();
427
-	}
424
+    $subscription = getpaid_get_invoice_subscription( $invoice );
425
+    if ( is_a( $subscription, 'WPInv_Subscription' ) ) {
426
+        $subscription->activate();
427
+    }
428 428
 }
429 429
 
430 430
 /**
@@ -433,7 +433,7 @@  discard block
 block discarded – undo
433 433
  * @return WPInv_Subscriptions
434 434
  */
435 435
 function getpaid_subscriptions() {
436
-	return getpaid()->get( 'subscriptions' );
436
+    return getpaid()->get( 'subscriptions' );
437 437
 }
438 438
 
439 439
 /**
@@ -452,15 +452,15 @@  discard block
 block discarded – undo
452 452
         return false;
453 453
     }
454 454
 
455
-	// Fetch the invoice subscription.
456
-	$subscription = getpaid_get_subscriptions(
457
-		array(
458
-			'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(),
459
-			'number'     => 1,
460
-		)
461
-	);
455
+    // Fetch the invoice subscription.
456
+    $subscription = getpaid_get_subscriptions(
457
+        array(
458
+            'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(),
459
+            'number'     => 1,
460
+        )
461
+    );
462 462
 
463
-	return empty( $subscription ) ? false : $subscription[0];
463
+    return empty( $subscription ) ? false : $subscription[0];
464 464
 
465 465
 }
466 466
 
@@ -477,48 +477,48 @@  discard block
 block discarded – undo
477 477
  */
478 478
 function getpaid_get_recurring_item_key( $cart_item ) {
479 479
 
480
-	$cart_key     = 'renews_';
481
-	$interval     = $cart_item->get_recurring_interval();
482
-	$period       = $cart_item->get_recurring_period( true );
483
-	$length       = $cart_item->get_recurring_limit() * $interval;
484
-	$trial_period = $cart_item->get_trial_period( true );
485
-	$trial_length = $cart_item->get_trial_interval();
486
-
487
-	// First start with the billing interval and period
488
-	switch ( $interval ) {
489
-		case 1 :
490
-			if ( 'day' == $period ) {
491
-				$cart_key .= 'daily';
492
-			} else {
493
-				$cart_key .= sprintf( '%sly', $period );
494
-			}
495
-			break;
496
-		case 2 :
497
-			$cart_key .= sprintf( 'every_2nd_%s', $period );
498
-			break;
499
-		case 3 :
500
-			$cart_key .= sprintf( 'every_3rd_%s', $period );
501
-		break;
502
-		default:
503
-			$cart_key .= sprintf( 'every_%dth_%s', $interval, $period );
504
-			break;
505
-	}
506
-
507
-	// Maybe add the optional maximum billing periods...
508
-	if ( $length > 0 ) {
509
-		$cart_key .= '_for_';
510
-		$cart_key .= sprintf( '%d_%s', $length, $period );
511
-		if ( $length > 1 ) {
512
-			$cart_key .= 's';
513
-		}
514
-	}
515
-
516
-	// And an optional free trial.
517
-	if ( $cart_item->has_free_trial() ) {
518
-		$cart_key .= sprintf( '_after_a_%d_%s_trial', $trial_length, $trial_period );
519
-	}
520
-
521
-	return apply_filters( 'getpaid_get_recurring_item_key', $cart_key, $cart_item );
480
+    $cart_key     = 'renews_';
481
+    $interval     = $cart_item->get_recurring_interval();
482
+    $period       = $cart_item->get_recurring_period( true );
483
+    $length       = $cart_item->get_recurring_limit() * $interval;
484
+    $trial_period = $cart_item->get_trial_period( true );
485
+    $trial_length = $cart_item->get_trial_interval();
486
+
487
+    // First start with the billing interval and period
488
+    switch ( $interval ) {
489
+        case 1 :
490
+            if ( 'day' == $period ) {
491
+                $cart_key .= 'daily';
492
+            } else {
493
+                $cart_key .= sprintf( '%sly', $period );
494
+            }
495
+            break;
496
+        case 2 :
497
+            $cart_key .= sprintf( 'every_2nd_%s', $period );
498
+            break;
499
+        case 3 :
500
+            $cart_key .= sprintf( 'every_3rd_%s', $period );
501
+        break;
502
+        default:
503
+            $cart_key .= sprintf( 'every_%dth_%s', $interval, $period );
504
+            break;
505
+    }
506
+
507
+    // Maybe add the optional maximum billing periods...
508
+    if ( $length > 0 ) {
509
+        $cart_key .= '_for_';
510
+        $cart_key .= sprintf( '%d_%s', $length, $period );
511
+        if ( $length > 1 ) {
512
+            $cart_key .= 's';
513
+        }
514
+    }
515
+
516
+    // And an optional free trial.
517
+    if ( $cart_item->has_free_trial() ) {
518
+        $cart_key .= sprintf( '_after_a_%d_%s_trial', $trial_length, $trial_period );
519
+    }
520
+
521
+    return apply_filters( 'getpaid_get_recurring_item_key', $cart_key, $cart_item );
522 522
 }
523 523
 
524 524
 /**
@@ -529,17 +529,17 @@  discard block
 block discarded – undo
529 529
  */
530 530
 function getpaid_get_subscription_groups( $invoice ) {
531 531
 
532
-	// Generate subscription groups.
533
-	$subscription_groups = array();
534
-	foreach ( $invoice->get_items() as $item ) {
532
+    // Generate subscription groups.
533
+    $subscription_groups = array();
534
+    foreach ( $invoice->get_items() as $item ) {
535 535
 
536
-		if ( $item->is_recurring() ) {
537
-			$subscription_groups[ getpaid_get_recurring_item_key( $item ) ][] = $item;
538
-		}
536
+        if ( $item->is_recurring() ) {
537
+            $subscription_groups[ getpaid_get_recurring_item_key( $item ) ][] = $item;
538
+        }
539 539
 
540
-	}
540
+    }
541 541
 
542
-	return $subscription_groups;
542
+    return $subscription_groups;
543 543
 }
544 544
 
545 545
 /**
@@ -553,57 +553,57 @@  discard block
 block discarded – undo
553 553
  */
554 554
 function getpaid_calculate_subscription_totals( $invoice ) {
555 555
 
556
-	// Generate subscription groups.
557
-	$subscription_groups = getpaid_get_subscription_groups( $invoice );
556
+    // Generate subscription groups.
557
+    $subscription_groups = getpaid_get_subscription_groups( $invoice );
558 558
 
559
-	// Now let's calculate the totals for each group of subscriptions
560
-	$subscription_totals = array();
559
+    // Now let's calculate the totals for each group of subscriptions
560
+    $subscription_totals = array();
561 561
 
562
-	foreach ( $subscription_groups as $subscription_key => $items ) {
562
+    foreach ( $subscription_groups as $subscription_key => $items ) {
563 563
 
564
-		if ( empty( $subscription_totals[ $subscription_key ] ) ) {
564
+        if ( empty( $subscription_totals[ $subscription_key ] ) ) {
565 565
 
566
-			$subscription_totals[ $subscription_key ] = array(
567
-				'initial_total'   => 0,
568
-				'recurring_total' => 0,
569
-				'items'           => array(),
570
-				'trialling'       => false,
571
-			);
566
+            $subscription_totals[ $subscription_key ] = array(
567
+                'initial_total'   => 0,
568
+                'recurring_total' => 0,
569
+                'items'           => array(),
570
+                'trialling'       => false,
571
+            );
572 572
 
573
-		}
573
+        }
574 574
 
575
-		/**
576
-		 * Get the totals of the group.
577
-		 * @var GetPaid_Form_Item $item
578
-		 */
579
-		foreach ( $items as $item ) {
575
+        /**
576
+         * Get the totals of the group.
577
+         * @var GetPaid_Form_Item $item
578
+         */
579
+        foreach ( $items as $item ) {
580 580
 
581
-			$subscription_totals[ $subscription_key ]['items'][$item->get_id()]  = $item->prepare_data_for_saving();
582
-			$subscription_totals[ $subscription_key ]['item_id']                 = $item->get_id();
583
-			$subscription_totals[ $subscription_key ]['period']                  = $item->get_recurring_period( true );
584
-			$subscription_totals[ $subscription_key ]['interval']                = $item->get_recurring_interval();
585
-			$subscription_totals[ $subscription_key ]['initial_total']          += $item->get_sub_total();
586
-			$subscription_totals[ $subscription_key ]['recurring_total']        += $item->get_recurring_sub_total();
587
-			$subscription_totals[ $subscription_key ]['recurring_limit']         = $item->get_recurring_limit();
581
+            $subscription_totals[ $subscription_key ]['items'][$item->get_id()]  = $item->prepare_data_for_saving();
582
+            $subscription_totals[ $subscription_key ]['item_id']                 = $item->get_id();
583
+            $subscription_totals[ $subscription_key ]['period']                  = $item->get_recurring_period( true );
584
+            $subscription_totals[ $subscription_key ]['interval']                = $item->get_recurring_interval();
585
+            $subscription_totals[ $subscription_key ]['initial_total']          += $item->get_sub_total();
586
+            $subscription_totals[ $subscription_key ]['recurring_total']        += $item->get_recurring_sub_total();
587
+            $subscription_totals[ $subscription_key ]['recurring_limit']         = $item->get_recurring_limit();
588 588
 
589
-			// Calculate the next renewal date.
590
-			$period       = $item->get_recurring_period( true );
591
-			$interval     = $item->get_recurring_interval();
589
+            // Calculate the next renewal date.
590
+            $period       = $item->get_recurring_period( true );
591
+            $interval     = $item->get_recurring_interval();
592 592
 
593
-			// If the subscription item has a trial period...
594
-			if ( $item->has_free_trial() ) {
595
-				$period   = $item->get_trial_period( true );
596
-				$interval = $item->get_trial_interval();
597
-				$subscription_totals[ $subscription_key ]['trialling'] = $interval . ' ' . $period;
598
-			}
593
+            // If the subscription item has a trial period...
594
+            if ( $item->has_free_trial() ) {
595
+                $period   = $item->get_trial_period( true );
596
+                $interval = $item->get_trial_interval();
597
+                $subscription_totals[ $subscription_key ]['trialling'] = $interval . ' ' . $period;
598
+            }
599 599
 
600
-			$subscription_totals[ $subscription_key ]['renews_on'] = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", current_time( 'timestamp' ) ) );
600
+            $subscription_totals[ $subscription_key ]['renews_on'] = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", current_time( 'timestamp' ) ) );
601 601
 
602
-		}
602
+        }
603 603
 
604
-	}
604
+    }
605 605
 
606
-	return apply_filters( 'getpaid_calculate_subscription_totals', $subscription_totals, $invoice );
606
+    return apply_filters( 'getpaid_calculate_subscription_totals', $subscription_totals, $invoice );
607 607
 }
608 608
 
609 609
 /**
@@ -614,17 +614,17 @@  discard block
 block discarded – undo
614 614
  */
615 615
 function getpaid_should_group_subscriptions( $invoice ) {
616 616
 
617
-	$recurring_items = 0;
617
+    $recurring_items = 0;
618 618
 
619
-	foreach ( $invoice->get_items() as $item ) {
619
+    foreach ( $invoice->get_items() as $item ) {
620 620
 
621
-		if ( $item->is_recurring() ) {
622
-			$recurring_items ++;
623
-		}
621
+        if ( $item->is_recurring() ) {
622
+            $recurring_items ++;
623
+        }
624 624
 
625
-	}
625
+    }
626 626
 
627
-	return apply_filters( 'getpaid_should_group_subscriptions', $recurring_items > 1, $invoice );
627
+    return apply_filters( 'getpaid_should_group_subscriptions', $recurring_items > 1, $invoice );
628 628
 }
629 629
 
630 630
 /**
@@ -634,14 +634,14 @@  discard block
 block discarded – undo
634 634
  * @return int
635 635
  */
636 636
 function getpaid_count_subscription_invoices( $parent_invoice_id ) {
637
-	global $wpdb;
638
-
639
-	return (int) $wpdb->get_var(
640
-		$wpdb->prepare(
641
-			"SELECT COUNT(ID) FROM $wpdb->posts WHERE ( post_parent=%d OR ID=%d ) AND post_status IN ( 'publish', 'wpi-processing', 'wpi-renewal' )",
642
-			$parent_invoice_id,
643
-			$parent_invoice_id
644
-		)
645
-	);
637
+    global $wpdb;
638
+
639
+    return (int) $wpdb->get_var(
640
+        $wpdb->prepare(
641
+            "SELECT COUNT(ID) FROM $wpdb->posts WHERE ( post_parent=%d OR ID=%d ) AND post_status IN ( 'publish', 'wpi-processing', 'wpi-renewal' )",
642
+            $parent_invoice_id,
643
+            $parent_invoice_id
644
+        )
645
+    );
646 646
 
647 647
 }
Please login to merge, or discard this patch.
Spacing   +139 added lines, -139 removed lines patch added patch discarded remove patch
@@ -13,18 +13,18 @@  discard block
 block discarded – undo
13 13
  * @return      WPInv_Subscription[]|WPInv_Subscription|false
14 14
  * @since       2.3.0
15 15
  */
16
-function getpaid_get_invoice_subscriptions( $invoice ) {
16
+function getpaid_get_invoice_subscriptions($invoice) {
17 17
 
18 18
     // Retrieve subscription groups.
19
-    $subscription_ids = wp_list_pluck( getpaid_get_invoice_subscription_groups( $invoice->get_id() ), 'subscription_id' );
19
+    $subscription_ids = wp_list_pluck(getpaid_get_invoice_subscription_groups($invoice->get_id()), 'subscription_id');
20 20
 
21 21
     // No subscription groups, normal subscription.
22
-    if ( empty( $subscription_ids ) ) {
23
-        return getpaid_subscriptions()->get_invoice_subscription( $invoice );
22
+    if (empty($subscription_ids)) {
23
+        return getpaid_subscriptions()->get_invoice_subscription($invoice);
24 24
     }
25 25
 
26 26
     // Subscription groups.
27
-    return array_filter( array_map( 'getpaid_get_subscription', $subscription_ids ) );
27
+    return array_filter(array_map('getpaid_get_subscription', $subscription_ids));
28 28
 
29 29
 }
30 30
 
@@ -35,9 +35,9 @@  discard block
 block discarded – undo
35 35
  * @return      array
36 36
  * @since       2.3.0
37 37
  */
38
-function getpaid_get_invoice_subscription_groups( $invoice_id ) {
39
-    $subscription_groups = get_post_meta( $invoice_id, 'getpaid_subscription_groups', true );
40
-    return empty( $subscription_groups ) ? array() : $subscription_groups;
38
+function getpaid_get_invoice_subscription_groups($invoice_id) {
39
+    $subscription_groups = get_post_meta($invoice_id, 'getpaid_subscription_groups', true);
40
+    return empty($subscription_groups) ? array() : $subscription_groups;
41 41
 }
42 42
 
43 43
 /**
@@ -48,10 +48,10 @@  discard block
 block discarded – undo
48 48
  * @return      array|false
49 49
  * @since       2.3.0
50 50
  */
51
-function getpaid_get_invoice_subscription_group( $invoice_id, $subscription_id ) {
52
-    $subscription_groups = getpaid_get_invoice_subscription_groups( $invoice_id );
53
-	$matching_group      = wp_list_filter( $subscription_groups, compact( 'subscription_id' ) );
54
-    return reset( $matching_group );
51
+function getpaid_get_invoice_subscription_group($invoice_id, $subscription_id) {
52
+    $subscription_groups = getpaid_get_invoice_subscription_groups($invoice_id);
53
+	$matching_group = wp_list_filter($subscription_groups, compact('subscription_id'));
54
+    return reset($matching_group);
55 55
 }
56 56
 
57 57
 /**
@@ -61,10 +61,10 @@  discard block
 block discarded – undo
61 61
  * @since       2.3.0
62 62
  * @return WPInv_Subscription|false
63 63
  */
64
-function getpaid_get_subscription( $subscription ) {
64
+function getpaid_get_subscription($subscription) {
65 65
 
66
-	if ( ! is_a( $subscription, 'WPInv_Subscription' ) ) {
67
-		$subscription = new WPInv_Subscription( $subscription );
66
+	if (!is_a($subscription, 'WPInv_Subscription')) {
67
+		$subscription = new WPInv_Subscription($subscription);
68 68
 	}
69 69
 
70 70
 	return $subscription->exists() ? $subscription : false;
@@ -79,26 +79,26 @@  discard block
 block discarded – undo
79 79
  *
80 80
  * @return int|array|WPInv_Subscription[]|GetPaid_Subscriptions_Query
81 81
  */
82
-function getpaid_get_subscriptions( $args = array(), $return = 'results' ) {
82
+function getpaid_get_subscriptions($args = array(), $return = 'results') {
83 83
 
84 84
 	// Do not retrieve all fields if we just want the count.
85
-	if ( 'count' == $return ) {
85
+	if ('count' == $return) {
86 86
 		$args['fields'] = 'id';
87 87
 		$args['number'] = 1;
88 88
 	}
89 89
 
90 90
 	// Do not count all matches if we just want the results.
91
-	if ( 'results' == $return ) {
91
+	if ('results' == $return) {
92 92
 		$args['count_total'] = false;
93 93
 	}
94 94
 
95
-	$query = new GetPaid_Subscriptions_Query( $args );
95
+	$query = new GetPaid_Subscriptions_Query($args);
96 96
 
97
-	if ( 'results' == $return ) {
97
+	if ('results' == $return) {
98 98
 		return $query->get_results();
99 99
 	}
100 100
 
101
-	if ( 'count' == $return ) {
101
+	if ('count' == $return) {
102 102
 		return $query->get_total();
103 103
 	}
104 104
 
@@ -115,13 +115,13 @@  discard block
 block discarded – undo
115 115
 	return apply_filters(
116 116
 		'getpaid_get_subscription_statuses',
117 117
 		array(
118
-			'pending'    => __( 'Pending', 'invoicing' ),
119
-			'trialling'  => __( 'Trialing', 'invoicing' ),
120
-			'active'     => __( 'Active', 'invoicing' ),
121
-			'failing'    => __( 'Failing', 'invoicing' ),
122
-			'expired'    => __( 'Expired', 'invoicing' ),
123
-			'completed'  => __( 'Complete', 'invoicing' ),
124
-			'cancelled'  => __( 'Cancelled', 'invoicing' ),
118
+			'pending'    => __('Pending', 'invoicing'),
119
+			'trialling'  => __('Trialing', 'invoicing'),
120
+			'active'     => __('Active', 'invoicing'),
121
+			'failing'    => __('Failing', 'invoicing'),
122
+			'expired'    => __('Expired', 'invoicing'),
123
+			'completed'  => __('Complete', 'invoicing'),
124
+			'cancelled'  => __('Cancelled', 'invoicing'),
125 125
 		)
126 126
 	);
127 127
 
@@ -132,9 +132,9 @@  discard block
 block discarded – undo
132 132
  *
133 133
  * @return string
134 134
  */
135
-function getpaid_get_subscription_status_label( $status ) {
135
+function getpaid_get_subscription_status_label($status) {
136 136
 	$statuses = getpaid_get_subscription_statuses();
137
-	return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) );
137
+	return isset($statuses[$status]) ? $statuses[$status] : ucfirst(sanitize_text_field($status));
138 138
 }
139 139
 
140 140
 /**
@@ -164,14 +164,14 @@  discard block
 block discarded – undo
164 164
  *
165 165
  * @return array
166 166
  */
167
-function getpaid_get_subscription_status_counts( $args = array() ) {
167
+function getpaid_get_subscription_status_counts($args = array()) {
168 168
 
169
-	$statuses = array_keys( getpaid_get_subscription_statuses() );
169
+	$statuses = array_keys(getpaid_get_subscription_statuses());
170 170
 	$counts   = array();
171 171
 
172
-	foreach ( $statuses as $status ) {
173
-		$_args             = wp_parse_args( "status=$status", $args );
174
-		$counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' );
172
+	foreach ($statuses as $status) {
173
+		$_args             = wp_parse_args("status=$status", $args);
174
+		$counts[$status] = getpaid_get_subscriptions($_args, 'count');
175 175
 	}
176 176
 
177 177
 	return $counts;
@@ -190,23 +190,23 @@  discard block
 block discarded – undo
190 190
 		array(
191 191
 
192 192
 			'day'   => array(
193
-				'singular' => __( '%s day', 'invoicing' ),
194
-				'plural'   => __( '%d days', 'invoicing' ),
193
+				'singular' => __('%s day', 'invoicing'),
194
+				'plural'   => __('%d days', 'invoicing'),
195 195
 			),
196 196
 
197 197
 			'week'   => array(
198
-				'singular' => __( '%s week', 'invoicing' ),
199
-				'plural'   => __( '%d weeks', 'invoicing' ),
198
+				'singular' => __('%s week', 'invoicing'),
199
+				'plural'   => __('%d weeks', 'invoicing'),
200 200
 			),
201 201
 
202 202
 			'month'   => array(
203
-				'singular' => __( '%s month', 'invoicing' ),
204
-				'plural'   => __( '%d months', 'invoicing' ),
203
+				'singular' => __('%s month', 'invoicing'),
204
+				'plural'   => __('%d months', 'invoicing'),
205 205
 			),
206 206
 
207 207
 			'year'   => array(
208
-				'singular' => __( '%s year', 'invoicing' ),
209
-				'plural'   => __( '%d years', 'invoicing' ),
208
+				'singular' => __('%s year', 'invoicing'),
209
+				'plural'   => __('%d years', 'invoicing'),
210 210
 			),
211 211
 
212 212
 		)
@@ -220,8 +220,8 @@  discard block
 block discarded – undo
220 220
  * @param string $trial_period
221 221
  * @return int
222 222
  */
223
-function getpaid_get_subscription_trial_period_interval( $trial_period ) {
224
-	return (int) preg_replace( '/[^0-9]/', '', $trial_period );
223
+function getpaid_get_subscription_trial_period_interval($trial_period) {
224
+	return (int) preg_replace('/[^0-9]/', '', $trial_period);
225 225
 }
226 226
 
227 227
 /**
@@ -230,8 +230,8 @@  discard block
 block discarded – undo
230 230
  * @param string $trial_period
231 231
  * @return string
232 232
  */
233
-function getpaid_get_subscription_trial_period_period( $trial_period ) {
234
-	return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) );
233
+function getpaid_get_subscription_trial_period_period($trial_period) {
234
+	return preg_replace('/[^a-z]/', '', strtolower($trial_period));
235 235
 }
236 236
 
237 237
 /**
@@ -241,9 +241,9 @@  discard block
 block discarded – undo
241 241
  * @param int $interval
242 242
  * @return string
243 243
  */
244
-function getpaid_get_subscription_period_label( $period, $interval = 1, $singular_prefix = '1' ) {
245
-	$label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label(  $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix );
246
-	return strtolower( sanitize_text_field( $label ) );
244
+function getpaid_get_subscription_period_label($period, $interval = 1, $singular_prefix = '1') {
245
+	$label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label($period, $interval) : getpaid_get_singular_subscription_period_label($period, $singular_prefix);
246
+	return strtolower(sanitize_text_field($label));
247 247
 }
248 248
 
249 249
 /**
@@ -252,19 +252,19 @@  discard block
 block discarded – undo
252 252
  * @param string $period
253 253
  * @return string
254 254
  */
255
-function getpaid_get_singular_subscription_period_label( $period, $singular_prefix = '1' ) {
255
+function getpaid_get_singular_subscription_period_label($period, $singular_prefix = '1') {
256 256
 
257 257
 	$periods = getpaid_get_subscription_periods();
258
-	$period  = strtolower( $period );
258
+	$period  = strtolower($period);
259 259
 
260
-	if ( isset( $periods[ $period ] ) ) {
261
-		return sprintf( $periods[ $period ]['singular'], $singular_prefix );
260
+	if (isset($periods[$period])) {
261
+		return sprintf($periods[$period]['singular'], $singular_prefix);
262 262
 	}
263 263
 
264 264
 	// Backwards compatibility.
265
-	foreach ( $periods as $key => $data ) {
266
-		if ( strpos( $key, $period ) === 0 ) {
267
-			return sprintf( $data['singular'], $singular_prefix );
265
+	foreach ($periods as $key => $data) {
266
+		if (strpos($key, $period) === 0) {
267
+			return sprintf($data['singular'], $singular_prefix);
268 268
 		}
269 269
 	}
270 270
 
@@ -279,19 +279,19 @@  discard block
 block discarded – undo
279 279
  * @param int $interval
280 280
  * @return string
281 281
  */
282
-function getpaid_get_plural_subscription_period_label( $period, $interval ) {
282
+function getpaid_get_plural_subscription_period_label($period, $interval) {
283 283
 
284 284
 	$periods = getpaid_get_subscription_periods();
285
-	$period  = strtolower( $period );
285
+	$period  = strtolower($period);
286 286
 
287
-	if ( isset( $periods[ $period ] ) ) {
288
-		return sprintf( $periods[ $period ]['plural'], $interval );
287
+	if (isset($periods[$period])) {
288
+		return sprintf($periods[$period]['plural'], $interval);
289 289
 	}
290 290
 
291 291
 	// Backwards compatibility.
292
-	foreach ( $periods as $key => $data ) {
293
-		if ( strpos( $key, $period ) === 0 ) {
294
-			return sprintf( $data['plural'], $interval );
292
+	foreach ($periods as $key => $data) {
293
+		if (strpos($key, $period) === 0) {
294
+			return sprintf($data['plural'], $interval);
295 295
 		}
296 296
 	}
297 297
 
@@ -305,32 +305,32 @@  discard block
 block discarded – undo
305 305
  * @param WPInv_Subscription $subscription
306 306
  * @return string
307 307
  */
308
-function getpaid_get_formatted_subscription_amount( $subscription ) {
308
+function getpaid_get_formatted_subscription_amount($subscription) {
309 309
 
310
-	$initial    = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() );
311
-	$recurring  = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() );
312
-	$period     = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' );
310
+	$initial    = wpinv_price($subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency());
311
+	$recurring  = wpinv_price($subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency());
312
+	$period     = getpaid_get_subscription_period_label($subscription->get_period(), $subscription->get_frequency(), '');
313 313
 	$bill_times = $subscription->get_bill_times();
314 314
 
315
-	if ( ! empty( $bill_times ) ) {
315
+	if (!empty($bill_times)) {
316 316
 		$bill_times = $subscription->get_frequency() * $bill_times;
317
-		$bill_times = getpaid_get_subscription_period_label( $subscription->get_period(), $bill_times );
317
+		$bill_times = getpaid_get_subscription_period_label($subscription->get_period(), $bill_times);
318 318
 	}
319 319
 
320 320
 	// Trial periods.
321
-	if ( $subscription->has_trial_period() ) {
321
+	if ($subscription->has_trial_period()) {
322 322
 
323
-		$trial_period   = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() );
324
-		$trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() );
323
+		$trial_period   = getpaid_get_subscription_trial_period_period($subscription->get_trial_period());
324
+		$trial_interval = getpaid_get_subscription_trial_period_interval($subscription->get_trial_period());
325 325
 
326
-		if ( empty( $bill_times ) ) {
326
+		if (empty($bill_times)) {
327 327
 
328 328
 			return sprintf(
329 329
 
330 330
 				// translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period
331
-				_x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ),
331
+				_x('%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing'),
332 332
 				$initial,
333
-				getpaid_get_subscription_period_label( $trial_period, $trial_interval ),
333
+				getpaid_get_subscription_period_label($trial_period, $trial_interval),
334 334
 				$recurring,
335 335
 				$period
336 336
 	
@@ -341,9 +341,9 @@  discard block
 block discarded – undo
341 341
 		return sprintf(
342 342
 
343 343
 			// translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period, $5: is the bill times
344
-			_x( '%1$s trial for %2$s then %3$s / %4$s for %5$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year for 4 years)', 'invoicing' ),
344
+			_x('%1$s trial for %2$s then %3$s / %4$s for %5$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year for 4 years)', 'invoicing'),
345 345
 			$initial,
346
-			getpaid_get_subscription_period_label( $trial_period, $trial_interval ),
346
+			getpaid_get_subscription_period_label($trial_period, $trial_interval),
347 347
 			$recurring,
348 348
 			$period,
349 349
 			$bill_times
@@ -351,14 +351,14 @@  discard block
 block discarded – undo
351 351
 
352 352
 	}
353 353
 
354
-	if ( $initial != $recurring ) {
354
+	if ($initial != $recurring) {
355 355
 
356
-		if ( empty( $bill_times ) ) {
356
+		if (empty($bill_times)) {
357 357
 
358 358
 			return sprintf(
359 359
 
360 360
 				// translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period
361
-				_x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ),
361
+				_x('Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing'),
362 362
 				$initial,
363 363
 				$recurring,
364 364
 				$period
@@ -370,7 +370,7 @@  discard block
 block discarded – undo
370 370
 		return sprintf(
371 371
 
372 372
 			// translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period, $4: is the bill times
373
-			_x( 'Initial payment of %1$s which renews at %2$s / %3$s for %4$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year for 5 years)', 'invoicing' ),
373
+			_x('Initial payment of %1$s which renews at %2$s / %3$s for %4$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year for 5 years)', 'invoicing'),
374 374
 			$initial,
375 375
 			$recurring,
376 376
 			$period,
@@ -380,12 +380,12 @@  discard block
 block discarded – undo
380 380
 
381 381
 	}
382 382
 
383
-	if ( empty( $bill_times ) ) {
383
+	if (empty($bill_times)) {
384 384
 
385 385
 		return sprintf(
386 386
 
387 387
 			// translators: $1: is the recurring amount, $2: is the recurring period
388
-			_x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ),
388
+			_x('%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing'),
389 389
 			$initial,
390 390
 			$period
391 391
 	
@@ -396,7 +396,7 @@  discard block
 block discarded – undo
396 396
 	return sprintf(
397 397
 
398 398
 		// translators: $1: is the bill times, $2: is the recurring amount, $3: is the recurring period
399
-		_x( '%2$s / %3$s for %1$s', 'Subscription amount. (e.g.: $120 / year for 5 years)', 'invoicing' ),
399
+		_x('%2$s / %3$s for %1$s', 'Subscription amount. (e.g.: $120 / year for 5 years)', 'invoicing'),
400 400
 		$bill_times,
401 401
 		$initial,
402 402
 		$period
@@ -411,8 +411,8 @@  discard block
 block discarded – undo
411 411
  * @param WPInv_Invoice $invoice
412 412
  * @return WPInv_Subscription|false
413 413
  */
414
-function getpaid_get_invoice_subscription( $invoice ) {
415
-	return getpaid_subscriptions()->get_invoice_subscription( $invoice );
414
+function getpaid_get_invoice_subscription($invoice) {
415
+	return getpaid_subscriptions()->get_invoice_subscription($invoice);
416 416
 }
417 417
 
418 418
 /**
@@ -420,9 +420,9 @@  discard block
 block discarded – undo
420 420
  *
421 421
  * @param WPInv_Invoice $invoice
422 422
  */
423
-function getpaid_activate_invoice_subscription( $invoice ) {
424
-	$subscription = getpaid_get_invoice_subscription( $invoice );
425
-	if ( is_a( $subscription, 'WPInv_Subscription' ) ) {
423
+function getpaid_activate_invoice_subscription($invoice) {
424
+	$subscription = getpaid_get_invoice_subscription($invoice);
425
+	if (is_a($subscription, 'WPInv_Subscription')) {
426 426
 		$subscription->activate();
427 427
 	}
428 428
 }
@@ -433,7 +433,7 @@  discard block
 block discarded – undo
433 433
  * @return WPInv_Subscriptions
434 434
  */
435 435
 function getpaid_subscriptions() {
436
-	return getpaid()->get( 'subscriptions' );
436
+	return getpaid()->get('subscriptions');
437 437
 }
438 438
 
439 439
 /**
@@ -442,13 +442,13 @@  discard block
 block discarded – undo
442 442
  * @since 2.3.0
443 443
  * @return WPInv_Subscription|bool
444 444
  */
445
-function wpinv_get_invoice_subscription( $invoice ) {
445
+function wpinv_get_invoice_subscription($invoice) {
446 446
 
447 447
     // Retrieve the invoice.
448
-    $invoice = new WPInv_Invoice( $invoice );
448
+    $invoice = new WPInv_Invoice($invoice);
449 449
 
450 450
     // Ensure it is a recurring invoice.
451
-    if ( ! $invoice->is_recurring() ) {
451
+    if (!$invoice->is_recurring()) {
452 452
         return false;
453 453
     }
454 454
 
@@ -460,7 +460,7 @@  discard block
 block discarded – undo
460 460
 		)
461 461
 	);
462 462
 
463
-	return empty( $subscription ) ? false : $subscription[0];
463
+	return empty($subscription) ? false : $subscription[0];
464 464
 
465 465
 }
466 466
 
@@ -475,50 +475,50 @@  discard block
 block discarded – undo
475 475
  * @param GetPaid_Form_Item|WPInv_Item $cart_item
476 476
  * @return string
477 477
  */
478
-function getpaid_get_recurring_item_key( $cart_item ) {
478
+function getpaid_get_recurring_item_key($cart_item) {
479 479
 
480 480
 	$cart_key     = 'renews_';
481 481
 	$interval     = $cart_item->get_recurring_interval();
482
-	$period       = $cart_item->get_recurring_period( true );
482
+	$period       = $cart_item->get_recurring_period(true);
483 483
 	$length       = $cart_item->get_recurring_limit() * $interval;
484
-	$trial_period = $cart_item->get_trial_period( true );
484
+	$trial_period = $cart_item->get_trial_period(true);
485 485
 	$trial_length = $cart_item->get_trial_interval();
486 486
 
487 487
 	// First start with the billing interval and period
488
-	switch ( $interval ) {
488
+	switch ($interval) {
489 489
 		case 1 :
490
-			if ( 'day' == $period ) {
490
+			if ('day' == $period) {
491 491
 				$cart_key .= 'daily';
492 492
 			} else {
493
-				$cart_key .= sprintf( '%sly', $period );
493
+				$cart_key .= sprintf('%sly', $period);
494 494
 			}
495 495
 			break;
496 496
 		case 2 :
497
-			$cart_key .= sprintf( 'every_2nd_%s', $period );
497
+			$cart_key .= sprintf('every_2nd_%s', $period);
498 498
 			break;
499 499
 		case 3 :
500
-			$cart_key .= sprintf( 'every_3rd_%s', $period );
500
+			$cart_key .= sprintf('every_3rd_%s', $period);
501 501
 		break;
502 502
 		default:
503
-			$cart_key .= sprintf( 'every_%dth_%s', $interval, $period );
503
+			$cart_key .= sprintf('every_%dth_%s', $interval, $period);
504 504
 			break;
505 505
 	}
506 506
 
507 507
 	// Maybe add the optional maximum billing periods...
508
-	if ( $length > 0 ) {
508
+	if ($length > 0) {
509 509
 		$cart_key .= '_for_';
510
-		$cart_key .= sprintf( '%d_%s', $length, $period );
511
-		if ( $length > 1 ) {
510
+		$cart_key .= sprintf('%d_%s', $length, $period);
511
+		if ($length > 1) {
512 512
 			$cart_key .= 's';
513 513
 		}
514 514
 	}
515 515
 
516 516
 	// And an optional free trial.
517
-	if ( $cart_item->has_free_trial() ) {
518
-		$cart_key .= sprintf( '_after_a_%d_%s_trial', $trial_length, $trial_period );
517
+	if ($cart_item->has_free_trial()) {
518
+		$cart_key .= sprintf('_after_a_%d_%s_trial', $trial_length, $trial_period);
519 519
 	}
520 520
 
521
-	return apply_filters( 'getpaid_get_recurring_item_key', $cart_key, $cart_item );
521
+	return apply_filters('getpaid_get_recurring_item_key', $cart_key, $cart_item);
522 522
 }
523 523
 
524 524
 /**
@@ -527,14 +527,14 @@  discard block
 block discarded – undo
527 527
  * @param WPInv_Invoice|GetPaid_Payment_Form_Submission|GetPaid_Payment_Form $invoice
528 528
  * @return array
529 529
  */
530
-function getpaid_get_subscription_groups( $invoice ) {
530
+function getpaid_get_subscription_groups($invoice) {
531 531
 
532 532
 	// Generate subscription groups.
533 533
 	$subscription_groups = array();
534
-	foreach ( $invoice->get_items() as $item ) {
534
+	foreach ($invoice->get_items() as $item) {
535 535
 
536
-		if ( $item->is_recurring() ) {
537
-			$subscription_groups[ getpaid_get_recurring_item_key( $item ) ][] = $item;
536
+		if ($item->is_recurring()) {
537
+			$subscription_groups[getpaid_get_recurring_item_key($item)][] = $item;
538 538
 		}
539 539
 
540 540
 	}
@@ -551,19 +551,19 @@  discard block
 block discarded – undo
551 551
  * @param WPInv_Invoice|GetPaid_Payment_Form_Submission|GetPaid_Payment_Form $invoice
552 552
  * @return array
553 553
  */
554
-function getpaid_calculate_subscription_totals( $invoice ) {
554
+function getpaid_calculate_subscription_totals($invoice) {
555 555
 
556 556
 	// Generate subscription groups.
557
-	$subscription_groups = getpaid_get_subscription_groups( $invoice );
557
+	$subscription_groups = getpaid_get_subscription_groups($invoice);
558 558
 
559 559
 	// Now let's calculate the totals for each group of subscriptions
560 560
 	$subscription_totals = array();
561 561
 
562
-	foreach ( $subscription_groups as $subscription_key => $items ) {
562
+	foreach ($subscription_groups as $subscription_key => $items) {
563 563
 
564
-		if ( empty( $subscription_totals[ $subscription_key ] ) ) {
564
+		if (empty($subscription_totals[$subscription_key])) {
565 565
 
566
-			$subscription_totals[ $subscription_key ] = array(
566
+			$subscription_totals[$subscription_key] = array(
567 567
 				'initial_total'   => 0,
568 568
 				'recurring_total' => 0,
569 569
 				'items'           => array(),
@@ -576,34 +576,34 @@  discard block
 block discarded – undo
576 576
 		 * Get the totals of the group.
577 577
 		 * @var GetPaid_Form_Item $item
578 578
 		 */
579
-		foreach ( $items as $item ) {
579
+		foreach ($items as $item) {
580 580
 
581
-			$subscription_totals[ $subscription_key ]['items'][$item->get_id()]  = $item->prepare_data_for_saving();
582
-			$subscription_totals[ $subscription_key ]['item_id']                 = $item->get_id();
583
-			$subscription_totals[ $subscription_key ]['period']                  = $item->get_recurring_period( true );
584
-			$subscription_totals[ $subscription_key ]['interval']                = $item->get_recurring_interval();
585
-			$subscription_totals[ $subscription_key ]['initial_total']          += $item->get_sub_total();
586
-			$subscription_totals[ $subscription_key ]['recurring_total']        += $item->get_recurring_sub_total();
587
-			$subscription_totals[ $subscription_key ]['recurring_limit']         = $item->get_recurring_limit();
581
+			$subscription_totals[$subscription_key]['items'][$item->get_id()]  = $item->prepare_data_for_saving();
582
+			$subscription_totals[$subscription_key]['item_id']                 = $item->get_id();
583
+			$subscription_totals[$subscription_key]['period']                  = $item->get_recurring_period(true);
584
+			$subscription_totals[$subscription_key]['interval']                = $item->get_recurring_interval();
585
+			$subscription_totals[$subscription_key]['initial_total']          += $item->get_sub_total();
586
+			$subscription_totals[$subscription_key]['recurring_total']        += $item->get_recurring_sub_total();
587
+			$subscription_totals[$subscription_key]['recurring_limit']         = $item->get_recurring_limit();
588 588
 
589 589
 			// Calculate the next renewal date.
590
-			$period       = $item->get_recurring_period( true );
590
+			$period       = $item->get_recurring_period(true);
591 591
 			$interval     = $item->get_recurring_interval();
592 592
 
593 593
 			// If the subscription item has a trial period...
594
-			if ( $item->has_free_trial() ) {
595
-				$period   = $item->get_trial_period( true );
594
+			if ($item->has_free_trial()) {
595
+				$period   = $item->get_trial_period(true);
596 596
 				$interval = $item->get_trial_interval();
597
-				$subscription_totals[ $subscription_key ]['trialling'] = $interval . ' ' . $period;
597
+				$subscription_totals[$subscription_key]['trialling'] = $interval . ' ' . $period;
598 598
 			}
599 599
 
600
-			$subscription_totals[ $subscription_key ]['renews_on'] = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", current_time( 'timestamp' ) ) );
600
+			$subscription_totals[$subscription_key]['renews_on'] = date('Y-m-d H:i:s', strtotime("+$interval $period", current_time('timestamp')));
601 601
 
602 602
 		}
603 603
 
604 604
 	}
605 605
 
606
-	return apply_filters( 'getpaid_calculate_subscription_totals', $subscription_totals, $invoice );
606
+	return apply_filters('getpaid_calculate_subscription_totals', $subscription_totals, $invoice);
607 607
 }
608 608
 
609 609
 /**
@@ -612,19 +612,19 @@  discard block
 block discarded – undo
612 612
  * @param WPInv_Invoice|GetPaid_Payment_Form_Submission|GetPaid_Payment_Form $invoice
613 613
  * @return array
614 614
  */
615
-function getpaid_should_group_subscriptions( $invoice ) {
615
+function getpaid_should_group_subscriptions($invoice) {
616 616
 
617 617
 	$recurring_items = 0;
618 618
 
619
-	foreach ( $invoice->get_items() as $item ) {
619
+	foreach ($invoice->get_items() as $item) {
620 620
 
621
-		if ( $item->is_recurring() ) {
622
-			$recurring_items ++;
621
+		if ($item->is_recurring()) {
622
+			$recurring_items++;
623 623
 		}
624 624
 
625 625
 	}
626 626
 
627
-	return apply_filters( 'getpaid_should_group_subscriptions', $recurring_items > 1, $invoice );
627
+	return apply_filters('getpaid_should_group_subscriptions', $recurring_items > 1, $invoice);
628 628
 }
629 629
 
630 630
 /**
@@ -633,7 +633,7 @@  discard block
 block discarded – undo
633 633
  * @param int $parent_invoice_id
634 634
  * @return int
635 635
  */
636
-function getpaid_count_subscription_invoices( $parent_invoice_id ) {
636
+function getpaid_count_subscription_invoices($parent_invoice_id) {
637 637
 	global $wpdb;
638 638
 
639 639
 	return (int) $wpdb->get_var(
Please login to merge, or discard this patch.
includes/gateways/class-getpaid-paypal-gateway-ipn-handler.php 2 patches
Indentation   +390 added lines, -390 removed lines patch added patch discarded remove patch
@@ -12,472 +12,472 @@
 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_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_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_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_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'] );
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
-		}
221
+        $this->validate_ipn_receiver_email( $invoice, $business_email );
222
+        $this->validate_ipn_currency( $invoice, $posted['mc_currency'] );
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
+        }
229 229
 
230
-		// Process a refund.
231
-		if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) {
230
+        // Process a refund.
231
+        if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) {
232 232
 
233
-			update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 );
233
+            update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 );
234 234
 
235
-			if ( ! $invoice->is_refunded() ) {
236
-				$invoice->update_status( 'wpi-refunded', $posted['reason_code'] );
237
-			}
235
+            if ( ! $invoice->is_refunded() ) {
236
+                $invoice->update_status( 'wpi-refunded', $posted['reason_code'] );
237
+            }
238 238
 
239
-			return wpinv_error_log( $posted['reason_code'], false );
240
-		}
239
+            return wpinv_error_log( $posted['reason_code'], false );
240
+        }
241 241
 
242
-		// Process payments.
243
-		if ( $payment_status == 'completed' ) {
242
+        // Process payments.
243
+        if ( $payment_status == 'completed' ) {
244 244
 
245
-			if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) {
246
-				return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false );
247
-			}
245
+            if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) {
246
+                return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false );
247
+            }
248 248
 
249
-			$this->validate_ipn_amount( $invoice, $posted['mc_gross'] );
249
+            $this->validate_ipn_amount( $invoice, $posted['mc_gross'] );
250 250
 
251
-			$note = '';
251
+            $note = '';
252 252
 
253
-			if ( ! empty( $posted['mc_fee'] ) ) {
254
-				$note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) );
255
-			}
253
+            if ( ! empty( $posted['mc_fee'] ) ) {
254
+                $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) );
255
+            }
256 256
 
257
-			if ( ! empty( $posted['payer_status'] ) ) {
258
-				$note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) );
259
-			}
257
+            if ( ! empty( $posted['payer_status'] ) ) {
258
+                $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) );
259
+            }
260 260
 
261
-			$invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) );
262
-			return wpinv_error_log( 'Invoice marked as paid.', false );
261
+            $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) );
262
+            return wpinv_error_log( 'Invoice marked as paid.', false );
263 263
 
264
-		}
264
+        }
265 265
 
266
-		// Pending payments.
267
-		if ( $payment_status == 'pending' ) {
266
+        // Pending payments.
267
+        if ( $payment_status == 'pending' ) {
268 268
 
269
-			/* translators: %s: pending reason. */
270
-			$invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) );
269
+            /* translators: %s: pending reason. */
270
+            $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) );
271 271
 
272
-			return wpinv_error_log( 'Invoice marked as "payment held".', false );
273
-		}
272
+            return wpinv_error_log( 'Invoice marked as "payment held".', false );
273
+        }
274 274
 
275
-		/* translators: %s: payment status. */
276
-		$invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) );
275
+        /* translators: %s: payment status. */
276
+        $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) );
277 277
 
278
-	}
278
+    }
279 279
 
280
-	/**
281
-	 * Handles one time payments.
282
-	 *
283
-	 * @param WPInv_Invoice $invoice  Invoice object.
284
-	 * @param array    $posted Posted data.
285
-	 */
286
-	protected function ipn_txn_cart( $invoice, $posted ) {
287
-		$this->ipn_txn_web_accept( $invoice, $posted );
288
-	}
280
+    /**
281
+     * Handles one time payments.
282
+     *
283
+     * @param WPInv_Invoice $invoice  Invoice object.
284
+     * @param array    $posted Posted data.
285
+     */
286
+    protected function ipn_txn_cart( $invoice, $posted ) {
287
+        $this->ipn_txn_web_accept( $invoice, $posted );
288
+    }
289 289
 
290
-	/**
291
-	 * Handles subscription sign ups.
292
-	 *
293
-	 * @param WPInv_Invoice $invoice  Invoice object.
294
-	 * @param array    $posted Posted data.
295
-	 */
296
-	protected function ipn_txn_subscr_signup( $invoice, $posted ) {
290
+    /**
291
+     * Handles subscription sign ups.
292
+     *
293
+     * @param WPInv_Invoice $invoice  Invoice object.
294
+     * @param array    $posted Posted data.
295
+     */
296
+    protected function ipn_txn_subscr_signup( $invoice, $posted ) {
297 297
 
298
-		wpinv_error_log( 'Processing subscription signup', false );
298
+        wpinv_error_log( 'Processing subscription signup', false );
299 299
 
300
-		// Make sure the invoice has a subscription.
301
-		$subscription = getpaid_get_invoice_subscription( $invoice );
300
+        // Make sure the invoice has a subscription.
301
+        $subscription = getpaid_get_invoice_subscription( $invoice );
302 302
 
303
-		if ( empty( $subscription ) ) {
304
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
305
-		}
303
+        if ( empty( $subscription ) ) {
304
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
305
+        }
306 306
 
307
-		wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
307
+        wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
308 308
 
309
-		// Validate the IPN.
310
-		$business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] );
311
-		$this->validate_ipn_receiver_email( $invoice, $business_email );
312
-		$this->validate_ipn_currency( $invoice, $posted['mc_currency'] );
309
+        // Validate the IPN.
310
+        $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] );
311
+        $this->validate_ipn_receiver_email( $invoice, $business_email );
312
+        $this->validate_ipn_currency( $invoice, $posted['mc_currency'] );
313 313
 
314
-		// Activate the subscription.
315
-		$duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() );
316
-		$subscription->set_date_created( current_time( 'mysql' ) );
317
-		$subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) );
318
-		$subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) );
319
-		$subscription->activate();
314
+        // Activate the subscription.
315
+        $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() );
316
+        $subscription->set_date_created( current_time( 'mysql' ) );
317
+        $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) );
318
+        $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) );
319
+        $subscription->activate();
320 320
 
321
-		// Set the transaction id.
322
-		if ( ! empty( $posted['txn_id'] ) ) {
323
-			$invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
324
-			$invoice->set_transaction_id( $posted['txn_id'] );
325
-		}
321
+        // Set the transaction id.
322
+        if ( ! empty( $posted['txn_id'] ) ) {
323
+            $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
324
+            $invoice->set_transaction_id( $posted['txn_id'] );
325
+        }
326 326
 
327
-		// Update the payment status.
328
-		$invoice->mark_paid();
327
+        // Update the payment status.
328
+        $invoice->mark_paid();
329 329
 
330
-		$invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
330
+        $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
331 331
 
332
-		wpinv_error_log( 'Subscription started.', false );
333
-	}
332
+        wpinv_error_log( 'Subscription started.', false );
333
+    }
334 334
 
335
-	/**
336
-	 * Handles subscription renewals.
337
-	 *
338
-	 * @param WPInv_Invoice $invoice  Invoice object.
339
-	 * @param array    $posted Posted data.
340
-	 */
341
-	protected function ipn_txn_subscr_payment( $invoice, $posted ) {
335
+    /**
336
+     * Handles subscription renewals.
337
+     *
338
+     * @param WPInv_Invoice $invoice  Invoice object.
339
+     * @param array    $posted Posted data.
340
+     */
341
+    protected function ipn_txn_subscr_payment( $invoice, $posted ) {
342 342
 
343
-		// Make sure the invoice has a subscription.
344
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
343
+        // Make sure the invoice has a subscription.
344
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
345 345
 
346
-		if ( empty( $subscription ) ) {
347
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
348
-		}
346
+        if ( empty( $subscription ) ) {
347
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
348
+        }
349 349
 
350
-		wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
350
+        wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
351 351
 
352
-		// PayPal sends a subscr_payment for the first payment too.
353
-		$date_completed = getpaid_format_date( $invoice->get_date_completed() );
354
-		$date_created   = getpaid_format_date( $invoice->get_date_created() );
355
-		$today_date     = getpaid_format_date( current_time( 'mysql' ) );
356
-		$payment_date   = getpaid_format_date( $posted['payment_date'] );
357
-		$subscribe_date = getpaid_format_date( $subscription->get_date_created() );
358
-		$dates          = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) );
352
+        // PayPal sends a subscr_payment for the first payment too.
353
+        $date_completed = getpaid_format_date( $invoice->get_date_completed() );
354
+        $date_created   = getpaid_format_date( $invoice->get_date_created() );
355
+        $today_date     = getpaid_format_date( current_time( 'mysql' ) );
356
+        $payment_date   = getpaid_format_date( $posted['payment_date'] );
357
+        $subscribe_date = getpaid_format_date( $subscription->get_date_created() );
358
+        $dates          = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) );
359 359
 
360
-		foreach( $dates as $date ) {
360
+        foreach( $dates as $date ) {
361 361
 
362
-			if ( $date !== $today_date && $date !== $payment_date ) {
363
-				continue;
364
-			}
362
+            if ( $date !== $today_date && $date !== $payment_date ) {
363
+                continue;
364
+            }
365 365
 
366
-			if ( ! empty( $posted['txn_id'] ) ) {
367
-				$invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) );	
368
-				$invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true );
369
-			}
366
+            if ( ! empty( $posted['txn_id'] ) ) {
367
+                $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) );	
368
+                $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true );
369
+            }
370 370
 
371
-			return $invoice->mark_paid();
372
-
373
-		}
371
+            return $invoice->mark_paid();
372
+
373
+        }
374 374
 
375
-		wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false );
376
-
377
-		// Abort if the payment is already recorded.
378
-		if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) {
379
-			return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false );
380
-		}
381
-
382
-		$args = array(
383
-			'transaction_id' => $posted['txn_id'],
384
-			'gateway'        => $this->id,
385
-		);
386
-
387
-		$invoice = wpinv_get_invoice( $subscription->add_payment( $args ) );
375
+        wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false );
376
+
377
+        // Abort if the payment is already recorded.
378
+        if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) {
379
+            return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false );
380
+        }
381
+
382
+        $args = array(
383
+            'transaction_id' => $posted['txn_id'],
384
+            'gateway'        => $this->id,
385
+        );
386
+
387
+        $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) );
388 388
 
389
-		if ( empty( $invoice ) ) {
390
-			return;
391
-		}
389
+        if ( empty( $invoice ) ) {
390
+            return;
391
+        }
392 392
 
393
-		$invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
394
-		$invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
393
+        $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
394
+        $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
395 395
 
396
-		$subscription->renew();
397
-		wpinv_error_log( 'Subscription renewed.', false );
396
+        $subscription->renew();
397
+        wpinv_error_log( 'Subscription renewed.', false );
398 398
 
399
-	}
399
+    }
400 400
 
401
-	/**
402
-	 * Handles subscription cancelations.
403
-	 *
404
-	 * @param WPInv_Invoice $invoice  Invoice object.
405
-	 */
406
-	protected function ipn_txn_subscr_cancel( $invoice ) {
401
+    /**
402
+     * Handles subscription cancelations.
403
+     *
404
+     * @param WPInv_Invoice $invoice  Invoice object.
405
+     */
406
+    protected function ipn_txn_subscr_cancel( $invoice ) {
407 407
 
408
-		// Make sure the invoice has a subscription.
409
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
410
-
411
-		if ( empty( $subscription ) ) {
412
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
413
-		}
414
-
415
-		wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false );
416
-		$subscription->cancel();
417
-		wpinv_error_log( 'Subscription cancelled.', false );
408
+        // Make sure the invoice has a subscription.
409
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
410
+
411
+        if ( empty( $subscription ) ) {
412
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
413
+        }
414
+
415
+        wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false );
416
+        $subscription->cancel();
417
+        wpinv_error_log( 'Subscription cancelled.', false );
418 418
 
419
-	}
419
+    }
420 420
 
421
-	/**
422
-	 * Handles subscription completions.
423
-	 *
424
-	 * @param WPInv_Invoice $invoice  Invoice object.
425
-	 * @param array    $posted Posted data.
426
-	 */
427
-	protected function ipn_txn_subscr_eot( $invoice ) {
421
+    /**
422
+     * Handles subscription completions.
423
+     *
424
+     * @param WPInv_Invoice $invoice  Invoice object.
425
+     * @param array    $posted Posted data.
426
+     */
427
+    protected function ipn_txn_subscr_eot( $invoice ) {
428 428
 
429
-		// Make sure the invoice has a subscription.
430
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
429
+        // Make sure the invoice has a subscription.
430
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
431 431
 
432
-		if ( empty( $subscription ) ) {
433
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
434
-		}
432
+        if ( empty( $subscription ) ) {
433
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
434
+        }
435 435
 
436
-		wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false );
437
-		$subscription->complete();
438
-		wpinv_error_log( 'Subscription completed.', false );
436
+        wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false );
437
+        $subscription->complete();
438
+        wpinv_error_log( 'Subscription completed.', false );
439 439
 
440
-	}
440
+    }
441 441
 
442
-	/**
443
-	 * Handles subscription fails.
444
-	 *
445
-	 * @param WPInv_Invoice $invoice  Invoice object.
446
-	 * @param array    $posted Posted data.
447
-	 */
448
-	protected function ipn_txn_subscr_failed( $invoice ) {
442
+    /**
443
+     * Handles subscription fails.
444
+     *
445
+     * @param WPInv_Invoice $invoice  Invoice object.
446
+     * @param array    $posted Posted data.
447
+     */
448
+    protected function ipn_txn_subscr_failed( $invoice ) {
449 449
 
450
-		// Make sure the invoice has a subscription.
451
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
450
+        // Make sure the invoice has a subscription.
451
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
452 452
 
453
-		if ( empty( $subscription ) ) {
454
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
455
-		}
453
+        if ( empty( $subscription ) ) {
454
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
455
+        }
456 456
 
457
-		wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false );
458
-		$subscription->failing();
459
-		wpinv_error_log( 'Subscription marked as failing.', false );
457
+        wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false );
458
+        $subscription->failing();
459
+        wpinv_error_log( 'Subscription marked as failing.', false );
460 460
 
461
-	}
461
+    }
462 462
 
463
-	/**
464
-	 * Handles subscription suspensions.
465
-	 *
466
-	 * @param WPInv_Invoice $invoice  Invoice object.
467
-	 * @param array    $posted Posted data.
468
-	 */
469
-	protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) {
463
+    /**
464
+     * Handles subscription suspensions.
465
+     *
466
+     * @param WPInv_Invoice $invoice  Invoice object.
467
+     * @param array    $posted Posted data.
468
+     */
469
+    protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) {
470 470
 
471
-		// Make sure the invoice has a subscription.
472
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
471
+        // Make sure the invoice has a subscription.
472
+        $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
473 473
 
474
-		if ( empty( $subscription ) ) {
475
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
476
-		}
477
-
478
-		wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false );
479
-		$subscription->cancel();
480
-		wpinv_error_log( 'Subscription cancelled.', false );
481
-	}
474
+        if ( empty( $subscription ) ) {
475
+            return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
476
+        }
477
+
478
+        wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false );
479
+        $subscription->cancel();
480
+        wpinv_error_log( 'Subscription cancelled.', false );
481
+    }
482 482
 
483 483
 }
Please login to merge, or discard this patch.
Spacing   +150 added lines, -150 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
  *
5 5
  */
6 6
 
7
-defined( 'ABSPATH' ) || exit;
7
+defined('ABSPATH') || exit;
8 8
 
9 9
 /**
10 10
  * Paypal Payment Gateway IPN handler class.
@@ -31,7 +31,7 @@  discard block
 block discarded – undo
31 31
 	 *
32 32
 	 * @param GetPaid_Paypal_Gateway $gateway
33 33
 	 */
34
-	public function __construct( $gateway ) {
34
+	public function __construct($gateway) {
35 35
 		$this->gateway = $gateway;
36 36
 		$this->verify_ipn();
37 37
 	}
@@ -43,37 +43,37 @@  discard block
 block discarded – undo
43 43
 	 */
44 44
 	public function verify_ipn() {
45 45
 
46
-		wpinv_error_log( 'GetPaid PayPal IPN Handler', false );
46
+		wpinv_error_log('GetPaid PayPal IPN Handler', false);
47 47
 
48 48
 		// Validate the IPN.
49
-		if ( empty( $_POST ) || ! $this->validate_ipn() ) {
50
-			wp_die( 'PayPal IPN Request Failure', 500 );
49
+		if (empty($_POST) || !$this->validate_ipn()) {
50
+			wp_die('PayPal IPN Request Failure', 500);
51 51
 		}
52 52
 
53 53
 		// Process the IPN.
54
-		$posted  = wp_unslash( $_POST );
55
-		$invoice = $this->get_ipn_invoice( $posted );
54
+		$posted  = wp_unslash($_POST);
55
+		$invoice = $this->get_ipn_invoice($posted);
56 56
 
57 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 );
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 61
 		}
62 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'] ) );
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 65
 
66
-		wpinv_error_log( 'Payment status:' . $posted['payment_status'], false );
67
-		wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false );
66
+		wpinv_error_log('Payment status:' . $posted['payment_status'], false);
67
+		wpinv_error_log('IPN Type:' . $posted['txn_type'], false);
68 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 );
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 73
 		}
74 74
 
75
-		wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false );
76
-		wp_die( 'Unsupported IPN type', 200 );
75
+		wpinv_error_log('Aborting, Unsupported IPN type:' . $posted['txn_type'], false);
76
+		wp_die('Unsupported IPN type', 200);
77 77
 
78 78
 	}
79 79
 
@@ -83,22 +83,22 @@  discard block
 block discarded – undo
83 83
 	 * @param array $posted
84 84
 	 * @return WPInv_Invoice
85 85
 	 */
86
-	protected function get_ipn_invoice( $posted ) {
86
+	protected function get_ipn_invoice($posted) {
87 87
 
88
-		wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false );
88
+		wpinv_error_log('Retrieving PayPal IPN Response Invoice', false);
89 89
 
90
-		if ( ! empty( $posted['custom'] ) ) {
91
-			$invoice = new WPInv_Invoice( $posted['custom'] );
90
+		if (!empty($posted['custom'])) {
91
+			$invoice = new WPInv_Invoice($posted['custom']);
92 92
 
93
-			if ( $invoice->exists() ) {
94
-				wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false );
93
+			if ($invoice->exists()) {
94
+				wpinv_error_log('Found invoice #' . $invoice->get_number(), false);
95 95
 				return $invoice;
96 96
 			}
97 97
 
98 98
 		}
99 99
 
100
-		wpinv_error_log( 'Could not retrieve the associated invoice.', false );
101
-		wp_die( 'Could not retrieve the associated invoice.', 200 );
100
+		wpinv_error_log('Could not retrieve the associated invoice.', false);
101
+		wp_die('Could not retrieve the associated invoice.', 200);
102 102
 	}
103 103
 
104 104
 	/**
@@ -106,14 +106,14 @@  discard block
 block discarded – undo
106 106
 	 */
107 107
 	protected function validate_ipn() {
108 108
 
109
-		wpinv_error_log( 'Validating PayPal IPN response', false );
109
+		wpinv_error_log('Validating PayPal IPN response', false);
110 110
 
111 111
 		// Retrieve the associated invoice.
112
-		$posted  = wp_unslash( $_POST );
113
-		$invoice = $this->get_ipn_invoice( $posted );
112
+		$posted  = wp_unslash($_POST);
113
+		$invoice = $this->get_ipn_invoice($posted);
114 114
 
115
-		if ( $this->gateway->is_sandbox( $invoice ) ) {
116
-			wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false );
115
+		if ($this->gateway->is_sandbox($invoice)) {
116
+			wpinv_error_log($posted, 'Invoice was processed in sandbox hence logging the posted data', false);
117 117
 		}
118 118
 
119 119
 		// Validate the IPN.
@@ -130,20 +130,20 @@  discard block
 block discarded – undo
130 130
 		);
131 131
 
132 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 );
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 134
 
135 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 );
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 138
 			return true;
139 139
 		}
140 140
 
141
-		if ( is_wp_error( $response ) ) {
142
-			wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' );
141
+		if (is_wp_error($response)) {
142
+			wpinv_error_log($response->get_error_message(), 'Received invalid response from PayPal IPN');
143 143
 			return false;
144 144
 		}
145 145
 
146
-		wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' );
146
+		wpinv_error_log($response['body'], 'Received invalid response from PayPal IPN');
147 147
 		return false;
148 148
 
149 149
 	}
@@ -154,17 +154,17 @@  discard block
 block discarded – undo
154 154
 	 * @param WPInv_Invoice $invoice          Invoice object.
155 155
 	 * @param string   $currency currency to validate.
156 156
 	 */
157
-	protected function validate_ipn_currency( $invoice, $currency ) {
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 161
 			/* translators: %s: currency code. */
162
-			$invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) );
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 );
164
+			wpinv_error_log("Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true);
165 165
 		}
166 166
 
167
-		wpinv_error_log( $currency, 'Validated IPN Currency', false );
167
+		wpinv_error_log($currency, 'Validated IPN Currency', false);
168 168
 	}
169 169
 
170 170
 	/**
@@ -173,16 +173,16 @@  discard block
 block discarded – undo
173 173
 	 * @param WPInv_Invoice $invoice          Invoice object.
174 174
 	 * @param float   $amount amount to validate.
175 175
 	 */
176
-	protected function validate_ipn_amount( $invoice, $amount ) {
177
-		if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) {
176
+	protected function validate_ipn_amount($invoice, $amount) {
177
+		if (number_format($invoice->get_total(), 2, '.', '') !== number_format($amount, 2, '.', '')) {
178 178
 
179 179
 			/* translators: %s: Amount. */
180
-			$invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $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 );
182
+			wpinv_error_log("Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true);
183 183
 		}
184 184
 
185
-		wpinv_error_log( $amount, 'Validated IPN Amount', false );
185
+		wpinv_error_log($amount, 'Validated IPN Amount', false);
186 186
 	}
187 187
 
188 188
 	/**
@@ -191,19 +191,19 @@  discard block
 block discarded – undo
191 191
 	 * @param WPInv_Invoice $invoice          Invoice object.
192 192
 	 * @param string   $receiver_email Email to validate.
193 193
 	 */
194
-	protected function validate_ipn_receiver_email( $invoice, $receiver_email ) {
195
-		$paypal_email = wpinv_get_option( 'paypal_email' );
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 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 ) );
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 );
203
+			return wpinv_error_log("IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true);
204 204
 		}
205 205
 
206
-		wpinv_error_log( 'Validated PayPal Email', false );
206
+		wpinv_error_log('Validated PayPal Email', false);
207 207
 	}
208 208
 
209 209
 	/**
@@ -212,68 +212,68 @@  discard block
 block discarded – undo
212 212
 	 * @param WPInv_Invoice $invoice  Invoice object.
213 213
 	 * @param array    $posted Posted data.
214 214
 	 */
215
-	protected function ipn_txn_web_accept( $invoice, $posted ) {
215
+	protected function ipn_txn_web_accept($invoice, $posted) {
216 216
 
217 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'] );
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 224
 		// Update the transaction id.
225
-		if ( ! empty( $posted['txn_id'] ) ) {
226
-			$invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) );
225
+		if (!empty($posted['txn_id'])) {
226
+			$invoice->set_transaction_id(wpinv_clean($posted['txn_id']));
227 227
 			$invoice->save();
228 228
 		}
229 229
 
230 230
 		// Process a refund.
231
-		if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) {
231
+		if ($payment_status == 'refunded' || $payment_status == 'reversed') {
232 232
 
233
-			update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 );
233
+			update_post_meta($invoice->get_id(), 'refunded_remotely', 1);
234 234
 
235
-			if ( ! $invoice->is_refunded() ) {
236
-				$invoice->update_status( 'wpi-refunded', $posted['reason_code'] );
235
+			if (!$invoice->is_refunded()) {
236
+				$invoice->update_status('wpi-refunded', $posted['reason_code']);
237 237
 			}
238 238
 
239
-			return wpinv_error_log( $posted['reason_code'], false );
239
+			return wpinv_error_log($posted['reason_code'], false);
240 240
 		}
241 241
 
242 242
 		// Process payments.
243
-		if ( $payment_status == 'completed' ) {
243
+		if ($payment_status == 'completed') {
244 244
 
245
-			if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) {
246
-				return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false );
245
+			if ($invoice->is_paid() && 'wpi_processing' != $invoice->get_status()) {
246
+				return wpinv_error_log('Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false);
247 247
 			}
248 248
 
249
-			$this->validate_ipn_amount( $invoice, $posted['mc_gross'] );
249
+			$this->validate_ipn_amount($invoice, $posted['mc_gross']);
250 250
 
251 251
 			$note = '';
252 252
 
253
-			if ( ! empty( $posted['mc_fee'] ) ) {
254
-				$note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) );
253
+			if (!empty($posted['mc_fee'])) {
254
+				$note = sprintf(__('PayPal Transaction Fee %.', 'invoicing'), sanitize_text_field($posted['mc_fee']));
255 255
 			}
256 256
 
257
-			if ( ! empty( $posted['payer_status'] ) ) {
258
-				$note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) );
257
+			if (!empty($posted['payer_status'])) {
258
+				$note = ' ' . sprintf(__('Buyer status %.', 'invoicing'), sanitize_text_field($posted['payer_status']));
259 259
 			}
260 260
 
261
-			$invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) );
262
-			return wpinv_error_log( 'Invoice marked as paid.', false );
261
+			$invoice->mark_paid((!empty($posted['txn_id']) ? sanitize_text_field($posted['txn_id']) : ''), trim($note));
262
+			return wpinv_error_log('Invoice marked as paid.', false);
263 263
 
264 264
 		}
265 265
 
266 266
 		// Pending payments.
267
-		if ( $payment_status == 'pending' ) {
267
+		if ($payment_status == 'pending') {
268 268
 
269 269
 			/* translators: %s: pending reason. */
270
-			$invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) );
270
+			$invoice->update_status('wpi-onhold', sprintf(__('Payment pending (%s).', 'invoicing'), $posted['pending_reason']));
271 271
 
272
-			return wpinv_error_log( 'Invoice marked as "payment held".', false );
272
+			return wpinv_error_log('Invoice marked as "payment held".', false);
273 273
 		}
274 274
 
275 275
 		/* translators: %s: payment status. */
276
-		$invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) );
276
+		$invoice->update_status('wpi-failed', sprintf(__('Payment %s via IPN.', 'invoicing'), sanitize_text_field($posted['payment_status'])));
277 277
 
278 278
 	}
279 279
 
@@ -283,8 +283,8 @@  discard block
 block discarded – undo
283 283
 	 * @param WPInv_Invoice $invoice  Invoice object.
284 284
 	 * @param array    $posted Posted data.
285 285
 	 */
286
-	protected function ipn_txn_cart( $invoice, $posted ) {
287
-		$this->ipn_txn_web_accept( $invoice, $posted );
286
+	protected function ipn_txn_cart($invoice, $posted) {
287
+		$this->ipn_txn_web_accept($invoice, $posted);
288 288
 	}
289 289
 
290 290
 	/**
@@ -293,43 +293,43 @@  discard block
 block discarded – undo
293 293
 	 * @param WPInv_Invoice $invoice  Invoice object.
294 294
 	 * @param array    $posted Posted data.
295 295
 	 */
296
-	protected function ipn_txn_subscr_signup( $invoice, $posted ) {
296
+	protected function ipn_txn_subscr_signup($invoice, $posted) {
297 297
 
298
-		wpinv_error_log( 'Processing subscription signup', false );
298
+		wpinv_error_log('Processing subscription signup', false);
299 299
 
300 300
 		// Make sure the invoice has a subscription.
301
-		$subscription = getpaid_get_invoice_subscription( $invoice );
301
+		$subscription = getpaid_get_invoice_subscription($invoice);
302 302
 
303
-		if ( empty( $subscription ) ) {
304
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
303
+		if (empty($subscription)) {
304
+			return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
305 305
 		}
306 306
 
307
-		wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
307
+		wpinv_error_log('Found subscription #' . $subscription->get_id(), false);
308 308
 
309 309
 		// Validate the IPN.
310
-		$business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] );
311
-		$this->validate_ipn_receiver_email( $invoice, $business_email );
312
-		$this->validate_ipn_currency( $invoice, $posted['mc_currency'] );
310
+		$business_email = isset($posted['business']) && is_email($posted['business']) ? trim($posted['business']) : trim($posted['receiver_email']);
311
+		$this->validate_ipn_receiver_email($invoice, $business_email);
312
+		$this->validate_ipn_currency($invoice, $posted['mc_currency']);
313 313
 
314 314
 		// Activate the subscription.
315
-		$duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() );
316
-		$subscription->set_date_created( current_time( 'mysql' ) );
317
-		$subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) );
318
-		$subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) );
315
+		$duration = strtotime($subscription->get_expiration()) - strtotime($subscription->get_date_created());
316
+		$subscription->set_date_created(current_time('mysql'));
317
+		$subscription->set_expiration(date('Y-m-d H:i:s', (current_time('timestamp') + $duration)));
318
+		$subscription->set_profile_id(sanitize_text_field($posted['subscr_id']));
319 319
 		$subscription->activate();
320 320
 
321 321
 		// Set the transaction id.
322
-		if ( ! empty( $posted['txn_id'] ) ) {
323
-			$invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
324
-			$invoice->set_transaction_id( $posted['txn_id'] );
322
+		if (!empty($posted['txn_id'])) {
323
+			$invoice->add_note(sprintf(__('PayPal Transaction ID: %s', 'invoicing'), $posted['txn_id']), false, false, true);
324
+			$invoice->set_transaction_id($posted['txn_id']);
325 325
 		}
326 326
 
327 327
 		// Update the payment status.
328 328
 		$invoice->mark_paid();
329 329
 
330
-		$invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
330
+		$invoice->add_note(sprintf(__('PayPal Subscription ID: %s', 'invoicing'), $posted['subscr_id']), false, false, true);
331 331
 
332
-		wpinv_error_log( 'Subscription started.', false );
332
+		wpinv_error_log('Subscription started.', false);
333 333
 	}
334 334
 
335 335
 	/**
@@ -338,45 +338,45 @@  discard block
 block discarded – undo
338 338
 	 * @param WPInv_Invoice $invoice  Invoice object.
339 339
 	 * @param array    $posted Posted data.
340 340
 	 */
341
-	protected function ipn_txn_subscr_payment( $invoice, $posted ) {
341
+	protected function ipn_txn_subscr_payment($invoice, $posted) {
342 342
 
343 343
 		// Make sure the invoice has a subscription.
344
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
344
+		$subscription = getpaid_subscriptions()->get_invoice_subscription($invoice);
345 345
 
346
-		if ( empty( $subscription ) ) {
347
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
346
+		if (empty($subscription)) {
347
+			return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
348 348
 		}
349 349
 
350
-		wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false );
350
+		wpinv_error_log('Found subscription #' . $subscription->get_id(), false);
351 351
 
352 352
 		// PayPal sends a subscr_payment for the first payment too.
353
-		$date_completed = getpaid_format_date( $invoice->get_date_completed() );
354
-		$date_created   = getpaid_format_date( $invoice->get_date_created() );
355
-		$today_date     = getpaid_format_date( current_time( 'mysql' ) );
356
-		$payment_date   = getpaid_format_date( $posted['payment_date'] );
357
-		$subscribe_date = getpaid_format_date( $subscription->get_date_created() );
358
-		$dates          = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) );
353
+		$date_completed = getpaid_format_date($invoice->get_date_completed());
354
+		$date_created   = getpaid_format_date($invoice->get_date_created());
355
+		$today_date     = getpaid_format_date(current_time('mysql'));
356
+		$payment_date   = getpaid_format_date($posted['payment_date']);
357
+		$subscribe_date = getpaid_format_date($subscription->get_date_created());
358
+		$dates          = array_filter(compact('date_completed', 'date_created', 'subscribe_date'));
359 359
 
360
-		foreach( $dates as $date ) {
360
+		foreach ($dates as $date) {
361 361
 
362
-			if ( $date !== $today_date && $date !== $payment_date ) {
362
+			if ($date !== $today_date && $date !== $payment_date) {
363 363
 				continue;
364 364
 			}
365 365
 
366
-			if ( ! empty( $posted['txn_id'] ) ) {
367
-				$invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) );	
368
-				$invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true );
366
+			if (!empty($posted['txn_id'])) {
367
+				$invoice->set_transaction_id(sanitize_text_field($posted['txn_id']));	
368
+				$invoice->add_note(wp_sprintf(__('PayPal Transaction ID: %s', 'invoicing'), sanitize_text_field($posted['txn_id'])), false, false, true);
369 369
 			}
370 370
 
371 371
 			return $invoice->mark_paid();
372 372
 
373 373
 		}
374 374
 
375
-		wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false );
375
+		wpinv_error_log('Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false);
376 376
 
377 377
 		// Abort if the payment is already recorded.
378
-		if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) {
379
-			return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false );
378
+		if (wpinv_get_id_by_transaction_id($posted['txn_id'])) {
379
+			return wpinv_error_log('Aborting, Transaction ' . $posted['txn_id'] . ' has already been processed', false);
380 380
 		}
381 381
 
382 382
 		$args = array(
@@ -384,17 +384,17 @@  discard block
 block discarded – undo
384 384
 			'gateway'        => $this->id,
385 385
 		);
386 386
 
387
-		$invoice = wpinv_get_invoice( $subscription->add_payment( $args ) );
387
+		$invoice = wpinv_get_invoice($subscription->add_payment($args));
388 388
 
389
-		if ( empty( $invoice ) ) {
389
+		if (empty($invoice)) {
390 390
 			return;
391 391
 		}
392 392
 
393
-		$invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true );
394
-		$invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true );
393
+		$invoice->add_note(wp_sprintf(__('PayPal Transaction ID: %s', 'invoicing'), $posted['txn_id']), false, false, true);
394
+		$invoice->add_note(wp_sprintf(__('PayPal Subscription ID: %s', 'invoicing'), $posted['subscr_id']), false, false, true);
395 395
 
396 396
 		$subscription->renew();
397
-		wpinv_error_log( 'Subscription renewed.', false );
397
+		wpinv_error_log('Subscription renewed.', false);
398 398
 
399 399
 	}
400 400
 
@@ -403,18 +403,18 @@  discard block
 block discarded – undo
403 403
 	 *
404 404
 	 * @param WPInv_Invoice $invoice  Invoice object.
405 405
 	 */
406
-	protected function ipn_txn_subscr_cancel( $invoice ) {
406
+	protected function ipn_txn_subscr_cancel($invoice) {
407 407
 
408 408
 		// Make sure the invoice has a subscription.
409
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
409
+		$subscription = getpaid_subscriptions()->get_invoice_subscription($invoice);
410 410
 
411
-		if ( empty( $subscription ) ) {
412
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
411
+		if (empty($subscription)) {
412
+			return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
413 413
 		}
414 414
 
415
-		wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false );
415
+		wpinv_error_log('Processing subscription cancellation for the invoice ' . $invoice->get_id(), false);
416 416
 		$subscription->cancel();
417
-		wpinv_error_log( 'Subscription cancelled.', false );
417
+		wpinv_error_log('Subscription cancelled.', false);
418 418
 
419 419
 	}
420 420
 
@@ -424,18 +424,18 @@  discard block
 block discarded – undo
424 424
 	 * @param WPInv_Invoice $invoice  Invoice object.
425 425
 	 * @param array    $posted Posted data.
426 426
 	 */
427
-	protected function ipn_txn_subscr_eot( $invoice ) {
427
+	protected function ipn_txn_subscr_eot($invoice) {
428 428
 
429 429
 		// Make sure the invoice has a subscription.
430
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
430
+		$subscription = getpaid_subscriptions()->get_invoice_subscription($invoice);
431 431
 
432
-		if ( empty( $subscription ) ) {
433
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
432
+		if (empty($subscription)) {
433
+			return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
434 434
 		}
435 435
 
436
-		wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false );
436
+		wpinv_error_log('Processing subscription end of life for the invoice ' . $invoice->get_id(), false);
437 437
 		$subscription->complete();
438
-		wpinv_error_log( 'Subscription completed.', false );
438
+		wpinv_error_log('Subscription completed.', false);
439 439
 
440 440
 	}
441 441
 
@@ -445,18 +445,18 @@  discard block
 block discarded – undo
445 445
 	 * @param WPInv_Invoice $invoice  Invoice object.
446 446
 	 * @param array    $posted Posted data.
447 447
 	 */
448
-	protected function ipn_txn_subscr_failed( $invoice ) {
448
+	protected function ipn_txn_subscr_failed($invoice) {
449 449
 
450 450
 		// Make sure the invoice has a subscription.
451
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
451
+		$subscription = getpaid_subscriptions()->get_invoice_subscription($invoice);
452 452
 
453
-		if ( empty( $subscription ) ) {
454
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
453
+		if (empty($subscription)) {
454
+			return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
455 455
 		}
456 456
 
457
-		wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false );
457
+		wpinv_error_log('Processing subscription payment failure for the invoice ' . $invoice->get_id(), false);
458 458
 		$subscription->failing();
459
-		wpinv_error_log( 'Subscription marked as failing.', false );
459
+		wpinv_error_log('Subscription marked as failing.', false);
460 460
 
461 461
 	}
462 462
 
@@ -466,18 +466,18 @@  discard block
 block discarded – undo
466 466
 	 * @param WPInv_Invoice $invoice  Invoice object.
467 467
 	 * @param array    $posted Posted data.
468 468
 	 */
469
-	protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) {
469
+	protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment($invoice) {
470 470
 
471 471
 		// Make sure the invoice has a subscription.
472
-		$subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice );
472
+		$subscription = getpaid_subscriptions()->get_invoice_subscription($invoice);
473 473
 
474
-		if ( empty( $subscription ) ) {
475
-			return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false );
474
+		if (empty($subscription)) {
475
+			return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false);
476 476
 		}
477 477
 
478
-		wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false );
478
+		wpinv_error_log('Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false);
479 479
 		$subscription->cancel();
480
-		wpinv_error_log( 'Subscription cancelled.', false );
480
+		wpinv_error_log('Subscription cancelled.', false);
481 481
 	}
482 482
 
483 483
 }
Please login to merge, or discard this patch.