Test Failed
Push — develop ( 250b7c...9f49e0 )
by Remco
04:26
created

Integration::get_config_factory_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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