Test Failed
Push — develop ( 262c11...e3581b )
by Remco
04:57
created

Integration::plugins_loaded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 28
ccs 10
cts 10
cp 1
crap 2
rs 9.8666
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\Gateways\Common\AbstractIntegration;
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 AbstractIntegration {
25
	/**
26
	 * REST route namespace.
27
	 *
28
	 * @var string
29
	 */
30
	const REST_ROUTE_NAMESPACE = 'pronamic-pay/adyen/v1';
31
32
	/**
33
	 * Integration constructor.
34
	 */
35 5
	public function __construct() {
36 5
		parent::__construct();
37
38 5
		$this->id            = 'adyen';
39 5
		$this->name          = 'Adyen';
40 5
		$this->provider      = 'adyen';
41 5
		$this->url           = __( 'https://www.adyen.com/', 'pronamic_ideal' );
42 5
		$this->product_url   = __( 'https://www.adyen.com/pricing', 'pronamic_ideal' );
43 5
		$this->dashboard_url = array(
44 5
			__( 'test', 'pronamic_ideal' ) => 'https://ca-test.adyen.com/ca/ca/login.shtml',
45 5
			__( 'live', 'pronamic_ideal' ) => 'https://ca-live.adyen.com/ca/ca/login.shtml',
46
		);
47 5
		$this->supports      = array(
48
			'webhook',
49
			'webhook_log',
50
		);
51
52 5
		$this->set_manual_url( __( 'https://www.pronamic.eu/manuals/using-adyen-pronamic-pay/', 'pronamic_ideal' ) );
53
54
		// Dependencies.
55 5
		$dependencies = $this->get_dependencies();
56
57 5
		$dependencies->add( new PhpExtensionDependency( 'intl' ) );
58
	}
59
60 5
	/**
61
	 * Plugins loaded.
62 5
	 */
63
	public function plugins_loaded() {
64
		if ( ! $this->get_dependencies()->are_met() ) {
65 5
			return;
66
		}
67 5
68
		// Notifications controller.
69
		$notifications_controller = new NotificationsController();
70 5
71
		$notifications_controller->setup();
72 5
73
		// Payments controller.
74
		$payments_controller = new PaymentsController();
75 5
76
		$payments_controller->setup();
77 5
78
		// Payments result controller.
79
		$payments_result_controller = new PaymentsResultController();
80 5
81 5
		$payments_result_controller->setup();
82 5
83
		// Site Health controller.
84
		$site_healht_controller = new SiteHealthController();
85
86
		$site_healht_controller->setup();
87
88
		// Settings.
89 1
		add_action( 'init', array( $this, 'init' ) );
90
		add_action( 'admin_init', array( $this, 'admin_init' ) );
91
	}
92
93 1
	/**
94 1
	 * Initialize.
95 1
	 *
96
	 * @return void
97 1
	 */
98
	public function init() {
99
		/*
100
		 * Authentication - User Name
101
		 */
102
		register_setting(
103
			'pronamic_pay',
104
			'pronamic_pay_adyen_notification_authentication_username',
105 1
			array(
106 1
				'type'              => 'string',
107 1
				'sanitize_callback' => 'sanitize_text_field',
108
			)
109 1
		);
110
111
		/*
112
		 * Authentication - Password
113 1
		 */
114
		register_setting(
115
			'pronamic_pay',
116
			'pronamic_pay_adyen_notification_authentication_password',
117
			array(
118
				'type'              => 'string',
119
				'sanitize_callback' => 'sanitize_text_field',
120 1
			)
121 1
		);
122 1
	}
123
124 1
	/**
125 1
	 * Admin initialize.
126 1
	 *
127
	 * @return void
128
	 */
129 1
	public function admin_init() {
130 1
		add_settings_section(
131 1
			'pronamic_pay_adyen_notification_authentication',
132 1
			/* translators: Translate 'notification' the same as in the Adyen dashboard. */
133 1
			_x( 'Adyen Notification Authentication', 'Adyen', 'pronamic_ideal' ),
134 1
			array( $this, 'settings_section_notification_authentication' ),
135
			'pronamic_pay'
136 1
		);
137
138
		add_settings_field(
139
			'pronamic_pay_adyen_notification_authentication_username',
140 1
			__( 'User Name', 'pronamic_ideal' ),
141 1
			array( __CLASS__, 'input_element' ),
142 1
			'pronamic_pay',
143 1
			'pronamic_pay_adyen_notification_authentication',
144 1
			array(
145 1
				'label_for' => 'pronamic_pay_adyen_notification_authentication_username',
146
			)
147 1
		);
148
149
		add_settings_field(
150 1
			'pronamic_pay_adyen_notification_authentication_password',
151
			__( 'Password', 'pronamic_ideal' ),
152
			array( __CLASS__, 'input_element' ),
153
			'pronamic_pay',
154
			'pronamic_pay_adyen_notification_authentication',
155
			array(
156
				'label_for' => 'pronamic_pay_adyen_notification_authentication_password',
157 1
			)
158 1
		);
159 1
	}
160 1
161 1
	/**
162 1
	 * Settings section notification authentication.
163
	 *
164
	 * @return void
165 1
	 */
166
	public function settings_section_notification_authentication() {
167
		printf(
168
			'<p>%s</p>',
169
			esc_html__(
170
				'Set the user name and password below and in the webhook authentication settings in the Adyen dashboard for increased security (recommended).',
171
				'pronamic_ideal'
172
			)
173 1
		);
174 1
	}
175
176 1
	/**
177 1
	 * Input text.
178
	 *
179 1
	 * @param array<string,string> $args Arguments.
180 1
	 * @return void
181 1
	 */
182 1
	public static function input_element( $args ) {
183 1
		$name = $args['label_for'];
184
185 1
		$value = get_option( $name );
186
		$value = strval( $value );
187
188
		printf(
189
			'<input name="%s" id="%s" value="%s" type="text" class="regular-text" />',
190
			esc_attr( $name ),
191
			esc_attr( $name ),
192 1
			esc_attr( $value )
193 1
		);
194
	}
195
196 1
	/**
197 1
	 * Get settings fields.
198 1
	 *
199 1
	 * @return array<int, array<string, int|string|bool|array<int,string>>>
200 1
	 */
201 1
	public function get_settings_fields() {
202
		$fields = array();
203 1
204
		// Merchant Account.
205
		$fields[] = array(
206
			'section'  => 'general',
207 1
			'filter'   => FILTER_SANITIZE_STRING,
208 1
			'meta_key' => '_pronamic_gateway_adyen_merchant_account',
209 1
			'title'    => _x( 'Merchant Account', 'adyen', 'pronamic_ideal' ),
210 1
			'type'     => 'text',
211 1
			'classes'  => array( 'regular-text', 'code' ),
212 1
			'tooltip'  => __( 'The merchant account identifier, with which you want to process the transaction.', 'pronamic_ideal' ),
213
		);
214 1
215 1
		// API Key.
216 1
		$fields[] = array(
217 1
			'section'     => 'general',
218 1
			'filter'      => FILTER_SANITIZE_STRING,
219
			'meta_key'    => '_pronamic_gateway_adyen_api_key',
220
			'title'       => _x( 'API Key', 'adyen', 'pronamic_ideal' ),
221
			'type'        => 'textarea',
222
			'classes'     => array( 'code' ),
223 1
			'tooltip'     => __( 'API key as mentioned in the payment provider dashboard.', 'pronamic_ideal' ),
224 1
			'description' => sprintf(
225 1
				'<a href="%s" target="_blank">%s</a>',
226 1
				esc_url( 'https://docs.adyen.com/developers/user-management/how-to-get-the-api-key' ),
227 1
				esc_html__( 'Adyen documentation: "How to get the API key".', 'pronamic_ideal' )
228 1
			),
229
		);
230 1
231 1
		// Live API URL prefix.
232 1
		$fields[] = array(
233 1
			'section'     => 'general',
234 1
			'filter'      => FILTER_SANITIZE_STRING,
235
			'meta_key'    => '_pronamic_gateway_adyen_api_live_url_prefix',
236
			'title'       => _x( 'API Live URL Prefix', 'adyen', 'pronamic_ideal' ),
237
			'type'        => 'text',
238
			'classes'     => array( 'regular-text', 'code' ),
239 1
			'tooltip'     => __( 'The unique prefix for the live API URL, as mentioned at <strong>Account » API URLs</strong> in the Adyen dashboard.', 'pronamic_ideal' ),
240 1
			'description' => sprintf(
241 1
				'<a href="%s" target="_blank">%s</a>',
242 1
				esc_url( 'https://docs.adyen.com/developers/development-resources/live-endpoints#liveurlprefix' ),
243 1
				esc_html__( 'Adyen documentation: "Live URL prefix".', 'pronamic_ideal' )
244 1
			),
245
		);
246
247
		// Origin Key.
248
		$fields[] = array(
249
			'section'     => 'general',
250 1
			'filter'      => FILTER_SANITIZE_STRING,
251 1
			'meta_key'    => '_pronamic_gateway_adyen_origin_key',
252 1
			'title'       => _x( 'Origin Key', 'adyen', 'pronamic_ideal' ),
253 1
			'type'        => 'text',
254 1
			'classes'     => array(
255
				'regular-text',
256
				'code',
257
				'pronamic-pay-form-control-lg',
258
			),
259 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' ),
260 1
			'description' => sprintf(
261 1
				'<a href="%s" target="_blank">%s</a>',
262 1
				esc_url( 'https://docs.adyen.com/user-management/how-to-get-an-origin-key' ),
263
				esc_html__( 'Adyen documentation: "How to get an origin key".', 'pronamic_ideal' )
264 1
			),
265
		);
266 1
267
		// Webhook URL.
268 1
		$fields[] = array(
269 1
			'section'  => 'feedback',
270 1
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
271
			'type'     => 'text',
272 1
			'classes'  => array( 'large-text', 'code' ),
273
			'value'    => rest_url( self::REST_ROUTE_NAMESPACE . '/notifications' ),
274
			'readonly' => true,
275
			'tooltip'  => sprintf(
276
				/* translators: %s: Adyen */
277
				__(
278
					'Copy the Webhook URL to the %s dashboard to receive automatic transaction status updates.',
279
					'pronamic_ideal'
280
				),
281
				__( 'Adyen', 'pronamic_ideal' )
282 1
			),
283 1
		);
284 1
285 1
		/**
286 1
		 * SSL Version.
287
		 *
288
		 * @link https://docs.adyen.com/developers/development-resources/notifications/set-up-notifications#step3configurenotificationsinthecustomerarea
289
		 * @link https://www.howsmyssl.com/a/check
290
		 */
291
		$fields[] = array(
292
			'section' => 'feedback',
293
			'title'   => __( 'SSL Version', 'pronamic_ideal' ),
294
			'type'    => 'description',
295 1
			'html'    => __( 'Choose the SSL Version of your server on the Adyen Customer Area.', 'pronamic_ideal' ),
296 1
		);
297 1
298 1
		/**
299 1
		 * Method.
300
		 *
301
		 * @link https://docs.adyen.com/developers/development-resources/notifications/set-up-notifications#step3configurenotificationsinthecustomerarea
302
		 * @link https://www.howsmyssl.com/a/check
303 1
		 */
304 1
		$fields[] = array(
305 1
			'section' => 'feedback',
306 1
			'title'   => _x( 'Method', 'adyen notification', 'pronamic_ideal' ),
307 1
			'type'    => 'description',
308 1
			'html'    => __( 'JSON', 'pronamic_ideal' ),
309 1
		);
310 1
311
		// Webhook authentication settings.
312 1
		$fields[] = array(
313
			'section' => 'feedback',
314 1
			'title'   => __( 'Authentication', 'pronamic_ideal' ),
315
			'type'    => 'description',
316
			'html'    => sprintf(
317
				'For webhook authentication settings, please visit <a href="%2$s" title="Settings">%1$s settings</a>.',
318
				__( 'Pronamic Pay', 'pronamic_ideal' ),
319
				add_query_arg(
320 1
					array(
321
						'page' => 'pronamic_pay_settings',
322
					),
323
					admin_url( 'admin.php' )
324
				)
325
			),
326
		);
327
328
		// Return fields.
329 1
		return $fields;
330 1
	}
331
332 1
	/**
333 1
	 * Get configuration by post ID.
334 1
	 *
335 1
	 * @param int $post_id Post ID.
336 1
	 * @return Config
337
	 */
338 1
	public function get_config( $post_id ) {
339
		$config = new Config();
340
341
		$config->mode                = $this->get_meta( $post_id, 'mode' );
342
		$config->api_key             = $this->get_meta( $post_id, 'adyen_api_key' );
343
		$config->api_live_url_prefix = $this->get_meta( $post_id, 'adyen_api_live_url_prefix' );
344
		$config->merchant_account    = $this->get_meta( $post_id, 'adyen_merchant_account' );
345
		$config->origin_key          = $this->get_meta( $post_id, 'adyen_origin_key' );
346
347 1
		return $config;
348 1
	}
349
350 1
	/**
351 1
	 * Get gateway.
352
	 *
353
	 * @param int $post_id Post ID.
354
	 * @return AbstractGateway
355
	 */
356
	public function get_gateway( $post_id ) {
357
		$config = $this->get_config( $post_id );
358
359
		if ( empty( $config->origin_key ) ) {
360
			return new WebSdkGateway( $config );
361
		}
362
363
		return new DropInGateway( $config );
364
	}
365
}
366