Test Failed
Push — travis/4010 ( cd7197 )
by Ravinder
12:19
created

Give_Stripe_Gateway   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 686
Duplicated Lines 14.58 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 100
loc 686
rs 7.354
c 0
b 0
f 0
wmc 52
lcom 1
cbo 9

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 27 3
A set_api_version() 32 32 3
A set_api_key() 0 3 1
A send_back_to_checkout() 0 3 1
A get_token_details() 0 31 3
A get_source_details() 29 29 3
A prepare_source() 29 29 3
A add_hidden_source_field() 0 13 2
B get_customer_card() 10 62 7
A save_stripe_customer_id() 0 21 3
A log_error() 0 37 3
A format_amount() 0 9 2
A verify_payment() 0 35 4
A prepare_metadata() 0 36 4
A create_charge() 0 47 4
A create_3d_secure_source() 0 37 2
A is_3d_secure_required() 0 18 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Give_Stripe_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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 Give_Stripe_Gateway, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Give - Stripe Core Gateway
4
 *
5
 * @since 2.5.0
6
 *
7
 * @package    Give
8
 * @subpackage Stripe Core
9
 * @copyright  Copyright (c) 2019, GiveWP
10
 * @license    https://opensource.org/licenses/gpl-license GNU Public License
11
 */
12
13
// Exit, if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Check for class Give_Stripe_Gateway exists.
20
 *
21
 * @since 2.5.0
22
 */
23
if ( ! class_exists( 'Give_Stripe_Gateway' ) ) {
24
25
	class Give_Stripe_Gateway {
26
27
		/**
28
		 * Default Gateway ID.
29
		 *
30
		 * @since  2.5.0
31
		 * @access public
32
		 *
33
		 * @var string
34
		 */
35
		public $id = '';
36
37
		/**
38
		 * Set Latest Stripe Version.
39
		 *
40
		 * @since  2.5.0
41
		 * @access public
42
		 *
43
		 * @var string
44
		 */
45
		public $api_version = '2019-03-14';
46
47
		/**
48
		 * Secret API Key.
49
		 *
50
		 * @access private
51
		 *
52
		 * @var string
53
		 */
54
		private $secret_key = '';
55
56
		/**
57
		 * Payment Intent.
58
		 *
59
		 * @since  2.5.0
60
		 * @access public
61
		 *
62
		 * @var \Stripe\PaymentIntent
63
		 */
64
		public $payment_intent;
65
66
		/**
67
		 * Give_Stripe_Gateway constructor.
68
		 *
69
		 * @since  2.5.0
70
		 * @access public
71
		 *
72
		 * @return bool|void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
73
		 */
74
		public function __construct() {
75
76
			// Bailout, if current gateway is not enabled.
77
			if ( ! give_is_gateway_active( $this->id ) ) {
78
				return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
79
			}
80
81
			// Set secret key received from Stripe.
82
			$this->secret_key = give_stripe_get_secret_key();
83
84
			// Set API Key.
85
			$this->set_api_key();
86
87
			// Set API Version.
88
			$this->set_api_version();
89
90
			// Call Payment Intent Class to utilize.
91
			$this->payment_intent = new Give_Stripe_Payment_Intent();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Give_Stripe_Payment_Intent() of type object<Give_Stripe_Payment_Intent> is incompatible with the declared type object<Stripe\PaymentIntent> of property $payment_intent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
93
			add_action( "give_gateway_{$this->id}", array( $this, 'process_payment' ) );
94
95
			// Add hidden field for source only if the gateway is not Stripe ACH.
96
			if ( 'stripe_ach' !== $this->id ) {
97
				add_action( 'give_donation_form_top', array( $this, 'add_hidden_source_field' ), 10, 2 );
98
			}
99
100
		}
101
102
		/**
103
		 * This function will help to set the latest Stripe API version.
104
		 *
105
		 * @since  2.5.0
106
		 * @access public
107
		 *
108
		 * @return void
109
		 */
110 View Code Duplication
		public function set_api_version() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
112
			// Set Application Info.
113
			give_stripe_set_app_info();
114
115
			try {
116
117
				// Set API Version to latest.
118
				\Stripe\Stripe::setApiVersion( $this->api_version );
119
120
			} catch ( \Stripe\Error\Base $e ) {
121
122
				// Log Error.
123
				$this->log_error( $e );
124
125
			} catch ( Exception $e ) {
126
127
				// Something went wrong outside of Stripe.
128
				give_record_gateway_error(
129
					__( 'Stripe Error', 'give' ),
130
					sprintf(
131
						/* translators: %s Exception Message Body */
132
						__( 'Unable to set Stripe API Version. Details: %s', 'give' ),
133
						$e->getMessage()
134
					)
135
				);
136
				give_set_error( 'stripe_error', __( 'An error occurred while processing the donation. Please try again.', 'give' ) );
137
138
				// Send donor back to checkout page on error.
139
				$this->send_back_to_checkout();
140
			}
141
		}
142
143
		/**
144
		 * This function will help you to set API Key and its related errors are shown.
145
		 *
146
		 * @since  2.5.0
147
		 * @access public
148
		 *
149
		 * @return void
150
		 */
151
		public function set_api_key() {
152
			give_stripe_set_api_key();
153
		}
154
155
		/**
156
		 * Send back to checkout based on the gateway id.
157
		 *
158
		 * @since  2.5.0
159
		 * @access public
160
		 *
161
		 * @return void
162
		 */
163
		public function send_back_to_checkout() {
164
			give_send_back_to_checkout( '?payment-mode=' . $this->id );
165
		}
166
167
		/**
168
		 * This function will be used to fetch token details from token id.
169
		 *
170
		 * @param string $id   Stripe Token ID.
171
		 * @param array  $args Additional arguments.
172
		 *
173
		 * @since  2.5.0
174
		 * @access public
175
		 *
176
		 * @return \Stripe\Token
177
		 */
178
		public function get_token_details( $id, $args = array() ) {
179
180
			// Set Application Info.
181
			give_stripe_set_app_info();
182
183
			try {
184
185
				$args = wp_parse_args( $args, give_stripe_get_connected_account_options() );
186
187
				// Retrieve Token Object.
188
				return \Stripe\Token::retrieve( $id, $args );
189
190
			} catch ( \Stripe\Error\Base $e ) {
191
				$this->log_error( $e );
192
			} catch ( Exception $e ) {
193
194
				// Something went wrong outside of Stripe.
195
				give_record_gateway_error(
196
					__( 'Stripe Token Error', 'give' ),
197
					sprintf(
198
						/* translators: %s Exception Message Body */
199
						__( 'Unable to retrieve token. Details: %s', 'give' ),
200
						$e->getMessage()
201
					)
202
				);
203
				give_set_error( 'stripe_error', __( 'An error occurred while processing the donation. Please try again.', 'give' ) );
204
205
				// Send donor back to checkout page on error.
206
				$this->send_back_to_checkout();
207
			}
208
		}
209
210
		/**
211
		 * This function will be used to fetch source details from source id.
212
		 *
213
		 * @param string $id Stripe Source ID.
214
		 *
215
		 * @since  2.5.0
216
		 * @access public
217
		 *
218
		 * @return \Stripe\Source
219
		 */
220 View Code Duplication
		public function get_source_details( $id ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
222
			// Set Application Info.
223
			give_stripe_set_app_info();
224
225
			try {
226
227
				// Retrieve Source Object.
228
				return \Stripe\Source::retrieve( $id, give_stripe_get_connected_account_options() );
229
230
			} catch ( \Stripe\Error\Base $e ) {
231
				$this->log_error( $e );
232
			} catch ( Exception $e ) {
233
234
				// Something went wrong outside of Stripe.
235
				give_record_gateway_error(
236
					__( 'Stripe Source Error', 'give' ),
237
					sprintf(
238
						/* translators: %s Exception Message Body */
239
						__( 'Unable to retrieve source. Details: %s', 'give' ),
240
						$e->getMessage()
241
					)
242
				);
243
				give_set_error( 'stripe_error', __( 'An error occurred while processing the donation. Please try again.', 'give' ) );
244
245
				// Send donor back to checkout page on error.
246
				$this->send_back_to_checkout();
247
			}
248
		}
249
250
		/**
251
		 * This function will prepare source based on the parameters provided.
252
		 *
253
		 * @param array $args List of arguments \Stripe\Source::create() supports.
254
		 *
255
		 * @since  2.5.0
256
		 * @access public
257
		 *
258
		 * @return \Stripe\Source
259
		 */
260 View Code Duplication
		public function prepare_source( $args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
262
			// Set Application Info.
263
			give_stripe_set_app_info();
264
265
			try {
266
267
				// Create Source Object.
268
				return \Stripe\Source::create( $args, give_stripe_get_connected_account_options() );
269
270
			} catch ( \Stripe\Error\Base $e ) {
271
				$this->log_error( $e );
272
			} catch ( Exception $e ) {
273
274
				// Something went wrong outside of Stripe.
275
				give_record_gateway_error(
276
					__( 'Stripe Error', 'give' ),
277
					sprintf(
278
						/* translators: %s Exception Message Body */
279
						__( 'Unable to create source. Details: %s', 'give' ),
280
						$e->getMessage()
281
					)
282
				);
283
				give_set_error( 'stripe_error', __( 'An error occurred while processing the donation. Please try again.', 'give' ) );
284
285
				// Send donor back to checkout page on error.
286
				$this->send_back_to_checkout();
287
			}
288
		}
289
290
		/**
291
		 * This function will add hidden source field.
292
		 *
293
		 * @param int   $form_id Donation Form ID.
294
		 * @param array $args    List of arguments.
295
		 *
296
		 * @since  2.5.0
297
		 * @access public
298
		 */
299
		public function add_hidden_source_field( $form_id, $args ) {
300
301
			$id_prefix = ! empty( $args['id_prefix'] ) ? $args['id_prefix'] : 0;
302
303
			echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
304
				'<input id="give-%1$s-source-%2$s" type="hidden" name="give_%1$s_source" value="">',
305
				esc_attr( $this->id ),
306
				esc_html( $id_prefix )
307
			);
308
			?>
309
310
			<?php
311
		}
312
313
		/**
314
		 * Get Customer's card.
315
		 *
316
		 * @param \Stripe\Customer $stripe_customer Stripe Customer Object.
317
		 * @param string           $id              Source or Token ID.
318
		 *
319
		 * @since 2.5.0
320
		 *
321
		 * @return \Stripe\Source|bool
322
		 */
323
		public function get_customer_card( $stripe_customer, $id ) {
324
325
			$card_exists = false;
326
			$all_sources = $stripe_customer->sources->all();
327
328
			if ( give_is_stripe_checkout_enabled() ) {
329
				$card = $this->get_token_details( $id );
330
			} else {
331
				$card = $this->get_source_details( $id );
332
			}
333
334
			$source_list = wp_list_pluck( $all_sources->data, 'id' );
335
336
			// Check whether the source is already attached to customer or not.
337
			if ( in_array( $id, $source_list, true ) ) {
338
				$card_exists = true;
339
			}
340
341
			// Create the card if none found above.
342
			if ( ! $card_exists ) {
343
				try {
344
345
					// Attach Source to existing Customer.
346
					$card = $stripe_customer->sources->create( array(
347
						'source' => $id,
348
					) );
349
350
				} catch ( \Stripe\Error\Base $e ) {
351
352
					// Log Error.
353
					$this->log_error( $e );
354
355
				} catch ( Exception $e ) {
356
357
					give_record_gateway_error(
358
						__( 'Stripe Card Error', 'give' ),
359
						sprintf(
360
							/* translators: %s Exception Error Message */
361
							__( 'The Stripe Gateway returned an error while processing a donation. Details: %s', 'give' ),
362
							$e->getMessage()
363
						)
364
					);
365
366
					// Send donor back to checkout page on error.
367
					$this->send_back_to_checkout();
368
				}
369
			}
370
371
			// Return Card Details, if exists.
372
			if ( ! empty( $card->id ) ) {
373
				return $card;
374 View Code Duplication
			} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
375
376
				give_set_error( 'stripe_error', __( 'An error occurred while processing the donation. Please try again.', 'give' ) );
377
				give_record_gateway_error( __( 'Stripe Error', 'give' ), __( 'An error occurred retrieving or creating the ', 'give' ) );
378
379
				// Send donor back to checkout page on error.
380
				$this->send_back_to_checkout();
381
382
				return false;
383
			}
384
		}
385
386
		/**
387
		 * Save Stripe Customer ID.
388
		 *
389
		 * @param string $stripe_customer_id Customer ID.
390
		 * @param int    $payment_id         Payment ID.
391
		 *
392
		 * @since 2.5.0
393
		 */
394
		public function save_stripe_customer_id( $stripe_customer_id, $payment_id ) {
395
396
			// Update customer meta.
397
			if ( class_exists( 'Give_DB_Donor_Meta' ) ) {
398
399
				$donor_id = give_get_payment_donor_id( $payment_id );
400
401
				// Get the Give donor.
402
				$donor = new Give_Donor( $donor_id );
403
404
				// Update donor meta.
405
				$donor->update_meta( give_stripe_get_customer_key(), $stripe_customer_id );
406
407
			} elseif ( is_user_logged_in() ) {
408
409
				// Support saving to legacy method of user method.
410
				update_user_meta( get_current_user_id(), give_stripe_get_customer_key(), $stripe_customer_id );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
411
412
			}
413
414
		}
415
416
		/**
417
		 * Log a Stripe Error.
418
		 *
419
		 * Logs in the Give db the error and also displays the error message to the donor.
420
		 *
421
		 * @param \Stripe\Error\Base|\Stripe\Error\Card $exception    Exception.
422
		 *
423
		 * @since 2.5.0
424
		 *
425
		 * @return bool
426
		 */
427
		public function log_error( $exception ) {
428
429
			$log_message = __( 'The Stripe payment gateway returned an error while processing the donation.', 'give' ) . '<br><br>';
430
			$exception_message = $exception->getMessage();
431
432
			// Bad Request of some sort.
433
			if ( ! empty( $exception_message ) ) {
434
				$log_message .= sprintf(
435
					/* translators: %s Exception Message */
436
					__( 'Message: %s', 'give' ),
437
					$exception_message
438
				) . '<br><br>';
439
440
				$trace_string = $exception->getTraceAsString();
441
442
				if ( ! empty( $trace_string ) ) {
443
					$log_message .= sprintf(
444
						/* translators: %s Trace String */
445
						__( 'Code: %s', 'give' ),
446
						$trace_string
447
					);
448
				}
449
450
				give_set_error( 'stripe_request_error', $exception_message );
451
			} else {
452
				give_set_error( 'stripe_request_error', __( 'The Stripe API request was invalid, please try again.', 'give' ) );
453
			}
454
455
			// Log it with DB.
456
			give_record_gateway_error( __( 'Stripe Error', 'give' ), $log_message );
457
458
			// Send donor back to checkout page on error.
459
			$this->send_back_to_checkout();
460
461
			return false;
462
463
		}
464
465
		/**
466
		 * Format currency for Stripe.
467
		 *
468
		 * @see https://support.stripe.com/questions/which-zero-decimal-currencies-does-stripe-support
469
		 *
470
		 * @param float $amount Donation amount.
471
		 *
472
		 * @return mixed
473
		 */
474
		public function format_amount( $amount ) {
475
476
			// Get the donation amount.
477
			if ( give_stripe_is_zero_decimal_currency() ) {
478
				return $amount;
479
			} else {
480
				return $amount * 100;
481
			}
482
		}
483
484
		/**
485
		 * Verify Payment.
486
		 *
487
		 * @param int            $payment_id         Payment ID.
488
		 * @param string         $stripe_customer_id Customer ID.
489
		 * @param \Stripe\Charge $charge             Stripe Charge Object.
490
		 */
491
		public function verify_payment( $payment_id, $stripe_customer_id, $charge ) {
492
493
			// Sanity checks: verify all vars exist.
494
			if ( $payment_id && ( ! empty( $stripe_customer_id ) || ! empty( $charge ) ) ) {
495
496
				/**
497
				 * This action hook is used to perform some additional steps to verify the payment.
498
				 *
499
				 * @param int            $payment_id         Payment ID.
500
				 * @param string         $stripe_customer_id Customer ID.
501
				 * @param \Stripe\Charge $charge             Stripe Charge Object.
502
				 *
503
				 * @since 2.5.0
504
				 */
505
				do_action( 'give_stripe_verify_payment', $payment_id, $stripe_customer_id, $charge );
506
507
				// @TODO use Stripe's API here to retrieve the invoice then confirm it has been paid.
508
				// Regular payment, publish it.
509
				give_update_payment_status( $payment_id, 'publish' );
510
511
				// Save Stripe customer id.
512
				$this->save_stripe_customer_id( $stripe_customer_id, $payment_id );
513
514
				// Send them to success page.
515
				give_send_to_success_page();
516
517
			} else {
518
519
				give_set_error( 'payment_not_recorded', __( 'Your donation could not be recorded, please contact the site administrator.', 'give-stripe' ) );
520
521
				// If errors are present, send the user back to the purchase page so they can be corrected.
522
				$this->send_back_to_checkout();
523
524
			} // End if().
525
		}
526
527
		/**
528
		 * This function will prepare metadata to send to Stripe.
529
		 *
530
		 * @param int $donation_id Donation ID.
531
		 *
532
		 * @since  2.5.0
533
		 * @access public
534
		 *
535
		 * @return array
536
		 */
537
		public function prepare_metadata( $donation_id = 0 ) {
538
539
			if ( ! $donation_id ) {
540
				return array();
541
			}
542
543
			$form_id = give_get_payment_form_id( $donation_id );
544
			$email   = give_get_payment_user_email( $donation_id );
545
546
			$args = array(
547
				'Email'            => $email,
548
				'Donation Post ID' => $donation_id,
549
			);
550
551
			// Add Sequential Metadata.
552
			$seq_donation_id = give_stripe_get_sequential_id( $donation_id );
553
			if ( $seq_donation_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $seq_donation_id of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
554
				$args['Sequential ID'] = $seq_donation_id;
555
			}
556
557
			// Add custom FFM fields to Stripe metadata.
558
			$args = array_merge( $args, give_stripe_get_custom_ffm_fields( $form_id, $donation_id ) );
559
560
			// Limit metadata passed to Stripe as maximum of 20 metadata is only allowed.
561
			if ( count( $args ) > 20 ) {
562
				$args = array_slice( $args, 0, 19, false );
563
				$args = array_merge(
564
					$args,
565
					array(
566
						'More Details' => esc_url_raw( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&id=' . $donation_id ) ),
567
					)
568
				);
569
			}
570
571
			return $args;
572
		}
573
574
		/**
575
		 * This function will help to charge with Stripe.
576
		 *
577
		 * @param int   $donation_id Donation ID with pending status.
578
		 * @param array $charge_args List of charge arguments.
579
		 *
580
		 * @since  2.5.0
581
		 * @access public
582
		 *
583
		 * @return \Stripe\Charge
584
		 */
585
		public function create_charge( $donation_id, $charge_args ) {
586
587
			// Set App Info to Stripe.
588
			give_stripe_set_app_info();
589
590
			try {
591
592
				$charge_args = apply_filters( "give_{$this->id}_create_charge_args", $charge_args );
593
594
				// Charge application fee, only if the Stripe premium add-on is not active.
595
				if ( ! defined( 'GIVE_STRIPE_VERSION' ) ) {
596
					// Set Application Fee Amount.
597
					$charge_args['application_fee_amount'] = give_stripe_get_application_fee_amount( $charge_args['amount'] );
598
				}
599
600
				$charge = \Stripe\Charge::create(
601
					$charge_args,
602
					give_stripe_get_connected_account_options()
603
				);
604
605
				// Add note for the charge.
606
				// Save Stripe's charge ID to the transaction.
607
				if ( ! empty( $charge ) ) {
608
					give_insert_payment_note( $donation_id, 'Stripe Charge ID: ' . $charge->id );
609
					give_set_payment_transaction_id( $donation_id, $charge->id );
610
				}
611
612
				return $charge;
613
614
			} catch ( Exception $e ) {
615
616
				give_record_gateway_error(
617
					__( 'Stripe Error', 'give' ),
618
					sprintf(
619
						/* translators: %s Exception Error Message */
620
						__( 'Unable to create a successful charge. Details: %s', 'give' ),
621
						$e->getMessage()
622
					)
623
				);
624
625
				give_set_error( 'stripe_charge_error', __( 'Error processing charge with Stripe. Please try again.', 'give' ) );
626
627
				// Redirect to donation failed page.
628
				wp_safe_redirect( give_get_failed_transaction_uri() );
629
630
			} // End try().
631
		}
632
633
		/**
634
		 * Create Source for Stripe 3D Secure Payments.
635
		 *
636
		 * @param int $donation_id Donation ID.
637
		 * @param int $source_id   Source ID/Object.
638
		 *
639
		 * @since  1.6
640
		 * @access public
641
	 	 *
642
		 * @return bool|\Stripe\Source
643
		 */
644
		public function create_3d_secure_source( $donation_id, $source_id ) {
645
			$form_id         = give_get_payment_form_id( $donation_id );
646
			$customer_id     = give_get_payment_meta( $donation_id, '_give_stripe_customer_id', true );
0 ignored issues
show
Unused Code introduced by
$customer_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
647
			$donation_amount = give_donation_amount( $donation_id );
648
649
			// Prepare basic source args.
650
			$source_args = array(
651
				'amount'               => $this->format_amount( $donation_amount ),
652
				'currency'             => give_get_currency( $form_id ),
653
				'type'                 => 'three_d_secure',
654
				'three_d_secure'       => array(
655
					'card'     => $source_id,
656
				),
657
				'statement_descriptor' => give_stripe_get_statement_descriptor(),
658
				'redirect'             => array(
659
					'return_url' => add_query_arg(
660
						array(
661
							'give-listener' => 'stripe_three_d_secure',
662
							'donation_id'   => $donation_id,
663
						),
664
						give_get_success_page_uri()
665
					),
666
				),
667
			);
668
669
			$source = $this->prepare_source( $source_args );
670
671
			// Add donation note for 3D secure source ID.
672
			if ( ! empty( $source->id ) ) {
673
				give_insert_payment_note( $donation_id, 'Stripe 3D Secure Source ID: ' . $source->id );
674
			}
675
676
			// Save 3D secure source id to donation.
677
			give_update_payment_meta( $donation_id, '_give_stripe_3dsecure_source_id', $source->id );
678
679
			return $source;
680
		}
681
682
		/**
683
		 * Is 3D secure payment required?
684
		 *
685
		 * @param \Stripe\Source $source_object Stripe Source Object.
686
		 *
687
		 * @since  1.6
688
		 * @access public
689
		 *
690
	 	 * @return bool
691
		 */
692
		public function is_3d_secure_required( $source_object ) {
693
694
			$is_3d_secure_enabled = give_is_setting_enabled( give_get_option( 'stripe_enable_three_d_secure_payments', '' ) );
695
696
			if ( $is_3d_secure_enabled ) {
697
				return apply_filters(
698
					'give_stripe_3d_secure_required',
699
					(
700
						! empty( $source_object->type ) &&
701
						'card' === $source_object->type &&
702
						'required' === $source_object->card->three_d_secure
703
					),
704
					$source_object
705
				);
706
			}
707
708
			return false;
709
		}
710
	}
711
}
712