Failed Conditions
Push — master ( f8b3b2...e22298 )
by Remco
21:14 queued 10:25
created

Integration   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 114
c 8
b 0
f 0
dl 0
loc 241
ccs 0
cts 150
cp 0
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
B admin_notice_tld_test() 0 60 8
A __construct() 0 44 4
A delete_access_token_meta() 0 3 1
A get_config() 0 12 1
A get_gateway() 0 2 1
A get_settings_fields() 0 71 1
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\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
use Pronamic\WordPress\Pay\Gateways\Common\AbstractIntegration;
14
15
/**
16
 * Integration
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.1.8
20
 * @since   1.0.0
21
 */
22
class Integration extends AbstractIntegration {
23
	/**
24
	 * Construct and initialize integration.
25
	 */
26
	public function __construct() {
27
		$this->id            = 'rabobank-omnikassa-2';
28
		$this->name          = 'Rabobank - OmniKassa 2.0';
29
		$this->product_url   = 'https://www.rabobank.nl/bedrijven/betalen/geld-ontvangen/rabo-omnikassa/';
30
		$this->dashboard_url = 'https://bankieren.rabobank.nl/omnikassa-dashboard/';
31
		$this->provider      = 'rabobank';
32
		$this->supports      = array(
33
			'webhook',
34
			'webhook_log',
35
		);
36
37
		/**
38
		 * Webhook listener function.
39
		 *
40
		 * @var callable $webhook_listener_function
41
		 */
42
		$webhook_listener_function = array( __NAMESPACE__ . '\WebhookListener', 'listen' );
43
44
		if ( ! \has_action( 'wp_loaded', $webhook_listener_function ) ) {
45
			\add_action( 'wp_loaded', $webhook_listener_function );
46
		}
47
48
		/**
49
		 * Save post.
50
		 *
51
		 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/post.php#L3724-L3736
52
		 * @var callable $delete_access_token_meta_function
53
		 */
54
		$delete_access_token_meta_function = array( $this, 'delete_access_token_meta' );
55
56
		if ( ! \has_action( 'save_post_pronamic_gateway', $delete_access_token_meta_function ) ) {
57
			\add_action( 'save_post_pronamic_gateway', $delete_access_token_meta_function );
58
		}
59
60
		/**
61
		 * Admin notices.
62
		 *
63
		 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-admin/admin-header.php#L259-L264
64
		 * @var callable $admin_notices_function
65
		 */
66
		$admin_notices_function = array( $this, 'admin_notice_tld_test' );
67
68
		if ( ! \has_action( 'admin_notices', $admin_notices_function ) ) {
69
			\add_action( 'admin_notices', $admin_notices_function );
70
		}
71
	}
72
73
	/**
74
	 * Admin notice TLD .test.
75
	 *
76
	 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-admin/admin-header.php#L259-L264
77
	 * @link https://developer.wordpress.org/reference/hooks/admin_notices/
78
	 * @link https://developer.wordpress.org/reference/functions/get_current_screen/
79
	 */
80
	public function admin_notice_tld_test() {
81
		if ( \has_filter( 'pronamic_pay_omnikassa_2_merchant_return_url' ) ) {
82
			return;
83
		}
84
85
		$screen = \get_current_screen();
86
87
		if ( null === $screen ) {
88
			return;
89
		}
90
91
		if ( 'pronamic_gateway' !== $screen->id ) {
92
			return;
93
		}
94
95
		$host = \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST );
96
97
		if ( \is_array( $host ) ) {
98
			return;
99
		}
100
101
		if ( '.test' !== \substr( $host, -5 ) ) {
102
			return;
103
		}
104
105
		$post_id = \get_the_ID();
106
107
		if ( empty( $post_id ) ) {
108
			return;
109
		}
110
111
		$gateway_id = \get_post_meta( $post_id, '_pronamic_gateway_id', true );
112
113
		if ( 'rabobank-omnikassa-2' !== $gateway_id ) {
114
			return;
115
		}
116
117
		$class   = 'notice notice-error';
118
		$message = \sprintf(
119
			/* translators: 1: Pronamic Pay, 2: Documentation link, 3: <code>.test</code> */
120
			\__( '%1$s — <a href="%2$s">OmniKassa 2 does not accept payments from %3$s environments</a>.', 'pronamic_ideal' ),
121
			\sprintf(
122
				'<strong>%s</strong>',
123
				\__( 'Pronamic Pay', 'pronamic_ideal' )
124
			),
125
			'https://github.com/wp-pay-gateways/omnikassa-2/tree/develop/documentation#merchantreturnurl-is-not-a-valid-web-address',
126
			'<code>.test</code>'
127
		);
128
129
		\printf(
130
			'<div class="%1$s"><p>%2$s</p></div>',
131
			\esc_attr( $class ),
132
			\wp_kses(
133
				$message,
134
				array(
135
					'a'      => array(
136
						'href' => true,
137
					),
138
					'code'   => array(),
139
					'strong' => array(),
140
				)
141
			)
142
		);
143
	}
144
145
	/**
146
	 * Get settings fields.
147
	 *
148
	 * @return array<array<string|int|array<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array<string|int|array<string>> at position 12 could not be parsed: Expected '>' at position 12, but found '>'.
Loading history...
149
	 */
150
	public function get_settings_fields() {
151
		$fields = array();
152
153
		// Refresh Token.
154
		$fields[] = array(
155
			'section'  => 'general',
156
			'filter'   => \FILTER_SANITIZE_STRING,
157
			'meta_key' => '_pronamic_gateway_omnikassa_2_refresh_token',
158
			'title'    => \_x( 'Refresh Token', 'omnikassa', 'pronamic_ideal' ),
159
			'type'     => 'textarea',
160
			'classes'  => array( 'code' ),
161
		);
162
163
		// Signing Key.
164
		$fields[] = array(
165
			'section'  => 'general',
166
			'filter'   => \FILTER_SANITIZE_STRING,
167
			'meta_key' => '_pronamic_gateway_omnikassa_2_signing_key',
168
			'title'    => \_x( 'Signing Key', 'omnikassa', 'pronamic_ideal' ),
169
			'type'     => 'text',
170
			'classes'  => array( 'large-text', 'code' ),
171
		);
172
173
		// Purchase ID.
174
		$code_field = \sprintf( '<code>%s</code>', 'merchantOrderId' );
175
176
		$fields[] = array(
177
			'section'     => 'advanced',
178
			'filter'      => \FILTER_SANITIZE_STRING,
179
			'meta_key'    => '_pronamic_gateway_omnikassa_2_order_id',
180
			'title'       => \__( 'Order ID', 'pronamic_ideal' ),
181
			'type'        => 'text',
182
			'classes'     => array( 'regular-text', 'code' ),
183
			'tooltip'     => \sprintf(
184
				/* translators: %s: <code>merchantOrderId</code> */
185
				\__( 'This setting defines the OmniKassa 2.0 %s field.', 'pronamic_ideal' ),
186
				$code_field
187
			),
188
			'description' => \sprintf(
189
				'%s<br />%s %s<br />%s',
190
				\sprintf(
191
					/* translators: %s: <code>merchantOrderId</code> */
192
					\__( 'The OmniKassa 2.0 %s field must consist strictly of 24 alphanumeric characters, other characters, such as ".", "@", " " (space), etc. are not allowed.', 'pronamic_ideal' ),
193
					$code_field
194
				),
195
				\__( 'Available tags:', 'pronamic_ideal' ),
196
				\sprintf(
197
					'<code>%s</code> <code>%s</code>',
198
					'{order_id}',
199
					'{payment_id}'
200
				),
201
				\sprintf(
202
					/* translators: %s: {payment_id} */
203
					\__( 'Default: <code>%s</code>', 'pronamic_ideal' ),
204
					'{payment_id}'
205
				)
206
			),
207
		);
208
209
		// Webhook.
210
		$fields[] = array(
211
			'section'  => 'feedback',
212
			'title'    => \__( 'Webhook URL', 'pronamic_ideal' ),
213
			'type'     => 'text',
214
			'classes'  => array( 'large-text', 'code' ),
215
			'value'    => \add_query_arg( 'omnikassa2_webhook', '', \home_url( '/' ) ),
216
			'readonly' => true,
217
			'tooltip'  => \__( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
218
		);
219
220
		return $fields;
221
	}
222
223
	/**
224
	 * Get configuration by post ID.
225
	 *
226
	 * @param string $post_id Post ID.
227
	 * @return Config
228
	 */
229
	public function get_config( $post_id ) {
230
		$config = new Config();
231
232
		$config->post_id                  = \intval( $post_id );
233
		$config->mode                     = $this->get_meta( $post_id, 'mode' );
234
		$config->refresh_token            = $this->get_meta( $post_id, 'omnikassa_2_refresh_token' );
235
		$config->signing_key              = $this->get_meta( $post_id, 'omnikassa_2_signing_key' );
236
		$config->access_token             = $this->get_meta( $post_id, 'omnikassa_2_access_token' );
237
		$config->access_token_valid_until = $this->get_meta( $post_id, 'omnikassa_2_access_token_valid_until' );
238
		$config->order_id                 = $this->get_meta( $post_id, 'omnikassa_2_order_id' );
239
240
		return $config;
241
	}
242
243
	/**
244
	 * Delete access token meta for the specified post ID.
245
	 *
246
	 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/post.php#L3724-L3736
247
	 * @link https://codex.wordpress.org/Function_Reference/delete_post_meta
248
	 * @param int $post_id Post ID.
249
	 */
250
	public static function delete_access_token_meta( $post_id ) {
251
		\delete_post_meta( $post_id, '_pronamic_gateway_omnikassa_2_access_token' );
252
		\delete_post_meta( $post_id, '_pronamic_gateway_omnikassa_2_access_token_valid_until' );
253
	}
254
255
	/**
256
	 * Get gateway.
257
	 *
258
	 * @param int $post_id Post ID.
259
	 * @return Gateway
260
	 */
261
	public function get_gateway( $post_id ) {
262
		return new Gateway( $this->get_config( $post_id ) );
263
	}
264
}
265