Passed
Pull Request — master (#388)
by Brian
08:17 queued 03:36
created

GetPaid_Payment_Gateway::get_option()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Abstract payment gateway
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Abstaract Payment Gateway class.
11
 *
12
 * Extended by individual payment gateways to handle payments.
13
 */
14
abstract class GetPaid_Payment_Gateway {
15
16
	/**
17
	 * Set if the place checkout button should be renamed on selection.
18
	 *
19
	 * @var string
20
	 */
21
	public $checkout_button_text;
22
23
	/**
24
	 * Boolean whether the method is enabled.
25
	 *
26
	 * @var bool
27
	 */
28
	public $enabled = true;
29
30
	/**
31
	 * Payment method id.
32
	 *
33
	 * @var string
34
	 */
35
	public $id;
36
37
	/**
38
	 * Payment method order.
39
	 *
40
	 * @var int
41
	 */
42
	public $order = 10;
43
44
	/**
45
	 * Payment method title for the frontend.
46
	 *
47
	 * @var string
48
	 */
49
	public $title;
50
51
	/**
52
	 * Payment method description for the frontend.
53
	 *
54
	 * @var string
55
	 */
56
	public $description;
57
58
	/**
59
	 * Gateway title.
60
	 *
61
	 * @var string
62
	 */
63
	public $method_title = '';
64
65
	/**
66
	 * Gateway description.
67
	 *
68
	 * @var string
69
	 */
70
	public $method_description = '';
71
72
	/**
73
	 * Countries this gateway is allowed for.
74
	 *
75
	 * @var array
76
	 */
77
	public $countries;
78
79
	/**
80
	 * Currencies this gateway is allowed for.
81
	 *
82
	 * @var array
83
	 */
84
	public $currencies;
85
86
	/**
87
	 * Currencies this gateway is not allowed for.
88
	 *
89
	 * @var array
90
	 */
91
	public $exclude_currencies;
92
93
	/**
94
	 * Maximum transaction amount, zero does not define a maximum.
95
	 *
96
	 * @var int
97
	 */
98
	public $max_amount = 0;
99
100
	/**
101
	 * Optional URL to view a transaction.
102
	 *
103
	 * @var string
104
	 */
105
	public $view_transaction_url = '';
106
107
	/**
108
	 * Optional URL to view a subscription.
109
	 *
110
	 * @var string
111
	 */
112
	public $view_subscription_url = '';
113
114
	/**
115
	 * Optional label to show for "new payment method" in the payment
116
	 * method/token selection radio selection.
117
	 *
118
	 * @var string
119
	 */
120
	public $new_method_label = '';
121
122
	/**
123
	 * Contains a users saved tokens for this gateway.
124
	 *
125
	 * @var array
126
	 */
127
	protected $tokens = array();
128
129
	/**
130
	 * An array of features that this gateway supports.
131
	 *
132
	 * @var array
133
	 */
134
	protected $supports = array();
135
136
	/**
137
	 * Class constructor.
138
	 */
139
	public function __construct() {
140
		$this->enabled = wpinv_is_gateway_active( $this->id );
141
142
		// Register gateway.
143
		add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) );
144
145
		// Enable Subscriptions.
146
		if ( $this->supports( 'subscription' ) ) {
147
			add_filter( "wpinv_{$this->id}_support_subscription", '__return_true' );
148
		}
149
150
		// Enable sandbox.
151
		if ( $this->supports( 'sandbox' ) ) {
152
			add_filter( "wpinv_{$this->id}_supports_sandbox", '__return_true' );
153
		}
154
155
		// Gateway settings.
156
		add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) );
157
		
158
159
		// Gateway checkout fiellds.
160
		add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 );
161
162
		// Process payment.
163
		add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 );
164
165
		// Change the checkout button text.
166
		if ( ! empty( $this->checkout_button_text ) ) {
167
			add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) );
168
		}
169
170
		// Check if a gateway is valid for a given currency.
171
		add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 );
172
173
		// Generate the transaction url.
174
		add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 );
175
176
		// Generate the subscription url.
177
		add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 );
178
179
		// Confirm payments.
180
		add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 );
181
182
		// Verify IPNs.
183
		add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) );
184
185
	}
186
187
	/**
188
	 * Checks if this gateway is a given gateway.
189
	 *
190
	 * @since 1.0.19
191
	 * @return bool
192
	 */
193
	public function is( $gateway ) {
194
		return $gateway == $this->id;
195
	}
196
197
	/**
198
	 * Returns a users saved tokens for this gateway.
199
	 *
200
	 * @since 1.0.19
201
	 * @return array
202
	 */
203
	public function get_tokens() {
204
205
		if ( count( $this->tokens ) > 0 ) {
206
			return $this->tokens;
207
		}
208
209
		if ( is_user_logged_in() && $this->supports( 'tokens' ) ) {
210
			$tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true );
211
212
			if ( is_array( $tokens ) ) {
213
				$this->tokens = $tokens;
214
			}
215
216
		}
217
218
		return $this->tokens;
219
	}
220
221
	/**
222
	 * Return the title for admin screens.
223
	 *
224
	 * @return string
225
	 */
226
	public function get_method_title() {
227
		return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this );
228
	}
229
230
	/**
231
	 * Return the description for admin screens.
232
	 *
233
	 * @return string
234
	 */
235
	public function get_method_description() {
236
		return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this );
237
	}
238
239
	/**
240
	 * Get the success url.
241
	 *
242
	 * @param WPInv_Invoice $invoice Invoice object.
243
	 * @return string
244
	 */
245
	public function get_return_url( $invoice ) {
246
247
		// Payment success url
248
		$return_url = add_query_arg(
249
			array(
250
				'payment-confirm' => $this->id,
251
				'invoice_key'     => $invoice->get_key(),
252
				'utm_nooverride'  => 1
253
			),
254
			wpinv_get_success_page_uri()
255
		);
256
257
		return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this );
258
	}
259
260
	/**
261
	 * Confirms payments when rendering the success page.
262
	 *
263
	 * @param string $content Success page content.
264
	 * @return string
265
	 */
266
	public function get_confirm_payment( $content ) {
267
		
268
		// Retrieve the invoice.
269
		$invoice_id = getpaid_get_current_invoice_id();
270
		$invoice    = wpinv_get_invoice( $invoice_id );
271
	
272
		// Ensure that it exists and that it is pending payment.
273
		if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) {
274
			return $content;
275
		}
276
	
277
		// Can the user view this invoice??
278
		if ( ! wpinv_user_can_view_invoice( $invoice ) ) {
279
			return $content;
280
		}
281
	
282
		// Show payment processing indicator.
283
		return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) );
284
	}
285
286
	/**
287
	 * Processes ipns and marks payments as complete.
288
	 *
289
	 * @return void
290
	 */
291
	public function verify_ipn() {}
292
293
	/**
294
	 * Get a link to the transaction on the 3rd party gateway site (if applicable).
295
	 *
296
	 * @param string $transaction_url transaction url.
297
	 * @param WPInv_Invoice $invoice Invoice object.
298
	 * @return string transaction URL, or empty string.
299
	 */
300
	public function filter_transaction_url( $transaction_url, $invoice ) {
301
302
		$transaction_id  = $invoice->get_transaction_id();
303
304
		if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) {
305
			$transaction_url = sprintf( $this->view_transaction_url, $transaction_id );
306
			$replace         = $this->is_sandbox( $invoice ) ? 'sandbox' : '';
307
			$transaction_url = str_replace( '{sandbox}', $replace, $transaction_url );
308
		}
309
310
		return $transaction_url;
311
	}
312
313
	/**
314
	 * Get a link to the subscription on the 3rd party gateway site (if applicable).
315
	 *
316
	 * @param string $subscription_url transaction url.
317
	 * @param WPInv_Invoice $invoice Invoice object.
318
	 * @return string subscription URL, or empty string.
319
	 */
320
	public function filter_subscription_url( $subscription_url, $invoice ) {
321
322
		$profile_id      = $invoice->get_subscription_id();
323
324
		if ( ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) {
325
326
			$subscription_url = sprintf( $this->view_subscription_url, $profile_id );
327
			$replace          = $this->is_sandbox( $invoice ) ? 'sandbox' : '';
328
			$subscription_url = str_replace( '{sandbox}', $replace, $subscription_url );
329
330
		}
331
332
		return $subscription_url;
333
	}
334
335
	/**
336
	 * Check if the gateway is available for use.
337
	 *
338
	 * @return bool
339
	 */
340
	public function is_available() {
341
		return ! empty( $this->enabled );
342
	}
343
344
	/**
345
	 * Return the gateway's title.
346
	 *
347
	 * @return string
348
	 */
349
	public function get_title() {
350
		return apply_filters( 'getpaid_gateway_title', $this->title, $this );
351
	}
352
353
	/**
354
	 * Return the gateway's description.
355
	 *
356
	 * @return string
357
	 */
358
	public function get_description() {
359
		return apply_filters( 'getpaid_gateway_description', $this->description, $this );
360
	}
361
362
	/**
363
	 * Process Payment.
364
	 *
365
	 *
366
	 * @param WPInv_Invoice $invoice Invoice.
367
	 * @param array $submission_data Posted checkout fields.
368
	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
369
	 * @return void
370
	 */
371
	public function process_payment( $invoice, $submission_data, $submission ) {
0 ignored issues
show
Unused Code introduced by
The parameter $submission_data is not used and could be removed. ( Ignorable by Annotation )

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

371
	public function process_payment( $invoice, /** @scrutinizer ignore-unused */ $submission_data, $submission ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $submission is not used and could be removed. ( Ignorable by Annotation )

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

371
	public function process_payment( $invoice, $submission_data, /** @scrutinizer ignore-unused */ $submission ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
372
		// Process the payment then either redirect to the success page or the gateway.
373
	}
374
375
	/**
376
	 * Process refund.
377
	 *
378
	 * If the gateway declares 'refunds' support, this will allow it to refund.
379
	 * a passed in amount.
380
	 *
381
	 * @param WPInv_Invoice $invoice Invoice.
382
	 * @param  float  $amount Refund amount.
383
	 * @param  string $reason Refund reason.
384
	 * @return WP_Error|bool True or false based on success, or a WP_Error object.
385
	 */
386
	public function process_refund( $invoice, $amount = null, $reason = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $reason is not used and could be removed. ( Ignorable by Annotation )

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

386
	public function process_refund( $invoice, $amount = null, /** @scrutinizer ignore-unused */ $reason = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $amount is not used and could be removed. ( Ignorable by Annotation )

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

386
	public function process_refund( $invoice, /** @scrutinizer ignore-unused */ $amount = null, $reason = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $invoice is not used and could be removed. ( Ignorable by Annotation )

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

386
	public function process_refund( /** @scrutinizer ignore-unused */ $invoice, $amount = null, $reason = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
387
		return false;
388
	}
389
390
	/**
391
	 * Displays the payment fields, credit cards etc.
392
	 * 
393
	 * @param int $invoice_id 0 or invoice id.
394
	 * @param GetPaid_Payment_Form $form Current payment form.
395
	 */
396
	public function payment_fields( $invoice_id, $form ) {}
0 ignored issues
show
Unused Code introduced by
The parameter $invoice_id is not used and could be removed. ( Ignorable by Annotation )

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

396
	public function payment_fields( /** @scrutinizer ignore-unused */ $invoice_id, $form ) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $form is not used and could be removed. ( Ignorable by Annotation )

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

396
	public function payment_fields( $invoice_id, /** @scrutinizer ignore-unused */ $form ) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
397
398
	/**
399
	 * Filters the gateway settings.
400
	 * 
401
	 * @param array $admin_settings
402
	 */
403
	public function admin_settings( $admin_settings ) {
404
		return $admin_settings;
405
	}
406
407
	/**
408
	 * Retrieves the value of a gateway setting.
409
	 * 
410
	 * @param string $option
411
	 */
412
	public function get_option( $option, $default = false ) {
413
		return wpinv_get_option( $this->id . '_' . $option, $default );
414
	}
415
416
	/**
417
	 * Check if a gateway supports a given feature.
418
	 *
419
	 * Gateways should override this to declare support (or lack of support) for a feature.
420
	 * For backward compatibility, gateways support 'products' by default, but nothing else.
421
	 *
422
	 * @param string $feature string The name of a feature to test support for.
423
	 * @return bool True if the gateway supports the feature, false otherwise.
424
	 * @since 1.0.19
425
	 */
426
	public function supports( $feature ) {
427
		return apply_filters( 'getpaid_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this );
428
	}
429
430
	/**
431
	 * Grab and display our saved payment methods.
432
	 *
433
	 * @since 1.0.19
434
	 */
435
	public function saved_payment_methods() {
436
		$html = '<ul class="getpaid-saved-payment-methods" data-count="' . esc_attr( count( $this->get_tokens() ) ) . '">';
437
438
		foreach ( $this->get_tokens() as $token ) {
439
			$html .= $this->get_saved_payment_method_option_html( $token );
440
		}
441
442
		$html .= $this->get_new_payment_method_option_html();
443
		$html .= '</ul>';
444
445
		echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this );
446
	}
447
448
	/**
449
	 * Gets saved payment method HTML from a token.
450
	 *
451
	 * @since 1.0.19
452
	 * @param  array $token Payment Token.
453
	 * @return string Generated payment method HTML
454
	 */
455
	public function get_saved_payment_method_option_html( $token ) {
456
457
		return sprintf(
458
			'<li class="getpaid-payment-method form-group">
459
				<label>
460
					<input name="getpaid-%1$s-payment-method" type="radio" value="%2$s" style="width:auto;" class="getpaid-saved-payment-method-token-input" %4$s />
461
					<span>%3$s</span>
462
				</label>
463
			</li>',
464
			esc_attr( $this->id ),
465
			esc_attr( $token['id'] ),
466
			esc_html( $token['name'] ),
467
			checked( empty( $token['default'] ), false, false )
468
		);
469
470
	}
471
472
	/**
473
	 * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method.
474
	 *
475
	 * @since 1.0.19
476
	 */
477
	public function get_new_payment_method_option_html() {
478
479
		$label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this );
480
481
		return sprintf(
482
			'<li class="getpaid-new-payment-method">
483
				<label>
484
					<input name="getpaid-%1$s-payment-method" type="radio" value="new" style="width:auto;" />
485
					<span>%2$s</span>
486
				</label>
487
			</li>',
488
			esc_attr( $this->id ),
489
			esc_html( $label )
490
		);
491
492
	}
493
494
	/**
495
	 * Outputs a checkbox for saving a new payment method to the database.
496
	 *
497
	 * @since 1.0.19
498
	 */
499
	public function save_payment_method_checkbox() {
500
	
501
		return sprintf(
502
			'<p class="form-group getpaid-save-payment-method">
503
				<label>
504
					<input name="getpaid-%1$s-new-payment-method" type="checkbox" value="true" style="width:auto;" />
505
					<span>%2$s</span>
506
				</label>
507
			</p>',
508
			esc_attr( $this->id ),
509
			esc_html__( 'Save payment method', 'invoicing' )
510
		);
511
512
	}
513
514
	/**
515
	 * Registers the gateway.
516
	 *
517
	 * @return array
518
	 */
519
	public function register_gateway( $gateways ) {
520
521
		$gateways[ $this->id ] = array(
522
523
			'admin_label'    => $this->method_title,
524
            'checkout_label' => $this->title,
525
			'ordering'       => $this->order,
526
527
		);
528
529
		return $gateways;
530
531
	}
532
533
	/**
534
	 * Checks whether or not this is a sandbox request.
535
	 *
536
	 * @param  WPInv_Invoice|null $invoice Invoice object or null.
537
	 * @return bool
538
	 */
539
	public function is_sandbox( $invoice = null ) {
540
541
		if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) {
542
			return $invoice->get_mode() == 'test';
543
		}
544
545
		return wpinv_is_test_mode( $this->id );
546
547
	}
548
549
	/**
550
	 * Renames the checkout button
551
	 *
552
	 * @return string
553
	 */
554
	public function rename_checkout_button() {
555
		return $this->checkout_button_text;
556
	}
557
558
	/**
559
	 * Validate gateway currency
560
	 *
561
	 * @return bool
562
	 */
563
	public function validate_currency( $validation, $currency ) {
564
565
		// Required currencies.
566
		if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) {
567
			return false;
568
		}
569
570
		// Excluded currencies.
571
		if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) {
572
			return false;
573
		}
574
575
		return $validation;
576
	}
577
578
}
579