Failed Conditions
Push — develop ( 0b6870...5edab0 )
by Reüel
31:14
created

Integration   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 403
Duplicated Lines 0 %

Test Coverage

Coverage 99.01%

Importance

Changes 14
Bugs 0 Features 0
Metric Value
eloc 214
c 14
b 0
f 0
dl 0
loc 403
ccs 201
cts 203
cp 0.9901
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A input_element() 0 11 1
A plugins_loaded() 0 28 2
A init() 0 22 1
A settings_section_notification_authentication() 0 6 1
A __construct() 0 27 1
A admin_init() 0 28 1
A get_gateway() 0 8 2
A get_config() 0 13 1
B get_settings_fields() 0 181 1
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 1
	}
100
101
	/**
102
	 * Initialize.
103
	 *
104
	 * @return void
105
	 */
106 1
	public function init() {
107
		/*
108
		 * Authentication - User Name
109
		 */
110 1
		register_setting(
111 1
			'pronamic_pay',
112 1
			'pronamic_pay_adyen_notification_authentication_username',
113
			array(
114 1
				'type'              => 'string',
115
				'sanitize_callback' => 'sanitize_text_field',
116
			)
117
		);
118
119
		/*
120
		 * Authentication - Password
121
		 */
122 1
		register_setting(
123 1
			'pronamic_pay',
124 1
			'pronamic_pay_adyen_notification_authentication_password',
125
			array(
126 1
				'type'              => 'string',
127
				'sanitize_callback' => 'sanitize_text_field',
128
			)
129
		);
130 1
	}
131
132
	/**
133
	 * Admin initialize.
134
	 *
135
	 * @return void
136
	 */
137 1
	public function admin_init() {
138 1
		add_settings_section(
139 1
			'pronamic_pay_adyen_notification_authentication',
140
			/* translators: Translate 'notification' the same as in the Adyen dashboard. */
141 1
			_x( 'Adyen Notification Authentication', 'Adyen', 'pronamic_ideal' ),
142 1
			array( $this, 'settings_section_notification_authentication' ),
143 1
			'pronamic_pay'
144
		);
145
146 1
		add_settings_field(
147 1
			'pronamic_pay_adyen_notification_authentication_username',
148 1
			__( 'User Name', 'pronamic_ideal' ),
149 1
			array( __CLASS__, 'input_element' ),
150 1
			'pronamic_pay',
151 1
			'pronamic_pay_adyen_notification_authentication',
152
			array(
153 1
				'label_for' => 'pronamic_pay_adyen_notification_authentication_username',
154
			)
155
		);
156
157 1
		add_settings_field(
158 1
			'pronamic_pay_adyen_notification_authentication_password',
159 1
			__( 'Password', 'pronamic_ideal' ),
160 1
			array( __CLASS__, 'input_element' ),
161 1
			'pronamic_pay',
162 1
			'pronamic_pay_adyen_notification_authentication',
163
			array(
164 1
				'label_for' => 'pronamic_pay_adyen_notification_authentication_password',
165
			)
166
		);
167 1
	}
168
169
	/**
170
	 * Settings section notification authentication.
171
	 *
172
	 * @return void
173
	 */
174 1
	public function settings_section_notification_authentication() {
175 1
		printf(
176 1
			'<p>%s</p>',
177 1
			esc_html__(
178 1
				'Set the user name and password below and in the webhook authentication settings in the Adyen dashboard for increased security (recommended).',
179 1
				'pronamic_ideal'
180
			)
181
		);
182 1
	}
183
184
	/**
185
	 * Input text.
186
	 *
187
	 * @param array<string,string> $args Arguments.
188
	 * @return void
189
	 */
190 1
	public static function input_element( $args ) {
191 1
		$name = $args['label_for'];
192
193 1
		$value = get_option( $name );
194 1
		$value = strval( $value );
195
196 1
		printf(
197 1
			'<input name="%s" id="%s" value="%s" type="text" class="regular-text" />',
198 1
			esc_attr( $name ),
199 1
			esc_attr( $name ),
200 1
			esc_attr( $value )
201
		);
202 1
	}
203
204
	/**
205
	 * Get settings fields.
206
	 *
207
	 * @return array<int, array<string, int|string|bool|array<int,string>>>
208
	 */
209 1
	public function get_settings_fields() {
210 1
		$fields = array();
211
212
		// Merchant Account.
213 1
		$fields[] = array(
214 1
			'section'  => 'general',
215 1
			'filter'   => FILTER_SANITIZE_STRING,
216 1
			'meta_key' => '_pronamic_gateway_adyen_merchant_account',
217 1
			'title'    => _x( 'Merchant Account', 'adyen', 'pronamic_ideal' ),
218 1
			'type'     => 'text',
219
			'classes'  => array( 'regular-text', 'code' ),
220 1
			'tooltip'  => __( 'The merchant account identifier, with which you want to process the transaction.', 'pronamic_ideal' ),
221
		);
222
223
		// API Key.
224 1
		$fields[] = array(
225 1
			'section'     => 'general',
226 1
			'filter'      => FILTER_SANITIZE_STRING,
227 1
			'meta_key'    => '_pronamic_gateway_adyen_api_key',
228 1
			'title'       => _x( 'API Key', 'adyen', 'pronamic_ideal' ),
229 1
			'type'        => 'textarea',
230
			'classes'     => array( 'code' ),
231 1
			'tooltip'     => __( 'API key as mentioned in the payment provider dashboard.', 'pronamic_ideal' ),
232 1
			'description' => sprintf(
233 1
				'<a href="%s" target="_blank">%s</a>',
234 1
				esc_url( 'https://docs.adyen.com/developers/user-management/how-to-get-the-api-key' ),
235 1
				esc_html__( 'Adyen documentation: "How to get the API key".', 'pronamic_ideal' )
236
			),
237
		);
238
239
		// Live API URL prefix.
240 1
		$fields[] = array(
241 1
			'section'     => 'general',
242 1
			'filter'      => FILTER_SANITIZE_STRING,
243 1
			'meta_key'    => '_pronamic_gateway_adyen_api_live_url_prefix',
244 1
			'title'       => _x( 'API Live URL Prefix', 'adyen', 'pronamic_ideal' ),
245 1
			'type'        => 'text',
246
			'classes'     => array( 'regular-text', 'code' ),
247 1
			'tooltip'     => __( 'The unique prefix for the live API URL, as mentioned at <strong>Account » API URLs</strong> in the Adyen dashboard.', 'pronamic_ideal' ),
248 1
			'description' => sprintf(
249 1
				'<a href="%s" target="_blank">%s</a>',
250 1
				esc_url( 'https://docs.adyen.com/developers/development-resources/live-endpoints#liveurlprefix' ),
251 1
				esc_html__( 'Adyen documentation: "Live URL prefix".', 'pronamic_ideal' )
252
			),
253
		);
254
255
		// Origin Key.
256 1
		$fields[] = array(
257 1
			'section'     => 'general',
258 1
			'filter'      => FILTER_SANITIZE_STRING,
259 1
			'meta_key'    => '_pronamic_gateway_adyen_origin_key',
260 1
			'title'       => _x( 'Origin Key', 'adyen', 'pronamic_ideal' ),
261 1
			'type'        => 'text',
262
			'classes'     => array(
263
				'regular-text',
264
				'code',
265
				'pronamic-pay-form-control-lg',
266
			),
267 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' ),
268 1
			'description' => sprintf(
269 1
				'<a href="%s" target="_blank">%s</a>',
270 1
				esc_url( 'https://docs.adyen.com/user-management/how-to-get-an-origin-key' ),
271 1
				esc_html__( 'Adyen documentation: "How to get an origin key".', 'pronamic_ideal' )
272
			),
273
		);
274
275
		// Apple Pay - Merchant identifier.
276 1
		$fields[] = array(
277 1
			'section'     => 'advanced',
278 1
			'filter'      => FILTER_SANITIZE_STRING,
279 1
			'meta_key'    => '_pronamic_gateway_adyen_apple_pay_merchant_id',
280 1
			'title'       => _x( 'Apple Pay Merchant ID', 'adyen', 'pronamic_ideal' ),
281 1
			'type'        => 'text',
282
			'classes'     => array( 'regular-text', 'code' ),
283 1
			'tooltip'     => __( 'Your Apple Pay Merchant ID. Required for accepting live payments.', 'pronamic_ideal' ),
284 1
			'description' => sprintf(
285 1
				'<a href="%s" target="_blank">%s</a><br /><a href="%s" target="_blank">%s</a>',
286 1
				esc_url( 'https://docs.adyen.com/payment-methods/apple-pay/web-drop-in#before-you-begin' ),
287 1
				esc_html__( 'Adyen documentation: "Apple Pay Drop-in - Before you begin".', 'pronamic_ideal' ),
288 1
				esc_url( 'https://developer.apple.com/documentation/apple_pay_on_the_web/configuring_your_environment' ),
289 1
				esc_html__( 'Apple documentation: "Configuring your environment".', 'pronamic_ideal' )
290
			),
291
		);
292
293
		// Apple Pay - Merchant Identity Certificate private key password.
294 1
		$fields[] = array(
295 1
			'section'     => 'advanced',
296 1
			'filter'      => FILTER_SANITIZE_STRING,
297 1
			'meta_key'    => '_pronamic_gateway_adyen_apple_pay_merchant_id_private_key_password',
298 1
			'title'       => _x( 'Apple Pay Merchant Identity Certificate Private Key Password', 'adyen', 'pronamic_ideal' ),
299 1
			'type'        => 'text',
300
			'classes'     => array( 'regular-text', 'code' ),
301 1
			'tooltip'     => __( 'Your Apple Pay Merchant Identity Certificate private key password.', 'pronamic_ideal' ),
302 1
			'description' => sprintf(
303 1
				'<a href="%s" target="_blank">%s</a>',
304 1
				esc_url( 'https://docs.adyen.com/payment-methods/apple-pay/enable-apple-pay#create-merchant-identity-certificate' ),
305 1
				esc_html__( 'Adyen documentation: "Enable Apple Pay - Create a merchant identity certificate".', 'pronamic_ideal' )
306
			),
307
		);
308
309
		// Google Pay - Merchant identifier.
310 1
		$fields[] = array(
311 1
			'section'     => 'advanced',
312 1
			'filter'      => FILTER_SANITIZE_STRING,
313 1
			'meta_key'    => '_pronamic_gateway_adyen_google_pay_merchant_identifier',
314 1
			'title'       => _x( 'Google Pay Merchant ID', 'adyen', 'pronamic_ideal' ),
315 1
			'type'        => 'text',
316
			'classes'     => array( 'regular-text', 'code' ),
317 1
			'tooltip'     => __( 'Your Google Merchant ID. Required for accepting live payments.', 'pronamic_ideal' ),
318 1
			'description' => sprintf(
319 1
				'<a href="%s" target="_blank">%s</a><br /><a href="%s" target="_blank">%s</a>',
320 1
				esc_url( 'https://docs.adyen.com/payment-methods/google-pay/web-drop-in#test-and-go-live' ),
321 1
				esc_html__( 'Adyen documentation: "Google Pay Drop-in - Test and go live".', 'pronamic_ideal' ),
322 1
				esc_url( 'https://developers.google.com/pay/api/web/guides/test-and-deploy/deploy-production-environment' ),
323 1
				esc_html__( 'Google documentation: "Deploy production environment".', 'pronamic_ideal' )
324
			),
325
		);
326
327
		// Webhook URL.
328 1
		$fields[] = array(
329 1
			'section'  => 'feedback',
330 1
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
331 1
			'type'     => 'text',
332
			'classes'  => array( 'large-text', 'code' ),
333 1
			'value'    => rest_url( self::REST_ROUTE_NAMESPACE . '/notifications' ),
334
			'readonly' => true,
335 1
			'tooltip'  => sprintf(
336
				/* translators: %s: Adyen */
337 1
				__(
338 1
					'Copy the Webhook URL to the %s dashboard to receive automatic transaction status updates.',
339 1
					'pronamic_ideal'
340
				),
341 1
				__( 'Adyen', 'pronamic_ideal' )
342
			),
343
		);
344
345
		/**
346
		 * SSL Version.
347
		 *
348
		 * @link https://docs.adyen.com/developers/development-resources/notifications/set-up-notifications#step3configurenotificationsinthecustomerarea
349
		 * @link https://www.howsmyssl.com/a/check
350
		 */
351 1
		$fields[] = array(
352 1
			'section' => 'feedback',
353 1
			'title'   => __( 'SSL Version', 'pronamic_ideal' ),
354 1
			'type'    => 'description',
355 1
			'html'    => __( 'Choose the SSL Version of your server on the Adyen Customer Area.', 'pronamic_ideal' ),
356
		);
357
358
		/**
359
		 * Method.
360
		 *
361
		 * @link https://docs.adyen.com/developers/development-resources/notifications/set-up-notifications#step3configurenotificationsinthecustomerarea
362
		 * @link https://www.howsmyssl.com/a/check
363
		 */
364 1
		$fields[] = array(
365 1
			'section' => 'feedback',
366 1
			'title'   => _x( 'Method', 'adyen notification', 'pronamic_ideal' ),
367 1
			'type'    => 'description',
368 1
			'html'    => __( 'JSON', 'pronamic_ideal' ),
369
		);
370
371
		// Webhook authentication settings.
372 1
		$fields[] = array(
373 1
			'section' => 'feedback',
374 1
			'title'   => __( 'Authentication', 'pronamic_ideal' ),
375 1
			'type'    => 'description',
376 1
			'html'    => sprintf(
377 1
				'For webhook authentication settings, please visit <a href="%2$s" title="Settings">%1$s settings</a>.',
378 1
				__( 'Pronamic Pay', 'pronamic_ideal' ),
379 1
				add_query_arg(
380
					array(
381 1
						'page' => 'pronamic_pay_settings',
382
					),
383 1
					admin_url( 'admin.php' )
384
				)
385
			),
386
		);
387
388
		// Return fields.
389 1
		return $fields;
390
	}
391
392
	/**
393
	 * Get configuration by post ID.
394
	 *
395
	 * @param int $post_id Post ID.
396
	 * @return Config
397
	 */
398 1
	public function get_config( $post_id ) {
399 1
		$config = new Config();
400
401 1
		$config->mode                                       = $this->get_meta( $post_id, 'mode' );
402 1
		$config->api_key                                    = $this->get_meta( $post_id, 'adyen_api_key' );
403 1
		$config->api_live_url_prefix                        = $this->get_meta( $post_id, 'adyen_api_live_url_prefix' );
404 1
		$config->merchant_account                           = $this->get_meta( $post_id, 'adyen_merchant_account' );
405 1
		$config->origin_key                                 = $this->get_meta( $post_id, 'adyen_origin_key' );
406 1
		$config->apple_pay_merchant_id                      = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id' );
407 1
		$config->apple_pay_merchant_id_private_key_password = $this->get_meta( $post_id, 'adyen_apple_pay_merchant_id_private_key_password' );
408 1
		$config->google_pay_merchant_identifier             = $this->get_meta( $post_id, 'adyen_google_pay_merchant_identifier' );
409
410 1
		return $config;
411
	}
412
413
	/**
414
	 * Get gateway.
415
	 *
416
	 * @param int $post_id Post ID.
417
	 * @return AbstractGateway
418
	 */
419 1
	public function get_gateway( $post_id ) {
420 1
		$config = $this->get_config( $post_id );
421
422 1
		if ( empty( $config->origin_key ) ) {
423 1
			return new WebSdkGateway( $config );
424
		}
425
426
		return new DropInGateway( $config );
427
	}
428
}
429