Failed Conditions
Push — develop ( 0cd19e...00933d )
by Remco
07:53
created

src/Integration.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Integration
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 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\AbstractGatewayIntegration;
14
15
/**
16
 * Integration
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.3.0
20
 * @since   1.0.0
21
 */
22
class Integration extends AbstractGatewayIntegration {
23
	/**
24
	 * REST route namespace.
25
	 *
26
	 * @var string
27
	 */
28
	const REST_ROUTE_NAMESPACE = 'pronamic-pay/omnikassa-2/v1';
29
30
	/**
31
	 * Construct OmniKassa 2.0 integration.
32
	 *
33
	 * @param array<string, string|array> $args Arguments.
34
	 */
35
	public function __construct( $args = array() ) {
36
		$args = \wp_parse_args(
37
			$args,
38
			array(
39
				'id'            => 'rabobank-omnikassa-2',
40
				'name'          => 'Rabobank - OmniKassa 2.0',
41
				'product_url'   => 'https://www.rabobank.nl/bedrijven/betalen/geld-ontvangen/rabo-omnikassa/',
42
				'dashboard_url' => 'https://bankieren.rabobank.nl/omnikassa-dashboard/',
43
				'provider'      => 'rabobank',
44
				'supports'      => array(
45
					'webhook',
46
					'webhook_log',
47
				),
48
				'manual_url'    => \__(
49
					'https://www.pronamic.eu/support/how-to-connect-rabo-omnikassa-2-0-with-wordpress-via-pronamic-pay/',
50
					'pronamic_ideal'
51
				),
52
			)
53
		);
54
55
		parent::__construct( $args );
56
57
		/**
58
		 * Save post.
59
		 *
60
		 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/post.php#L3724-L3736
61
		 * @var callable $delete_access_token_meta_function
62
		 */
63
		$delete_access_token_meta_function = array( $this, 'delete_access_token_meta' );
64
65
		if ( ! \has_action( 'save_post_pronamic_gateway', $delete_access_token_meta_function ) ) {
66
			\add_action( 'save_post_pronamic_gateway', $delete_access_token_meta_function );
67
		}
68
69
		/**
70
		 * Admin notices.
71
		 *
72
		 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-admin/admin-header.php#L259-L264
73
		 * @var callable $admin_notices_function
74
		 */
75
		$admin_notices_function = array( $this, 'admin_notice_tld_test' );
76
77
		if ( ! \has_action( 'admin_notices', $admin_notices_function ) ) {
78
			\add_action( 'admin_notices', $admin_notices_function );
79
		}
80
	}
81
82
	/**
83
	 * Setup gateway integration.
84
	 *
85
	 * @return void
86
	 */
87
	public function setup() {
88
		// Check if dependencies are met and integration is active.
89
		if ( ! $this->is_active() ) {
90
			return;
91
		}
92
93
		// Webhook controller.
94
		$webhook_controller = new WebhookController();
95
96
		$webhook_controller->setup();
97
	}
98
99
	/**
100
	 * Admin notice TLD .test.
101
	 *
102
	 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-admin/admin-header.php#L259-L264
103
	 * @link https://developer.wordpress.org/reference/hooks/admin_notices/
104
	 * @link https://developer.wordpress.org/reference/functions/get_current_screen/
105
	 * @return void
106
	 */
107
	public function admin_notice_tld_test() {
108
		if ( \has_filter( 'pronamic_pay_omnikassa_2_merchant_return_url' ) ) {
109
			return;
110
		}
111
112
		$screen = \get_current_screen();
113
114
		if ( null === $screen ) {
115
			return;
116
		}
117
118
		if ( 'pronamic_gateway' !== $screen->id ) {
119
			return;
120
		}
121
122
		$host = \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST );
123
124
		if ( \is_array( $host ) ) {
125
			return;
126
		}
127
128
		if ( '.test' !== \substr( $host, -5 ) ) {
129
			return;
130
		}
131
132
		$post_id = \get_the_ID();
133
134
		if ( empty( $post_id ) ) {
135
			return;
136
		}
137
138
		$gateway_id = \get_post_meta( $post_id, '_pronamic_gateway_id', true );
139
140
		if ( 'rabobank-omnikassa-2' !== $gateway_id ) {
141
			return;
142
		}
143
144
		$class   = 'notice notice-error';
145
		$message = \sprintf(
146
			/* translators: 1: Pronamic Pay, 2: Documentation link, 3: <code>.test</code> */
147
			\__(
148
				'%1$s — <a href="%2$s">OmniKassa 2 does not accept payments from %3$s environments</a>.',
149
				'pronamic_ideal'
150
			),
151
			\sprintf(
152
				'<strong>%s</strong>',
153
				\__( 'Pronamic Pay', 'pronamic_ideal' )
154
			),
155
			'https://github.com/wp-pay-gateways/omnikassa-2/tree/develop/documentation#merchantreturnurl-is-not-a-valid-web-address',
156
			'<code>.test</code>'
157
		);
158
159
		\printf(
160
			'<div class="%1$s"><p>%2$s</p></div>',
161
			\esc_attr( $class ),
162
			\wp_kses(
163
				$message,
164
				array(
165
					'a'      => array(
166
						'href' => true,
167
					),
168
					'code'   => array(),
169
					'strong' => array(),
170
				)
171
			)
172
		);
173
	}
174
175
	/**
176
	 * Get settings fields.
177
	 *
178
	 * @return array<int, array<string, callable|int|string|bool|array<int|string,int|string>>>
179
	 */
180
	public function get_settings_fields() {
181
		$fields = array();
182
183
		// Refresh Token.
184
		$fields[] = array(
185
			'section'  => 'general',
186
			'filter'   => \FILTER_SANITIZE_STRING,
187
			'meta_key' => '_pronamic_gateway_omnikassa_2_refresh_token',
188
			'title'    => \_x( 'Refresh Token', 'omnikassa', 'pronamic_ideal' ),
189
			'type'     => 'textarea',
190
			'classes'  => array( 'code' ),
191
		);
192
193
		// Signing Key.
194
		$fields[] = array(
195
			'section'  => 'general',
196
			'filter'   => \FILTER_SANITIZE_STRING,
197
			'meta_key' => '_pronamic_gateway_omnikassa_2_signing_key',
198
			'title'    => \_x( 'Signing Key', 'omnikassa', 'pronamic_ideal' ),
199
			'type'     => 'text',
200
			'classes'  => array( 'large-text', 'code' ),
201
		);
202
203
		// Purchase ID.
204
		$code_field = \sprintf( '<code>%s</code>', 'merchantOrderId' );
205
206
		$fields[] = array(
207
			'section'     => 'advanced',
208
			'filter'      => \FILTER_SANITIZE_STRING,
209
			'meta_key'    => '_pronamic_gateway_omnikassa_2_order_id',
210
			'title'       => \__( 'Order ID', 'pronamic_ideal' ),
211
			'type'        => 'text',
212
			'classes'     => array( 'regular-text', 'code' ),
213
			'tooltip'     => \sprintf(
214
				/* translators: %s: <code>merchantOrderId</code> */
215
				\__( 'This setting defines the OmniKassa 2.0 %s field.', 'pronamic_ideal' ),
216
				$code_field
217
			),
218
			'description' => \sprintf(
219
				'%s<br />%s %s<br />%s',
220
				\sprintf(
221
					/* translators: %s: <code>merchantOrderId</code> */
222
					\__(
223
						'The OmniKassa 2.0 %s field must consist strictly of 24 alphanumeric characters, other characters, such as ".", "@", " " (space), etc. are not allowed.',
224
						'pronamic_ideal'
225
					),
226
					$code_field
227
				),
228
				\__( 'Available tags:', 'pronamic_ideal' ),
229
				\sprintf(
230
					'<code>%s</code> <code>%s</code>',
231
					'{order_id}',
232
					'{payment_id}'
233
				),
234
				\sprintf(
235
					/* translators: %s: default code */
236
					\__( 'Default: <code>%s</code>', 'pronamic_ideal' ),
237
					'{payment_id}'
238
				)
239
			),
240
		);
241
242
		// Webhook.
243
		$fields[] = array(
244
			'section'  => 'feedback',
245
			'title'    => \__( 'Webhook URL', 'pronamic_ideal' ),
246
			'type'     => 'text',
247
			'classes'  => array( 'large-text', 'code' ),
248
			'value'    => \rest_url( self::REST_ROUTE_NAMESPACE . '/webhook/' . \get_the_ID() ),
0 ignored issues
show
Are you sure get_the_ID() of type false|integer can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

248
			'value'    => \rest_url( self::REST_ROUTE_NAMESPACE . '/webhook/' . /** @scrutinizer ignore-type */ \get_the_ID() ),
Loading history...
249
			'readonly' => true,
250
			'tooltip'  => \__(
251
				'The Webhook URL as sent with each transaction to receive automatic payment status updates on.',
252
				'pronamic_ideal'
253
			),
254
		);
255
256
		return $fields;
257
	}
258
259
	/**
260
	 * Get configuration by post ID.
261
	 *
262
	 * @param int $post_id Post ID.
263
	 * @return Config
264
	 */
265
	public function get_config( $post_id ) {
266
		$config = new Config();
267
268
		$config->post_id                  = \intval( $post_id );
269
		$config->mode                     = $this->get_meta( $post_id, 'mode' );
270
		$config->refresh_token            = $this->get_meta( $post_id, 'omnikassa_2_refresh_token' );
271
		$config->signing_key              = $this->get_meta( $post_id, 'omnikassa_2_signing_key' );
272
		$config->access_token             = $this->get_meta( $post_id, 'omnikassa_2_access_token' );
273
		$config->access_token_valid_until = $this->get_meta( $post_id, 'omnikassa_2_access_token_valid_until' );
274
		$config->order_id                 = $this->get_meta( $post_id, 'omnikassa_2_order_id' );
275
276
		return $config;
277
	}
278
279
	/**
280
	 * Delete access token meta for the specified post ID.
281
	 *
282
	 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/post.php#L3724-L3736
283
	 * @link https://codex.wordpress.org/Function_Reference/delete_post_meta
284
	 * @param int $post_id Post ID.
285
	 * @return void
286
	 */
287
	public static function delete_access_token_meta( $post_id ) {
288
		\delete_post_meta( $post_id, '_pronamic_gateway_omnikassa_2_access_token' );
289
		\delete_post_meta( $post_id, '_pronamic_gateway_omnikassa_2_access_token_valid_until' );
290
	}
291
292
	/**
293
	 * Get gateway.
294
	 *
295
	 * @param int $post_id Post ID.
296
	 * @return Gateway
297
	 */
298
	public function get_gateway( $post_id ) {
299
		return new Gateway( $this->get_config( $post_id ) );
300
	}
301
}
302