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