Passed
Push — master ( 6e3436...9f811d )
by Brian
04:49
created

GetPaid_Paypal_Gateway   F

Complexity

Total Complexity 65

Size/Duplication

Total Lines 694
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 2
Metric Value
eloc 255
c 7
b 1
f 2
dl 0
loc 694
rs 3.2
wmc 65

20 Methods

Rating   Name   Duplication   Size   Complexity  
A get_paypal_args() 0 19 4
A get_request_url() 0 15 3
A process_payment() 0 19 1
A get_line_items() 0 2 1
A fix_request_length() 0 15 2
A get_line_item_args_single_item() 0 7 1
A get_transaction_args() 0 23 3
A prepare_line_items() 0 13 4
A delete_line_items() 0 2 1
A add_line_item() 0 23 2
A get_line_item_args() 0 24 5
F process_subscription() 0 109 22
A sandbox_notice() 0 6 1
A verify_ipn() 0 2 1
A get_connect_url() 0 21 1
B connect_paypal() 0 58 9
A __construct() 0 13 1
A display_connect_buttons() 0 8 1
A maybe_get_connect_url() 0 2 1
A admin_settings() 0 47 1

How to fix   Complexity   

Complex Class

Complex classes like GetPaid_Paypal_Gateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GetPaid_Paypal_Gateway, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Paypal payment gateway
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Paypal Payment Gateway class.
11
 *
12
 */
13
class GetPaid_Paypal_Gateway extends GetPaid_Payment_Gateway {
14
15
    /**
16
	 * Payment method id.
17
	 *
18
	 * @var string
19
	 */
20
    public $id = 'paypal';
21
22
    /**
23
	 * An array of features that this gateway supports.
24
	 *
25
	 * @var array
26
	 */
27
    protected $supports = array( 'subscription', 'sandbox', 'single_subscription_group' );
28
29
    /**
30
	 * Payment method order.
31
	 *
32
	 * @var int
33
	 */
34
    public $order = 1;
35
36
    /**
37
	 * Stores line items to send to PayPal.
38
	 *
39
	 * @var array
40
	 */
41
    protected $line_items = array();
42
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
	 */
55
    protected $endpoint;
56
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' );
63
64
    /**
65
	 * URL to view a transaction.
66
	 *
67
	 * @var string
68
	 */
69
    public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s';
70
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';
77
78
    /**
79
	 * Class constructor.
80
	 */
81
	public function __construct() {
82
83
        $this->title                = __( 'PayPal Standard', 'invoicing' );
84
        $this->method_title         = __( 'PayPal Standard', 'invoicing' );
85
        $this->checkout_button_text = __( 'Proceed to PayPal', 'invoicing' );
86
        $this->notify_url           = wpinv_get_ipn_url( $this->id );
87
88
		add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 );
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' ) );
92
		add_action( 'wpinv_paypal_connect', array( $this, 'display_connect_buttons' ) );
93
		parent::__construct();
94
    }
95
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 ) {
106
107
        // Get redirect url.
108
        $paypal_redirect = $this->get_request_url( $invoice );
109
110
        // Add a note about the request url.
111
        $invoice->add_note(
112
            sprintf(
113
                __( 'Redirecting to PayPal: %s', 'invoicing' ),
114
                esc_url( $paypal_redirect )
115
            ),
116
            false,
117
            false,
118
            true
119
        );
120
121
        // Redirect to PayPal
122
        wp_redirect( $paypal_redirect );
123
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
124
125
    }
126
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 ) {
134
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?';
137
138
        // Retrieve paypal args.
139
        $paypal_args       = map_deep( $this->get_paypal_args( $invoice ), 'urlencode' );
140
141
        if ( $invoice->is_recurring() ) {
142
            $paypal_args['bn'] = 'GetPaid_Subscribe_WPS_US';
143
        } else {
144
            $paypal_args['bn'] = 'GetPaid_ShoppingCart_WPS_US';
145
        }
146
147
        return add_query_arg( $paypal_args, $this->endpoint );
148
149
	}
150
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 ) {
158
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 );
176
    }
177
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
		$email = $this->is_sandbox( $invoice ) ? wpinv_get_option( 'paypal_sandbox_email', wpinv_get_option( 'paypal_email', '' ) ) : wpinv_get_option( 'paypal_email', '' );
187
		return array(
188
            'cmd'           => '_cart',
189
            'business'      => $email,
190
            'no_shipping'   => '1',
191
            'shipping'      => '0',
192
            'no_note'       => '1',
193
            'charset'       => 'utf-8',
194
            'rm'            => is_ssl() ? 2 : 1,
195
            'upload'        => 1,
196
            'currency_code' => $invoice->get_currency(), // https://developer.paypal.com/docs/nvp-soap-api/currency-codes/#paypal
197
            'return'        => esc_url_raw( $this->get_return_url( $invoice ) ),
198
            'cancel_return' => esc_url_raw( $invoice->get_checkout_payment_url() ),
199
            'notify_url'    => getpaid_limit_length( $this->notify_url, 255 ),
200
            'invoice'       => getpaid_limit_length( $invoice->get_number(), 127 ),
201
            'custom'        => $invoice->get_id(),
202
            'first_name'    => getpaid_limit_length( $invoice->get_first_name(), 32 ),
203
            'last_name'     => getpaid_limit_length( $invoice->get_last_name(), 64 ),
204
            'country'       => getpaid_limit_length( $invoice->get_country(), 2 ),
205
            'email'         => getpaid_limit_length( $invoice->get_email(), 127 ),
206
            'cbt'           => get_bloginfo( 'name' ),
207
        );
208
209
    }
210
211
    /**
212
	 * Get line item args for paypal request.
213
	 *
214
	 * @param  WPInv_Invoice $invoice Invoice object.
215
	 * @param  bool     $force_one_line_item Create only one item for this invoice.
216
	 * @return array
217
	 */
218
	protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
219
220
        // Maybe send invoice as a single item.
221
		if ( $force_one_line_item ) {
222
            return $this->get_line_item_args_single_item( $invoice );
223
        }
224
225
        // Send each line item individually.
226
        $line_item_args = array();
227
228
        // Prepare line items.
229
        $this->prepare_line_items( $invoice );
230
231
        // Add taxes to the cart
232
        if ( wpinv_use_taxes() && $invoice->is_taxable() ) {
233
            $line_item_args['tax_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_tax(), 2 );
0 ignored issues
show
Unused Code introduced by
The call to wpinv_sanitize_amount() has too many arguments starting with 2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

233
            $line_item_args['tax_cart'] = /** @scrutinizer ignore-call */ wpinv_sanitize_amount( (float) $invoice->get_total_tax(), 2 );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
234
        }
235
236
        // Add discount.
237
        if ( $invoice->get_total_discount() > 0 ) {
238
            $line_item_args['discount_amount_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_discount(), 2 );
239
        }
240
241
		return array_merge( $line_item_args, $this->get_line_items() );
242
243
    }
244
245
    /**
246
	 * Get line item args for paypal request as a single line item.
247
	 *
248
	 * @param  WPInv_Invoice $invoice Invoice object.
249
	 * @return array
250
	 */
251
	protected function get_line_item_args_single_item( $invoice ) {
252
		$this->delete_line_items();
253
254
        $item_name = sprintf( __( 'Invoice #%s', 'invoicing' ), $invoice->get_number() );
255
		$this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
0 ignored issues
show
Bug introduced by
It seems like wpinv_round_amount((doub...->get_total(), 2, true) can also be of type string; however, parameter $amount of GetPaid_Paypal_Gateway::add_line_item() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

255
		$this->add_line_item( $item_name, 1, /** @scrutinizer ignore-type */ wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
Loading history...
256
257
		return $this->get_line_items();
258
    }
259
260
    /**
261
	 * Return all line items.
262
	 */
263
	protected function get_line_items() {
264
		return $this->line_items;
265
	}
266
267
    /**
268
	 * Remove all line items.
269
	 */
270
	protected function delete_line_items() {
271
		$this->line_items = array();
272
    }
273
274
    /**
275
	 * Prepare line items to send to paypal.
276
	 *
277
	 * @param  WPInv_Invoice $invoice Invoice object.
278
	 */
279
	protected function prepare_line_items( $invoice ) {
280
		$this->delete_line_items();
281
282
		// Items.
283
		foreach ( $invoice->get_items() as $item ) {
284
			$amount   = $item->get_price();
285
			$quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
286
			$this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
287
        }
288
289
        // Fees.
290
		foreach ( $invoice->get_fees() as $fee => $data ) {
291
            $this->add_line_item( $fee, 1, wpinv_sanitize_amount( $data['initial_fee'] ) );
292
        }
293
294
    }
295
296
    /**
297
	 * Add PayPal Line Item.
298
	 *
299
	 * @param  string $item_name Item name.
300
	 * @param  float    $quantity Item quantity.
301
	 * @param  float  $amount Amount.
302
	 * @param  string $item_number Item number.
303
	 */
304
	protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
305
		$index = ( count( $this->line_items ) / 4 ) + 1;
306
307
		$item = apply_filters(
308
			'getpaid_paypal_line_item',
309
			array(
310
				'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
311
				'quantity'    => (float) $quantity,
312
				'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
0 ignored issues
show
Unused Code introduced by
The call to wpinv_sanitize_amount() has too many arguments starting with 2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

312
				'amount'      => /** @scrutinizer ignore-call */ wpinv_sanitize_amount( (float) $amount, 2 ),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
313
				'item_number' => $item_number,
314
			),
315
			$item_name,
316
			$quantity,
317
			$amount,
318
			$item_number
319
		);
320
321
		$this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
322
        $this->line_items[ 'quantity_' . $index ]    = $item['quantity'];
323
324
        // The price or amount of the product, service, or contribution, not including shipping, handling, or tax.
325
		$this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
326
		$this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
327
    }
328
329
    /**
330
	 * If the default request with line items is too long, generate a new one with only one line item.
331
	 *
332
	 * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
333
	 *
334
	 * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
335
	 * @param array    $paypal_args Arguments sent to Paypal in the request.
336
	 * @return array
337
	 */
338
	protected function fix_request_length( $invoice, $paypal_args ) {
339
		$max_paypal_length = 2083;
340
		$query_candidate   = http_build_query( $paypal_args, '', '&' );
341
342
		if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
343
			return $paypal_args;
344
		}
345
346
		return apply_filters(
347
			'getpaid_paypal_args',
348
			array_merge(
349
				$this->get_transaction_args( $invoice ),
350
				$this->get_line_item_args( $invoice, true )
351
			),
352
			$invoice
353
		);
354
355
    }
356
357
    /**
358
	 * Processes recurring invoices.
359
	 *
360
	 * @param  array $paypal_args PayPal args.
361
	 * @param  WPInv_Invoice    $invoice Invoice object.
362
	 */
363
	public function process_subscription( $paypal_args, $invoice ) {
364
365
        // Make sure this is a subscription.
366
        if ( ! $invoice->is_recurring() || ! $subscription = getpaid_get_invoice_subscription( $invoice ) ) {
367
            return $paypal_args;
368
        }
369
370
        // It's a subscription
371
        $paypal_args['cmd'] = '_xclick-subscriptions';
372
373
        // Subscription name.
374
        $paypal_args['item_name'] = sprintf( __( 'Invoice #%s', 'invoicing' ), $invoice->get_number() );
375
376
        // Get subscription args.
377
        $period                 = strtoupper( substr( $subscription->get_period(), 0, 1 ) );
378
        $interval               = (int) $subscription->get_frequency();
379
        $bill_times             = (int) $subscription->get_bill_times();
380
        $initial_amount         = (float) wpinv_sanitize_amount( $invoice->get_initial_total(), 2 );
0 ignored issues
show
Unused Code introduced by
The call to wpinv_sanitize_amount() has too many arguments starting with 2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

380
        $initial_amount         = (float) /** @scrutinizer ignore-call */ wpinv_sanitize_amount( $invoice->get_initial_total(), 2 );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
381
        $recurring_amount       = (float) wpinv_sanitize_amount( $invoice->get_recurring_total(), 2 );
382
        $subscription_item      = $invoice->get_recurring( true );
383
384
		// Convert 365 days to 1 year.
385
		if ( 'D' == $period && 365 == $interval ) {
386
			$period = 'Y';
387
			$interval = 1;
388
		}
389
390
        if ( $subscription_item->has_free_trial() ) {
391
392
            $paypal_args['a1'] = 0 == $initial_amount ? 0 : $initial_amount;
393
394
			// Trial period length.
395
			$paypal_args['p1'] = $subscription_item->get_trial_interval();
396
397
			// Trial period.
398
			$paypal_args['t1'] = $subscription_item->get_trial_period();
399
400
        } elseif ( $initial_amount != $recurring_amount ) {
401
402
            // No trial period, but initial amount includes a sign-up fee and/or other items, so charge it as a separate period.
403
404
            if ( 1 == $bill_times ) {
405
                $param_number = 3;
406
            } else {
407
                $param_number = 1;
408
            }
409
410
            $paypal_args[ 'a' . $param_number ] = $initial_amount ? $initial_amount : 0;
411
412
            // Sign Up interval
413
            $paypal_args[ 'p' . $param_number ] = $interval;
414
415
            // Sign Up unit of duration
416
            $paypal_args[ 't' . $param_number ] = $period;
417
418
        }
419
420
        // We have a recurring payment
421
		if ( ! isset( $param_number ) || 1 == $param_number ) {
422
423
			// Subscription price
424
			$paypal_args['a3'] = $recurring_amount;
425
426
			// Subscription duration
427
			$paypal_args['p3'] = $interval;
428
429
			// Subscription period
430
			$paypal_args['t3'] = $period;
431
432
        }
433
434
        // Recurring payments
435
		if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
436
437
			// Non-recurring payments
438
			$paypal_args['src'] = 0;
439
440
		} else {
441
442
			$paypal_args['src'] = 1;
443
444
			if ( $bill_times > 0 ) {
445
446
				// An initial period is being used to charge a sign-up fee
447
				if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
448
					$bill_times--;
449
				}
450
451
                // Make sure it's not over the max of 52
452
                $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 );
453
454
			}
455
        }
456
457
        // Force return URL so that order description & instructions display
458
        $paypal_args['rm'] = 2;
459
460
        // Get rid of redudant items.
461
        foreach ( array( 'item_name_1', 'quantity_1', 'amount_1', 'item_number_1' ) as $arg ) {
462
463
            if ( isset( $paypal_args[ $arg ] ) ) {
464
                unset( $paypal_args[ $arg ] );
465
            }
466
}
467
468
        return apply_filters(
469
			'getpaid_paypal_subscription_args',
470
			$paypal_args,
471
			$invoice
472
        );
473
474
    }
475
476
    /**
477
	 * Processes ipns and marks payments as complete.
478
	 *
479
	 * @return void
480
	 */
481
	public function verify_ipn() {
482
        new GetPaid_Paypal_Gateway_IPN_Handler( $this );
483
    }
484
485
    /**
486
     * Returns a sandbox notice.
487
     */
488
    public function sandbox_notice() {
489
490
        return sprintf(
491
			__( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %1$sPayPal Sandbox Testing Guide%2$s for more details.', 'invoicing' ),
492
			'<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
493
			'</a>'
494
		);
495
496
    }
497
498
	/**
499
	 * Filters the gateway settings.
500
	 *
501
	 * @param array $admin_settings
502
	 */
503
	public function admin_settings( $admin_settings ) {
504
505
        $currencies = sprintf(
506
            __( 'Supported Currencies: %s', 'invoicing' ),
507
            implode( ', ', $this->currencies )
508
        );
509
510
        $admin_settings['paypal_active']['desc'] .= " ($currencies)";
511
        $admin_settings['paypal_desc']['std']     = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
512
513
		// Access tokens.
514
		$live_email      = wpinv_get_option( 'paypal_email' );
0 ignored issues
show
Unused Code introduced by
The assignment to $live_email is dead and can be removed.
Loading history...
515
		$sandbox_email   = wpinv_get_option( 'paypal_sandbox_email' );
0 ignored issues
show
Unused Code introduced by
The assignment to $sandbox_email is dead and can be removed.
Loading history...
516
517
		$admin_settings['paypal_connect'] = array(
518
			'type' => 'hook',
519
			'id'   => 'paypal_connect',
520
			'name' => __( 'Connect to PayPal', 'invoicing' ),
521
		);
522
523
        $admin_settings['paypal_email'] = array(
524
            'type'  => 'text',
525
			'class' => 'live-auth-data',
526
            'id'    => 'paypal_email',
527
            'name'  => __( 'Live Email Address', 'invoicing' ),
528
            'desc'  => __( 'The email address of your PayPal account.', 'invoicing' ),
529
        );
530
531
		$admin_settings['paypal_sandbox_email'] = array(
532
            'type'  => 'text',
533
			'class' => 'sandbox-auth-data',
534
            'id'    => 'paypal_sandbox_email',
535
            'name'  => __( 'Sandbox Email Address', 'invoicing' ),
536
            'desc'  => __( 'The email address of your sandbox PayPal account.', 'invoicing' ),
537
			'std'   => wpinv_get_option( 'paypal_email', '' ),
538
        );
539
540
        $admin_settings['paypal_ipn_url'] = array(
541
            'type'     => 'ipn_url',
542
            'id'       => 'paypal_ipn_url',
543
            'name'     => __( 'IPN Url', 'invoicing' ),
544
            'std'      => $this->notify_url,
545
            'desc'     => __( "If you've not enabled IPNs in your paypal account, use the above URL to enable them.", 'invoicing' ) . ' <a href="https://developer.paypal.com/docs/api-basics/notifications/ipn/"><em>' . __( 'Learn more.', 'invoicing' ) . '</em></a>',
546
            'readonly' => true,
547
        );
548
549
		return $admin_settings;
550
	}
551
552
	/**
553
	 * Retrieves the PayPal connect URL when using the setup wizzard.
554
	 *
555
	 *
556
     * @param array $data
557
     * @return string
558
	 */
559
	public static function maybe_get_connect_url( $url = '', $data = array() ) {
560
		return self::get_connect_url( false, urldecode( $data['redirect'] ) );
561
	}
562
563
	/**
564
	 * Retrieves the PayPal connect URL.
565
	 *
566
	 *
567
     * @param bool $is_sandbox
568
	 * @param string $redirect
569
     * @return string
570
	 */
571
	public static function get_connect_url( $is_sandbox, $redirect = '' ) {
572
573
        $redirect_url = add_query_arg(
574
            array(
575
                'getpaid-admin-action' => 'connect_paypal',
576
                'page'                 => 'wpinv-settings',
577
                'live_mode'            => (int) empty( $is_sandbox ),
578
                'tab'                  => 'gateways',
579
                'section'              => 'paypal',
580
                'getpaid-nonce'        => wp_create_nonce( 'getpaid-nonce' ),
581
				'redirect'             => urlencode( $redirect ),
582
            ),
583
            admin_url( 'admin.php' )
584
        );
585
586
        return add_query_arg(
587
            array(
588
                'live_mode'    => (int) empty( $is_sandbox ),
589
                'redirect_url' => urlencode( str_replace( '&amp;', '&', $redirect_url ) ),
590
            ),
591
            'https://ayecode.io/oauth/paypal'
592
        );
593
594
    }
595
596
	/**
597
	 * Generates settings page js.
598
	 *
599
     * @return void
600
	 */
601
	public static function display_connect_buttons() {
602
603
        ?>
604
			<div class="wpinv-paypal-connect-live">
605
				<a class="button button-primary" href="<?php echo esc_url( self::get_connect_url( false ) ); ?>"><?php esc_html_e( 'Connect to PayPal', 'invoicing' ); ?></a>
606
			</div>
607
			<div class="wpinv-paypal-connect-sandbox">
608
				<a class="button button-primary" href="<?php echo esc_url( self::get_connect_url( true ) ); ?>"><?php esc_html_e( 'Connect to PayPal Sandbox', 'invoicing' ); ?></a>
609
			</div>
610
611
            <script>
612
                jQuery(document).ready(function() {
613
614
                    jQuery( '#wpinv-settings-paypal_sandbox' ).on ( 'change', function( e ) {
615
616
						jQuery( '.wpinv-paypal-connect-live, .live-auth-data' ).toggle( ! this.checked )
617
						jQuery( '.wpinv-paypal-connect-sandbox, .sandbox-auth-data' ).toggle( this.checked )
618
619
						if ( this.checked ) {
620
621
							if ( jQuery('#wpinv-settings-paypal_sandbox_email').val().length > 0 ) {
622
								jQuery('.wpinv-paypal-connect-sandbox').closest('tr').hide()
623
							} else {
624
								jQuery('.wpinv-paypal-connect-sandbox').closest('tr').show()
625
							}
626
						} else {
627
							if ( jQuery('#wpinv-settings-paypal_email').val().length > 0 ) {
628
								jQuery('.wpinv-paypal-connect-live').closest('tr').hide()
629
							} else {
630
								jQuery('.wpinv-paypal-connect-live').closest('tr').show()
631
							}
632
						}
633
                    })
634
635
                    // Set initial state.
636
                    jQuery( '#wpinv-settings-paypal_sandbox' ).trigger( 'change' )
637
638
                });
639
            </script>
640
        <?php
641
    }
642
643
	/**
644
	 * Connects to PayPal.
645
	 *
646
	 * @param array $data Connection data.
647
	 * @return void
648
	 */
649
	public function connect_paypal( $data ) {
650
651
		$sandbox      = $this->is_sandbox();
652
		$data         = wp_unslash( $data );
653
		$access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
654
655
		if ( isset( $data['live_mode'] ) ) {
656
			$sandbox = empty( $data['live_mode'] );
657
		}
658
659
		wpinv_update_option( 'paypal_sandbox', (int) $sandbox );
660
		wpinv_update_option( 'paypal_active', 1 );
661
662
		if ( ! empty( $data['error_description'] ) ) {
663
			getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
664
		} else {
665
666
			// Retrieve the user info.
667
			$user_info = wp_remote_get(
668
				! $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',
669
				array(
670
671
					'headers' => array(
672
						'Authorization' => 'Bearer ' . $access_token,
673
						'Content-type'  => 'application/json',
674
					),
675
676
				)
677
			);
678
679
			if ( is_wp_error( $user_info ) ) {
680
				getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
681
			} else {
682
683
				// Create application.
684
				$user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
685
686
				if ( $sandbox ) {
687
					wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
688
					wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
689
					set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
690
					getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) );
691
				} else {
692
					wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
693
					wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
694
					set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
695
					getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) );
696
				}
697
}
698
}
699
700
		$redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) : urldecode( $data['redirect'] );
701
702
		if ( isset( $data['step'] ) ) {
703
			$redirect = add_query_arg( 'step', $data['step'], $redirect );
704
		}
705
		wp_redirect( $redirect );
706
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
707
	}
708
709
}
710