Failed Conditions
Push — develop ( ea4858...c314dc )
by Reüel
07:27
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-2020 Pronamic
11
 * Company: Pronamic
12
 *
13
 * @author  Remco Tolsma
14
 * @version 2.0.5
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
				'manual_url'       => null,
30
				'dashboard_url'    => null,
31
				'provider'         => null,
32
				'aquirer_url'      => null,
33
				'aquirer_test_url' => null,
34
			)
35
		);
36
37
		$this->id            = $args['id'];
38
		$this->name          = $args['name'];
39
		$this->url           = $args['url'];
40
		$this->product_url   = $args['product_url'];
41
		$this->dashboard_url = $args['dashboard_url'];
42
		$this->provider      = $args['provider'];
43
44
		$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...
45
		$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...
46
47
		$this->set_manual_url( $args['manual_url'] );
48
49
		// Supported features.
50
		$this->supports = array(
51
			'payment_status_request',
52
		);
53
54
		// Actions.
55
		add_action( 'current_screen', array( $this, 'maybe_download_private_certificate' ) );
56
		add_action( 'current_screen', array( $this, 'maybe_download_private_key' ) );
57
	}
58
59
	public function get_settings_fields() {
60
		$fields = parent::get_settings_fields();
61
62
		/*
63
		 * Private Key and Certificate
64
		 */
65
66
		// Private key and certificate information.
67
		$fields[] = array(
68
			'section'  => 'general',
69
			'title'    => __( 'Private key and certificate', 'pronamic_ideal' ),
70
			'type'     => 'description',
71
			'callback' => array( $this, 'field_security' ),
72
		);
73
74
		// Organization.
75
		$fields[] = array(
76
			'section'  => 'general',
77
			'filter'   => FILTER_SANITIZE_STRING,
78
			'group'    => 'pk-cert',
79
			'meta_key' => '_pronamic_gateway_organization',
80
			'title'    => __( 'Organization', 'pronamic_ideal' ),
81
			'type'     => 'text',
82
			'tooltip'  => __( 'Organization name, e.g. Pronamic', 'pronamic_ideal' ),
83
		);
84
85
		// Organization Unit.
86
		$fields[] = array(
87
			'section'  => 'general',
88
			'filter'   => FILTER_SANITIZE_STRING,
89
			'group'    => 'pk-cert',
90
			'meta_key' => '_pronamic_gateway_organization_unit',
91
			'title'    => __( 'Organization Unit', 'pronamic_ideal' ),
92
			'type'     => 'text',
93
			'tooltip'  => __( 'Organization unit, e.g. Administration', 'pronamic_ideal' ),
94
		);
95
96
		// Locality.
97
		$fields[] = array(
98
			'section'  => 'general',
99
			'filter'   => FILTER_SANITIZE_STRING,
100
			'group'    => 'pk-cert',
101
			'meta_key' => '_pronamic_gateway_locality',
102
			'title'    => __( 'City', 'pronamic_ideal' ),
103
			'type'     => 'text',
104
			'tooltip'  => __( 'City, e.g. Amsterdam', 'pronamic_ideal' ),
105
		);
106
107
		// State or Province.
108
		$fields[] = array(
109
			'section'  => 'general',
110
			'filter'   => FILTER_SANITIZE_STRING,
111
			'group'    => 'pk-cert',
112
			'meta_key' => '_pronamic_gateway_state_or_province',
113
			'title'    => __( 'State / province', 'pronamic_ideal' ),
114
			'type'     => 'text',
115
			'tooltip'  => __( 'State or province, e.g. Friesland', 'pronamic_ideal' ),
116
		);
117
118
		// Country.
119
		$locale = explode( '_', get_locale() );
120
121
		$locale = array_pop( $locale );
122
123
		$fields[] = array(
124
			'section'     => 'general',
125
			'filter'      => FILTER_SANITIZE_STRING,
126
			'group'       => 'pk-cert',
127
			'meta_key'    => '_pronamic_gateway_country',
128
			'title'       => __( 'Country', 'pronamic_ideal' ),
129
			'type'        => 'text',
130
			'tooltip'     => sprintf(
131
				'%s %s (ISO-3166-1 alpha-2)',
132
				__( '2 letter country code, e.g.', 'pronamic_ideal' ),
133
				strtoupper( $locale )
134
			),
135
			'size'        => 2,
136
			'description' => sprintf(
137
				'%s %s',
138
				__( '2 letter country code, e.g.', 'pronamic_ideal' ),
139
				strtoupper( $locale )
140
			),
141
		);
142
143
		// Email Address.
144
		$fields[] = array(
145
			'section'  => 'general',
146
			'filter'   => FILTER_SANITIZE_STRING,
147
			'group'    => 'pk-cert',
148
			'meta_key' => '_pronamic_gateway_email',
149
			'title'    => __( 'E-mail address', 'pronamic_ideal' ),
150
			'tooltip'  => sprintf(
151
				/* translators: %s: admin email */
152
				__( 'E-mail address, e.g. %s', 'pronamic_ideal' ),
153
				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

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

219
		$certificate = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
Loading history...
220
221
		?>
222
		<p>
223
			<?php if ( empty( $certificate ) ) : ?>
224
225
				<span
226
					class="dashicons dashicons-no"></span> <?php esc_html_e( 'The private key and certificate have not yet been configured.', 'pronamic_ideal' ); ?>
227
				<br/>
228
229
				<br/>
230
231
				<?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' ); ?>
232
233
			<?php else : ?>
234
235
				<span
236
					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' ); ?>
237
				<br/>
238
239
				<br/>
240
241
				<?php
242
243
				submit_button(
244
					__( 'Download certificate', 'pronamic_ideal' ),
245
					'secondary',
246
					'download_private_certificate',
247
					false
248
				);
249
250
				?>
251
252
			<?php endif; ?>
253
		</p>
254
		<?php
255
	}
256
257
	/**
258
	 * Field private key.
259
	 *
260
	 * @param array $field Field.
261
	 */
262
	public function field_private_key( $field ) {
263
		$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

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

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

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

438
			header( 'Content-Type: application/x-x509-ca-cert; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
439
440
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
441
			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

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

458
			header( 'Content-Type: application/pgp-keys; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
459
460
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
461
			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

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

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

582
				$cert = openssl_csr_sign( $csr, null, $pkey, /** @scrutinizer ignore-type */ $number_days_valid, $args, time() );
Loading history...
583
584
				openssl_x509_export( $cert, $certificate );
585
586
				update_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', $certificate );
587
			}
588
		}
589
	}
590
591
	public function get_config( $post_id ) {
592
		$mode = get_post_meta( $post_id, '_pronamic_gateway_mode', true );
593
594
		$config = new Config();
595
596
		$config->payment_server_url = $this->aquirer_url;
597
598
		if ( 'test' === $mode && null !== $this->aquirer_test_url ) {
599
			$config->payment_server_url = $this->aquirer_test_url;
600
		}
601
602
		$config->merchant_id = get_post_meta( $post_id, '_pronamic_gateway_ideal_merchant_id', true );
603
		$config->sub_id      = get_post_meta( $post_id, '_pronamic_gateway_ideal_sub_id', true );
604
		$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...
605
606
		$config->private_key          = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key', true );
607
		$config->private_key_password = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key_password', true );
608
		$config->private_certificate  = get_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', true );
609
610
		return $config;
611
	}
612
613
	/**
614
	 * Get gateway.
615
	 *
616
	 * @param int $post_id Post ID.
617
	 * @return Gateway
618
	 */
619
	public function get_gateway( $post_id ) {
620
		return new Gateway( $this->get_config( $post_id ) );
621
	}
622
}
623