Passed
Push — master ( 114ba5...63d401 )
by Brian
03:52
created
includes/gateways/class-getpaid-paypal-gateway.php 1 patch
Indentation   +343 added lines, -343 removed lines patch added patch discarded remove patch
@@ -13,96 +13,96 @@  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( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 );
88
+        add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 );
89 89
         add_filter( 'getpaid_paypal_sandbox_notice', array( $this, 'sandbox_notice' ) );
90
-		add_filter( 'getpaid_get_paypal_connect_url', array( $this, 'maybe_get_connect_url' ), 10, 2 );
91
-		add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) );
90
+        add_filter( 'getpaid_get_paypal_connect_url', array( $this, 'maybe_get_connect_url' ), 10, 2 );
91
+        add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) );
92 92
 
93 93
         parent::__construct();
94 94
     }
95 95
 
96 96
     /**
97
-	 * Process Payment.
98
-	 *
99
-	 *
100
-	 * @param WPInv_Invoice $invoice Invoice.
101
-	 * @param array $submission_data Posted checkout fields.
102
-	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
103
-	 * @return array
104
-	 */
105
-	public function process_payment( $invoice, $submission_data, $submission ) {
97
+     * Process Payment.
98
+     *
99
+     *
100
+     * @param WPInv_Invoice $invoice Invoice.
101
+     * @param array $submission_data Posted checkout fields.
102
+     * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
103
+     * @return array
104
+     */
105
+    public function process_payment( $invoice, $submission_data, $submission ) {
106 106
 
107 107
         // Get redirect url.
108 108
         $paypal_redirect = $this->get_request_url( $invoice );
@@ -125,15 +125,15 @@  discard block
 block discarded – undo
125 125
     }
126 126
 
127 127
     /**
128
-	 * Get the PayPal request URL for an invoice.
129
-	 *
130
-	 * @param  WPInv_Invoice $invoice Invoice object.
131
-	 * @return string
132
-	 */
133
-	public function get_request_url( $invoice ) {
128
+     * Get the PayPal request URL for an invoice.
129
+     *
130
+     * @param  WPInv_Invoice $invoice Invoice object.
131
+     * @return string
132
+     */
133
+    public function get_request_url( $invoice ) {
134 134
 
135 135
         // Endpoint for this request
136
-		$this->endpoint    = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?';
136
+        $this->endpoint    = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?';
137 137
 
138 138
         // Retrieve paypal args.
139 139
         $paypal_args       = map_deep( $this->get_paypal_args( $invoice ), 'urlencode' );
@@ -146,44 +146,44 @@  discard block
 block discarded – undo
146 146
 
147 147
         return add_query_arg( $paypal_args, $this->endpoint );
148 148
 
149
-	}
149
+    }
150 150
 
151 151
     /**
152
-	 * Get PayPal Args for passing to PP.
153
-	 *
154
-	 * @param  WPInv_Invoice $invoice Invoice object.
155
-	 * @return array
156
-	 */
157
-	protected function get_paypal_args( $invoice ) {
152
+     * Get PayPal Args for passing to PP.
153
+     *
154
+     * @param  WPInv_Invoice $invoice Invoice object.
155
+     * @return array
156
+     */
157
+    protected function get_paypal_args( $invoice ) {
158 158
 
159 159
         // Whether or not to send the line items as one item.
160
-		$force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice );
161
-
162
-		if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) {
163
-			$force_one_line_item = true;
164
-		}
165
-
166
-		$paypal_args = apply_filters(
167
-			'getpaid_paypal_args',
168
-			array_merge(
169
-				$this->get_transaction_args( $invoice ),
170
-				$this->get_line_item_args( $invoice, $force_one_line_item )
171
-			),
172
-			$invoice
173
-		);
174
-
175
-		return $this->fix_request_length( $invoice, $paypal_args );
160
+        $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice );
161
+
162
+        if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) {
163
+            $force_one_line_item = true;
164
+        }
165
+
166
+        $paypal_args = apply_filters(
167
+            'getpaid_paypal_args',
168
+            array_merge(
169
+                $this->get_transaction_args( $invoice ),
170
+                $this->get_line_item_args( $invoice, $force_one_line_item )
171
+            ),
172
+            $invoice
173
+        );
174
+
175
+        return $this->fix_request_length( $invoice, $paypal_args );
176 176
     }
177 177
 
178 178
     /**
179
-	 * Get transaction args for paypal request.
180
-	 *
181
-	 * @param WPInv_Invoice $invoice Invoice object.
182
-	 * @return array
183
-	 */
184
-	protected function get_transaction_args( $invoice ) {
185
-
186
-		return array(
179
+     * Get transaction args for paypal request.
180
+     *
181
+     * @param WPInv_Invoice $invoice Invoice object.
182
+     * @return array
183
+     */
184
+    protected function get_transaction_args( $invoice ) {
185
+
186
+        return array(
187 187
             'cmd'           => '_cart',
188 188
             'business'      => wpinv_get_option( 'paypal_email', false ),
189 189
             'no_shipping'   => '1',
@@ -208,16 +208,16 @@  discard block
 block discarded – undo
208 208
     }
209 209
 
210 210
     /**
211
-	 * Get line item args for paypal request.
212
-	 *
213
-	 * @param  WPInv_Invoice $invoice Invoice object.
214
-	 * @param  bool     $force_one_line_item Create only one item for this invoice.
215
-	 * @return array
216
-	 */
217
-	protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
211
+     * Get line item args for paypal request.
212
+     *
213
+     * @param  WPInv_Invoice $invoice Invoice object.
214
+     * @param  bool     $force_one_line_item Create only one item for this invoice.
215
+     * @return array
216
+     */
217
+    protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
218 218
 
219 219
         // Maybe send invoice as a single item.
220
-		if ( $force_one_line_item ) {
220
+        if ( $force_one_line_item ) {
221 221
             return $this->get_line_item_args_single_item( $invoice );
222 222
         }
223 223
 
@@ -237,129 +237,129 @@  discard block
 block discarded – undo
237 237
             $line_item_args['discount_amount_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_discount(), 2 );
238 238
         }
239 239
 
240
-		return array_merge( $line_item_args, $this->get_line_items() );
240
+        return array_merge( $line_item_args, $this->get_line_items() );
241 241
 
242 242
     }
243 243
 
244 244
     /**
245
-	 * Get line item args for paypal request as a single line item.
246
-	 *
247
-	 * @param  WPInv_Invoice $invoice Invoice object.
248
-	 * @return array
249
-	 */
250
-	protected function get_line_item_args_single_item( $invoice ) {
251
-		$this->delete_line_items();
245
+     * Get line item args for paypal request as a single line item.
246
+     *
247
+     * @param  WPInv_Invoice $invoice Invoice object.
248
+     * @return array
249
+     */
250
+    protected function get_line_item_args_single_item( $invoice ) {
251
+        $this->delete_line_items();
252 252
 
253 253
         $item_name = sprintf( __( 'Invoice #%s', 'invoicing' ), $invoice->get_number() );
254
-		$this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
254
+        $this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
255 255
 
256
-		return $this->get_line_items();
256
+        return $this->get_line_items();
257 257
     }
258 258
 
259 259
     /**
260
-	 * Return all line items.
261
-	 */
262
-	protected function get_line_items() {
263
-		return $this->line_items;
264
-	}
260
+     * Return all line items.
261
+     */
262
+    protected function get_line_items() {
263
+        return $this->line_items;
264
+    }
265 265
 
266 266
     /**
267
-	 * Remove all line items.
268
-	 */
269
-	protected function delete_line_items() {
270
-		$this->line_items = array();
267
+     * Remove all line items.
268
+     */
269
+    protected function delete_line_items() {
270
+        $this->line_items = array();
271 271
     }
272 272
 
273 273
     /**
274
-	 * Prepare line items to send to paypal.
275
-	 *
276
-	 * @param  WPInv_Invoice $invoice Invoice object.
277
-	 */
278
-	protected function prepare_line_items( $invoice ) {
279
-		$this->delete_line_items();
280
-
281
-		// Items.
282
-		foreach ( $invoice->get_items() as $item ) {
283
-			$amount   = $item->get_price();
284
-			$quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
285
-			$this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
274
+     * Prepare line items to send to paypal.
275
+     *
276
+     * @param  WPInv_Invoice $invoice Invoice object.
277
+     */
278
+    protected function prepare_line_items( $invoice ) {
279
+        $this->delete_line_items();
280
+
281
+        // Items.
282
+        foreach ( $invoice->get_items() as $item ) {
283
+            $amount   = $item->get_price();
284
+            $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
285
+            $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
286 286
         }
287 287
 
288 288
         // Fees.
289
-		foreach ( $invoice->get_fees() as $fee => $data ) {
289
+        foreach ( $invoice->get_fees() as $fee => $data ) {
290 290
             $this->add_line_item( $fee, 1, wpinv_sanitize_amount( $data['initial_fee'] ) );
291 291
         }
292 292
 
293 293
     }
294 294
 
295 295
     /**
296
-	 * Add PayPal Line Item.
297
-	 *
298
-	 * @param  string $item_name Item name.
299
-	 * @param  float    $quantity Item quantity.
300
-	 * @param  float  $amount Amount.
301
-	 * @param  string $item_number Item number.
302
-	 */
303
-	protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
304
-		$index = ( count( $this->line_items ) / 4 ) + 1;
305
-
306
-		$item = apply_filters(
307
-			'getpaid_paypal_line_item',
308
-			array(
309
-				'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
310
-				'quantity'    => (float) $quantity,
311
-				'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
312
-				'item_number' => $item_number,
313
-			),
314
-			$item_name,
315
-			$quantity,
316
-			$amount,
317
-			$item_number
318
-		);
319
-
320
-		$this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
296
+     * Add PayPal Line Item.
297
+     *
298
+     * @param  string $item_name Item name.
299
+     * @param  float    $quantity Item quantity.
300
+     * @param  float  $amount Amount.
301
+     * @param  string $item_number Item number.
302
+     */
303
+    protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
304
+        $index = ( count( $this->line_items ) / 4 ) + 1;
305
+
306
+        $item = apply_filters(
307
+            'getpaid_paypal_line_item',
308
+            array(
309
+                'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
310
+                'quantity'    => (float) $quantity,
311
+                'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
312
+                'item_number' => $item_number,
313
+            ),
314
+            $item_name,
315
+            $quantity,
316
+            $amount,
317
+            $item_number
318
+        );
319
+
320
+        $this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
321 321
         $this->line_items[ 'quantity_' . $index ]    = $item['quantity'];
322 322
 
323 323
         // The price or amount of the product, service, or contribution, not including shipping, handling, or tax.
324
-		$this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
325
-		$this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
324
+        $this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
325
+        $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
326 326
     }
327 327
 
328 328
     /**
329
-	 * If the default request with line items is too long, generate a new one with only one line item.
330
-	 *
331
-	 * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
332
-	 *
333
-	 * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
334
-	 * @param array    $paypal_args Arguments sent to Paypal in the request.
335
-	 * @return array
336
-	 */
337
-	protected function fix_request_length( $invoice, $paypal_args ) {
338
-		$max_paypal_length = 2083;
339
-		$query_candidate   = http_build_query( $paypal_args, '', '&' );
340
-
341
-		if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
342
-			return $paypal_args;
343
-		}
344
-
345
-		return apply_filters(
346
-			'getpaid_paypal_args',
347
-			array_merge(
348
-				$this->get_transaction_args( $invoice ),
349
-				$this->get_line_item_args( $invoice, true )
350
-			),
351
-			$invoice
352
-		);
329
+     * If the default request with line items is too long, generate a new one with only one line item.
330
+     *
331
+     * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
332
+     *
333
+     * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
334
+     * @param array    $paypal_args Arguments sent to Paypal in the request.
335
+     * @return array
336
+     */
337
+    protected function fix_request_length( $invoice, $paypal_args ) {
338
+        $max_paypal_length = 2083;
339
+        $query_candidate   = http_build_query( $paypal_args, '', '&' );
340
+
341
+        if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
342
+            return $paypal_args;
343
+        }
344
+
345
+        return apply_filters(
346
+            'getpaid_paypal_args',
347
+            array_merge(
348
+                $this->get_transaction_args( $invoice ),
349
+                $this->get_line_item_args( $invoice, true )
350
+            ),
351
+            $invoice
352
+        );
353 353
 
354 354
     }
355 355
 
356 356
     /**
357
-	 * Processes recurring invoices.
358
-	 *
359
-	 * @param  array $paypal_args PayPal args.
360
-	 * @param  WPInv_Invoice    $invoice Invoice object.
361
-	 */
362
-	public function process_subscription( $paypal_args, $invoice ) {
357
+     * Processes recurring invoices.
358
+     *
359
+     * @param  array $paypal_args PayPal args.
360
+     * @param  WPInv_Invoice    $invoice Invoice object.
361
+     */
362
+    public function process_subscription( $paypal_args, $invoice ) {
363 363
 
364 364
         // Make sure this is a subscription.
365 365
         if ( ! $invoice->is_recurring() || ! $subscription = getpaid_get_invoice_subscription( $invoice ) ) {
@@ -384,11 +384,11 @@  discard block
 block discarded – undo
384 384
 
385 385
             $paypal_args['a1'] = 0 == $initial_amount ? 0 : $initial_amount;
386 386
 
387
-			// Trial period length.
388
-			$paypal_args['p1'] = $subscription_item->get_trial_interval();
387
+            // Trial period length.
388
+            $paypal_args['p1'] = $subscription_item->get_trial_interval();
389 389
 
390
-			// Trial period.
391
-			$paypal_args['t1'] = $subscription_item->get_trial_period();
390
+            // Trial period.
391
+            $paypal_args['t1'] = $subscription_item->get_trial_period();
392 392
 
393 393
         } else if ( $initial_amount != $recurring_amount ) {
394 394
 
@@ -411,40 +411,40 @@  discard block
 block discarded – undo
411 411
         }
412 412
 
413 413
         // We have a recurring payment
414
-		if ( ! isset( $param_number ) || 1 == $param_number ) {
414
+        if ( ! isset( $param_number ) || 1 == $param_number ) {
415 415
 
416
-			// Subscription price
417
-			$paypal_args['a3'] = $recurring_amount;
416
+            // Subscription price
417
+            $paypal_args['a3'] = $recurring_amount;
418 418
 
419
-			// Subscription duration
420
-			$paypal_args['p3'] = $interval;
419
+            // Subscription duration
420
+            $paypal_args['p3'] = $interval;
421 421
 
422
-			// Subscription period
423
-			$paypal_args['t3'] = $period;
422
+            // Subscription period
423
+            $paypal_args['t3'] = $period;
424 424
 
425 425
         }
426 426
 
427 427
         // Recurring payments
428
-		if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
428
+        if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
429 429
 
430
-			// Non-recurring payments
431
-			$paypal_args['src'] = 0;
430
+            // Non-recurring payments
431
+            $paypal_args['src'] = 0;
432 432
 
433
-		} else {
433
+        } else {
434 434
 
435
-			$paypal_args['src'] = 1;
435
+            $paypal_args['src'] = 1;
436 436
 
437
-			if ( $bill_times > 0 ) {
437
+            if ( $bill_times > 0 ) {
438 438
 
439
-				// An initial period is being used to charge a sign-up fee
440
-				if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
441
-					$bill_times--;
442
-				}
439
+                // An initial period is being used to charge a sign-up fee
440
+                if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
441
+                    $bill_times--;
442
+                }
443 443
 
444 444
                 // Make sure it's not over the max of 52
445 445
                 $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 );
446 446
 
447
-			}
447
+            }
448 448
         }
449 449
 
450 450
         // Force return URL so that order description & instructions display
@@ -460,19 +460,19 @@  discard block
 block discarded – undo
460 460
         }
461 461
 
462 462
         return apply_filters(
463
-			'getpaid_paypal_subscription_args',
464
-			$paypal_args,
465
-			$invoice
463
+            'getpaid_paypal_subscription_args',
464
+            $paypal_args,
465
+            $invoice
466 466
         );
467 467
 
468 468
     }
469 469
 
470 470
     /**
471
-	 * Processes ipns and marks payments as complete.
472
-	 *
473
-	 * @return void
474
-	 */
475
-	public function verify_ipn() {
471
+     * Processes ipns and marks payments as complete.
472
+     *
473
+     * @return void
474
+     */
475
+    public function verify_ipn() {
476 476
         new GetPaid_Paypal_Gateway_IPN_Handler( $this );
477 477
     }
478 478
 
@@ -482,19 +482,19 @@  discard block
 block discarded – undo
482 482
     public function sandbox_notice() {
483 483
 
484 484
         return sprintf(
485
-			__( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %sPayPal Sandbox Testing Guide%s for more details.', 'invoicing' ),
486
-			'<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
487
-			'</a>'
488
-		);
485
+            __( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %sPayPal Sandbox Testing Guide%s for more details.', 'invoicing' ),
486
+            '<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
487
+            '</a>'
488
+        );
489 489
 
490 490
     }
491 491
 
492
-	/**
493
-	 * Filters the gateway settings.
494
-	 *
495
-	 * @param array $admin_settings
496
-	 */
497
-	public function admin_settings( $admin_settings ) {
492
+    /**
493
+     * Filters the gateway settings.
494
+     *
495
+     * @param array $admin_settings
496
+     */
497
+    public function admin_settings( $admin_settings ) {
498 498
 
499 499
         $currencies = sprintf(
500 500
             __( 'Supported Currencies: %s', 'invoicing' ),
@@ -504,39 +504,39 @@  discard block
 block discarded – undo
504 504
         $admin_settings['paypal_active']['desc'] .= " ($currencies)";
505 505
         $admin_settings['paypal_desc']['std']     = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
506 506
 
507
-		// Access tokens.
508
-		$live_email      = wpinv_get_option( 'paypal_email' );
509
-		$sandbox_email   = wpinv_get_option( 'paypal_sandbox_email' );
510
-
511
-		$admin_settings['paypal_connect'] = array(
512
-			'type'       => 'raw_html',
513
-			'id'         => 'paypal_connect',
514
-			'name'       => __( 'Connect to PayPal', 'invoicing' ),
515
-			'desc'       => sprintf(
516
-				'<div class="wpinv-paypal-connect-live"><a class="button button-primary" href="%s">%s</a></div><div class="wpinv-paypal-connect-sandbox"><a class="button button-primary" href="%s">%s</a></div>%s',
517
-				esc_url( self::get_connect_url( false ) ),
518
-				__( 'Connect to PayPal', 'invoicing' ),
519
-				esc_url( self::get_connect_url( true ) ),
520
-				__( 'Connect to PayPal Sandox', 'invoicing' ),
521
-				$this->get_js()
522
-			),
523
-		);
507
+        // Access tokens.
508
+        $live_email      = wpinv_get_option( 'paypal_email' );
509
+        $sandbox_email   = wpinv_get_option( 'paypal_sandbox_email' );
510
+
511
+        $admin_settings['paypal_connect'] = array(
512
+            'type'       => 'raw_html',
513
+            'id'         => 'paypal_connect',
514
+            'name'       => __( 'Connect to PayPal', 'invoicing' ),
515
+            'desc'       => sprintf(
516
+                '<div class="wpinv-paypal-connect-live"><a class="button button-primary" href="%s">%s</a></div><div class="wpinv-paypal-connect-sandbox"><a class="button button-primary" href="%s">%s</a></div>%s',
517
+                esc_url( self::get_connect_url( false ) ),
518
+                __( 'Connect to PayPal', 'invoicing' ),
519
+                esc_url( self::get_connect_url( true ) ),
520
+                __( 'Connect to PayPal Sandox', 'invoicing' ),
521
+                $this->get_js()
522
+            ),
523
+        );
524 524
 
525 525
         $admin_settings['paypal_email'] = array(
526 526
             'type'  => 'text',
527
-			'class' => 'live-auth-data',
527
+            'class' => 'live-auth-data',
528 528
             'id'    => 'paypal_email',
529 529
             'name'  => __( 'Live Email Address', 'invoicing' ),
530 530
             'desc'  => __( 'The email address of your PayPal account.', 'invoicing' ),
531 531
         );
532 532
 
533
-		$admin_settings['paypal_sandbox_email'] = array(
533
+        $admin_settings['paypal_sandbox_email'] = array(
534 534
             'type'  => 'text',
535
-			'class' => 'sandbox-auth-data',
535
+            'class' => 'sandbox-auth-data',
536 536
             'id'    => 'paypal_sandbox_email',
537 537
             'name'  => __( 'Sandbox Email Address', 'invoicing' ),
538 538
             'desc'  => __( 'The email address of your sandbox PayPal account.', 'invoicing' ),
539
-			'std'   => wpinv_get_option( 'paypal_email', '' ),
539
+            'std'   => wpinv_get_option( 'paypal_email', '' ),
540 540
         );
541 541
 
542 542
         $admin_settings['paypal_ipn_url'] = array(
@@ -548,29 +548,29 @@  discard block
 block discarded – undo
548 548
             'readonly' => true,
549 549
         );
550 550
 
551
-		return $admin_settings;
552
-	}
551
+        return $admin_settings;
552
+    }
553 553
 
554
-	/**
555
-	 * Retrieves the PayPal connect URL when using the setup wizzard.
556
-	 *
557
-	 *
554
+    /**
555
+     * Retrieves the PayPal connect URL when using the setup wizzard.
556
+     *
557
+     *
558 558
      * @param array $data
559 559
      * @return string
560
-	 */
561
-	public static function maybe_get_connect_url( $url = '', $data ) {
562
-		return self::get_connect_url( false, urldecode( $data['redirect'] ) );
563
-	}
564
-
565
-	/**
566
-	 * Retrieves the PayPal connect URL.
567
-	 *
568
-	 *
560
+     */
561
+    public static function maybe_get_connect_url( $url = '', $data ) {
562
+        return self::get_connect_url( false, urldecode( $data['redirect'] ) );
563
+    }
564
+
565
+    /**
566
+     * Retrieves the PayPal connect URL.
567
+     *
568
+     *
569 569
      * @param bool $is_sandbox
570
-	 * @param string $redirect
570
+     * @param string $redirect
571 571
      * @return string
572
-	 */
573
-	public static function get_connect_url( $is_sandbox, $redirect = '' ) {
572
+     */
573
+    public static function get_connect_url( $is_sandbox, $redirect = '' ) {
574 574
 
575 575
         $redirect_url = add_query_arg(
576 576
             array(
@@ -580,7 +580,7 @@  discard block
 block discarded – undo
580 580
                 'tab'                  => 'gateways',
581 581
                 'section'              => 'paypal',
582 582
                 'getpaid-nonce'        => wp_create_nonce( 'getpaid-nonce' ),
583
-				'redirect'             => urlencode( $redirect ),
583
+                'redirect'             => urlencode( $redirect ),
584 584
             ),
585 585
             admin_url( 'admin.php' )
586 586
         );
@@ -595,12 +595,12 @@  discard block
 block discarded – undo
595 595
 
596 596
     }
597 597
 
598
-	/**
599
-	 * Generates settings page js.
600
-	 *
598
+    /**
599
+     * Generates settings page js.
600
+     *
601 601
      * @return void
602
-	 */
603
-	public static function get_js() {
602
+     */
603
+    public static function get_js() {
604 604
         ob_start();
605 605
         ?>
606 606
             <script>
@@ -636,72 +636,72 @@  discard block
 block discarded – undo
636 636
         return ob_get_clean();
637 637
     }
638 638
 
639
-	/**
640
-	 * Connects to PayPal.
641
-	 *
642
-	 * @param array $data Connection data.
643
-	 * @return void
644
-	 */
645
-	public function connect_paypal( $data ) {
646
-
647
-		$sandbox      = $this->is_sandbox();
648
-		$data         = wp_unslash( $data );
649
-		$access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
650
-
651
-		if ( isset( $data['live_mode'] ) ) {
652
-			$sandbox = empty( $data['live_mode'] );
653
-		}
654
-
655
-		wpinv_update_option( 'paypal_sandbox', (int) $sandbox );
656
-		wpinv_update_option( 'paypal_active', 1 );
657
-
658
-		if ( ! empty( $data['error_description'] ) ) {
659
-			getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
660
-		} else {
661
-
662
-			// Retrieve the user info.
663
-			$user_info = wp_remote_get(
664
-				! $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',
665
-				array(
666
-
667
-					'headers' => array(
668
-						'Authorization' => 'Bearer ' . $access_token,
669
-						'Content-type'  => 'application/json',
670
-					)
671
-
672
-				)
673
-			);
674
-
675
-			if ( is_wp_error( $user_info ) ) {
676
-				getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
677
-			} else {
678
-
679
-				// Create application.
680
-				$user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
681
-
682
-				if ( $sandbox ) {
683
-					wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
684
-					wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
685
-					set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
686
-					getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) );
687
-				} else {
688
-					wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
689
-					wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
690
-					set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
691
-					getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) );
692
-				}
693
-
694
-			}
695
-
696
-		}
697
-
698
-		$redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) : urldecode( $data['redirect'] );
699
-
700
-		if ( isset( $data['step'] ) ) {
701
-			$redirect = add_query_arg( 'step', $data['step'], $redirect );
702
-		}
703
-		wp_redirect( $redirect );
704
-		exit;
705
-	}
639
+    /**
640
+     * Connects to PayPal.
641
+     *
642
+     * @param array $data Connection data.
643
+     * @return void
644
+     */
645
+    public function connect_paypal( $data ) {
646
+
647
+        $sandbox      = $this->is_sandbox();
648
+        $data         = wp_unslash( $data );
649
+        $access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
650
+
651
+        if ( isset( $data['live_mode'] ) ) {
652
+            $sandbox = empty( $data['live_mode'] );
653
+        }
654
+
655
+        wpinv_update_option( 'paypal_sandbox', (int) $sandbox );
656
+        wpinv_update_option( 'paypal_active', 1 );
657
+
658
+        if ( ! empty( $data['error_description'] ) ) {
659
+            getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
660
+        } else {
661
+
662
+            // Retrieve the user info.
663
+            $user_info = wp_remote_get(
664
+                ! $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',
665
+                array(
666
+
667
+                    'headers' => array(
668
+                        'Authorization' => 'Bearer ' . $access_token,
669
+                        'Content-type'  => 'application/json',
670
+                    )
671
+
672
+                )
673
+            );
674
+
675
+            if ( is_wp_error( $user_info ) ) {
676
+                getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
677
+            } else {
678
+
679
+                // Create application.
680
+                $user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
681
+
682
+                if ( $sandbox ) {
683
+                    wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
684
+                    wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
685
+                    set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
686
+                    getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) );
687
+                } else {
688
+                    wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
689
+                    wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
690
+                    set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
691
+                    getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) );
692
+                }
693
+
694
+            }
695
+
696
+        }
697
+
698
+        $redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) : urldecode( $data['redirect'] );
699
+
700
+        if ( isset( $data['step'] ) ) {
701
+            $redirect = add_query_arg( 'step', $data['step'], $redirect );
702
+        }
703
+        wp_redirect( $redirect );
704
+        exit;
705
+    }
706 706
 
707 707
 }
Please login to merge, or discard this patch.