Failed Conditions
Push — develop ( 397f1c...61c5d3 )
by Reüel
05:20
created

Integration::get_config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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