Passed
Pull Request — master (#781)
by Kiran
04:57
created
includes/gateways/class-getpaid-paypal-gateway.php 1 patch
Indentation   +404 added lines, -404 removed lines patch added patch discarded remove patch
@@ -13,97 +13,97 @@  discard block
 block discarded – undo
13 13
 class GetPaid_Paypal_Gateway extends GetPaid_Payment_Gateway {
14 14
 
15 15
     /**
16
-	 * Payment method id.
17
-	 *
18
-	 * @var string
19
-	 */
16
+     * Payment method id.
17
+     *
18
+     * @var string
19
+     */
20 20
     public $id = 'paypal';
21 21
 
22 22
     /**
23
-	 * An array of features that this gateway supports.
24
-	 *
25
-	 * @var array
26
-	 */
23
+     * An array of features that this gateway supports.
24
+     *
25
+     * @var array
26
+     */
27 27
     protected $supports = array( 'subscription', 'sandbox', 'single_subscription_group' );
28 28
 
29 29
     /**
30
-	 * Payment method order.
31
-	 *
32
-	 * @var int
33
-	 */
30
+     * Payment method order.
31
+     *
32
+     * @var int
33
+     */
34 34
     public $order = 1;
35 35
 
36 36
     /**
37
-	 * Stores line items to send to PayPal.
38
-	 *
39
-	 * @var array
40
-	 */
37
+     * Stores line items to send to PayPal.
38
+     *
39
+     * @var array
40
+     */
41 41
     protected $line_items = array();
42 42
 
43 43
     /**
44
-	 * Endpoint for requests from PayPal.
45
-	 *
46
-	 * @var string
47
-	 */
48
-	protected $notify_url;
49
-
50
-	/**
51
-	 * Endpoint for requests to PayPal.
52
-	 *
53
-	 * @var string
54
-	 */
44
+     * Endpoint for requests from PayPal.
45
+     *
46
+     * @var string
47
+     */
48
+    protected $notify_url;
49
+
50
+    /**
51
+     * Endpoint for requests to PayPal.
52
+     *
53
+     * @var string
54
+     */
55 55
     protected $endpoint;
56 56
 
57 57
     /**
58
-	 * Currencies this gateway is allowed for.
59
-	 *
60
-	 * @var array
61
-	 */
62
-	public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' );
58
+     * Currencies this gateway is allowed for.
59
+     *
60
+     * @var array
61
+     */
62
+    public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' );
63 63
 
64 64
     /**
65
-	 * URL to view a transaction.
66
-	 *
67
-	 * @var string
68
-	 */
65
+     * URL to view a transaction.
66
+     *
67
+     * @var string
68
+     */
69 69
     public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s';
70 70
 
71 71
     /**
72
-	 * URL to view a subscription.
73
-	 *
74
-	 * @var string
75
-	 */
76
-	public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s';
72
+     * URL to view a subscription.
73
+     *
74
+     * @var string
75
+     */
76
+    public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s';
77 77
 
78 78
     /**
79
-	 * Class constructor.
80
-	 */
81
-	public function __construct() {
79
+     * Class constructor.
80
+     */
81
+    public function __construct() {
82 82
 
83 83
         $this->title                = __( 'PayPal Standard', 'invoicing' );
84 84
         $this->method_title         = __( 'PayPal Standard', 'invoicing' );
85 85
         $this->checkout_button_text = __( 'Proceed to PayPal', 'invoicing' );
86 86
         $this->notify_url           = wpinv_get_ipn_url( $this->id );
87 87
 
88
-		add_filter( 'wpinv_subscription_cancel_url', array( $this, 'filter_cancel_subscription_url' ), 10, 2 );
89
-		add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 );
88
+        add_filter( 'wpinv_subscription_cancel_url', array( $this, 'filter_cancel_subscription_url' ), 10, 2 );
89
+        add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 );
90 90
         add_filter( 'getpaid_paypal_sandbox_notice', array( $this, 'sandbox_notice' ) );
91
-		add_filter( 'getpaid_get_paypal_connect_url', array( $this, 'maybe_get_connect_url' ), 10, 2 );
92
-		add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) );
93
-		add_action( 'wpinv_paypal_connect', array( $this, 'display_connect_buttons' ) );
94
-		parent::__construct();
91
+        add_filter( 'getpaid_get_paypal_connect_url', array( $this, 'maybe_get_connect_url' ), 10, 2 );
92
+        add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) );
93
+        add_action( 'wpinv_paypal_connect', array( $this, 'display_connect_buttons' ) );
94
+        parent::__construct();
95 95
     }
96 96
 
97 97
     /**
98
-	 * Process Payment.
99
-	 *
100
-	 *
101
-	 * @param WPInv_Invoice $invoice Invoice.
102
-	 * @param array $submission_data Posted checkout fields.
103
-	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
104
-	 * @return array
105
-	 */
106
-	public function process_payment( $invoice, $submission_data, $submission ) {
98
+     * Process Payment.
99
+     *
100
+     *
101
+     * @param WPInv_Invoice $invoice Invoice.
102
+     * @param array $submission_data Posted checkout fields.
103
+     * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
104
+     * @return array
105
+     */
106
+    public function process_payment( $invoice, $submission_data, $submission ) {
107 107
 
108 108
         // Get redirect url.
109 109
         $paypal_redirect = $this->get_request_url( $invoice );
@@ -126,15 +126,15 @@  discard block
 block discarded – undo
126 126
     }
127 127
 
128 128
     /**
129
-	 * Get the PayPal request URL for an invoice.
130
-	 *
131
-	 * @param  WPInv_Invoice $invoice Invoice object.
132
-	 * @return string
133
-	 */
134
-	public function get_request_url( $invoice ) {
129
+     * Get the PayPal request URL for an invoice.
130
+     *
131
+     * @param  WPInv_Invoice $invoice Invoice object.
132
+     * @return string
133
+     */
134
+    public function get_request_url( $invoice ) {
135 135
 
136 136
         // Endpoint for this request
137
-		$this->endpoint    = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?';
137
+        $this->endpoint    = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?';
138 138
 
139 139
         // Retrieve paypal args.
140 140
         $paypal_args       = map_deep( $this->get_paypal_args( $invoice ), 'urlencode' );
@@ -147,45 +147,45 @@  discard block
 block discarded – undo
147 147
 
148 148
         return add_query_arg( $paypal_args, $this->endpoint );
149 149
 
150
-	}
150
+    }
151 151
 
152 152
     /**
153
-	 * Get PayPal Args for passing to PP.
154
-	 *
155
-	 * @param  WPInv_Invoice $invoice Invoice object.
156
-	 * @return array
157
-	 */
158
-	protected function get_paypal_args( $invoice ) {
153
+     * Get PayPal Args for passing to PP.
154
+     *
155
+     * @param  WPInv_Invoice $invoice Invoice object.
156
+     * @return array
157
+     */
158
+    protected function get_paypal_args( $invoice ) {
159 159
 
160 160
         // Whether or not to send the line items as one item.
161
-		$force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice );
162
-
163
-		if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) {
164
-			$force_one_line_item = true;
165
-		}
166
-
167
-		$paypal_args = apply_filters(
168
-			'getpaid_paypal_args',
169
-			array_merge(
170
-				$this->get_transaction_args( $invoice ),
171
-				$this->get_line_item_args( $invoice, $force_one_line_item )
172
-			),
173
-			$invoice
174
-		);
175
-
176
-		return $this->fix_request_length( $invoice, $paypal_args );
161
+        $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice );
162
+
163
+        if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) {
164
+            $force_one_line_item = true;
165
+        }
166
+
167
+        $paypal_args = apply_filters(
168
+            'getpaid_paypal_args',
169
+            array_merge(
170
+                $this->get_transaction_args( $invoice ),
171
+                $this->get_line_item_args( $invoice, $force_one_line_item )
172
+            ),
173
+            $invoice
174
+        );
175
+
176
+        return $this->fix_request_length( $invoice, $paypal_args );
177 177
     }
178 178
 
179 179
     /**
180
-	 * Get transaction args for paypal request.
181
-	 *
182
-	 * @param WPInv_Invoice $invoice Invoice object.
183
-	 * @return array
184
-	 */
185
-	protected function get_transaction_args( $invoice ) {
186
-
187
-		$email = $this->is_sandbox( $invoice ) ? wpinv_get_option( 'paypal_sandbox_email', wpinv_get_option( 'paypal_email', '' ) ) : wpinv_get_option( 'paypal_email', '' );
188
-		return array(
180
+     * Get transaction args for paypal request.
181
+     *
182
+     * @param WPInv_Invoice $invoice Invoice object.
183
+     * @return array
184
+     */
185
+    protected function get_transaction_args( $invoice ) {
186
+
187
+        $email = $this->is_sandbox( $invoice ) ? wpinv_get_option( 'paypal_sandbox_email', wpinv_get_option( 'paypal_email', '' ) ) : wpinv_get_option( 'paypal_email', '' );
188
+        return array(
189 189
             'cmd'           => '_cart',
190 190
             'business'      => $email,
191 191
             'no_shipping'   => '1',
@@ -210,16 +210,16 @@  discard block
 block discarded – undo
210 210
     }
211 211
 
212 212
     /**
213
-	 * Get line item args for paypal request.
214
-	 *
215
-	 * @param  WPInv_Invoice $invoice Invoice object.
216
-	 * @param  bool     $force_one_line_item Create only one item for this invoice.
217
-	 * @return array
218
-	 */
219
-	protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
213
+     * Get line item args for paypal request.
214
+     *
215
+     * @param  WPInv_Invoice $invoice Invoice object.
216
+     * @param  bool     $force_one_line_item Create only one item for this invoice.
217
+     * @return array
218
+     */
219
+    protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
220 220
 
221 221
         // Maybe send invoice as a single item.
222
-		if ( $force_one_line_item ) {
222
+        if ( $force_one_line_item ) {
223 223
             return $this->get_line_item_args_single_item( $invoice );
224 224
         }
225 225
 
@@ -239,134 +239,134 @@  discard block
 block discarded – undo
239 239
             $line_item_args['discount_amount_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_discount(), 2 );
240 240
         }
241 241
 
242
-		return array_merge( $line_item_args, $this->get_line_items() );
242
+        return array_merge( $line_item_args, $this->get_line_items() );
243 243
 
244 244
     }
245 245
 
246 246
     /**
247
-	 * Get line item args for paypal request as a single line item.
248
-	 *
249
-	 * @param  WPInv_Invoice $invoice Invoice object.
250
-	 * @return array
251
-	 */
252
-	protected function get_line_item_args_single_item( $invoice ) {
253
-		$this->delete_line_items();
247
+     * Get line item args for paypal request as a single line item.
248
+     *
249
+     * @param  WPInv_Invoice $invoice Invoice object.
250
+     * @return array
251
+     */
252
+    protected function get_line_item_args_single_item( $invoice ) {
253
+        $this->delete_line_items();
254 254
 
255 255
         $item_name = wp_sprintf( __( 'Invoice %s', 'invoicing' ), $invoice->get_number() );
256
-		$this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
256
+        $this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
257 257
 
258
-		return $this->get_line_items();
258
+        return $this->get_line_items();
259 259
     }
260 260
 
261 261
     /**
262
-	 * Return all line items.
263
-	 */
264
-	protected function get_line_items() {
265
-		return $this->line_items;
266
-	}
262
+     * Return all line items.
263
+     */
264
+    protected function get_line_items() {
265
+        return $this->line_items;
266
+    }
267 267
 
268 268
     /**
269
-	 * Remove all line items.
270
-	 */
271
-	protected function delete_line_items() {
272
-		$this->line_items = array();
269
+     * Remove all line items.
270
+     */
271
+    protected function delete_line_items() {
272
+        $this->line_items = array();
273 273
     }
274 274
 
275 275
     /**
276
-	 * Prepare line items to send to paypal.
277
-	 *
278
-	 * @param  WPInv_Invoice $invoice Invoice object.
279
-	 */
280
-	protected function prepare_line_items( $invoice ) {
281
-		$this->delete_line_items();
282
-
283
-		// Items.
284
-		foreach ( $invoice->get_items() as $item ) {
285
-			$amount   = $item->get_price();
286
-			$quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
287
-			$this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
276
+     * Prepare line items to send to paypal.
277
+     *
278
+     * @param  WPInv_Invoice $invoice Invoice object.
279
+     */
280
+    protected function prepare_line_items( $invoice ) {
281
+        $this->delete_line_items();
282
+
283
+        // Items.
284
+        foreach ( $invoice->get_items() as $item ) {
285
+            $amount   = $item->get_price();
286
+            $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
287
+            $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
288 288
         }
289 289
 
290 290
         // Fees.
291
-		foreach ( $invoice->get_fees() as $fee => $data ) {
291
+        foreach ( $invoice->get_fees() as $fee => $data ) {
292 292
             $this->add_line_item( $fee, 1, wpinv_sanitize_amount( $data['initial_fee'] ) );
293 293
         }
294 294
 
295 295
     }
296 296
 
297 297
     /**
298
-	 * Add PayPal Line Item.
299
-	 *
300
-	 * @param  string $item_name Item name.
301
-	 * @param  float    $quantity Item quantity.
302
-	 * @param  float  $amount Amount.
303
-	 * @param  string $item_number Item number.
304
-	 */
305
-	protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
306
-		$index = ( count( $this->line_items ) / 4 ) + 1;
307
-
308
-		/**
309
-		 * Prevent error "Things don't appear to be working at the moment. (https://www.sandbox.paypal.com/webapps/hermes/error)"
310
-		 */
311
-		$item_name = str_replace( "#", "", $item_name );
312
-
313
-		$item = apply_filters(
314
-			'getpaid_paypal_line_item',
315
-			array(
316
-				'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
317
-				'quantity'    => (float) $quantity,
318
-				'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
319
-				'item_number' => $item_number,
320
-			),
321
-			$item_name,
322
-			$quantity,
323
-			$amount,
324
-			$item_number
325
-		);
326
-
327
-		$this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
298
+     * Add PayPal Line Item.
299
+     *
300
+     * @param  string $item_name Item name.
301
+     * @param  float    $quantity Item quantity.
302
+     * @param  float  $amount Amount.
303
+     * @param  string $item_number Item number.
304
+     */
305
+    protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
306
+        $index = ( count( $this->line_items ) / 4 ) + 1;
307
+
308
+        /**
309
+         * Prevent error "Things don't appear to be working at the moment. (https://www.sandbox.paypal.com/webapps/hermes/error)"
310
+         */
311
+        $item_name = str_replace( "#", "", $item_name );
312
+
313
+        $item = apply_filters(
314
+            'getpaid_paypal_line_item',
315
+            array(
316
+                'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
317
+                'quantity'    => (float) $quantity,
318
+                'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
319
+                'item_number' => $item_number,
320
+            ),
321
+            $item_name,
322
+            $quantity,
323
+            $amount,
324
+            $item_number
325
+        );
326
+
327
+        $this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
328 328
         $this->line_items[ 'quantity_' . $index ]    = $item['quantity'];
329 329
 
330 330
         // The price or amount of the product, service, or contribution, not including shipping, handling, or tax.
331
-		$this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
332
-		$this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
331
+        $this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
332
+        $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
333 333
     }
334 334
 
335 335
     /**
336
-	 * If the default request with line items is too long, generate a new one with only one line item.
337
-	 *
338
-	 * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
339
-	 *
340
-	 * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
341
-	 * @param array    $paypal_args Arguments sent to Paypal in the request.
342
-	 * @return array
343
-	 */
344
-	protected function fix_request_length( $invoice, $paypal_args ) {
345
-		$max_paypal_length = 2083;
346
-		$query_candidate   = http_build_query( $paypal_args, '', '&' );
347
-
348
-		if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
349
-			return $paypal_args;
350
-		}
351
-
352
-		return apply_filters(
353
-			'getpaid_paypal_args',
354
-			array_merge(
355
-				$this->get_transaction_args( $invoice ),
356
-				$this->get_line_item_args( $invoice, true )
357
-			),
358
-			$invoice
359
-		);
336
+     * If the default request with line items is too long, generate a new one with only one line item.
337
+     *
338
+     * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
339
+     *
340
+     * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
341
+     * @param array    $paypal_args Arguments sent to Paypal in the request.
342
+     * @return array
343
+     */
344
+    protected function fix_request_length( $invoice, $paypal_args ) {
345
+        $max_paypal_length = 2083;
346
+        $query_candidate   = http_build_query( $paypal_args, '', '&' );
347
+
348
+        if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
349
+            return $paypal_args;
350
+        }
351
+
352
+        return apply_filters(
353
+            'getpaid_paypal_args',
354
+            array_merge(
355
+                $this->get_transaction_args( $invoice ),
356
+                $this->get_line_item_args( $invoice, true )
357
+            ),
358
+            $invoice
359
+        );
360 360
 
361 361
     }
362 362
 
363 363
     /**
364
-	 * Processes recurring invoices.
365
-	 *
366
-	 * @param  array $paypal_args PayPal args.
367
-	 * @param  WPInv_Invoice    $invoice Invoice object.
368
-	 */
369
-	public function process_subscription( $paypal_args, $invoice ) {
364
+     * Processes recurring invoices.
365
+     *
366
+     * @param  array $paypal_args PayPal args.
367
+     * @param  WPInv_Invoice    $invoice Invoice object.
368
+     */
369
+    public function process_subscription( $paypal_args, $invoice ) {
370 370
 
371 371
         // Make sure this is a subscription.
372 372
         if ( ! $invoice->is_recurring() || ! $subscription = getpaid_get_invoice_subscription( $invoice ) ) {
@@ -387,21 +387,21 @@  discard block
 block discarded – undo
387 387
         $recurring_amount       = (float) wpinv_sanitize_amount( $invoice->get_recurring_total(), 2 );
388 388
         $subscription_item      = $invoice->get_recurring( true );
389 389
 
390
-		// Convert 365 days to 1 year.
391
-		if ( 'D' == $period && 365 == $interval ) {
392
-			$period = 'Y';
393
-			$interval = 1;
394
-		}
390
+        // Convert 365 days to 1 year.
391
+        if ( 'D' == $period && 365 == $interval ) {
392
+            $period = 'Y';
393
+            $interval = 1;
394
+        }
395 395
 
396 396
         if ( $subscription_item->has_free_trial() ) {
397 397
 
398 398
             $paypal_args['a1'] = 0 == $initial_amount ? 0 : $initial_amount;
399 399
 
400
-			// Trial period length.
401
-			$paypal_args['p1'] = $subscription_item->get_trial_interval();
400
+            // Trial period length.
401
+            $paypal_args['p1'] = $subscription_item->get_trial_interval();
402 402
 
403
-			// Trial period.
404
-			$paypal_args['t1'] = $subscription_item->get_trial_period();
403
+            // Trial period.
404
+            $paypal_args['t1'] = $subscription_item->get_trial_period();
405 405
 
406 406
         } elseif ( $initial_amount != $recurring_amount ) {
407 407
 
@@ -424,40 +424,40 @@  discard block
 block discarded – undo
424 424
         }
425 425
 
426 426
         // We have a recurring payment
427
-		if ( ! isset( $param_number ) || 1 == $param_number ) {
427
+        if ( ! isset( $param_number ) || 1 == $param_number ) {
428 428
 
429
-			// Subscription price
430
-			$paypal_args['a3'] = $recurring_amount;
429
+            // Subscription price
430
+            $paypal_args['a3'] = $recurring_amount;
431 431
 
432
-			// Subscription duration
433
-			$paypal_args['p3'] = $interval;
432
+            // Subscription duration
433
+            $paypal_args['p3'] = $interval;
434 434
 
435
-			// Subscription period
436
-			$paypal_args['t3'] = $period;
435
+            // Subscription period
436
+            $paypal_args['t3'] = $period;
437 437
 
438 438
         }
439 439
 
440 440
         // Recurring payments
441
-		if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
441
+        if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
442 442
 
443
-			// Non-recurring payments
444
-			$paypal_args['src'] = 0;
443
+            // Non-recurring payments
444
+            $paypal_args['src'] = 0;
445 445
 
446
-		} else {
446
+        } else {
447 447
 
448
-			$paypal_args['src'] = 1;
448
+            $paypal_args['src'] = 1;
449 449
 
450
-			if ( $bill_times > 0 ) {
450
+            if ( $bill_times > 0 ) {
451 451
 
452
-				// An initial period is being used to charge a sign-up fee
453
-				if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
454
-					$bill_times--;
455
-				}
452
+                // An initial period is being used to charge a sign-up fee
453
+                if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
454
+                    $bill_times--;
455
+                }
456 456
 
457 457
                 // Make sure it's not over the max of 52
458 458
                 $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 );
459 459
 
460
-			}
460
+            }
461 461
         }
462 462
 
463 463
         // Force return URL so that order description & instructions display
@@ -472,19 +472,19 @@  discard block
 block discarded – undo
472 472
 }
473 473
 
474 474
         return apply_filters(
475
-			'getpaid_paypal_subscription_args',
476
-			$paypal_args,
477
-			$invoice
475
+            'getpaid_paypal_subscription_args',
476
+            $paypal_args,
477
+            $invoice
478 478
         );
479 479
 
480 480
     }
481 481
 
482 482
     /**
483
-	 * Processes ipns and marks payments as complete.
484
-	 *
485
-	 * @return void
486
-	 */
487
-	public function verify_ipn() {
483
+     * Processes ipns and marks payments as complete.
484
+     *
485
+     * @return void
486
+     */
487
+    public function verify_ipn() {
488 488
         new GetPaid_Paypal_Gateway_IPN_Handler( $this );
489 489
     }
490 490
 
@@ -494,19 +494,19 @@  discard block
 block discarded – undo
494 494
     public function sandbox_notice() {
495 495
 
496 496
         return sprintf(
497
-			__( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %1$sPayPal Sandbox Testing Guide%2$s for more details.', 'invoicing' ),
498
-			'<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
499
-			'</a>'
500
-		);
497
+            __( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %1$sPayPal Sandbox Testing Guide%2$s for more details.', 'invoicing' ),
498
+            '<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
499
+            '</a>'
500
+        );
501 501
 
502 502
     }
503 503
 
504
-	/**
505
-	 * Filters the gateway settings.
506
-	 *
507
-	 * @param array $admin_settings
508
-	 */
509
-	public function admin_settings( $admin_settings ) {
504
+    /**
505
+     * Filters the gateway settings.
506
+     *
507
+     * @param array $admin_settings
508
+     */
509
+    public function admin_settings( $admin_settings ) {
510 510
 
511 511
         $currencies = sprintf(
512 512
             __( 'Supported Currencies: %s', 'invoicing' ),
@@ -516,66 +516,66 @@  discard block
 block discarded – undo
516 516
         $admin_settings['paypal_active']['desc'] .= " ($currencies)";
517 517
         $admin_settings['paypal_desc']['std']     = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
518 518
 
519
-		// Access tokens.
520
-		$live_email      = wpinv_get_option( 'paypal_email' );
521
-		$sandbox_email   = wpinv_get_option( 'paypal_sandbox_email' );
519
+        // Access tokens.
520
+        $live_email      = wpinv_get_option( 'paypal_email' );
521
+        $sandbox_email   = wpinv_get_option( 'paypal_sandbox_email' );
522 522
 
523
-		$admin_settings['paypal_connect'] = array(
524
-			'type' => 'hook',
525
-			'id'   => 'paypal_connect',
526
-			'name' => __( 'Connect to PayPal', 'invoicing' ),
527
-		);
523
+        $admin_settings['paypal_connect'] = array(
524
+            'type' => 'hook',
525
+            'id'   => 'paypal_connect',
526
+            'name' => __( 'Connect to PayPal', 'invoicing' ),
527
+        );
528 528
 
529 529
         $admin_settings['paypal_email'] = array(
530 530
             'type'  => 'text',
531
-			'class' => 'live-auth-data',
531
+            'class' => 'live-auth-data',
532 532
             'id'    => 'paypal_email',
533 533
             'name'  => __( 'Live Email Address', 'invoicing' ),
534 534
             'desc'  => __( 'The email address of your PayPal account.', 'invoicing' ),
535 535
         );
536 536
 
537
-		$admin_settings['paypal_sandbox_email'] = array(
537
+        $admin_settings['paypal_sandbox_email'] = array(
538 538
             'type'  => 'text',
539
-			'class' => 'sandbox-auth-data',
539
+            'class' => 'sandbox-auth-data',
540 540
             'id'    => 'paypal_sandbox_email',
541 541
             'name'  => __( 'Sandbox Email Address', 'invoicing' ),
542 542
             'desc'  => __( 'The email address of your sandbox PayPal account.', 'invoicing' ),
543
-			'std'   => wpinv_get_option( 'paypal_email', '' ),
543
+            'std'   => wpinv_get_option( 'paypal_email', '' ),
544
+        );
545
+
546
+        // Client ID and secret.
547
+        $admin_settings['paypal_client_id'] = array(
548
+            'type'  => 'text',
549
+            'class' => 'live-auth-data',
550
+            'id'    => 'paypal_client_id',
551
+            'name'  => __( 'Live Client ID', 'invoicing' ),
552
+            'desc'  => __( 'The client ID of your PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
553
+        );
554
+
555
+        $admin_settings['paypal_sandbox_client_id'] = array(
556
+            'type'  => 'text',
557
+            'class' => 'sandbox-auth-data',
558
+            'id'    => 'paypal_sandbox_client_id',
559
+            'name'  => __( 'Sandbox Client ID', 'invoicing' ),
560
+            'desc'  => __( 'The client ID of your sandbox PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
561
+            'std'   => wpinv_get_option( 'paypal_client_id', '' ),
544 562
         );
545 563
 
546
-		// Client ID and secret.
547
-		$admin_settings['paypal_client_id'] = array(
548
-			'type'  => 'text',
549
-			'class' => 'live-auth-data',
550
-			'id'    => 'paypal_client_id',
551
-			'name'  => __( 'Live Client ID', 'invoicing' ),
552
-			'desc'  => __( 'The client ID of your PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
553
-		);
554
-
555
-		$admin_settings['paypal_sandbox_client_id'] = array(
556
-			'type'  => 'text',
557
-			'class' => 'sandbox-auth-data',
558
-			'id'    => 'paypal_sandbox_client_id',
559
-			'name'  => __( 'Sandbox Client ID', 'invoicing' ),
560
-			'desc'  => __( 'The client ID of your sandbox PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
561
-			'std'   => wpinv_get_option( 'paypal_client_id', '' ),
562
-		);
563
-
564
-		$admin_settings['paypal_secret'] = array(
565
-			'type'  => 'text',
566
-			'class' => 'live-auth-data',
567
-			'id'    => 'paypal_secret',
568
-			'name'  => __( 'Live Secret', 'invoicing' ),
569
-			'desc'  => __( 'The secret of your PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
570
-		);
571
-
572
-		$admin_settings['paypal_sandbox_secret'] = array(
573
-			'type'  => 'text',
574
-			'class' => 'sandbox-auth-data',
575
-			'id'    => 'paypal_sandbox_secret',
576
-			'name'  => __( 'Sandbox Secret', 'invoicing' ),
577
-			'desc'  => __( 'The secret of your sandbox PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
578
-		);
564
+        $admin_settings['paypal_secret'] = array(
565
+            'type'  => 'text',
566
+            'class' => 'live-auth-data',
567
+            'id'    => 'paypal_secret',
568
+            'name'  => __( 'Live Secret', 'invoicing' ),
569
+            'desc'  => __( 'The secret of your PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
570
+        );
571
+
572
+        $admin_settings['paypal_sandbox_secret'] = array(
573
+            'type'  => 'text',
574
+            'class' => 'sandbox-auth-data',
575
+            'id'    => 'paypal_sandbox_secret',
576
+            'name'  => __( 'Sandbox Secret', 'invoicing' ),
577
+            'desc'  => __( 'The secret of your sandbox PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
578
+        );
579 579
 
580 580
         $admin_settings['paypal_ipn_url'] = array(
581 581
             'type'     => 'ipn_url',
@@ -586,57 +586,57 @@  discard block
 block discarded – undo
586 586
             'readonly' => true,
587 587
         );
588 588
 
589
-		return $admin_settings;
590
-	}
591
-
592
-	/**
593
-	 * Retrieves the URL to cancel a subscription.
594
-	 *
595
-	 * @param string $url
596
-	 * @param WPInv_Subscription $subscription
597
-	 */
598
-	public function filter_cancel_subscription_url( $url, $subscription ) {
599
-
600
-		if ( $this->id !== $subscription->get_gateway() ) {
601
-			return $url;
602
-		}
603
-
604
-		// Get the PayPal profile ID.
605
-		$profile_id = $subscription->get_profile_id();
606
-
607
-		// Bail if no profile ID.
608
-		if ( empty( $profile_id ) ) {
609
-			return $url;
610
-		}
611
-
612
-		$cancel_url = 'https://www.paypal.com/myaccount/autopay/connect/%s/cancel';
613
-		if ( $this->is_sandbox( $subscription->get_parent_payment() ) ) {
614
-			$cancel_url = 'https://www.sandbox.paypal.com/myaccount/autopay/connect/%s/cancel';
615
-		}
616
-
617
-		return sprintf( $cancel_url, $profile_id );
618
-	}
619
-
620
-	/**
621
-	 * Retrieves the PayPal connect URL when using the setup wizzard.
622
-	 *
623
-	 *
589
+        return $admin_settings;
590
+    }
591
+
592
+    /**
593
+     * Retrieves the URL to cancel a subscription.
594
+     *
595
+     * @param string $url
596
+     * @param WPInv_Subscription $subscription
597
+     */
598
+    public function filter_cancel_subscription_url( $url, $subscription ) {
599
+
600
+        if ( $this->id !== $subscription->get_gateway() ) {
601
+            return $url;
602
+        }
603
+
604
+        // Get the PayPal profile ID.
605
+        $profile_id = $subscription->get_profile_id();
606
+
607
+        // Bail if no profile ID.
608
+        if ( empty( $profile_id ) ) {
609
+            return $url;
610
+        }
611
+
612
+        $cancel_url = 'https://www.paypal.com/myaccount/autopay/connect/%s/cancel';
613
+        if ( $this->is_sandbox( $subscription->get_parent_payment() ) ) {
614
+            $cancel_url = 'https://www.sandbox.paypal.com/myaccount/autopay/connect/%s/cancel';
615
+        }
616
+
617
+        return sprintf( $cancel_url, $profile_id );
618
+    }
619
+
620
+    /**
621
+     * Retrieves the PayPal connect URL when using the setup wizzard.
622
+     *
623
+     *
624 624
      * @param array $data
625 625
      * @return string
626
-	 */
627
-	public static function maybe_get_connect_url( $url = '', $data = array() ) {
628
-		return self::get_connect_url( false, urldecode( $data['redirect'] ) );
629
-	}
630
-
631
-	/**
632
-	 * Retrieves the PayPal connect URL.
633
-	 *
634
-	 *
626
+     */
627
+    public static function maybe_get_connect_url( $url = '', $data = array() ) {
628
+        return self::get_connect_url( false, urldecode( $data['redirect'] ) );
629
+    }
630
+
631
+    /**
632
+     * Retrieves the PayPal connect URL.
633
+     *
634
+     *
635 635
      * @param bool $is_sandbox
636
-	 * @param string $redirect
636
+     * @param string $redirect
637 637
      * @return string
638
-	 */
639
-	public static function get_connect_url( $is_sandbox, $redirect = '' ) {
638
+     */
639
+    public static function get_connect_url( $is_sandbox, $redirect = '' ) {
640 640
 
641 641
         $redirect_url = add_query_arg(
642 642
             array(
@@ -646,7 +646,7 @@  discard block
 block discarded – undo
646 646
                 'tab'                  => 'gateways',
647 647
                 'section'              => 'paypal',
648 648
                 'getpaid-nonce'        => wp_create_nonce( 'getpaid-nonce' ),
649
-				'redirect'             => urlencode( $redirect ),
649
+                'redirect'             => urlencode( $redirect ),
650 650
             ),
651 651
             admin_url( 'admin.php' )
652 652
         );
@@ -661,12 +661,12 @@  discard block
 block discarded – undo
661 661
 
662 662
     }
663 663
 
664
-	/**
665
-	 * Generates settings page js.
666
-	 *
664
+    /**
665
+     * Generates settings page js.
666
+     *
667 667
      * @return void
668
-	 */
669
-	public static function display_connect_buttons() {
668
+     */
669
+    public static function display_connect_buttons() {
670 670
 
671 671
         ?>
672 672
 			<div class="wpinv-paypal-connect-live">
@@ -708,70 +708,70 @@  discard block
 block discarded – undo
708 708
         <?php
709 709
     }
710 710
 
711
-	/**
712
-	 * Connects to PayPal.
713
-	 *
714
-	 * @param array $data Connection data.
715
-	 * @return void
716
-	 */
717
-	public function connect_paypal( $data ) {
718
-
719
-		$sandbox      = $this->is_sandbox();
720
-		$data         = wp_unslash( $data );
721
-		$access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
722
-
723
-		if ( isset( $data['live_mode'] ) ) {
724
-			$sandbox = empty( $data['live_mode'] );
725
-		}
726
-
727
-		wpinv_update_option( 'paypal_sandbox', (int) $sandbox );
728
-		wpinv_update_option( 'paypal_active', 1 );
729
-
730
-		if ( ! empty( $data['error_description'] ) ) {
731
-			getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
732
-		} else {
733
-
734
-			// Retrieve the user info.
735
-			$user_info = wp_remote_get(
736
-				! $sandbox ? 'https://api-m.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1' : 'https://api-m.sandbox.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1',
737
-				array(
738
-
739
-					'headers' => array(
740
-						'Authorization' => 'Bearer ' . $access_token,
741
-						'Content-type'  => 'application/json',
742
-					),
743
-
744
-				)
745
-			);
746
-
747
-			if ( is_wp_error( $user_info ) ) {
748
-				getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
749
-			} else {
750
-
751
-				// Create application.
752
-				$user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
753
-
754
-				if ( $sandbox ) {
755
-					wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
756
-					wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
757
-					set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
758
-					getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) );
759
-				} else {
760
-					wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
761
-					wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
762
-					set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
763
-					getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) );
764
-				}
711
+    /**
712
+     * Connects to PayPal.
713
+     *
714
+     * @param array $data Connection data.
715
+     * @return void
716
+     */
717
+    public function connect_paypal( $data ) {
718
+
719
+        $sandbox      = $this->is_sandbox();
720
+        $data         = wp_unslash( $data );
721
+        $access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
722
+
723
+        if ( isset( $data['live_mode'] ) ) {
724
+            $sandbox = empty( $data['live_mode'] );
725
+        }
726
+
727
+        wpinv_update_option( 'paypal_sandbox', (int) $sandbox );
728
+        wpinv_update_option( 'paypal_active', 1 );
729
+
730
+        if ( ! empty( $data['error_description'] ) ) {
731
+            getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
732
+        } else {
733
+
734
+            // Retrieve the user info.
735
+            $user_info = wp_remote_get(
736
+                ! $sandbox ? 'https://api-m.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1' : 'https://api-m.sandbox.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1',
737
+                array(
738
+
739
+                    'headers' => array(
740
+                        'Authorization' => 'Bearer ' . $access_token,
741
+                        'Content-type'  => 'application/json',
742
+                    ),
743
+
744
+                )
745
+            );
746
+
747
+            if ( is_wp_error( $user_info ) ) {
748
+                getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
749
+            } else {
750
+
751
+                // Create application.
752
+                $user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
753
+
754
+                if ( $sandbox ) {
755
+                    wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
756
+                    wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
757
+                    set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
758
+                    getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) );
759
+                } else {
760
+                    wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
761
+                    wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
762
+                    set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
763
+                    getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) );
764
+                }
765 765
 }
766 766
 }
767 767
 
768
-		$redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) : urldecode( $data['redirect'] );
768
+        $redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) : urldecode( $data['redirect'] );
769 769
 
770
-		if ( isset( $data['step'] ) ) {
771
-			$redirect = add_query_arg( 'step', $data['step'], $redirect );
772
-		}
773
-		wp_redirect( $redirect );
774
-		exit;
775
-	}
770
+        if ( isset( $data['step'] ) ) {
771
+            $redirect = add_query_arg( 'step', $data['step'], $redirect );
772
+        }
773
+        wp_redirect( $redirect );
774
+        exit;
775
+    }
776 776
 
777 777
 }
Please login to merge, or discard this patch.