Failed Conditions
Push — develop ( 6f03e2...5a3ee9 )
by Remco
05:50
created

src/Integration.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Integration
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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;
0 ignored issues
show
The type Pronamic\WordPress\Pay\AbstractGatewayIntegration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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