Passed
Push — master ( c24352...fabb71 )
by Brian
14:05 queued 08:38
created

GetPaid_Payment_Gateway::saved_payment_methods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 0
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
		// Gateway settings.
151
		add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 );
152
153
		// Process payment.
154
		add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 );
155
156
		// Change the checkout button text.
157
		if ( ! empty( $this->checkout_button_text ) ) {
158
			add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) );
159
		}
160
161
		// Check if a gateway is valid for a given currency.
162
		add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 );
163
164
		// Generate the transaction url.
165
		add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 );
166
167
		// Generate the subscription url.
168
		add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 );
169
170
		// Confirm payments.
171
		add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 );
172
173
		// Verify IPNs.
174
		add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) );
175
176
	}
177
178
	/**
179
	 * Checks if this gateway is a given gateway.
180
	 *
181
	 * @since 1.0.19
182
	 * @return bool
183
	 */
184
	public function is( $gateway ) {
185
		return $gateway == $this->id;
186
	}
187
188
	/**
189
	 * Returns a users saved tokens for this gateway.
190
	 *
191
	 * @since 1.0.19
192
	 * @return array
193
	 */
194
	public function get_tokens() {
195
196
		if ( count( $this->tokens ) > 0 ) {
197
			return $this->tokens;
198
		}
199
200
		if ( is_user_logged_in() && $this->supports( 'tokens' ) ) {
201
			$tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true );
202
203
			if ( is_array( $tokens ) ) {
204
				$this->tokens = $tokens;
205
			}
206
207
		}
208
209
		return $this->tokens;
210
	}
211
212
	/**
213
	 * Return the title for admin screens.
214
	 *
215
	 * @return string
216
	 */
217
	public function get_method_title() {
218
		return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this );
219
	}
220
221
	/**
222
	 * Return the description for admin screens.
223
	 *
224
	 * @return string
225
	 */
226
	public function get_method_description() {
227
		return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this );
228
	}
229
230
	/**
231
	 * Get the success url.
232
	 *
233
	 * @param WPInv_Invoice $invoice Invoice object.
234
	 * @return string
235
	 */
236
	public function get_return_url( $invoice ) {
237
238
		// Payment success url
239
		$return_url = add_query_arg(
240
			array(
241
				'payment-confirm' => $this->id,
242
				'invoice_key'     => $invoice->get_key(),
243
				'utm_nooverride'  => 1
244
			),
245
			wpinv_get_success_page_uri()
246
		);
247
248
		return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this );
249
	}
250
251
	/**
252
	 * Confirms payments when rendering the success page.
253
	 *
254
	 * @param string $content Success page content.
255
	 * @return string
256
	 */
257
	public function get_confirm_payment( $content ) {
258
		
259
		// Retrieve the invoice.
260
		$invoice_id = getpaid_get_current_invoice_id();
261
		$invoice    = wpinv_get_invoice( $invoice_id );
262
	
263
		// Ensure that it exists and that it is pending payment.
264
		if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) {
265
			return $content;
266
		}
267
	
268
		// Can the user view this invoice??
269
		if ( ! wpinv_user_can_view_invoice( $invoice ) ) {
270
			return $content;
271
		}
272
	
273
		// Show payment processing indicator.
274
		return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) );
275
	}
276
277
	/**
278
	 * Processes ipns and marks payments as complete.
279
	 *
280
	 * @return void
281
	 */
282
	public function verify_ipn() {}
283
284
	/**
285
	 * Get a link to the transaction on the 3rd party gateway site (if applicable).
286
	 *
287
	 * @param string $transaction_url transaction url.
288
	 * @param WPInv_Invoice $invoice Invoice object.
289
	 * @return string transaction URL, or empty string.
290
	 */
291
	public function filter_transaction_url( $transaction_url, $invoice ) {
292
293
		$transaction_id  = $invoice->get_transaction_id();
294
295
		if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) {
296
			$transaction_url = sprintf( $this->view_transaction_url, $transaction_id );
297
			$replace         = $this->is_sandbox( $invoice ) ? 'sandbox' : '';
298
			$transaction_url = str_replace( '{sandbox}', $replace, $transaction_url );
299
		}
300
301
		return $transaction_url;
302
	}
303
304
	/**
305
	 * Get a link to the subscription on the 3rd party gateway site (if applicable).
306
	 *
307
	 * @param string $subscription_url transaction url.
308
	 * @param WPInv_Invoice $invoice Invoice object.
309
	 * @return string subscription URL, or empty string.
310
	 */
311
	public function filter_subscription_url( $subscription_url, $invoice ) {
312
313
		$profile_id      = $invoice->get_subscription_id();
314
315
		if ( ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) {
316
317
			$subscription_url = sprintf( $this->view_subscription_url, $profile_id );
318
			$replace          = $this->is_sandbox( $invoice ) ? 'sandbox' : '';
319
			$subscription_url = str_replace( '{sandbox}', $replace, $subscription_url );
320
321
		}
322
323
		return $subscription_url;
324
	}
325
326
	/**
327
	 * Check if the gateway is available for use.
328
	 *
329
	 * @return bool
330
	 */
331
	public function is_available() {
332
		return ! empty( $this->enabled );
333
	}
334
335
	/**
336
	 * Return the gateway's title.
337
	 *
338
	 * @return string
339
	 */
340
	public function get_title() {
341
		return apply_filters( 'getpaid_gateway_title', $this->title, $this );
342
	}
343
344
	/**
345
	 * Return the gateway's description.
346
	 *
347
	 * @return string
348
	 */
349
	public function get_description() {
350
		return apply_filters( 'getpaid_gateway_description', $this->description, $this );
351
	}
352
353
	/**
354
	 * Process Payment.
355
	 *
356
	 *
357
	 * @param WPInv_Invoice $invoice Invoice.
358
	 * @param array $submission_data Posted checkout fields.
359
	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
360
	 * @return void
361
	 */
362
	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

362
	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

362
	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...
363
		// Process the payment then either redirect to the success page or the gateway.
364
	}
365
366
	/**
367
	 * Process refund.
368
	 *
369
	 * If the gateway declares 'refunds' support, this will allow it to refund.
370
	 * a passed in amount.
371
	 *
372
	 * @param WPInv_Invoice $invoice Invoice.
373
	 * @param  float  $amount Refund amount.
374
	 * @param  string $reason Refund reason.
375
	 * @return WP_Error|bool True or false based on success, or a WP_Error object.
376
	 */
377
	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

377
	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

377
	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

377
	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...
378
		return false;
379
	}
380
381
	/**
382
	 * Displays the payment fields, credit cards etc.
383
	 * 
384
	 * @param int $invoice_id 0 or invoice id.
385
	 * @param GetPaid_Payment_Form $form Current payment form.
386
	 */
387
	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

387
	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

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