Failed Conditions
Push — develop ( 3f8bd2...52e6a5 )
by Remco
138:45 queued 134:20
created

src/Integration.php (15 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\IDealAdvancedV3;
4
5
use Pronamic\WordPress\Pay\Gateways\IDeal\AbstractIntegration;
6
7
/**
8
 * Title: iDEAL Advanced v3 integration
9
 * Description:
10
 * Copyright: 2005-2019 Pronamic
11
 * Company: Pronamic
12
 *
13
 * @author  Remco Tolsma
14
 * @version 2.0.0
15
 * @since   2.0.0
16
 */
17
class Integration extends AbstractIntegration {
18
	/**
19
	 * Settings constructor.
20
	 */
21
	public function __construct( $args = array() ) {
22
		$args = wp_parse_args(
23
			$args,
24
			array(
25
				'id'               => 'ideal-advanced-v3',
26
				'name'             => 'iDEAL Advanced v3',
27
				'url'              => __( 'https://www.ideal.nl/en/', 'pronamic_ideal' ),
28
				'product_url'      => __( 'https://www.ideal.nl/en/', 'pronamic_ideal' ),
29
				'dashboard_url'    => null,
30
				'provider'         => null,
31
				'aquirer_url'      => null,
32
				'aquirer_test_url' => null,
33
			)
34
		);
35
36
		$this->id            = $args['id'];
37
		$this->name          = $args['name'];
38
		$this->url           = $args['url'];
39
		$this->product_url   = $args['product_url'];
40
		$this->dashboard_url = $args['dashboard_url'];
41
		$this->provider      = $args['provider'];
42
43
		$this->aquirer_url      = $args['aquirer_url'];
0 ignored issues
show
Bug Best Practice introduced by
The property aquirer_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
		$this->aquirer_test_url = $args['aquirer_test_url'];
0 ignored issues
show
Bug Best Practice introduced by
The property aquirer_test_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
46
		// Supported features.
47
		$this->supports = array(
48
			'payment_status_request',
49
		);
50
51
		// Actions.
52
		add_action( 'current_screen', array( $this, 'maybe_download_private_certificate' ) );
53
		add_action( 'current_screen', array( $this, 'maybe_download_private_key' ) );
54
	}
55
56
	public function get_settings_fields() {
57
		$fields = parent::get_settings_fields();
58
59
		/*
60
		 * Private Key and Certificate
61
		 */
62
63
		// Private key and certificate information.
64
		$fields[] = array(
65
			'section'  => 'general',
66
			'title'    => __( 'Private key and certificate', 'pronamic_ideal' ),
67
			'type'     => 'description',
68
			'callback' => array( $this, 'field_security' ),
69
		);
70
71
		// Organization.
72
		$fields[] = array(
73
			'section'  => 'general',
74
			'filter'   => FILTER_SANITIZE_STRING,
75
			'group'    => 'pk-cert',
76
			'meta_key' => '_pronamic_gateway_organization',
77
			'title'    => __( 'Organization', 'pronamic_ideal' ),
78
			'type'     => 'text',
79
			'tooltip'  => __( 'Organization name, e.g. Pronamic', 'pronamic_ideal' ),
80
		);
81
82
		// Organization Unit.
83
		$fields[] = array(
84
			'section'  => 'general',
85
			'filter'   => FILTER_SANITIZE_STRING,
86
			'group'    => 'pk-cert',
87
			'meta_key' => '_pronamic_gateway_organization_unit',
88
			'title'    => __( 'Organization Unit', 'pronamic_ideal' ),
89
			'type'     => 'text',
90
			'tooltip'  => __( 'Organization unit, e.g. Administration', 'pronamic_ideal' ),
91
		);
92
93
		// Locality.
94
		$fields[] = array(
95
			'section'  => 'general',
96
			'filter'   => FILTER_SANITIZE_STRING,
97
			'group'    => 'pk-cert',
98
			'meta_key' => '_pronamic_gateway_locality',
99
			'title'    => __( 'City', 'pronamic_ideal' ),
100
			'type'     => 'text',
101
			'tooltip'  => __( 'City, e.g. Amsterdam', 'pronamic_ideal' ),
102
		);
103
104
		// State or Province.
105
		$fields[] = array(
106
			'section'  => 'general',
107
			'filter'   => FILTER_SANITIZE_STRING,
108
			'group'    => 'pk-cert',
109
			'meta_key' => '_pronamic_gateway_state_or_province',
110
			'title'    => __( 'State / province', 'pronamic_ideal' ),
111
			'type'     => 'text',
112
			'tooltip'  => __( 'State or province, e.g. Friesland', 'pronamic_ideal' ),
113
		);
114
115
		// Country.
116
		$locale = explode( '_', get_locale() );
117
118
		$locale = array_pop( $locale );
119
120
		$fields[] = array(
121
			'section'     => 'general',
122
			'filter'      => FILTER_SANITIZE_STRING,
123
			'group'       => 'pk-cert',
124
			'meta_key'    => '_pronamic_gateway_country',
125
			'title'       => __( 'Country', 'pronamic_ideal' ),
126
			'type'        => 'text',
127
			'tooltip'     => sprintf(
128
				'%s %s (ISO-3166-1 alpha-2)',
129
				__( '2 letter country code, e.g.', 'pronamic_ideal' ),
130
				strtoupper( $locale )
131
			),
132
			'size'        => 2,
133
			'description' => sprintf(
134
				'%s %s',
135
				__( '2 letter country code, e.g.', 'pronamic_ideal' ),
136
				strtoupper( $locale )
137
			),
138
		);
139
140
		// Email Address.
141
		$fields[] = array(
142
			'section'  => 'general',
143
			'filter'   => FILTER_SANITIZE_STRING,
144
			'group'    => 'pk-cert',
145
			'meta_key' => '_pronamic_gateway_email',
146
			'title'    => __( 'E-mail address', 'pronamic_ideal' ),
147
			'tooltip'  => sprintf(
148
				/* translators: %s: admin email */
149
				__( 'E-mail address, e.g. %s', 'pronamic_ideal' ),
150
				get_option( 'admin_email' )
0 ignored issues
show
It seems like get_option('admin_email') can also be of type false; however, parameter $args of sprintf() 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

150
				/** @scrutinizer ignore-type */ get_option( 'admin_email' )
Loading history...
151
			),
152
			'type'     => 'text',
153
		);
154
155
		// Number Days Valid.
156
		$fields[] = array(
157
			'section'  => 'general',
158
			'filter'   => FILTER_SANITIZE_NUMBER_INT,
159
			'group'    => 'pk-cert',
160
			'meta_key' => '_pronamic_gateway_number_days_valid',
161
			'title'    => __( 'Number Days Valid', 'pronamic_ideal' ),
162
			'type'     => 'text',
163
			'default'  => 1825,
164
			'tooltip'  => __( 'Number of days the generated certificate will be valid for, e.g. 1825 days for the maximum duration of 5 years.', 'pronamic_ideal' ),
165
		);
166
167
		// Private Key Password.
168
		$fields[] = array(
169
			'section'  => 'general',
170
			'filter'   => FILTER_SANITIZE_STRING,
171
			'group'    => 'pk-cert',
172
			'meta_key' => '_pronamic_gateway_ideal_private_key_password',
173
			'title'    => __( 'Private Key Password', 'pronamic_ideal' ),
174
			'type'     => 'text',
175
			'classes'  => array( 'regular-text', 'code' ),
176
			'default'  => wp_generate_password(),
177
			'tooltip'  => __( 'A random password which will be used for the generation of the private key and certificate.', 'pronamic_ideal' ),
178
		);
179
180
		// Private Key.
181
		$fields[] = array(
182
			'section'  => 'general',
183
			'filter'   => FILTER_SANITIZE_STRING,
184
			'group'    => 'pk-cert',
185
			'meta_key' => '_pronamic_gateway_ideal_private_key',
186
			'title'    => __( 'Private Key', 'pronamic_ideal' ),
187
			'type'     => 'textarea',
188
			'callback' => array( $this, 'field_private_key' ),
189
			'classes'  => array( 'code' ),
190
			'tooltip'  => __( 'The private key is used for secure communication with the payment provider. If left empty, the private key will be generated using the given private key password.', 'pronamic_ideal' ),
191
		);
192
193
		// Private Certificate.
194
		$fields[] = array(
195
			'section'  => 'general',
196
			'filter'   => FILTER_SANITIZE_STRING,
197
			'group'    => 'pk-cert',
198
			'meta_key' => '_pronamic_gateway_ideal_private_certificate',
199
			'title'    => __( 'Private Certificate', 'pronamic_ideal' ),
200
			'type'     => 'textarea',
201
			'callback' => array( $this, 'field_private_certificate' ),
202
			'classes'  => array( 'code' ),
203
			'tooltip'  => __( 'The certificate is used for secure communication with the payment provider. If left empty, the certificate will be generated using the private key and given organization details.', 'pronamic_ideal' ),
204
		);
205
206
		// Return.
207
		return $fields;
208
	}
209
210
	/**
211
	 * Field security
212
	 *
213
	 * @param array $field Field.
214
	 */
215
	public function field_security( $field ) {
216
		$certificate = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_certificate', 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

216
		$certificate = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
Loading history...
217
218
		?>
219
		<p>
220
			<?php if ( empty( $certificate ) ) : ?>
221
222
				<span
223
					class="dashicons dashicons-no"></span> <?php esc_html_e( 'The private key and certificate have not yet been configured.', 'pronamic_ideal' ); ?>
224
				<br/>
225
226
				<br/>
227
228
				<?php esc_html_e( 'A private key and certificate are required for communication with the payment provider. Enter the organization details from the iDEAL account below to generate these required files.', 'pronamic_ideal' ); ?>
229
230
			<?php else : ?>
231
232
				<span
233
					class="dashicons dashicons-yes"></span> <?php esc_html_e( 'A private key and certificate have been configured. The certificate must be uploaded to the payment provider dashboard to complete configuration.', 'pronamic_ideal' ); ?>
234
				<br/>
235
236
				<br/>
237
238
				<?php
239
240
				submit_button(
241
					__( 'Download certificate', 'pronamic_ideal' ),
242
					'secondary',
243
					'download_private_certificate',
244
					false
245
				);
246
247
				?>
248
249
				<a class="pronamic-pay-btn-link" href="#" id="pk-cert-fields-toggle"><?php esc_html_e( 'Show details…', 'pronamic_ideal' ); ?></a>
250
251
			<?php endif; ?>
252
		</p>
253
		<?php
254
	}
255
256
	/**
257
	 * Field private key.
258
	 *
259
	 * @param array $field Field.
260
	 */
261
	public function field_private_key( $field ) {
262
		$private_key          = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_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

262
		$private_key          = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_key', true );
Loading history...
263
		$private_key_password = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_key_password', true );
264
		$number_days_valid    = get_post_meta( get_the_ID(), '_pronamic_gateway_number_days_valid', true );
265
266
		$filename = __( 'ideal.key', 'pronamic_ideal' );
267
268
		if ( ! empty( $private_key_password ) && ! empty( $number_days_valid ) ) {
269
			$command = sprintf(
270
				'openssl genrsa -aes128 -out %s -passout pass:%s 2048',
271
				escapeshellarg( $filename ),
272
				escapeshellarg( $private_key_password )
273
			);
274
275
			?>
276
277
			<p><?php esc_html_e( 'OpenSSL command', 'pronamic_ideal' ); ?></p>
278
			<input id="pronamic_ideal_openssl_command_key" name="pronamic_ideal_openssl_command_key" value="<?php echo esc_attr( $command ); ?>" type="text" class="large-text code" readonly="readonly"/>
279
280
			<?php
281
		} else {
282
			printf(
283
				'<p class="pronamic-pay-description description">%s</p>',
284
				esc_html__( 'Leave empty and save the configuration to generate the private key or view the OpenSSL command.', 'pronamic_ideal' )
285
			);
286
		}
287
288
		?>
289
		<p>
290
			<?php
291
292
			if ( ! empty( $private_key ) ) {
293
				submit_button(
294
					__( 'Download', 'pronamic_ideal' ),
295
					'secondary',
296
					'download_private_key',
297
					false
298
				);
299
300
				echo ' ';
301
			}
302
303
			printf(
304
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
305
				esc_html__( 'Upload', 'pronamic_ideal' ),
306
				'_pronamic_gateway_ideal_private_key_file'
307
			);
308
309
			?>
310
		</p>
311
		<?php
312
	}
313
314
	/**
315
	 * Field private certificate.
316
	 *
317
	 * @param array $field Field.
318
	 */
319
	public function field_private_certificate( $field ) {
320
		$certificate = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_certificate', 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

320
		$certificate = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
Loading history...
321
322
		$private_key_password = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_key_password', true );
323
		$number_days_valid    = get_post_meta( get_the_ID(), '_pronamic_gateway_number_days_valid', true );
324
325
		$filename_key = __( 'ideal.key', 'pronamic_ideal' );
326
		$filename_cer = __( 'ideal.cer', 'pronamic_ideal' );
327
328
		// @link http://www.openssl.org/docs/apps/req.html
329
		$subj_args = array(
330
			'C'            => get_post_meta( get_the_ID(), '_pronamic_gateway_country', true ),
331
			'ST'           => get_post_meta( get_the_ID(), '_pronamic_gateway_state_or_province', true ),
332
			'L'            => get_post_meta( get_the_ID(), '_pronamic_gateway_locality', true ),
333
			'O'            => get_post_meta( get_the_ID(), '_pronamic_gateway_organization', true ),
334
			'OU'           => get_post_meta( get_the_ID(), '_pronamic_gateway_organization_unit', true ),
335
			'CN'           => get_post_meta( get_the_ID(), '_pronamic_gateway_organization', true ),
336
			'emailAddress' => get_post_meta( get_the_ID(), '_pronamic_gateway_email', true ),
337
		);
338
339
		$subj_args = array_filter( $subj_args );
340
341
		$subj = '';
342
		foreach ( $subj_args as $type => $value ) {
343
			$subj .= '/' . $type . '=' . addslashes( $value );
344
		}
345
346
		if ( ! empty( $subj ) ) {
347
			$command = trim(
348
				sprintf(
349
					'openssl req -x509 -sha256 -new -key %s -passin pass:%s -days %s -out %s %s',
350
					escapeshellarg( $filename_key ),
351
					escapeshellarg( $private_key_password ),
0 ignored issues
show
It seems like $private_key_password can also be of type false; however, parameter $arg of escapeshellarg() 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

351
					escapeshellarg( /** @scrutinizer ignore-type */ $private_key_password ),
Loading history...
352
					escapeshellarg( $number_days_valid ),
353
					escapeshellarg( $filename_cer ),
354
					empty( $subj ) ? '' : sprintf( '-subj %s', escapeshellarg( $subj ) )
355
				)
356
			);
357
358
			?>
359
360
			<p><?php esc_html_e( 'OpenSSL command', 'pronamic_ideal' ); ?></p>
361
			<input id="pronamic_ideal_openssl_command_certificate" name="pronamic_ideal_openssl_command_certificate" value="<?php echo esc_attr( $command ); ?>" type="text" class="large-text code" readonly="readonly"/>
362
363
			<?php
364
		} else {
365
			printf(
366
				'<p class="pronamic-pay-description description">%s</p>',
367
				esc_html__( 'Leave empty and save the configuration to generate the certificate or view the OpenSSL command.', 'pronamic_ideal' )
368
			);
369
		}
370
371
		if ( ! empty( $certificate ) ) {
372
			$fingerprint = Security::get_sha_fingerprint( $certificate );
373
			$fingerprint = str_split( $fingerprint, 2 );
374
			$fingerprint = implode( ':', $fingerprint );
375
376
			echo '<dl>';
377
378
			echo '<dt>', esc_html__( 'SHA Fingerprint', 'pronamic_ideal' ), '</dt>';
379
			echo '<dd>', esc_html( $fingerprint ), '</dd>';
380
381
			$info = openssl_x509_parse( $certificate );
382
383
			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...
384
				$date_format = __( 'M j, Y @ G:i', 'pronamic_ideal' );
385
386
				if ( isset( $info['validFrom_time_t'] ) ) {
387
					echo '<dt>', esc_html__( 'Valid From', 'pronamic_ideal' ), '</dt>';
388
					echo '<dd>', esc_html( date_i18n( $date_format, $info['validFrom_time_t'] ) ), '</dd>';
389
				}
390
391
				if ( isset( $info['validTo_time_t'] ) ) {
392
					echo '<dt>', esc_html__( 'Valid To', 'pronamic_ideal' ), '</dt>';
393
					echo '<dd>', esc_html( date_i18n( $date_format, $info['validTo_time_t'] ) ), '</dd>';
394
				}
395
			}
396
397
			echo '</dl>';
398
		}
399
400
		?>
401
		<p>
402
			<?php
403
404
			if ( ! empty( $certificate ) ) {
405
				submit_button(
406
					__( 'Download', 'pronamic_ideal' ),
407
					'secondary',
408
					'download_private_certificate',
409
					false
410
				);
411
412
				echo ' ';
413
			}
414
415
			printf(
416
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
417
				esc_html__( 'Upload', 'pronamic_ideal' ),
418
				'_pronamic_gateway_ideal_private_certificate_file'
419
			);
420
421
			?>
422
		</p>
423
		<?php
424
	}
425
426
	/**
427
	 * Download private certificate
428
	 */
429
	public function maybe_download_private_certificate() {
430
		if ( filter_has_var( INPUT_POST, 'download_private_certificate' ) ) {
431
			$post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_STRING );
432
433
			$filename = sprintf( 'ideal-private-certificate-%s.cer', $post_id );
434
435
			header( 'Content-Description: File Transfer' );
436
			header( 'Content-Disposition: attachment; filename=' . $filename );
437
			header( 'Content-Type: application/x-x509-ca-cert; 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

437
			header( 'Content-Type: application/x-x509-ca-cert; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
438
439
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
440
			echo get_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', true );
0 ignored issues
show
Are you sure get_post_meta($post_id, ...ate_certificate', 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

440
			echo /** @scrutinizer ignore-type */ get_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', true );
Loading history...
441
442
			exit;
443
		}
444
	}
445
446
	/**
447
	 * Download private key
448
	 */
449
	public function maybe_download_private_key() {
450
		if ( filter_has_var( INPUT_POST, 'download_private_key' ) ) {
451
			$post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_STRING );
452
453
			$filename = sprintf( 'ideal-private-key-%s.key', $post_id );
454
455
			header( 'Content-Description: File Transfer' );
456
			header( 'Content-Disposition: attachment; filename=' . $filename );
457
			header( 'Content-Type: application/pgp-keys; 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

457
			header( 'Content-Type: application/pgp-keys; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
458
459
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
460
			echo get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key', true );
0 ignored issues
show
Are you sure get_post_meta($post_id, ...eal_private_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

460
			echo /** @scrutinizer ignore-type */ get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key', true );
Loading history...
461
462
			exit;
463
		}
464
	}
465
466
	/**
467
	 * Save post.
468
	 *
469
	 * @param int $post_id Post ID.
470
	 */
471
	public function save_post( $post_id ) {
472
		// Files.
473
		$files = array(
474
			'_pronamic_gateway_ideal_private_key_file' => '_pronamic_gateway_ideal_private_key',
475
			'_pronamic_gateway_ideal_private_certificate_file' => '_pronamic_gateway_ideal_private_certificate',
476
		);
477
478
		foreach ( $files as $name => $meta_key ) {
479
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
480
			if ( isset( $_FILES[ $name ] ) && UPLOAD_ERR_OK === $_FILES[ $name ]['error'] ) {
481
				// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
482
				$value = file_get_contents( $_FILES[ $name ]['tmp_name'] );
483
484
				update_post_meta( $post_id, $meta_key, $value );
485
			}
486
		}
487
488
		// Generate private key and certificate.
489
		$private_key          = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key', true );
490
		$private_key_password = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key_password', true );
491
492
		if ( empty( $private_key_password ) ) {
493
			// Without private key password we can't create private key and certifiate.
494
			return;
495
		}
496
497
		if ( ! in_array( 'aes-128-cbc', openssl_get_cipher_methods(), true ) ) {
498
			// Without AES-128-CBC ciphter method we can't create private key and certificate.
499
			return;
500
		}
501
502
		// Private key.
503
		$pkey = openssl_pkey_get_private( $private_key, $private_key_password );
504
505
		if ( false === $pkey ) {
506
			// If we can't open the private key we will create a new private key and certificate.
507
			if ( defined( 'OPENSSL_CIPHER_AES_128_CBC' ) ) {
508
				$cipher = OPENSSL_CIPHER_AES_128_CBC;
509
			} elseif ( defined( 'OPENSSL_CIPHER_3DES' ) ) {
510
				// @link https://www.pronamic.nl/wp-content/uploads/2011/12/iDEAL_Advanced_PHP_EN_V2.2.pdf
511
				$cipher = OPENSSL_CIPHER_3DES;
512
			} else {
513
				// Unable to create private key without cipher.
514
				return;
515
			}
516
517
			$args = array(
518
				'digest_alg'             => 'SHA256',
519
				'private_key_bits'       => 2048,
520
				'private_key_type'       => OPENSSL_KEYTYPE_RSA,
521
				'encrypt_key'            => true,
522
				'encrypt_key_cipher'     => $cipher,
523
				'subjectKeyIdentifier'   => 'hash',
524
				'authorityKeyIdentifier' => 'keyid:always,issuer:always',
525
				'basicConstraints'       => 'CA:true',
526
			);
527
528
			$pkey = openssl_pkey_new( $args );
529
530
			if ( false === $pkey ) {
531
				return;
532
			}
533
534
			// Export key.
535
			$result = openssl_pkey_export( $pkey, $private_key, $private_key_password, $args );
0 ignored issues
show
It seems like $private_key can also be of type false; however, parameter $out 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

535
			$result = openssl_pkey_export( $pkey, /** @scrutinizer ignore-type */ $private_key, $private_key_password, $args );
Loading history...
536
537
			if ( false === $result ) {
538
				return;
539
			}
540
541
			update_post_meta( $post_id, '_pronamic_gateway_ideal_private_key', $private_key );
542
543
			// Delete private certificate since this is no longer valid.
544
			delete_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate' );
545
		}
546
547
		// Certificate.
548
		$private_certificate = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', true );
549
		$number_days_valid   = get_post_meta( $post_id, '_pronamic_gateway_number_days_valid', true );
550
551
		if ( empty( $private_certificate ) ) {
552
			$required_keys = array(
553
				'countryName',
554
				'stateOrProvinceName',
555
				'localityName',
556
				'organizationName',
557
				'commonName',
558
				'emailAddress',
559
			);
560
561
			$distinguished_name = array(
562
				'countryName'            => get_post_meta( $post_id, '_pronamic_gateway_country', true ),
563
				'stateOrProvinceName'    => get_post_meta( $post_id, '_pronamic_gateway_state_or_province', true ),
564
				'localityName'           => get_post_meta( $post_id, '_pronamic_gateway_locality', true ),
565
				'organizationName'       => get_post_meta( $post_id, '_pronamic_gateway_organization', true ),
566
				'organizationalUnitName' => get_post_meta( $post_id, '_pronamic_gateway_organization_unit', true ),
567
				'commonName'             => get_post_meta( $post_id, '_pronamic_gateway_organization', true ),
568
				'emailAddress'           => get_post_meta( $post_id, '_pronamic_gateway_email', true ),
569
			);
570
571
			$distinguished_name = array_filter( $distinguished_name );
572
573
			/*
574
			 * Create certificate only if distinguished name contains all required elements
575
			 *
576
			 * @link http://stackoverflow.com/questions/13169588/how-to-check-if-multiple-array-keys-exists
577
			 */
578
			if ( count( array_intersect_key( array_flip( $required_keys ), $distinguished_name ) ) === count( $required_keys ) ) {
579
				$csr = openssl_csr_new( $distinguished_name, $pkey );
580
581
				$cert = openssl_csr_sign( $csr, null, $pkey, $number_days_valid, $args, time() );
0 ignored issues
show
It seems like $number_days_valid can also be of type false and string; however, parameter $days of openssl_csr_sign() 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

581
				$cert = openssl_csr_sign( $csr, null, $pkey, /** @scrutinizer ignore-type */ $number_days_valid, $args, time() );
Loading history...
582
583
				openssl_x509_export( $cert, $certificate );
584
585
				update_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', $certificate );
586
			}
587
		}
588
	}
589
590
	public function get_config( $post_id ) {
591
		$mode = get_post_meta( $post_id, '_pronamic_gateway_mode', true );
592
593
		$config = new Config();
594
595
		$config->payment_server_url = $this->aquirer_url;
596
597
		if ( 'test' === $mode && null !== $this->aquirer_test_url ) {
598
			$config->payment_server_url = $this->aquirer_test_url;
599
		}
600
601
		$config->merchant_id = get_post_meta( $post_id, '_pronamic_gateway_ideal_merchant_id', true );
602
		$config->sub_id      = get_post_meta( $post_id, '_pronamic_gateway_ideal_sub_id', true );
603
		$config->purchase_id = get_post_meta( $post_id, '_pronamic_gateway_ideal_purchase_id', true );
0 ignored issues
show
The property purchase_id does not seem to exist on Pronamic\WordPress\Pay\G...\IDealAdvancedV3\Config.
Loading history...
604
605
		$config->private_key          = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key', true );
606
		$config->private_key_password = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key_password', true );
607
		$config->private_certificate  = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', true );
608
609
		return $config;
610
	}
611
612
	/**
613
	 * Get gateway.
614
	 *
615
	 * @param int $post_id Post ID.
616
	 * @return Gateway
617
	 */
618
	public function get_gateway( $post_id ) {
619
		return new Gateway( $this->get_config( $post_id ) );
620
	}
621
}
622