Failed Conditions
Push — develop ( 55631e...797086 )
by Remco
07:57
created

src/Integration.php (1 issue)

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
		$fields[] = array(
175
			'section'     => 'advanced',
176
			'filter'      => \FILTER_SANITIZE_STRING,
177
			'meta_key'    => '_pronamic_gateway_omnikassa_2_order_id',
178
			'title'       => \__( 'Order ID', 'pronamic_ideal' ),
179
			'type'        => 'text',
180
			'classes'     => array( 'regular-text', 'code' ),
181
			'tooltip'     => \sprintf(
182
				/* translators: %s: Parameter */
183
				\__( 'The OmniKassa %s parameter.', 'pronamic_ideal' ),
184
				\sprintf( '<code>%s</code>', 'orderId' )
185
			),
186
			'description' => \sprintf(
187
				'%s %s<br />%s',
188
				\__( 'Available tags:', 'pronamic_ideal' ),
189
				\sprintf(
190
					'<code>%s</code> <code>%s</code>',
191
					'{order_id}',
192
					'{payment_id}'
193
				),
194
				\sprintf(
195
					/* translators: %s: {payment_id} */
196
					\__( 'Default: <code>%s</code>', 'pronamic_ideal' ),
197
					'{payment_id}'
198
				)
199
			),
200
		);
201
202
		// Webhook.
203
		$fields[] = array(
204
			'section'  => 'feedback',
205
			'title'    => \__( 'Webhook URL', 'pronamic_ideal' ),
206
			'type'     => 'text',
207
			'classes'  => array( 'large-text', 'code' ),
208
			'value'    => \add_query_arg( 'omnikassa2_webhook', '', \home_url( '/' ) ),
209
			'readonly' => true,
210
			'tooltip'  => \__( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
211
		);
212
213
		return $fields;
214
	}
215
216
	/**
217
	 * Get configuration by post ID.
218
	 *
219
	 * @param string $post_id Post ID.
220
	 * @return Config
221
	 */
222
	public function get_config( $post_id ) {
223
		$config = new Config();
224
225
		$config->post_id                  = \intval( $post_id );
226
		$config->mode                     = $this->get_meta( $post_id, 'mode' );
227
		$config->refresh_token            = $this->get_meta( $post_id, 'omnikassa_2_refresh_token' );
228
		$config->signing_key              = $this->get_meta( $post_id, 'omnikassa_2_signing_key' );
229
		$config->access_token             = $this->get_meta( $post_id, 'omnikassa_2_access_token' );
230
		$config->access_token_valid_until = $this->get_meta( $post_id, 'omnikassa_2_access_token_valid_until' );
231
		$config->order_id                 = $this->get_meta( $post_id, 'omnikassa_2_order_id' );
232
233
		return $config;
234
	}
235
236
	/**
237
	 * Delete access token meta for the specified post ID.
238
	 *
239
	 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/post.php#L3724-L3736
240
	 * @link https://codex.wordpress.org/Function_Reference/delete_post_meta
241
	 * @param int $post_id Post ID.
242
	 */
243
	public static function delete_access_token_meta( $post_id ) {
244
		\delete_post_meta( $post_id, '_pronamic_gateway_omnikassa_2_access_token' );
245
		\delete_post_meta( $post_id, '_pronamic_gateway_omnikassa_2_access_token_valid_until' );
246
	}
247
248
	/**
249
	 * Get gateway.
250
	 *
251
	 * @param int $post_id Post ID.
252
	 * @return Gateway
253
	 */
254
	public function get_gateway( $post_id ) {
255
		return new Gateway( $this->get_config( $post_id ) );
256
	}
257
}
258