Failed Conditions
Push — develop ( c509ff...5a092b )
by Reüel
02:42
created

Settings::save_post()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * Settings
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\Core\GatewaySettings;
14
15
/**
16
 * Settings
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.0
20
 * @since   1.0.0
21
 */
22
class Settings extends GatewaySettings {
23
	/**
24
	 * Webhook username meta key.
25
	 *
26
	 * @var string
27
	 */
28
	const WEBHOOK_USERNAME_META_KEY = '_pronamic_gateway_adyen_webhook_username';
29
30
	/**
31
	 * Webhook password meta key.
32
	 *
33
	 * @var string
34
	 */
35
	const WEBHOOK_PASSWORD_META_KEY = '_pronamic_gateway_adyen_webhook_password';
36
37
	/**
38
	 * Constructs and initialize settings.
39
	 */
40
	public function __construct() {
41
		add_filter( 'pronamic_pay_gateway_sections', array( $this, 'sections' ) );
42
		add_filter( 'pronamic_pay_gateway_fields', array( $this, 'fields' ) );
43
	}
44
45
	/**
46
	 * Sections.
47
	 *
48
	 * @param array $sections Sections.
49
	 * @return array
50
	 */
51
	public function sections( array $sections ) {
52
		$sections['adyen'] = array(
53
			'title'   => __( 'Adyen', 'pronamic_ideal' ),
54
			'methods' => array( 'adyen' ),
55
		);
56
57
		// Transaction feedback.
58
		$sections['adyen_feedback'] = array(
59
			'title'       => __( 'Transaction feedback', 'pronamic_ideal' ),
60
			'methods'     => array( 'adyen' ),
61
			'description' => sprintf(
62
				'%s %s',
63
				__(
64
					'The URLs below need to be copied to the payment provider dashboard to receive automatic transaction status updates.',
65
					'pronamic_ideal'
66
				),
67
				__(
68
					'Set the user name and password below and in the webhook authentication settings in the Adyen dashboard for increased security (recommended).',
69
					'pronamic_ideal'
70
				)
71
			),
72
		);
73
74
		return $sections;
75
	}
76
77
	/**
78
	 * Fields.
79
	 *
80
	 * @param array $fields Fields.
81
	 * @return array
82
	 */
83
	public function fields( array $fields ) {
84
		// API Key.
85
		$fields[] = array(
86
			'filter'   => FILTER_SANITIZE_STRING,
87
			'section'  => 'adyen',
88
			'meta_key' => '_pronamic_gateway_adyen_api_key',
89
			'title'    => _x( 'API Key', 'adyen', 'pronamic_ideal' ),
90
			'type'     => 'textarea',
91
			'classes'  => array( 'code' ),
92
			'methods'  => array( 'adyen' ),
93
			'tooltip'  => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ),
94
		);
95
96
		// Live API URL prefix.
97
		$fields[] = array(
98
			'filter'   => FILTER_SANITIZE_STRING,
99
			'section'  => 'adyen',
100
			'meta_key' => '_pronamic_gateway_adyen_live_url_prefix',
101
			'title'    => _x( 'API Live URL Prefix', 'adyen', 'pronamic_ideal' ),
102
			'type'     => 'text',
103
			'classes'  => array( 'regular-text', 'code' ),
104
			'methods'  => array( 'adyen' ),
105
			'tooltip'  => __( 'The unique prefix for the live API URL, as mentioned at <strong>Account » API URLs</strong> in the Adyen dashboard', 'pronamic_ideal' ),
106
		);
107
108
		// Merchant Account.
109
		$fields[] = array(
110
			'filter'   => FILTER_SANITIZE_STRING,
111
			'section'  => 'adyen',
112
			'meta_key' => '_pronamic_gateway_adyen_merchant_account',
113
			'title'    => _x( 'Merchant Account', 'adyen', 'pronamic_ideal' ),
114
			'type'     => 'text',
115
			'classes'  => array( 'regular-text', 'code' ),
116
			'methods'  => array( 'adyen' ),
117
			'tooltip'  => __( 'The merchant account identifier, with which you want to process the transaction', 'pronamic_ideal' ),
118
		);
119
120
		// Transaction feedback.
121
		$fields[] = array(
122
			'section' => 'adyen',
123
			'methods' => array( 'adyen' ),
124
			'title'   => __( 'Transaction feedback', 'pronamic_ideal' ),
125
			'type'    => 'description',
126
			'html'    => sprintf(
127
				'<span class="dashicons dashicons-warning"></span> %s',
128
				__(
129
					'Receiving payment status updates needs additional configuration, if not yet completed.',
130
					'pronamic_ideal'
131
				)
132
			),
133
		);
134
135
		// Webhook URL.
136
		$fields[] = array(
137
			'section'  => 'adyen_feedback',
138
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
139
			'type'     => 'text',
140
			'classes'  => array( 'large-text', 'code' ),
141
			'value'    => add_query_arg( 'adyen_webhook', '', home_url( '/' ) ),
142
			'readonly' => true,
143
			'tooltip'  => sprintf(
144
				/* translators: %s: Adyen */
145
				__(
146
					'Copy the Webhook URL to the %s dashboard to receive automatic transaction status updates.',
147
					'pronamic_ideal'
148
				),
149
				__( 'Adyen', 'pronamic_ideal' )
150
			),
151
		);
152
153
		// Webhook authentication username.
154
		$fields[] = array(
155
			'filter'   => FILTER_SANITIZE_STRING,
156
			'section'  => 'adyen_feedback',
157
			'meta_key' => self::WEBHOOK_USERNAME_META_KEY,
158
			'title'    => _x( 'User Name', 'adyen', 'pronamic_ideal' ),
159
			'type'     => 'text',
160
			'classes'  => array( 'regular-text', 'code' ),
161
			'methods'  => array( 'adyen' ),
162
			'tooltip'  => __(
163
				'The webhook authentication user name, as mentioned at <strong>Account » Server communication</strong> in the Adyen dashboard',
164
				'pronamic_ideal'
165
			),
166
		);
167
168
		// Webhook authentication password.
169
		$fields[] = array(
170
			'filter'   => FILTER_SANITIZE_STRING,
171
			'section'  => 'adyen_feedback',
172
			'meta_key' => self::WEBHOOK_PASSWORD_META_KEY,
173
			'title'    => _x( 'Password', 'adyen', 'pronamic_ideal' ),
174
			'type'     => 'text',
175
			'classes'  => array( 'regular-text', 'code' ),
176
			'methods'  => array( 'adyen' ),
177
			'tooltip'  => __(
178
				'The webhook authentication password, as mentioned at <strong>Account » Server communication</strong> in the Adyen dashboard',
179
				'pronamic_ideal'
180
			),
181
		);
182
183
		return $fields;
184
	}
185
186
	/**
187
	 * Save post.
188
	 *
189
	 * @param array $data Data to save.
190
	 *
191
	 * @return array
192
	 */
193
	public function save_post( $data ) {
194
		// Trim settings.
195
		$fields = array(
196
			self::WEBHOOK_USERNAME_META_KEY,
197
			self::WEBHOOK_PASSWORD_META_KEY,
198
		);
199
200
		foreach ( $fields as $field ) {
201
			if ( ! isset( $data[ $field ] ) ) {
202
				continue;
203
			}
204
205
			$data[ $field ] = trim( $data[ $field ] );
206
		}
207
208
		return $data;
209
	}
210
}
211