Failed Conditions
Push — develop ( 89f61e...ae8897 )
by Reüel
06:06
created

src/Integration.php (5 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.1.1
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<string, array<string>> $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
	 * Setup gateway integration.
68
	 *
69
	 * @return void
70
	 */
71 1
	public function setup() {
72
		// Check if dependencies are met and integration is active.
73 1
		if ( ! $this->is_active() ) {
74
			return;
75
		}
76
77
		// Notifications controller.
78 1
		$notifications_controller = new NotificationsController();
79
80 1
		$notifications_controller->setup();
81
82
		// Payments controller.
83 1
		$payments_controller = new PaymentsController();
84
85 1
		$payments_controller->setup();
86
87
		// Payments result controller.
88 1
		$payments_result_controller = new PaymentsResultController();
89
90 1
		$payments_result_controller->setup();
91
92
		// Site Health controller.
93 1
		$site_healht_controller = new SiteHealthController();
94
95 1
		$site_healht_controller->setup();
96
97
		// Settings.
98 1
		add_action( 'init', array( $this, 'init' ) );
99 1
		add_action( 'admin_init', array( $this, 'admin_init' ), 15 );
100
101
		// Actions.
102 1
		add_action( 'current_screen', array( $this, 'maybe_download_certificate_or_key' ) );
103 1
	}
104
105
	/**
106
	 * Initialize.
107
	 *
108
	 * @return void
109
	 */
110 1
	public function init() {
111
		/*
112
		 * Authentication - User Name
113
		 */
114 1
		register_setting(
115 1
			'pronamic_pay',
116 1
			'pronamic_pay_adyen_notification_authentication_username',
117
			array(
118 1
				'type'              => 'string',
119
				'sanitize_callback' => 'sanitize_text_field',
120
			)
121
		);
122
123
		/*
124
		 * Authentication - Password
125
		 */
126 1
		register_setting(
127 1
			'pronamic_pay',
128 1
			'pronamic_pay_adyen_notification_authentication_password',
129
			array(
130 1
				'type'              => 'string',
131
				'sanitize_callback' => 'sanitize_text_field',
132
			)
133
		);
134 1
	}
135
136
	/**
137
	 * Admin initialize.
138
	 *
139
	 * @return void
140
	 */
141 1
	public function admin_init() {
142 1
		add_settings_section(
143 1
			'pronamic_pay_adyen_notification_authentication',
144
			/* translators: Translate 'notification' the same as in the Adyen dashboard. */
145 1
			_x( 'Adyen Notification Authentication', 'Adyen', 'pronamic_ideal' ),
146 1
			array( $this, 'settings_section_notification_authentication' ),
147 1
			'pronamic_pay'
148
		);
149
150 1
		add_settings_field(
151 1
			'pronamic_pay_adyen_notification_authentication_username',
152 1
			__( 'User Name', 'pronamic_ideal' ),
153 1
			array( __CLASS__, 'input_element' ),
154 1
			'pronamic_pay',
155 1
			'pronamic_pay_adyen_notification_authentication',
156
			array(
157 1
				'label_for' => 'pronamic_pay_adyen_notification_authentication_username',
158
			)
159
		);
160
161 1
		add_settings_field(
162 1
			'pronamic_pay_adyen_notification_authentication_password',
163 1
			__( 'Password', 'pronamic_ideal' ),
164 1
			array( __CLASS__, 'input_element' ),
165 1
			'pronamic_pay',
166 1
			'pronamic_pay_adyen_notification_authentication',
167
			array(
168 1
				'label_for' => 'pronamic_pay_adyen_notification_authentication_password',
169
			)
170
		);
171 1
	}
172
173
	/**
174
	 * Settings section notification authentication.
175
	 *
176
	 * @return void
177
	 */
178 1
	public function settings_section_notification_authentication() {
179 1
		printf(
180 1
			'<p>%s</p>',
181 1
			esc_html__(
182 1
				'Set the user name and password below and in the webhook authentication settings in the Adyen dashboard for increased security (recommended).',
183 1
				'pronamic_ideal'
184
			)
185
		);
186 1
	}
187
188
	/**
189
	 * Input text.
190
	 *
191
	 * @param array<string,string> $args Arguments.
192
	 * @return void
193
	 */
194 1
	public static function input_element( $args ) {
195 1
		$name = $args['label_for'];
196
197 1
		$value = get_option( $name );
198 1
		$value = strval( $value );
199
200 1
		printf(
201 1
			'<input name="%s" id="%s" value="%s" type="text" class="regular-text" />',
202 1
			esc_attr( $name ),
203 1
			esc_attr( $name ),
204 1
			esc_attr( $value )
205
		);
206 1
	}
207
208
	/**
209
	 * Get settings fields.
210
	 *
211
	 * @return array<int, array<string, callable|int|string|bool|array<int|string,int|string>>>
212
	 */
213 1
	public function get_settings_fields() {
214 1
		$fields = array();
215
216
		// Merchant Account.
217 1
		$fields[] = array(
218 1
			'section'  => 'general',
219 1
			'filter'   => FILTER_SANITIZE_STRING,
220 1
			'meta_key' => '_pronamic_gateway_adyen_merchant_account',
221 1
			'title'    => _x( 'Merchant Account', 'adyen', 'pronamic_ideal' ),
222 1
			'type'     => 'text',
223
			'classes'  => array( 'regular-text', 'code' ),
224 1
			'tooltip'  => __( 'The merchant account identifier, with which you want to process the transaction.', 'pronamic_ideal' ),
225
		);
226
227
		// API Key.
228 1
		$fields[] = array(
229 1
			'section'     => 'general',
230 1
			'filter'      => FILTER_SANITIZE_STRING,
231 1
			'meta_key'    => '_pronamic_gateway_adyen_api_key',
232 1
			'title'       => _x( 'API Key', 'adyen', 'pronamic_ideal' ),
233 1
			'type'        => 'textarea',
234
			'classes'     => array( 'code' ),
235 1
			'tooltip'     => __( 'API key as mentioned in the payment provider dashboard.', 'pronamic_ideal' ),
236 1
			'description' => sprintf(
237 1
				'<a href="%s" target="_blank">%s</a>',
238 1
				esc_url( 'https://docs.adyen.com/developers/user-management/how-to-get-the-api-key' ),
239 1
				esc_html__( 'Adyen documentation: "How to get the API key".', 'pronamic_ideal' )
240
			),
241
		);
242
243
		// Live API URL prefix.
244 1
		$fields[] = array(
245 1
			'section'     => 'general',
246 1
			'filter'      => FILTER_SANITIZE_STRING,
247 1
			'meta_key'    => '_pronamic_gateway_adyen_api_live_url_prefix',
248 1
			'title'       => _x( 'API Live URL Prefix', 'adyen', 'pronamic_ideal' ),
249 1
			'type'        => 'text',
250
			'classes'     => array( 'regular-text', 'code' ),
251 1
			'tooltip'     => __( 'The unique prefix for the live API URL, as mentioned at <strong>Account » API URLs</strong> in the Adyen dashboard.', 'pronamic_ideal' ),
252 1
			'description' => sprintf(
253 1
				'<a href="%s" target="_blank">%s</a>',
254 1
				esc_url( 'https://docs.adyen.com/developers/development-resources/live-endpoints#liveurlprefix' ),
255 1
				esc_html__( 'Adyen documentation: "Live URL prefix".', 'pronamic_ideal' )
256
			),
257
		);
258
259
		// Origin Key.
260 1
		$fields[] = array(
261 1
			'section'     => 'general',
262 1
			'filter'      => FILTER_SANITIZE_STRING,
263 1
			'meta_key'    => '_pronamic_gateway_adyen_origin_key',
264 1
			'title'       => _x( 'Origin Key', 'adyen', 'pronamic_ideal' ),
265 1
			'type'        => 'text',
266
			'classes'     => array(
267
				'regular-text',
268
				'code',
269
				'pronamic-pay-form-control-lg',
270
			),
271 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' ),
272 1
			'description' => sprintf(
273 1
				'<a href="%s" target="_blank">%s</a>',
274 1
				esc_url( 'https://docs.adyen.com/user-management/how-to-get-an-origin-key' ),
275 1
				esc_html__( 'Adyen documentation: "How to get an origin key".', 'pronamic_ideal' )
276
			),
277
		);
278
279
		// Merchant Order Reference.
280 1
		$fields[] = array(
281 1
			'section'     => 'advanced',
282
			'filter'      => array(
283
				'filter' => \FILTER_SANITIZE_STRING,
284
				'flags'  => \FILTER_FLAG_NO_ENCODE_QUOTES,
285
			),
286 1
			'meta_key'    => '_pronamic_gateway_adyen_merchant_order_reference',
287 1
			'title'       => __( 'Merchant Order Reference', 'pronamic_ideal' ),
288 1
			'type'        => 'text',
289
			'classes'     => array( 'regular-text', 'code' ),
290 1
			'tooltip'     => \sprintf(
291
				/* translators: %s: <code>{orderId}</code> */
292 1
				\__( 'The Adyen %s parameter.', 'pronamic_ideal' ),
293 1
				\sprintf( '<code>%s</code>', 'merchantOrderReference' )
294
			),
295 1
			'description' => \sprintf(
296 1
				'%s %s<br />%s',
297 1
				\__( 'Available tags:', 'pronamic_ideal' ),
298 1
				\sprintf(
299 1
					'<code>%s</code> <code>%s</code>',
300 1
					'{order_id}',
301 1
					'{payment_id}'
302
				),
303 1
				\sprintf(
304
					/* translators: %s: {payment_id} */
305 1
					\__( 'Default: <code>%s</code>', 'pronamic_ideal' ),
306 1
					'{payment_id}'
307
				)
308
			),
309
		);
310
311
		// Apple Pay - Merchant identifier.
312 1
		$fields[] = array(
313 1
			'section'     => 'advanced',
314
			'filter'      => \FILTER_SANITIZE_STRING,
315 1
			'meta_key'    => '_pronamic_gateway_adyen_apple_pay_merchant_id',
316 1
			'title'       => _x( 'Apple Pay Merchant ID', 'adyen', 'pronamic_ideal' ),
317 1
			'type'        => 'text',
318
			'classes'     => array( 'regular-text', 'code' ),
319 1
			'tooltip'     => __( 'Your Apple Pay Merchant ID. Required for accepting live payments.', 'pronamic_ideal' ),
320 1
			'description' => sprintf(
321 1
				'<a href="%s" target="_blank">%s</a><br /><a href="%s" target="_blank">%s</a>',
322 1
				esc_url( 'https://docs.adyen.com/payment-methods/apple-pay/web-drop-in#before-you-begin' ),
323 1
				esc_html__( 'Adyen documentation: "Apple Pay Drop-in - Before you begin".', 'pronamic_ideal' ),
324 1
				esc_url( 'https://developer.apple.com/documentation/apple_pay_on_the_web/configuring_your_environment' ),
325 1
				esc_html__( 'Apple documentation: "Configuring your environment".', 'pronamic_ideal' )
326
			),
327
		);
328
329
		// Apple Pay - Merchant Identity PKCS#12.
330 1
		$fields[] = array(
331 1
			'section'     => 'advanced',
332
			'filter'      => \FILTER_SANITIZE_STRING,
333 1
			'meta_key'    => '_pronamic_gateway_adyen_apple_pay_merchant_id_certificate',
334 1
			'title'       => __( 'Apple Pay Merchant Identity Certificate', 'pronamic_ideal' ),
335 1
			'type'        => 'textarea',
336 1
			'callback'    => array( $this, 'field_certificate' ),
337
			'classes'     => array( 'code' ),
338 1
			'tooltip'     => __( 'The Apple Pay Merchant Identity certificate required for secure communication with Apple.', 'pronamic_ideal' ),
339 1
			'description' => sprintf(
340 1
				'<a href="%s" target="_blank">%s</a>',
341 1
				esc_url( 'https://docs.adyen.com/payment-methods/apple-pay/enable-apple-pay#create-merchant-identity-certificate' ),
342 1
				esc_html__( 'Adyen documentation: "Enable Apple Pay - Create a merchant identity certificate".', 'pronamic_ideal' )
343
			),
344
		);
345
346
		// Apple Pay - Merchant Identity private key.
347 1
		$fields[] = array(
348 1
			'section'  => 'advanced',
349
			'filter'   => \FILTER_SANITIZE_STRING,
350 1
			'meta_key' => '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key',
351 1
			'title'    => __( 'Apple Pay Merchant Identity Private Key', 'pronamic_ideal' ),
352 1
			'type'     => 'textarea',
353 1
			'callback' => array( $this, 'field_private_key' ),
354
			'classes'  => array( 'code' ),
355 1
			'tooltip'  => __( 'The private key of the Apple Pay Merchant Identity certificate for secure communication with Apple.', 'pronamic_ideal' ),
356
		);
357
358
		// Apple Pay - Merchant Identity certificate private key password.
359 1
		$fields[] = array(
360 1
			'section'  => 'advanced',
361
			'filter'   => \FILTER_SANITIZE_STRING,
362 1
			'meta_key' => '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key_password',
363 1
			'title'    => _x( 'Apple Pay Merchant Identity Private Key Password', 'adyen', 'pronamic_ideal' ),
364 1
			'type'     => 'text',
365
			'classes'  => array( 'regular-text', 'code' ),
366 1
			'tooltip'  => __( 'Your Apple Pay Merchant Identity Certificate private key password.', 'pronamic_ideal' ),
367
		);
368
369
		// Google Pay - Merchant identifier.
370 1
		$fields[] = array(
371 1
			'section'     => 'advanced',
372
			'filter'      => \FILTER_SANITIZE_STRING,
373 1
			'meta_key'    => '_pronamic_gateway_adyen_google_pay_merchant_identifier',
374 1
			'title'       => _x( 'Google Pay Merchant ID', 'adyen', 'pronamic_ideal' ),
375 1
			'type'        => 'text',
376
			'classes'     => array( 'regular-text', 'code' ),
377 1
			'tooltip'     => __( 'Your Google Merchant ID. Required for accepting live payments.', 'pronamic_ideal' ),
378 1
			'description' => sprintf(
379 1
				'<a href="%s" target="_blank">%s</a><br /><a href="%s" target="_blank">%s</a>',
380 1
				esc_url( 'https://docs.adyen.com/payment-methods/google-pay/web-drop-in#test-and-go-live' ),
381 1
				esc_html__( 'Adyen documentation: "Google Pay Drop-in - Test and go live".', 'pronamic_ideal' ),
382 1
				esc_url( 'https://developers.google.com/pay/api/web/guides/test-and-deploy/deploy-production-environment' ),
383 1
				esc_html__( 'Google documentation: "Deploy production environment".', 'pronamic_ideal' )
384
			),
385
		);
386
387
		// Webhook URL.
388 1
		$fields[] = array(
389 1
			'section'  => 'feedback',
390 1
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
391 1
			'type'     => 'text',
392
			'classes'  => array( 'large-text', 'code' ),
393 1
			'value'    => rest_url( self::REST_ROUTE_NAMESPACE . '/notifications' ),
394
			'readonly' => true,
395 1
			'tooltip'  => sprintf(
396
				/* translators: %s: Adyen */
397 1
				__(
398 1
					'Copy the Webhook URL to the %s dashboard to receive automatic transaction status updates.',
399 1
					'pronamic_ideal'
400
				),
401 1
				__( 'Adyen', 'pronamic_ideal' )
402
			),
403
		);
404
405
		/**
406
		 * SSL Version.
407
		 *
408
		 * @link https://docs.adyen.com/developers/development-resources/notifications/set-up-notifications#step3configurenotificationsinthecustomerarea
409
		 * @link https://www.howsmyssl.com/a/check
410
		 */
411 1
		$fields[] = array(
412 1
			'section' => 'feedback',
413 1
			'title'   => __( 'SSL Version', 'pronamic_ideal' ),
414 1
			'type'    => 'description',
415 1
			'html'    => __( 'Choose the SSL Version of your server on the Adyen Customer Area.', 'pronamic_ideal' ),
416
		);
417
418
		/**
419
		 * Method.
420
		 *
421
		 * @link https://docs.adyen.com/developers/development-resources/notifications/set-up-notifications#step3configurenotificationsinthecustomerarea
422
		 * @link https://www.howsmyssl.com/a/check
423
		 */
424 1
		$fields[] = array(
425 1
			'section' => 'feedback',
426 1
			'title'   => _x( 'Method', 'adyen notification', 'pronamic_ideal' ),
427 1
			'type'    => 'description',
428 1
			'html'    => __( 'JSON', 'pronamic_ideal' ),
429
		);
430
431
		// Webhook authentication settings.
432 1
		$fields[] = array(
433 1
			'section' => 'feedback',
434 1
			'title'   => __( 'Authentication', 'pronamic_ideal' ),
435 1
			'type'    => 'description',
436 1
			'html'    => \sprintf(
437
				/* translators: %s: Pronamic Pay settings page URL. */
438 1
				__( 'Go to the <a href="%s">Pronamic Pay settings page</a> for webhook authentication settings.', 'pronamic_ideal' ),
439 1
				\esc_url(
440 1
					\add_query_arg(
441
						array(
442 1
							'page' => 'pronamic_pay_settings',
443
						),
444 1
						\admin_url( 'admin.php' )
445
					)
446
				)
447
			),
448
		);
449
450
		// Return fields.
451 1
		return $fields;
452
	}
453
454
	/**
455
	 * Field certificate.
456
	 *
457
	 * @param array<string> $field Field.
458
	 * @return void
459
	 */
460
	public function field_certificate( $field ) {
461
		if ( ! \array_key_exists( 'meta_key', $field ) ) {
462
			return;
463
		}
464
465
		$post_id = \get_the_ID();
466
467
		if ( false === $post_id ) {
468
			return;
469
		}
470
471
		$certificate = \get_post_meta( $post_id, $field['meta_key'], true );
472
473
		if ( ! empty( $certificate ) ) {
474
			$fingerprint = Security::get_sha_fingerprint( $certificate );
475
476
			echo '<dl>';
477
478
			if ( null !== $fingerprint ) {
479
				$fingerprint = \str_split( $fingerprint, 2 );
480
				$fingerprint = \implode( ':', $fingerprint );
481
482
				echo '<dt>', \esc_html__( 'SHA Fingerprint', 'pronamic_ideal' ), '</dt>';
483
				echo '<dd>', \esc_html( $fingerprint ), '</dd>';
484
			}
485
486
			$info = \openssl_x509_parse( $certificate );
487
488
			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...
489
				$date_format = __( 'M j, Y @ G:i', 'pronamic_ideal' );
490
491
				if ( isset( $info['validFrom_time_t'] ) ) {
492
					echo '<dt>', \esc_html__( 'Valid From', 'pronamic_ideal' ), '</dt>';
493
					echo '<dd>', \esc_html( \date_i18n( $date_format, $info['validFrom_time_t'] ) ), '</dd>';
494
				}
495
496
				if ( isset( $info['validTo_time_t'] ) ) {
497
					echo '<dt>', \esc_html__( 'Valid To', 'pronamic_ideal' ), '</dt>';
498
					echo '<dd>', \esc_html( \date_i18n( $date_format, $info['validTo_time_t'] ) ), '</dd>';
499
				}
500
			}
501
502
			echo '</dl>';
503
		} elseif ( false !== \strpos( $field['meta_key'], 'apple_pay' ) ) {
504
			\printf(
505
				'<p class="pronamic-pay-description description">%s</p><p>&nbsp;</p>',
506
				\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' )
507
			);
508
		}
509
510
		?>
511
		<p>
512
			<?php
513
514
			if ( ! empty( $certificate ) ) {
515
				\submit_button(
516
					__( 'Download', 'pronamic_ideal' ),
517
					'secondary',
518
					'download' . $field['meta_key'],
519
					false
520
				);
521
522
				echo ' ';
523
			}
524
525
			\printf(
526
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
527
				\esc_html__( 'Upload', 'pronamic_ideal' ),
528
				\esc_attr( $field['meta_key'] . '_file' )
529
			);
530
531
			?>
532
		</p>
533
		<?php
534
	}
535
536
	/**
537
	 * Field private key.
538
	 *
539
	 * @param array<string> $field Field.
540
	 * @return void
541
	 */
542
	public function field_private_key( $field ) {
543
		if ( ! \array_key_exists( 'meta_key', $field ) ) {
544
			return;
545
		}
546
547
		$post_id = \get_the_ID();
548
549
		if ( false === $post_id ) {
550
			return;
551
		}
552
553
		$private_key = \get_post_meta( $post_id, $field['meta_key'], true );
554
555
		?>
556
		<p>
557
			<?php
558
559
			if ( ! empty( $private_key ) ) {
560
				\submit_button(
561
					__( 'Download', 'pronamic_ideal' ),
562
					'secondary',
563
					'download' . $field['meta_key'],
564
					false
565
				);
566
567
				echo ' ';
568
			}
569
570
			if ( empty( $private_key ) && false !== \strpos( $field['meta_key'], 'apple_pay' ) ) {
571
				\printf(
572
					'<p class="pronamic-pay-description description">%s</p><p>&nbsp;</p>',
573
					\esc_html__( 'Leave empty to auto fill when uploading an Apple Pay Merchant Identity PKCS#12 certificate file.', 'pronamic_ideal' )
574
				);
575
			}
576
577
			\printf(
578
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
579
				\esc_html__( 'Upload', 'pronamic_ideal' ),
580
				\esc_attr( $field['meta_key'] . '_file' )
581
			);
582
583
			?>
584
		</p>
585
		<?php
586
	}
587
588
	/**
589
	 * Download certificate or key in Privacy Enhanced Mail (PEM) format.
590
	 *
591
	 * @return void
592
	 */
593
	public function maybe_download_certificate_or_key() {
594
		// Certificate fields and download filename.
595
		$fields = array(
596
			'_pronamic_gateway_adyen_apple_pay_merchant_id_certificate' => 'apple-pay-merchant-identity-certificate-%s.pem',
597
			'_pronamic_gateway_adyen_apple_pay_merchant_id_private_key' => 'apple-pay-merchant-identity-private-key-%s.pem',
598
		);
599
600
		// Check download actions.
601
		$is_download_action = false;
602
603
		foreach ( $fields as $meta_key => $filename ) {
604
			if ( \filter_has_var( \INPUT_POST, 'download' . $meta_key ) ) {
605
				$is_download_action = true;
606
607
				break;
608
			}
609
		}
610
611
		// No valid download action found.
612
		if ( false === $is_download_action ) {
613
			return;
614
		}
615
616
		$post_id = filter_input( \INPUT_POST, 'post_ID', \FILTER_SANITIZE_STRING );
617
618
		$filename = sprintf( $filename, $post_id );
619
620
		header( 'Content-Description: File Transfer' );
621
		header( 'Content-Disposition: attachment; filename=' . $filename );
622
		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

622
		header( 'Content-Type: application/x-pem-file; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
623
624
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
625
		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

625
		echo /** @scrutinizer ignore-type */ get_post_meta( $post_id, $meta_key, true );
Loading history...
626
627
		exit;
628
	}
629
630
	/**
631
	 * Save post.
632
	 *
633
	 * @param int $post_id Post ID.
634
	 * @return void
635
	 */
636
	public function save_post( $post_id ) {
637
		// Files.
638
		$files = array(
639
			'_pronamic_gateway_adyen_apple_pay_merchant_id_certificate_file' => '_pronamic_gateway_adyen_apple_pay_merchant_id_certificate',
640
			'_pronamic_gateway_adyen_apple_pay_merchant_id_private_key_file' => '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key',
641
		);
642
643
		foreach ( $files as $name => $meta_key ) {
644
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
645
			if ( isset( $_FILES[ $name ] ) && \UPLOAD_ERR_OK === $_FILES[ $name ]['error'] ) {
646
				// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
647
				$value = file_get_contents( $_FILES[ $name ]['tmp_name'], true );
648
649
				update_post_meta( $post_id, $meta_key, $value );
650
			}
651
		}
652
653
		// Update Apple Pay Merchant Identity certificate and private key from uploaded PKCS#12 file.
654
		$apple_pay_merchant_id_pkcs12 = get_post_meta( $post_id, '_pronamic_gateway_adyen_apple_pay_merchant_id_certificate', true );
655
656
		if ( ! empty( $apple_pay_merchant_id_pkcs12 ) ) {
657
			// Try to read file without using password.
658
			$pkcs12_read = \openssl_pkcs12_read( $apple_pay_merchant_id_pkcs12, $certs, '' );
659
660
			$password = \get_post_meta( $post_id, '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key_password', true );
661
662
			// Try to read file with private key password.
663
			if ( false === $pkcs12_read ) {
664
				$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

664
				$pkcs12_read = \openssl_pkcs12_read( $apple_pay_merchant_id_pkcs12, $certs, /** @scrutinizer ignore-type */ $password );
Loading history...
665
			}
666
667
			if ( true === $pkcs12_read ) {
668
				if ( isset( $certs['cert'] ) ) {
669
					\update_post_meta( $post_id, '_pronamic_gateway_adyen_apple_pay_merchant_id_certificate', $certs['cert'] );
670
				}
671
672
				if ( isset( $certs['pkey'] ) ) {
673
					$private_key = $certs['pkey'];
674
675
					$cipher = null;
676
677
					// Try to export the private key encrypted.
678
					if ( defined( 'OPENSSL_CIPHER_AES_128_CBC' ) ) {
679
						$cipher = \OPENSSL_CIPHER_AES_128_CBC;
680
					} elseif ( defined( 'OPENSSL_CIPHER_3DES' ) ) {
681
						$cipher = \OPENSSL_CIPHER_3DES;
682
					}
683
684
					if ( null !== $cipher && '' !== $password ) {
685
						$args = array(
686
							'digest_alg'             => 'SHA256',
687
							'private_key_bits'       => 2048,
688
							'private_key_type'       => \OPENSSL_KEYTYPE_RSA,
689
							'encrypt_key'            => true,
690
							'encrypt_key_cipher'     => $cipher,
691
							'subjectKeyIdentifier'   => 'hash',
692
							'authorityKeyIdentifier' => 'keyid:always,issuer:always',
693
							'basicConstraints'       => 'CA:true',
694
						);
695
696
						\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

696
						\openssl_pkey_export( $certs['pkey'], $private_key, /** @scrutinizer ignore-type */ $password, $args );
Loading history...
697
					}
698
699
					\update_post_meta( $post_id, '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key', $private_key );
700
				}
701
			}
702
		}
703
	}
704
705
	/**
706
	 * Get configuration by post ID.
707
	 *
708
	 * @param int $post_id Post ID.
709
	 * @return Config
710
	 */
711 1
	public function get_config( $post_id ) {
712 1
		$config = new Config();
713
714 1
		$config->mode                                       = $this->get_meta( $post_id, 'mode' );
715 1
		$config->api_key                                    = $this->get_meta( $post_id, 'adyen_api_key' );
716 1
		$config->api_live_url_prefix                        = $this->get_meta( $post_id, 'adyen_api_live_url_prefix' );
717 1
		$config->merchant_account                           = $this->get_meta( $post_id, 'adyen_merchant_account' );
718 1
		$config->origin_key                                 = $this->get_meta( $post_id, 'adyen_origin_key' );
719 1
		$config->merchant_order_reference                   = $this->get_meta( $post_id, 'adyen_merchant_order_reference' );
720 1
		$config->apple_pay_merchant_id                      = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id' );
721 1
		$config->apple_pay_merchant_id_certificate          = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id_certificate' );
722 1
		$config->apple_pay_merchant_id_private_key          = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id_private_key' );
723 1
		$config->apple_pay_merchant_id_private_key_password = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id_private_key_password' );
724 1
		$config->google_pay_merchant_identifier             = $this->get_meta( $post_id, 'adyen_google_pay_merchant_identifier' );
725
726 1
		return $config;
727
	}
728
729
	/**
730
	 * Get gateway.
731
	 *
732
	 * @param int $post_id Post ID.
733
	 * @return AbstractGateway
734
	 */
735 1
	public function get_gateway( $post_id ) {
736 1
		$config = $this->get_config( $post_id );
737
738 1
		if ( empty( $config->origin_key ) ) {
739 1
			return new WebSdkGateway( $config );
740
		}
741
742
		return new DropInGateway( $config );
743
	}
744
}
745