Failed Conditions
Push — develop ( 2c6150...0c555f )
by Remco
08:47 queued 10s
created

src/Integration.php (7 issues)

1
<?php
2
/**
3
 * Integration
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use Pronamic\WordPress\Pay\Dependencies\PhpExtensionDependency;
14
use Pronamic\WordPress\Pay\AbstractGatewayIntegration;
15
use Pronamic\WordPress\Pay\Util as Pay_Util;
16
17
/**
18
 * Integration
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.5
22
 * @since   1.0.0
23
 */
24
class Integration extends AbstractGatewayIntegration {
25
	/**
26
	 * REST route namespace.
27
	 *
28
	 * @var string
29
	 */
30
	const REST_ROUTE_NAMESPACE = 'pronamic-pay/adyen/v1';
31
32
	/**
33
	 * Construct Adyen integration.
34
	 *
35
	 * @param array $args Arguments.
36
	 */
37 5
	public function __construct( $args = array() ) {
38 5
		$args = wp_parse_args(
39 5
			$args,
40
			array(
41 5
				'id'            => 'adyen',
42 5
				'name'          => 'Adyen',
43 5
				'provider'      => 'adyen',
44 5
				'url'           => \__( 'https://www.adyen.com/', 'pronamic_ideal' ),
45 5
				'product_url'   => \__( 'https://www.adyen.com/pricing', 'pronamic_ideal' ),
46
				'dashboard_url' => array(
47 5
					\__( 'test', 'pronamic_ideal' ) => 'https://ca-test.adyen.com/ca/ca/login.shtml',
48 5
					\__( 'live', 'pronamic_ideal' ) => 'https://ca-live.adyen.com/ca/ca/login.shtml',
49
				),
50 5
				'manual_url'    => \__( 'https://www.pronamic.eu/manuals/using-adyen-pronamic-pay/', 'pronamic_ideal' ),
51
				'supports'      => array(
52
					'webhook',
53
					'webhook_log',
54
				),
55
			)
56
		);
57
58 5
		parent::__construct( $args );
59
60
		// Dependencies.
61 5
		$dependencies = $this->get_dependencies();
62
63 5
		$dependencies->add( new PhpExtensionDependency( 'intl' ) );
64 5
	}
65
66
	/**
67
	 * Plugins loaded.
68
	 *
69
	 * @return void
70
	 */
71 1
	public function plugins_loaded() {
72 1
		if ( ! $this->get_dependencies()->are_met() ) {
73
			return;
74
		}
75
76
		// Notifications controller.
77 1
		$notifications_controller = new NotificationsController();
78
79 1
		$notifications_controller->setup();
80
81
		// Payments controller.
82 1
		$payments_controller = new PaymentsController();
83
84 1
		$payments_controller->setup();
85
86
		// Payments result controller.
87 1
		$payments_result_controller = new PaymentsResultController();
88
89 1
		$payments_result_controller->setup();
90
91
		// Site Health controller.
92 1
		$site_healht_controller = new SiteHealthController();
93
94 1
		$site_healht_controller->setup();
95
96
		// Settings.
97 1
		add_action( 'init', array( $this, 'init' ) );
98 1
		add_action( 'admin_init', array( $this, 'admin_init' ) );
99
100
		// Actions.
101 1
		add_action( 'current_screen', array( $this, 'maybe_download_certificate_or_key' ) );
102 1
	}
103
104
	/**
105
	 * Initialize.
106
	 *
107
	 * @return void
108
	 */
109 1
	public function init() {
110
		/*
111
		 * Authentication - User Name
112
		 */
113 1
		register_setting(
114 1
			'pronamic_pay',
115 1
			'pronamic_pay_adyen_notification_authentication_username',
116
			array(
117 1
				'type'              => 'string',
118
				'sanitize_callback' => 'sanitize_text_field',
119
			)
120
		);
121
122
		/*
123
		 * Authentication - Password
124
		 */
125 1
		register_setting(
126 1
			'pronamic_pay',
127 1
			'pronamic_pay_adyen_notification_authentication_password',
128
			array(
129 1
				'type'              => 'string',
130
				'sanitize_callback' => 'sanitize_text_field',
131
			)
132
		);
133 1
	}
134
135
	/**
136
	 * Admin initialize.
137
	 *
138
	 * @return void
139
	 */
140 1
	public function admin_init() {
141 1
		add_settings_section(
142 1
			'pronamic_pay_adyen_notification_authentication',
143
			/* translators: Translate 'notification' the same as in the Adyen dashboard. */
144 1
			_x( 'Adyen Notification Authentication', 'Adyen', 'pronamic_ideal' ),
145 1
			array( $this, 'settings_section_notification_authentication' ),
146 1
			'pronamic_pay'
147
		);
148
149 1
		add_settings_field(
150 1
			'pronamic_pay_adyen_notification_authentication_username',
151 1
			__( 'User Name', 'pronamic_ideal' ),
152 1
			array( __CLASS__, 'input_element' ),
153 1
			'pronamic_pay',
154 1
			'pronamic_pay_adyen_notification_authentication',
155
			array(
156 1
				'label_for' => 'pronamic_pay_adyen_notification_authentication_username',
157
			)
158
		);
159
160 1
		add_settings_field(
161 1
			'pronamic_pay_adyen_notification_authentication_password',
162 1
			__( 'Password', 'pronamic_ideal' ),
163 1
			array( __CLASS__, 'input_element' ),
164 1
			'pronamic_pay',
165 1
			'pronamic_pay_adyen_notification_authentication',
166
			array(
167 1
				'label_for' => 'pronamic_pay_adyen_notification_authentication_password',
168
			)
169
		);
170 1
	}
171
172
	/**
173
	 * Settings section notification authentication.
174
	 *
175
	 * @return void
176
	 */
177 1
	public function settings_section_notification_authentication() {
178 1
		printf(
179 1
			'<p>%s</p>',
180 1
			esc_html__(
181 1
				'Set the user name and password below and in the webhook authentication settings in the Adyen dashboard for increased security (recommended).',
182 1
				'pronamic_ideal'
183
			)
184
		);
185 1
	}
186
187
	/**
188
	 * Input text.
189
	 *
190
	 * @param array<string,string> $args Arguments.
191
	 * @return void
192
	 */
193 1
	public static function input_element( $args ) {
194 1
		$name = $args['label_for'];
195
196 1
		$value = get_option( $name );
197 1
		$value = strval( $value );
198
199 1
		printf(
200 1
			'<input name="%s" id="%s" value="%s" type="text" class="regular-text" />',
201 1
			esc_attr( $name ),
202 1
			esc_attr( $name ),
203 1
			esc_attr( $value )
204
		);
205 1
	}
206
207
	/**
208
	 * Get settings fields.
209
	 *
210
	 * @return array<int, array<string, int|string|bool|array<int,string>>>
211
	 */
212 1
	public function get_settings_fields() {
213 1
		$fields = array();
214
215
		// Merchant Account.
216 1
		$fields[] = array(
217 1
			'section'  => 'general',
218 1
			'filter'   => FILTER_SANITIZE_STRING,
219 1
			'meta_key' => '_pronamic_gateway_adyen_merchant_account',
220 1
			'title'    => _x( 'Merchant Account', 'adyen', 'pronamic_ideal' ),
221 1
			'type'     => 'text',
222
			'classes'  => array( 'regular-text', 'code' ),
223 1
			'tooltip'  => __( 'The merchant account identifier, with which you want to process the transaction.', 'pronamic_ideal' ),
224
		);
225
226
		// API Key.
227 1
		$fields[] = array(
228 1
			'section'     => 'general',
229 1
			'filter'      => FILTER_SANITIZE_STRING,
230 1
			'meta_key'    => '_pronamic_gateway_adyen_api_key',
231 1
			'title'       => _x( 'API Key', 'adyen', 'pronamic_ideal' ),
232 1
			'type'        => 'textarea',
233
			'classes'     => array( 'code' ),
234 1
			'tooltip'     => __( 'API key as mentioned in the payment provider dashboard.', 'pronamic_ideal' ),
235 1
			'description' => sprintf(
236 1
				'<a href="%s" target="_blank">%s</a>',
237 1
				esc_url( 'https://docs.adyen.com/developers/user-management/how-to-get-the-api-key' ),
238 1
				esc_html__( 'Adyen documentation: "How to get the API key".', 'pronamic_ideal' )
239
			),
240
		);
241
242
		// Live API URL prefix.
243 1
		$fields[] = array(
244 1
			'section'     => 'general',
245 1
			'filter'      => FILTER_SANITIZE_STRING,
246 1
			'meta_key'    => '_pronamic_gateway_adyen_api_live_url_prefix',
247 1
			'title'       => _x( 'API Live URL Prefix', 'adyen', 'pronamic_ideal' ),
248 1
			'type'        => 'text',
249
			'classes'     => array( 'regular-text', 'code' ),
250 1
			'tooltip'     => __( 'The unique prefix for the live API URL, as mentioned at <strong>Account » API URLs</strong> in the Adyen dashboard.', 'pronamic_ideal' ),
251 1
			'description' => sprintf(
252 1
				'<a href="%s" target="_blank">%s</a>',
253 1
				esc_url( 'https://docs.adyen.com/developers/development-resources/live-endpoints#liveurlprefix' ),
254 1
				esc_html__( 'Adyen documentation: "Live URL prefix".', 'pronamic_ideal' )
255
			),
256
		);
257
258
		// Origin Key.
259 1
		$fields[] = array(
260 1
			'section'     => 'general',
261 1
			'filter'      => FILTER_SANITIZE_STRING,
262 1
			'meta_key'    => '_pronamic_gateway_adyen_origin_key',
263 1
			'title'       => _x( 'Origin Key', 'adyen', 'pronamic_ideal' ),
264 1
			'type'        => 'text',
265
			'classes'     => array(
266
				'regular-text',
267
				'code',
268
				'pronamic-pay-form-control-lg',
269
			),
270 1
			'tooltip'     => __( 'An origin key is a client-side key that is used to validate Adyen\'s JavaScript component library. It is required for the Drop-in and Component integrations.', 'pronamic_ideal' ),
271 1
			'description' => sprintf(
272 1
				'<a href="%s" target="_blank">%s</a>',
273 1
				esc_url( 'https://docs.adyen.com/user-management/how-to-get-an-origin-key' ),
274 1
				esc_html__( 'Adyen documentation: "How to get an origin key".', 'pronamic_ideal' )
275
			),
276
		);
277
278
		// Apple Pay - Merchant identifier.
279 1
		$fields[] = array(
280 1
			'section'     => 'advanced',
281
			'filter'      => \FILTER_SANITIZE_STRING,
282 1
			'meta_key'    => '_pronamic_gateway_adyen_apple_pay_merchant_id',
283 1
			'title'       => _x( 'Apple Pay Merchant ID', 'adyen', 'pronamic_ideal' ),
284 1
			'type'        => 'text',
285
			'classes'     => array( 'regular-text', 'code' ),
286 1
			'tooltip'     => __( 'Your Apple Pay Merchant ID. Required for accepting live payments.', 'pronamic_ideal' ),
287 1
			'description' => sprintf(
288 1
				'<a href="%s" target="_blank">%s</a><br /><a href="%s" target="_blank">%s</a>',
289 1
				esc_url( 'https://docs.adyen.com/payment-methods/apple-pay/web-drop-in#before-you-begin' ),
290 1
				esc_html__( 'Adyen documentation: "Apple Pay Drop-in - Before you begin".', 'pronamic_ideal' ),
291 1
				esc_url( 'https://developer.apple.com/documentation/apple_pay_on_the_web/configuring_your_environment' ),
292 1
				esc_html__( 'Apple documentation: "Configuring your environment".', 'pronamic_ideal' )
293
			),
294
		);
295
296
		// Apple Pay - Merchant Identity PKCS#12.
297 1
		$fields[] = array(
298 1
			'section'     => 'advanced',
299
			'filter'      => \FILTER_SANITIZE_STRING,
300 1
			'meta_key'    => '_pronamic_gateway_adyen_apple_pay_merchant_id_certificate',
301 1
			'title'       => __( 'Apple Pay Merchant Identity Certificate', 'pronamic_ideal' ),
302 1
			'type'        => 'textarea',
303 1
			'callback'    => array( $this, 'field_certificate' ),
304
			'classes'     => array( 'code' ),
305 1
			'tooltip'     => __( 'The Apple Pay Merchant Identity certificate required for secure communication with Apple.', 'pronamic_ideal' ),
306 1
			'description' => sprintf(
307 1
				'<a href="%s" target="_blank">%s</a>',
308 1
				esc_url( 'https://docs.adyen.com/payment-methods/apple-pay/enable-apple-pay#create-merchant-identity-certificate' ),
309 1
				esc_html__( 'Adyen documentation: "Enable Apple Pay - Create a merchant identity certificate".', 'pronamic_ideal' )
310
			),
311
		);
312
313
		// Apple Pay - Merchant Identity private key.
314 1
		$fields[] = array(
315 1
			'section'  => 'advanced',
316
			'filter'   => \FILTER_SANITIZE_STRING,
317 1
			'meta_key' => '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key',
318 1
			'title'    => __( 'Apple Pay Merchant Identity Private Key', 'pronamic_ideal' ),
319 1
			'type'     => 'textarea',
320 1
			'callback' => array( $this, 'field_private_key' ),
321
			'classes'  => array( 'code' ),
322 1
			'tooltip'  => __( 'The private key of the Apple Pay Merchant Identity certificate for secure communication with Apple.', 'pronamic_ideal' ),
323
		);
324
325
		// Apple Pay - Merchant Identity certificate private key password.
326 1
		$fields[] = array(
327 1
			'section'  => 'advanced',
328
			'filter'   => \FILTER_SANITIZE_STRING,
329 1
			'meta_key' => '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key_password',
330 1
			'title'    => _x( 'Apple Pay Merchant Identity Private Key Password', 'adyen', 'pronamic_ideal' ),
331 1
			'type'     => 'text',
332
			'classes'  => array( 'regular-text', 'code' ),
333 1
			'tooltip'  => __( 'Your Apple Pay Merchant Identity Certificate private key password.', 'pronamic_ideal' ),
334
		);
335
336
		// Google Pay - Merchant identifier.
337 1
		$fields[] = array(
338 1
			'section'     => 'advanced',
339
			'filter'      => \FILTER_SANITIZE_STRING,
340 1
			'meta_key'    => '_pronamic_gateway_adyen_google_pay_merchant_identifier',
341 1
			'title'       => _x( 'Google Pay Merchant ID', 'adyen', 'pronamic_ideal' ),
342 1
			'type'        => 'text',
343
			'classes'     => array( 'regular-text', 'code' ),
344 1
			'tooltip'     => __( 'Your Google Merchant ID. Required for accepting live payments.', 'pronamic_ideal' ),
345 1
			'description' => sprintf(
346 1
				'<a href="%s" target="_blank">%s</a><br /><a href="%s" target="_blank">%s</a>',
347 1
				esc_url( 'https://docs.adyen.com/payment-methods/google-pay/web-drop-in#test-and-go-live' ),
348 1
				esc_html__( 'Adyen documentation: "Google Pay Drop-in - Test and go live".', 'pronamic_ideal' ),
349 1
				esc_url( 'https://developers.google.com/pay/api/web/guides/test-and-deploy/deploy-production-environment' ),
350 1
				esc_html__( 'Google documentation: "Deploy production environment".', 'pronamic_ideal' )
351
			),
352
		);
353
354
		// Webhook URL.
355 1
		$fields[] = array(
356 1
			'section'  => 'feedback',
357 1
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
358 1
			'type'     => 'text',
359
			'classes'  => array( 'large-text', 'code' ),
360 1
			'value'    => rest_url( self::REST_ROUTE_NAMESPACE . '/notifications' ),
361
			'readonly' => true,
362 1
			'tooltip'  => sprintf(
363
				/* translators: %s: Adyen */
364 1
				__(
365 1
					'Copy the Webhook URL to the %s dashboard to receive automatic transaction status updates.',
366 1
					'pronamic_ideal'
367
				),
368 1
				__( 'Adyen', 'pronamic_ideal' )
369
			),
370
		);
371
372
		/**
373
		 * SSL Version.
374
		 *
375
		 * @link https://docs.adyen.com/developers/development-resources/notifications/set-up-notifications#step3configurenotificationsinthecustomerarea
376
		 * @link https://www.howsmyssl.com/a/check
377
		 */
378 1
		$fields[] = array(
379 1
			'section' => 'feedback',
380 1
			'title'   => __( 'SSL Version', 'pronamic_ideal' ),
381 1
			'type'    => 'description',
382 1
			'html'    => __( 'Choose the SSL Version of your server on the Adyen Customer Area.', 'pronamic_ideal' ),
383
		);
384
385
		/**
386
		 * Method.
387
		 *
388
		 * @link https://docs.adyen.com/developers/development-resources/notifications/set-up-notifications#step3configurenotificationsinthecustomerarea
389
		 * @link https://www.howsmyssl.com/a/check
390
		 */
391 1
		$fields[] = array(
392 1
			'section' => 'feedback',
393 1
			'title'   => _x( 'Method', 'adyen notification', 'pronamic_ideal' ),
394 1
			'type'    => 'description',
395 1
			'html'    => __( 'JSON', 'pronamic_ideal' ),
396
		);
397
398
		// Webhook authentication settings.
399 1
		$fields[] = array(
400 1
			'section' => 'feedback',
401 1
			'title'   => __( 'Authentication', 'pronamic_ideal' ),
402 1
			'type'    => 'description',
403 1
			'html'    => sprintf(
404 1
				'For webhook authentication settings, please visit <a href="%2$s" title="Settings">%1$s settings</a>.',
405 1
				__( 'Pronamic Pay', 'pronamic_ideal' ),
406 1
				add_query_arg(
407
					array(
408 1
						'page' => 'pronamic_pay_settings',
409
					),
410 1
					admin_url( 'admin.php' )
411
				)
412
			),
413
		);
414
415
		// Return fields.
416 1
		return $fields;
417
	}
418
419
	/**
420
	 * Field certificate.
421
	 *
422
	 * @param array $field Field.
423
	 * @return void
424
	 */
425
	public function field_certificate( $field ) {
426
		if ( ! \array_key_exists( 'meta_key', $field ) ) {
427
			return;
428
		}
429
430
		$certificate = \get_post_meta( get_the_ID(), $field['meta_key'], true );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, 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

430
		$certificate = \get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), $field['meta_key'], true );
Loading history...
431
432
		if ( ! empty( $certificate ) ) {
433
			$fingerprint = Security::get_sha_fingerprint( $certificate );
434
435
			echo '<dl>';
436
437
			if ( null !== $fingerprint ) {
438
				$fingerprint = \str_split( $fingerprint, 2 );
439
				$fingerprint = \implode( ':', $fingerprint );
440
441
				echo '<dt>', \esc_html__( 'SHA Fingerprint', 'pronamic_ideal' ), '</dt>';
442
				echo '<dd>', \esc_html( $fingerprint ), '</dd>';
443
			}
444
445
			$info = \openssl_x509_parse( $certificate );
446
447
			if ( $info ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $info of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
448
				$date_format = __( 'M j, Y @ G:i', 'pronamic_ideal' );
449
450
				if ( isset( $info['validFrom_time_t'] ) ) {
451
					echo '<dt>', \esc_html__( 'Valid From', 'pronamic_ideal' ), '</dt>';
452
					echo '<dd>', \esc_html( \date_i18n( $date_format, $info['validFrom_time_t'] ) ), '</dd>';
453
				}
454
455
				if ( isset( $info['validTo_time_t'] ) ) {
456
					echo '<dt>', \esc_html__( 'Valid To', 'pronamic_ideal' ), '</dt>';
457
					echo '<dd>', \esc_html( \date_i18n( $date_format, $info['validTo_time_t'] ) ), '</dd>';
458
				}
459
			}
460
461
			echo '</dl>';
462
		} elseif ( false !== \strpos( $field['meta_key'], 'apple_pay' ) ) {
463
			\printf(
464
				'<p class="pronamic-pay-description description">%s</p><p>&nbsp;</p>',
465
				\esc_html__( 'Upload an Apple Pay Merchant Identity certificate, which can be exported from Keychain Access on Mac as a PKCS#12 (*.p12) file.', 'pronamic_ideal' )
466
			);
467
		}
468
469
		?>
470
		<p>
471
			<?php
472
473
			if ( ! empty( $certificate ) ) {
474
				\submit_button(
475
					__( 'Download', 'pronamic_ideal' ),
476
					'secondary',
477
					'download' . $field['meta_key'],
478
					false
479
				);
480
481
				echo ' ';
482
			}
483
484
			\printf(
485
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
486
				\esc_html__( 'Upload', 'pronamic_ideal' ),
487
				\esc_attr( $field['meta_key'] . '_file' )
488
			);
489
490
			?>
491
		</p>
492
		<?php
493
	}
494
495
	/**
496
	 * Field private key.
497
	 *
498
	 * @param array $field Field.
499
	 * @return void
500
	 */
501
	public function field_private_key( $field ) {
502
		if ( ! \array_key_exists( 'meta_key', $field ) ) {
503
			return;
504
		}
505
506
		$private_key = \get_post_meta( \get_the_ID(), $field['meta_key'], true );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, 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

506
		$private_key = \get_post_meta( /** @scrutinizer ignore-type */ \get_the_ID(), $field['meta_key'], true );
Loading history...
507
508
		?>
509
		<p>
510
			<?php
511
512
			if ( ! empty( $private_key ) ) {
513
				\submit_button(
514
					__( 'Download', 'pronamic_ideal' ),
515
					'secondary',
516
					'download' . $field['meta_key'],
517
					false
518
				);
519
520
				echo ' ';
521
			}
522
523
			if ( empty( $private_key ) && false !== \strpos( $field['meta_key'], 'apple_pay' ) ) {
524
				\printf(
525
					'<p class="pronamic-pay-description description">%s</p><p>&nbsp;</p>',
526
					\esc_html__( 'Leave empty to auto fill when uploading an Apple Pay Merchant Identity PKCS#12 certificate file.', 'pronamic_ideal' )
527
				);
528
			}
529
530
			\printf(
531
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
532
				\esc_html__( 'Upload', 'pronamic_ideal' ),
533
				\esc_attr( $field['meta_key'] . '_file' )
534
			);
535
536
			?>
537
		</p>
538
		<?php
539
	}
540
541
	/**
542
	 * Download certificate or key in Privacy Enhanced Mail (PEM) format.
543
	 *
544
	 * @return void
545
	 */
546
	public function maybe_download_certificate_or_key() {
547
		// Certificate fields and download filename.
548
		$fields = array(
549
			'_pronamic_gateway_adyen_apple_pay_merchant_id_certificate' => 'apple-pay-merchant-identity-certificate-%s.pem',
550
			'_pronamic_gateway_adyen_apple_pay_merchant_id_private_key' => 'apple-pay-merchant-identity-private-key-%s.pem',
551
		);
552
553
		// Check download actions.
554
		$is_download_action = false;
555
556
		foreach ( $fields as $meta_key => $filename ) {
557
			if ( \filter_has_var( \INPUT_POST, 'download' . $meta_key ) ) {
558
				$is_download_action = true;
559
560
				break;
561
			}
562
		}
563
564
		// No valid download action found.
565
		if ( false === $is_download_action ) {
566
			return;
567
		}
568
569
		$post_id = filter_input( \INPUT_POST, 'post_ID', \FILTER_SANITIZE_STRING );
570
571
		$filename = sprintf( $filename, $post_id );
572
573
		header( 'Content-Description: File Transfer' );
574
		header( 'Content-Disposition: attachment; filename=' . $filename );
575
		header( 'Content-Type: application/x-pem-file; charset=' . get_option( 'blog_charset' ), true );
0 ignored issues
show
Are you sure get_option('blog_charset') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

575
		header( 'Content-Type: application/x-pem-file; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
576
577
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
578
		echo get_post_meta( $post_id, $meta_key, true );
0 ignored issues
show
Are you sure get_post_meta($post_id, $meta_key, true) of type false|mixed|string can be used in echo? ( Ignorable by Annotation )

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

578
		echo /** @scrutinizer ignore-type */ get_post_meta( $post_id, $meta_key, true );
Loading history...
579
580
		exit;
581
	}
582
583
	/**
584
	 * Save post.
585
	 *
586
	 * @param int $post_id Post ID.
587
	 * @return void
588
	 */
589
	public function save_post( $post_id ) {
590
		// Files.
591
		$files = array(
592
			'_pronamic_gateway_adyen_apple_pay_merchant_id_certificate_file' => '_pronamic_gateway_adyen_apple_pay_merchant_id_certificate',
593
			'_pronamic_gateway_adyen_apple_pay_merchant_id_private_key_file' => '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key',
594
		);
595
596
		foreach ( $files as $name => $meta_key ) {
597
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
598
			if ( isset( $_FILES[ $name ] ) && \UPLOAD_ERR_OK === $_FILES[ $name ]['error'] ) {
599
				// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
600
				$value = file_get_contents( $_FILES[ $name ]['tmp_name'] );
601
602
				if ( '_pronamic_gateway_adyen_apple_pay_merchant_id_certificate' === $meta_key ) {
603
					$apple_pay_merchant_id_pkcs12 = $value;
604
				}
605
606
				update_post_meta( $post_id, $meta_key, $value );
607
			}
608
		}
609
610
		// Update Apple Pay Merchant Identity certificate and private key from uploaded PKCS#12 file.
611
		if ( isset( $apple_pay_merchant_id_pkcs12 ) ) {
612
			// Try to read file without using password.
613
			$pkcs12_read = \openssl_pkcs12_read( $apple_pay_merchant_id_pkcs12, $certs, '' );
614
615
			$password = \get_post_meta( $post_id, '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key_password', true );
616
617
			// Try to read file with private key password.
618
			if ( false === $pkcs12_read ) {
619
				$pkcs12_read = \openssl_pkcs12_read( $apple_pay_merchant_id_pkcs12, $certs, $password );
0 ignored issues
show
It seems like $password can also be of type false; however, parameter $pass of openssl_pkcs12_read() does only seem to accept string, 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

619
				$pkcs12_read = \openssl_pkcs12_read( $apple_pay_merchant_id_pkcs12, $certs, /** @scrutinizer ignore-type */ $password );
Loading history...
620
			}
621
622
			if ( true === $pkcs12_read ) {
623
				if ( isset( $certs['cert'] ) ) {
624
					\update_post_meta( $post_id, '_pronamic_gateway_adyen_apple_pay_merchant_id_certificate', $certs['cert'] );
625
				}
626
627
				if ( isset( $certs['pkey'] ) ) {
628
					$private_key = $certs['pkey'];
629
630
					$cipher = null;
631
632
					// Try to export the private key encrypted.
633
					if ( defined( 'OPENSSL_CIPHER_AES_128_CBC' ) ) {
634
						$cipher = \OPENSSL_CIPHER_AES_128_CBC;
635
					} elseif ( defined( 'OPENSSL_CIPHER_3DES' ) ) {
636
						$cipher = \OPENSSL_CIPHER_3DES;
637
					}
638
639
					if ( null !== $cipher && '' !== $password ) {
640
						$args = array(
641
							'digest_alg'             => 'SHA256',
642
							'private_key_bits'       => 2048,
643
							'private_key_type'       => \OPENSSL_KEYTYPE_RSA,
644
							'encrypt_key'            => true,
645
							'encrypt_key_cipher'     => $cipher,
646
							'subjectKeyIdentifier'   => 'hash',
647
							'authorityKeyIdentifier' => 'keyid:always,issuer:always',
648
							'basicConstraints'       => 'CA:true',
649
						);
650
651
						\openssl_pkey_export( $certs['pkey'], $private_key, $password, $args );
0 ignored issues
show
It seems like $password can also be of type false; however, parameter $passphrase of openssl_pkey_export() does only seem to accept string, 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

651
						\openssl_pkey_export( $certs['pkey'], $private_key, /** @scrutinizer ignore-type */ $password, $args );
Loading history...
652
					}
653
654
					\update_post_meta( $post_id, '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key', $private_key );
655
				}
656
			}
657
		}
658
	}
659
660
	/**
661
	 * Get configuration by post ID.
662
	 *
663
	 * @param int $post_id Post ID.
664
	 * @return Config
665
	 */
666 1
	public function get_config( $post_id ) {
667 1
		$config = new Config();
668
669 1
		$config->mode                                       = $this->get_meta( $post_id, 'mode' );
670 1
		$config->api_key                                    = $this->get_meta( $post_id, 'adyen_api_key' );
671 1
		$config->api_live_url_prefix                        = $this->get_meta( $post_id, 'adyen_api_live_url_prefix' );
672 1
		$config->merchant_account                           = $this->get_meta( $post_id, 'adyen_merchant_account' );
673 1
		$config->origin_key                                 = $this->get_meta( $post_id, 'adyen_origin_key' );
674 1
		$config->apple_pay_merchant_id                      = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id' );
675 1
		$config->apple_pay_merchant_id_certificate          = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id_certificate' );
676 1
		$config->apple_pay_merchant_id_private_key          = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id_private_key' );
677 1
		$config->apple_pay_merchant_id_private_key_password = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id_private_key_password' );
678 1
		$config->google_pay_merchant_identifier             = $this->get_meta( $post_id, 'adyen_google_pay_merchant_identifier' );
679
680 1
		return $config;
681
	}
682
683
	/**
684
	 * Get gateway.
685
	 *
686
	 * @param int $post_id Post ID.
687
	 * @return AbstractGateway
688
	 */
689 1
	public function get_gateway( $post_id ) {
690 1
		$config = $this->get_config( $post_id );
691
692 1
		if ( empty( $config->origin_key ) ) {
693 1
			return new WebSdkGateway( $config );
694
		}
695
696
		return new DropInGateway( $config );
697
	}
698
}
699