Failed Conditions
Push — master ( cc3467...84f509 )
by Reüel
09:31 queued 01:25
created

Integration::get_gateway()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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