Failed Conditions
Push — develop ( ba4778...2cbb4d )
by Reüel
07:11
created

src/Integration.php (1 issue)

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