Completed
Push — master ( 83a557...8e0143 )
by Remco
11:52 queued 05:23
created

Integration::get_settings_fields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 109
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 62
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 67
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 109
ccs 62
cts 62
cp 1
crap 1
rs 8.72

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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