Failed Conditions
Push — develop ( 58945b...d0a9f4 )
by Reüel
04:15
created

src/Integration.php (2 issues)

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