Passed
Pull Request — master (#605)
by Brian
05:22
created
includes/gateways/class-getpaid-paypal-gateway.php 1 patch
Indentation   +377 added lines, -377 removed lines patch added patch discarded remove patch
@@ -13,95 +13,95 @@  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_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) );
90
+        add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) );
91 91
 
92 92
         parent::__construct();
93 93
     }
94 94
 
95 95
     /**
96
-	 * Process Payment.
97
-	 *
98
-	 *
99
-	 * @param WPInv_Invoice $invoice Invoice.
100
-	 * @param array $submission_data Posted checkout fields.
101
-	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
102
-	 * @return array
103
-	 */
104
-	public function process_payment( $invoice, $submission_data, $submission ) {
96
+     * Process Payment.
97
+     *
98
+     *
99
+     * @param WPInv_Invoice $invoice Invoice.
100
+     * @param array $submission_data Posted checkout fields.
101
+     * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
102
+     * @return array
103
+     */
104
+    public function process_payment( $invoice, $submission_data, $submission ) {
105 105
 
106 106
         // Get redirect url.
107 107
         $paypal_redirect = $this->get_request_url( $invoice );
@@ -124,15 +124,15 @@  discard block
 block discarded – undo
124 124
     }
125 125
 
126 126
     /**
127
-	 * Get the PayPal request URL for an invoice.
128
-	 *
129
-	 * @param  WPInv_Invoice $invoice Invoice object.
130
-	 * @return string
131
-	 */
132
-	public function get_request_url( $invoice ) {
127
+     * Get the PayPal request URL for an invoice.
128
+     *
129
+     * @param  WPInv_Invoice $invoice Invoice object.
130
+     * @return string
131
+     */
132
+    public function get_request_url( $invoice ) {
133 133
 
134 134
         // Endpoint for this request
135
-		$this->endpoint    = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?';
135
+        $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 136
 
137 137
         // Retrieve paypal args.
138 138
         $paypal_args       = map_deep( $this->get_paypal_args( $invoice ), 'urlencode' );
@@ -145,44 +145,44 @@  discard block
 block discarded – undo
145 145
 
146 146
         return add_query_arg( $paypal_args, $this->endpoint );
147 147
 
148
-	}
148
+    }
149 149
 
150 150
     /**
151
-	 * Get PayPal Args for passing to PP.
152
-	 *
153
-	 * @param  WPInv_Invoice $invoice Invoice object.
154
-	 * @return array
155
-	 */
156
-	protected function get_paypal_args( $invoice ) {
151
+     * Get PayPal Args for passing to PP.
152
+     *
153
+     * @param  WPInv_Invoice $invoice Invoice object.
154
+     * @return array
155
+     */
156
+    protected function get_paypal_args( $invoice ) {
157 157
 
158 158
         // Whether or not to send the line items as one item.
159
-		$force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice );
160
-
161
-		if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) {
162
-			$force_one_line_item = true;
163
-		}
164
-
165
-		$paypal_args = apply_filters(
166
-			'getpaid_paypal_args',
167
-			array_merge(
168
-				$this->get_transaction_args( $invoice ),
169
-				$this->get_line_item_args( $invoice, $force_one_line_item )
170
-			),
171
-			$invoice
172
-		);
173
-
174
-		return $this->fix_request_length( $invoice, $paypal_args );
159
+        $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice );
160
+
161
+        if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) {
162
+            $force_one_line_item = true;
163
+        }
164
+
165
+        $paypal_args = apply_filters(
166
+            'getpaid_paypal_args',
167
+            array_merge(
168
+                $this->get_transaction_args( $invoice ),
169
+                $this->get_line_item_args( $invoice, $force_one_line_item )
170
+            ),
171
+            $invoice
172
+        );
173
+
174
+        return $this->fix_request_length( $invoice, $paypal_args );
175 175
     }
176 176
 
177 177
     /**
178
-	 * Get transaction args for paypal request.
179
-	 *
180
-	 * @param WPInv_Invoice $invoice Invoice object.
181
-	 * @return array
182
-	 */
183
-	protected function get_transaction_args( $invoice ) {
184
-
185
-		return array(
178
+     * Get transaction args for paypal request.
179
+     *
180
+     * @param WPInv_Invoice $invoice Invoice object.
181
+     * @return array
182
+     */
183
+    protected function get_transaction_args( $invoice ) {
184
+
185
+        return array(
186 186
             'cmd'           => '_cart',
187 187
             'business'      => wpinv_get_option( 'paypal_email', false ),
188 188
             'no_shipping'   => '1',
@@ -207,16 +207,16 @@  discard block
 block discarded – undo
207 207
     }
208 208
 
209 209
     /**
210
-	 * Get line item args for paypal request.
211
-	 *
212
-	 * @param  WPInv_Invoice $invoice Invoice object.
213
-	 * @param  bool     $force_one_line_item Create only one item for this invoice.
214
-	 * @return array
215
-	 */
216
-	protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
210
+     * Get line item args for paypal request.
211
+     *
212
+     * @param  WPInv_Invoice $invoice Invoice object.
213
+     * @param  bool     $force_one_line_item Create only one item for this invoice.
214
+     * @return array
215
+     */
216
+    protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
217 217
 
218 218
         // Maybe send invoice as a single item.
219
-		if ( $force_one_line_item ) {
219
+        if ( $force_one_line_item ) {
220 220
             return $this->get_line_item_args_single_item( $invoice );
221 221
         }
222 222
 
@@ -236,129 +236,129 @@  discard block
 block discarded – undo
236 236
             $line_item_args['discount_amount_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_discount(), 2 );
237 237
         }
238 238
 
239
-		return array_merge( $line_item_args, $this->get_line_items() );
239
+        return array_merge( $line_item_args, $this->get_line_items() );
240 240
 
241 241
     }
242 242
 
243 243
     /**
244
-	 * Get line item args for paypal request as a single line item.
245
-	 *
246
-	 * @param  WPInv_Invoice $invoice Invoice object.
247
-	 * @return array
248
-	 */
249
-	protected function get_line_item_args_single_item( $invoice ) {
250
-		$this->delete_line_items();
244
+     * Get line item args for paypal request as a single line item.
245
+     *
246
+     * @param  WPInv_Invoice $invoice Invoice object.
247
+     * @return array
248
+     */
249
+    protected function get_line_item_args_single_item( $invoice ) {
250
+        $this->delete_line_items();
251 251
 
252 252
         $item_name = sprintf( __( 'Invoice #%s', 'invoicing' ), $invoice->get_number() );
253
-		$this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
253
+        $this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
254 254
 
255
-		return $this->get_line_items();
255
+        return $this->get_line_items();
256 256
     }
257 257
 
258 258
     /**
259
-	 * Return all line items.
260
-	 */
261
-	protected function get_line_items() {
262
-		return $this->line_items;
263
-	}
259
+     * Return all line items.
260
+     */
261
+    protected function get_line_items() {
262
+        return $this->line_items;
263
+    }
264 264
 
265 265
     /**
266
-	 * Remove all line items.
267
-	 */
268
-	protected function delete_line_items() {
269
-		$this->line_items = array();
266
+     * Remove all line items.
267
+     */
268
+    protected function delete_line_items() {
269
+        $this->line_items = array();
270 270
     }
271 271
 
272 272
     /**
273
-	 * Prepare line items to send to paypal.
274
-	 *
275
-	 * @param  WPInv_Invoice $invoice Invoice object.
276
-	 */
277
-	protected function prepare_line_items( $invoice ) {
278
-		$this->delete_line_items();
279
-
280
-		// Items.
281
-		foreach ( $invoice->get_items() as $item ) {
282
-			$amount   = $item->get_price();
283
-			$quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
284
-			$this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
273
+     * Prepare line items to send to paypal.
274
+     *
275
+     * @param  WPInv_Invoice $invoice Invoice object.
276
+     */
277
+    protected function prepare_line_items( $invoice ) {
278
+        $this->delete_line_items();
279
+
280
+        // Items.
281
+        foreach ( $invoice->get_items() as $item ) {
282
+            $amount   = $item->get_price();
283
+            $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
284
+            $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
285 285
         }
286 286
 
287 287
         // Fees.
288
-		foreach ( $invoice->get_fees() as $fee => $data ) {
288
+        foreach ( $invoice->get_fees() as $fee => $data ) {
289 289
             $this->add_line_item( $fee, 1, wpinv_sanitize_amount( $data['initial_fee'] ) );
290 290
         }
291 291
 
292 292
     }
293 293
 
294 294
     /**
295
-	 * Add PayPal Line Item.
296
-	 *
297
-	 * @param  string $item_name Item name.
298
-	 * @param  float    $quantity Item quantity.
299
-	 * @param  float  $amount Amount.
300
-	 * @param  string $item_number Item number.
301
-	 */
302
-	protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
303
-		$index = ( count( $this->line_items ) / 4 ) + 1;
304
-
305
-		$item = apply_filters(
306
-			'getpaid_paypal_line_item',
307
-			array(
308
-				'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
309
-				'quantity'    => (float) $quantity,
310
-				'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
311
-				'item_number' => $item_number,
312
-			),
313
-			$item_name,
314
-			$quantity,
315
-			$amount,
316
-			$item_number
317
-		);
318
-
319
-		$this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
295
+     * Add PayPal Line Item.
296
+     *
297
+     * @param  string $item_name Item name.
298
+     * @param  float    $quantity Item quantity.
299
+     * @param  float  $amount Amount.
300
+     * @param  string $item_number Item number.
301
+     */
302
+    protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
303
+        $index = ( count( $this->line_items ) / 4 ) + 1;
304
+
305
+        $item = apply_filters(
306
+            'getpaid_paypal_line_item',
307
+            array(
308
+                'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
309
+                'quantity'    => (float) $quantity,
310
+                'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
311
+                'item_number' => $item_number,
312
+            ),
313
+            $item_name,
314
+            $quantity,
315
+            $amount,
316
+            $item_number
317
+        );
318
+
319
+        $this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
320 320
         $this->line_items[ 'quantity_' . $index ]    = $item['quantity'];
321 321
 
322 322
         // The price or amount of the product, service, or contribution, not including shipping, handling, or tax.
323
-		$this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
324
-		$this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
323
+        $this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
324
+        $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
325 325
     }
326 326
 
327 327
     /**
328
-	 * If the default request with line items is too long, generate a new one with only one line item.
329
-	 *
330
-	 * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
331
-	 *
332
-	 * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
333
-	 * @param array    $paypal_args Arguments sent to Paypal in the request.
334
-	 * @return array
335
-	 */
336
-	protected function fix_request_length( $invoice, $paypal_args ) {
337
-		$max_paypal_length = 2083;
338
-		$query_candidate   = http_build_query( $paypal_args, '', '&' );
339
-
340
-		if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
341
-			return $paypal_args;
342
-		}
343
-
344
-		return apply_filters(
345
-			'getpaid_paypal_args',
346
-			array_merge(
347
-				$this->get_transaction_args( $invoice ),
348
-				$this->get_line_item_args( $invoice, true )
349
-			),
350
-			$invoice
351
-		);
328
+     * If the default request with line items is too long, generate a new one with only one line item.
329
+     *
330
+     * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
331
+     *
332
+     * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
333
+     * @param array    $paypal_args Arguments sent to Paypal in the request.
334
+     * @return array
335
+     */
336
+    protected function fix_request_length( $invoice, $paypal_args ) {
337
+        $max_paypal_length = 2083;
338
+        $query_candidate   = http_build_query( $paypal_args, '', '&' );
339
+
340
+        if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
341
+            return $paypal_args;
342
+        }
343
+
344
+        return apply_filters(
345
+            'getpaid_paypal_args',
346
+            array_merge(
347
+                $this->get_transaction_args( $invoice ),
348
+                $this->get_line_item_args( $invoice, true )
349
+            ),
350
+            $invoice
351
+        );
352 352
 
353 353
     }
354 354
 
355 355
     /**
356
-	 * Processes recurring invoices.
357
-	 *
358
-	 * @param  array $paypal_args PayPal args.
359
-	 * @param  WPInv_Invoice    $invoice Invoice object.
360
-	 */
361
-	public function process_subscription( $paypal_args, $invoice ) {
356
+     * Processes recurring invoices.
357
+     *
358
+     * @param  array $paypal_args PayPal args.
359
+     * @param  WPInv_Invoice    $invoice Invoice object.
360
+     */
361
+    public function process_subscription( $paypal_args, $invoice ) {
362 362
 
363 363
         // Make sure this is a subscription.
364 364
         if ( ! $invoice->is_recurring() || ! $subscription = getpaid_get_invoice_subscription( $invoice ) ) {
@@ -383,11 +383,11 @@  discard block
 block discarded – undo
383 383
 
384 384
             $paypal_args['a1'] = 0 == $initial_amount ? 0 : $initial_amount;
385 385
 
386
-			// Trial period length.
387
-			$paypal_args['p1'] = $subscription_item->get_trial_interval();
386
+            // Trial period length.
387
+            $paypal_args['p1'] = $subscription_item->get_trial_interval();
388 388
 
389
-			// Trial period.
390
-			$paypal_args['t1'] = $subscription_item->get_trial_period();
389
+            // Trial period.
390
+            $paypal_args['t1'] = $subscription_item->get_trial_period();
391 391
 
392 392
         } else if ( $initial_amount != $recurring_amount ) {
393 393
 
@@ -410,40 +410,40 @@  discard block
 block discarded – undo
410 410
         }
411 411
 
412 412
         // We have a recurring payment
413
-		if ( ! isset( $param_number ) || 1 == $param_number ) {
413
+        if ( ! isset( $param_number ) || 1 == $param_number ) {
414 414
 
415
-			// Subscription price
416
-			$paypal_args['a3'] = $recurring_amount;
415
+            // Subscription price
416
+            $paypal_args['a3'] = $recurring_amount;
417 417
 
418
-			// Subscription duration
419
-			$paypal_args['p3'] = $interval;
418
+            // Subscription duration
419
+            $paypal_args['p3'] = $interval;
420 420
 
421
-			// Subscription period
422
-			$paypal_args['t3'] = $period;
421
+            // Subscription period
422
+            $paypal_args['t3'] = $period;
423 423
 
424 424
         }
425 425
 
426 426
         // Recurring payments
427
-		if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
427
+        if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
428 428
 
429
-			// Non-recurring payments
430
-			$paypal_args['src'] = 0;
429
+            // Non-recurring payments
430
+            $paypal_args['src'] = 0;
431 431
 
432
-		} else {
432
+        } else {
433 433
 
434
-			$paypal_args['src'] = 1;
434
+            $paypal_args['src'] = 1;
435 435
 
436
-			if ( $bill_times > 0 ) {
436
+            if ( $bill_times > 0 ) {
437 437
 
438
-				// An initial period is being used to charge a sign-up fee
439
-				if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
440
-					$bill_times--;
441
-				}
438
+                // An initial period is being used to charge a sign-up fee
439
+                if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
440
+                    $bill_times--;
441
+                }
442 442
 
443 443
                 // Make sure it's not over the max of 52
444 444
                 $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 );
445 445
 
446
-			}
446
+            }
447 447
         }
448 448
 
449 449
         // Force return URL so that order description & instructions display
@@ -459,19 +459,19 @@  discard block
 block discarded – undo
459 459
         }
460 460
 
461 461
         return apply_filters(
462
-			'getpaid_paypal_subscription_args',
463
-			$paypal_args,
464
-			$invoice
462
+            'getpaid_paypal_subscription_args',
463
+            $paypal_args,
464
+            $invoice
465 465
         );
466 466
 
467 467
     }
468 468
 
469 469
     /**
470
-	 * Processes ipns and marks payments as complete.
471
-	 *
472
-	 * @return void
473
-	 */
474
-	public function verify_ipn() {
470
+     * Processes ipns and marks payments as complete.
471
+     *
472
+     * @return void
473
+     */
474
+    public function verify_ipn() {
475 475
         new GetPaid_Paypal_Gateway_IPN_Handler( $this );
476 476
     }
477 477
 
@@ -481,19 +481,19 @@  discard block
 block discarded – undo
481 481
     public function sandbox_notice() {
482 482
 
483 483
         return sprintf(
484
-			__( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %sPayPal Sandbox Testing Guide%s for more details.', 'invoicing' ),
485
-			'<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
486
-			'</a>'
487
-		);
484
+            __( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %sPayPal Sandbox Testing Guide%s for more details.', 'invoicing' ),
485
+            '<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
486
+            '</a>'
487
+        );
488 488
 
489 489
     }
490 490
 
491
-	/**
492
-	 * Filters the gateway settings.
493
-	 *
494
-	 * @param array $admin_settings
495
-	 */
496
-	public function admin_settings( $admin_settings ) {
491
+    /**
492
+     * Filters the gateway settings.
493
+     *
494
+     * @param array $admin_settings
495
+     */
496
+    public function admin_settings( $admin_settings ) {
497 497
 
498 498
         $currencies = sprintf(
499 499
             __( 'Supported Currencies: %s', 'invoicing' ),
@@ -503,89 +503,89 @@  discard block
 block discarded – undo
503 503
         $admin_settings['paypal_active']['desc'] .= " ($currencies)";
504 504
         $admin_settings['paypal_desc']['std']     = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
505 505
 
506
-		// Access tokens.
506
+        // Access tokens.
507 507
         $live_account    = wpinv_get_option( 'paypal_live_access_token' );
508 508
         $sandbox_account = wpinv_get_option( 'paypal_test_access_token' );
509 509
 
510
-		$admin_settings['paypal_connect'] = array(
511
-			'type'       => 'raw_html',
512
-			'id'         => 'paypal_connect',
513
-			'name'       => __( 'Connect to PayPal', 'invoicing' ),
514
-			'desc'       => sprintf(
515
-				'<div class="wpinv-paypal-connect-live"><a class="button button-primary" href="%s">%s</a><br><strong style="color: green">%s</strong></div><div class="wpinv-paypal-connect-sandbox"><a class="button button-primary" href="%s">%s</a><br><strong style="color: green">%s</strong></div>%s',
516
-				esc_url( self::get_connect_url( false ) ),
517
-				__( 'Connect to PayPal', 'invoicing' ),
518
-				__( 'Connected', 'invoicing' ),
519
-				esc_url( self::get_connect_url( true ) ),
520
-				__( 'Connect to PayPal Sandox', 'invoicing' ),
521
-				__( 'Connected', 'invoicing' ),
522
-				$this->get_js()
523
-			),
524
-		);
525
-
526
-		$admin_settings['disable_paypal_connect'] = array(
527
-			'type'       => 'checkbox',
528
-			'id'         => 'disable_paypal_connect',
529
-			'name'       => __( 'Manual Mode', 'invoicing' ),
530
-			'desc'       => __( 'Manually enter your credentials', 'invoicing' ),
531
-			'std'        => false,
532
-		);
510
+        $admin_settings['paypal_connect'] = array(
511
+            'type'       => 'raw_html',
512
+            'id'         => 'paypal_connect',
513
+            'name'       => __( 'Connect to PayPal', 'invoicing' ),
514
+            'desc'       => sprintf(
515
+                '<div class="wpinv-paypal-connect-live"><a class="button button-primary" href="%s">%s</a><br><strong style="color: green">%s</strong></div><div class="wpinv-paypal-connect-sandbox"><a class="button button-primary" href="%s">%s</a><br><strong style="color: green">%s</strong></div>%s',
516
+                esc_url( self::get_connect_url( false ) ),
517
+                __( 'Connect to PayPal', 'invoicing' ),
518
+                __( 'Connected', 'invoicing' ),
519
+                esc_url( self::get_connect_url( true ) ),
520
+                __( 'Connect to PayPal Sandox', 'invoicing' ),
521
+                __( 'Connected', 'invoicing' ),
522
+                $this->get_js()
523
+            ),
524
+        );
525
+
526
+        $admin_settings['disable_paypal_connect'] = array(
527
+            'type'       => 'checkbox',
528
+            'id'         => 'disable_paypal_connect',
529
+            'name'       => __( 'Manual Mode', 'invoicing' ),
530
+            'desc'       => __( 'Manually enter your credentials', 'invoicing' ),
531
+            'std'        => false,
532
+        );
533 533
 
534 534
         $admin_settings['paypal_email'] = array(
535 535
             'type'  => 'text',
536
-			'class' => 'live-auth-data',
536
+            'class' => 'live-auth-data',
537 537
             'id'    => 'paypal_email',
538 538
             'name'  => __( 'Live Email Address', 'invoicing' ),
539 539
             'desc'  => __( 'The email address of your PayPal account.', 'invoicing' ),
540 540
         );
541 541
 
542
-		$admin_settings['paypal_merchant_id'] = array(
542
+        $admin_settings['paypal_merchant_id'] = array(
543 543
             'type'  => 'text',
544
-			'class' => 'live-auth-data',
544
+            'class' => 'live-auth-data',
545 545
             'id'    => 'paypal_merchant_id',
546 546
             'name'  => __( 'Live Merchant ID', 'invoicing' ),
547 547
         );
548 548
 
549
-		$admin_settings['paypal_client_id'] = array(
549
+        $admin_settings['paypal_client_id'] = array(
550 550
             'type'  => 'text',
551
-			'class' => 'live-auth-data',
551
+            'class' => 'live-auth-data',
552 552
             'id'    => 'paypal_client_id',
553 553
             'name'  => __( 'Live Client ID', 'invoicing' ),
554 554
         );
555 555
 
556
-		$admin_settings['paypal_client_secret'] = array(
556
+        $admin_settings['paypal_client_secret'] = array(
557 557
             'type'  => 'text',
558
-			'class' => 'live-auth-data',
558
+            'class' => 'live-auth-data',
559 559
             'id'    => 'paypal_client_secret',
560 560
             'name'  => __( 'Live Client Secret', 'invoicing' ),
561 561
         );
562 562
 
563
-		$admin_settings['paypal_sandbox_email'] = array(
563
+        $admin_settings['paypal_sandbox_email'] = array(
564 564
             'type'  => 'text',
565
-			'class' => 'sandbox-auth-data',
565
+            'class' => 'sandbox-auth-data',
566 566
             'id'    => 'paypal_sandbox_email',
567 567
             'name'  => __( 'Sandbox Email Address', 'invoicing' ),
568 568
             'desc'  => __( 'The email address of your sandbox PayPal account.', 'invoicing' ),
569
-			'std'   => wpinv_get_option( 'paypal_email', '' ),
569
+            'std'   => wpinv_get_option( 'paypal_email', '' ),
570 570
         );
571 571
 
572
-		$admin_settings['paypal_sandbox_merchant_id'] = array(
572
+        $admin_settings['paypal_sandbox_merchant_id'] = array(
573 573
             'type'  => 'text',
574
-			'class' => 'sandbox-auth-data',
574
+            'class' => 'sandbox-auth-data',
575 575
             'id'    => 'paypal_sandbox_merchant_id',
576 576
             'name'  => __( 'Sandbox Merchant ID', 'invoicing' ),
577 577
         );
578 578
 
579
-		$admin_settings['paypal_sandbox_client_id'] = array(
579
+        $admin_settings['paypal_sandbox_client_id'] = array(
580 580
             'type'  => 'text',
581
-			'class' => 'sandbox-auth-data',
581
+            'class' => 'sandbox-auth-data',
582 582
             'id'    => 'paypal_sandbox_client_id',
583 583
             'name'  => __( 'Sandbox Client ID', 'invoicing' ),
584 584
         );
585 585
 
586
-		$admin_settings['paypal_sandbox_client_secret'] = array(
586
+        $admin_settings['paypal_sandbox_client_secret'] = array(
587 587
             'type'  => 'text',
588
-			'class' => 'sandbox-auth-data',
588
+            'class' => 'sandbox-auth-data',
589 589
             'id'    => 'paypal_sandbox_client_secret',
590 590
             'name'  => __( 'Sandbox Client Secret', 'invoicing' ),
591 591
         );
@@ -599,17 +599,17 @@  discard block
 block discarded – undo
599 599
             'readonly' => true,
600 600
         );
601 601
 
602
-		return $admin_settings;
603
-	}
602
+        return $admin_settings;
603
+    }
604 604
 
605
-	/**
606
-	 * Retrieves the PayPal connect URL.
607
-	 *
608
-	 *
605
+    /**
606
+     * Retrieves the PayPal connect URL.
607
+     *
608
+     *
609 609
      * @param bool $is_sandbox
610 610
      * @return string
611
-	 */
612
-	public static function get_connect_url( $is_sandbox ) {
611
+     */
612
+    public static function get_connect_url( $is_sandbox ) {
613 613
 
614 614
         $redirect_url = add_query_arg(
615 615
             array(
@@ -633,12 +633,12 @@  discard block
 block discarded – undo
633 633
 
634 634
     }
635 635
 
636
-	/**
637
-	 * Generates settings page js.
638
-	 *
636
+    /**
637
+     * Generates settings page js.
638
+     *
639 639
      * @return void
640
-	 */
641
-	public static function get_js() {
640
+     */
641
+    public static function get_js() {
642 642
         ob_start();
643 643
         ?>
644 644
             <script>
@@ -684,103 +684,103 @@  discard block
 block discarded – undo
684 684
         return ob_get_clean();
685 685
     }
686 686
 
687
-	/**
688
-	 * Connects to PayPal.
689
-	 *
690
-	 * @param array $data Connection data.
691
-	 * @return void
692
-	 */
693
-	public function connect_paypal( $data ) {
694
-
695
-		$sandbox      = $this->is_sandbox();
696
-		$data         = wp_unslash( $data );
697
-		$access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
698
-
699
-		if ( isset( $data['live_mode'] ) ) {
700
-			$sandbox = empty( $data['live_mode'] );
701
-		}
702
-
703
-		wpinv_update_option( 'stripe_sandbox', (int) $sandbox );
704
-		wpinv_update_option( 'stripe_active', 1 );
705
-
706
-		if ( ! empty( $data['error_description'] ) ) {
707
-			getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
708
-		} else {
709
-
710
-			// Retrieve the user info.
711
-			$user_info = wp_remote_get(
712
-				! $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',
713
-				array(
714
-
715
-					'headers' => array(
716
-						'Authorization' => 'Bearer ' . $access_token,
717
-						'Content-type'  => 'application/json',
718
-					)
719
-
720
-				)
721
-			);
722
-
723
-			if ( is_wp_error( $user_info ) ) {
724
-				getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
725
-			} else {
726
-
727
-				// Create application.
728
-				$user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
729
-				$app       = wp_remote_post(
730
-					! $sandbox ? 'https://api-m.paypal.com/v1/identity/applications' : 'https://api-m.sandbox.paypal.com/v1/identity/applications',
731
-					array(
732
-
733
-						'body'    => array(
734
-							'application_type' => 'web',
735
-							'redirect_uris'    => array(
736
-								add_query_arg( 'getpaid_oauth', 'paypal', home_url() ),
737
-							),
738
-							'client_name'      => 'GetPaid',
739
-							'contacts'         => array( $user_info->emails[0]->value ),
740
-							'payer_id'         => $user_info->payer_id,
741
-							'migrated_app'     => '',
742
-						),
743
-						'headers' => array(
744
-							'Authorization' => 'Bearer ' . $access_token,
745
-							'Content-type'  => 'application/json',
746
-						)
747
-
748
-					)
749
-				);
750
-
751
-				if ( is_wp_error( $app ) ) {
752
-					getpaid_admin()->show_error( wp_kses_post( $app->get_error_message() ) );
753
-				} else {
754
-
755
-					$app = json_decode( $app );
756
-					if ( $sandbox ) {
757
-						wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
758
-						wpinv_update_option( 'paypal_sandbox_merchant_id', '' );
759
-						wpinv_update_option( 'paypal_sandbox_client_id', sanitize_text_field( $app->client_id ) );
760
-						wpinv_update_option( 'paypal_sandbox_client_secret', sanitize_text_field( $app->client_secret ) );
761
-						wpinv_update_option( 'paypal_sandbox_client_secret_expires_at', sanitize_text_field( $app->client_secret_expires_at ) );
762
-						wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
763
-						set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
764
-						getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'wpinv-stripe' ) );
765
-					} else {
766
-						wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
767
-						wpinv_update_option( 'paypal_merchant_id', '' );
768
-						wpinv_update_option( 'paypal_client_id', sanitize_text_field( $app->client_id ) );
769
-						wpinv_update_option( 'paypal_client_secret', sanitize_text_field( $app->client_secret ) );
770
-						wpinv_update_option( 'paypal_client_secret_expires_at', sanitize_text_field( $app->client_secret_expires_at ) );
771
-						wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
772
-						set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
773
-						getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'wpinv-stripe' ) );
774
-					}
687
+    /**
688
+     * Connects to PayPal.
689
+     *
690
+     * @param array $data Connection data.
691
+     * @return void
692
+     */
693
+    public function connect_paypal( $data ) {
694
+
695
+        $sandbox      = $this->is_sandbox();
696
+        $data         = wp_unslash( $data );
697
+        $access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
698
+
699
+        if ( isset( $data['live_mode'] ) ) {
700
+            $sandbox = empty( $data['live_mode'] );
701
+        }
702
+
703
+        wpinv_update_option( 'stripe_sandbox', (int) $sandbox );
704
+        wpinv_update_option( 'stripe_active', 1 );
705
+
706
+        if ( ! empty( $data['error_description'] ) ) {
707
+            getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
708
+        } else {
709
+
710
+            // Retrieve the user info.
711
+            $user_info = wp_remote_get(
712
+                ! $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',
713
+                array(
714
+
715
+                    'headers' => array(
716
+                        'Authorization' => 'Bearer ' . $access_token,
717
+                        'Content-type'  => 'application/json',
718
+                    )
719
+
720
+                )
721
+            );
722
+
723
+            if ( is_wp_error( $user_info ) ) {
724
+                getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
725
+            } else {
775 726
 
776
-				}
727
+                // Create application.
728
+                $user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
729
+                $app       = wp_remote_post(
730
+                    ! $sandbox ? 'https://api-m.paypal.com/v1/identity/applications' : 'https://api-m.sandbox.paypal.com/v1/identity/applications',
731
+                    array(
732
+
733
+                        'body'    => array(
734
+                            'application_type' => 'web',
735
+                            'redirect_uris'    => array(
736
+                                add_query_arg( 'getpaid_oauth', 'paypal', home_url() ),
737
+                            ),
738
+                            'client_name'      => 'GetPaid',
739
+                            'contacts'         => array( $user_info->emails[0]->value ),
740
+                            'payer_id'         => $user_info->payer_id,
741
+                            'migrated_app'     => '',
742
+                        ),
743
+                        'headers' => array(
744
+                            'Authorization' => 'Bearer ' . $access_token,
745
+                            'Content-type'  => 'application/json',
746
+                        )
747
+
748
+                    )
749
+                );
750
+
751
+                if ( is_wp_error( $app ) ) {
752
+                    getpaid_admin()->show_error( wp_kses_post( $app->get_error_message() ) );
753
+                } else {
754
+
755
+                    $app = json_decode( $app );
756
+                    if ( $sandbox ) {
757
+                        wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
758
+                        wpinv_update_option( 'paypal_sandbox_merchant_id', '' );
759
+                        wpinv_update_option( 'paypal_sandbox_client_id', sanitize_text_field( $app->client_id ) );
760
+                        wpinv_update_option( 'paypal_sandbox_client_secret', sanitize_text_field( $app->client_secret ) );
761
+                        wpinv_update_option( 'paypal_sandbox_client_secret_expires_at', sanitize_text_field( $app->client_secret_expires_at ) );
762
+                        wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
763
+                        set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
764
+                        getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'wpinv-stripe' ) );
765
+                    } else {
766
+                        wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
767
+                        wpinv_update_option( 'paypal_merchant_id', '' );
768
+                        wpinv_update_option( 'paypal_client_id', sanitize_text_field( $app->client_id ) );
769
+                        wpinv_update_option( 'paypal_client_secret', sanitize_text_field( $app->client_secret ) );
770
+                        wpinv_update_option( 'paypal_client_secret_expires_at', sanitize_text_field( $app->client_secret_expires_at ) );
771
+                        wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
772
+                        set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
773
+                        getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'wpinv-stripe' ) );
774
+                    }
775
+
776
+                }
777 777
 
778
-			}
778
+            }
779 779
 
780
-		}
780
+        }
781 781
 
782
-		wp_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) );
783
-		exit;
784
-	}
782
+        wp_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) );
783
+        exit;
784
+    }
785 785
 
786 786
 }
Please login to merge, or discard this patch.