Failed Conditions
Push — master ( 42f2fb...4d20cd )
by Remco
22:47 queued 13:20
created

Integration::get_config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 2
rs 9.9666
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.0
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
		 *
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
		 *
66
		 * @var callable $admin_notices_function
67
		 */
68
		$admin_notices_function = array( $this, 'admin_notice_tld_test' );
69
70
		if ( ! has_action( 'admin_notices', $admin_notices_function ) ) {
71
			add_action( 'admin_notices', $admin_notices_function );
72
		}
73
	}
74
75
	/**
76
	 * Admin notice TLD .test.
77
	 *
78
	 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-admin/admin-header.php#L259-L264
79
	 * @link https://developer.wordpress.org/reference/hooks/admin_notices/
80
	 * @link https://developer.wordpress.org/reference/functions/get_current_screen/
81
	 */
82
	public function admin_notice_tld_test() {
83
		if ( has_filter( 'pronamic_pay_omnikassa_2_merchant_return_url' ) ) {
84
			return;
85
		}
86
87
		$screen = get_current_screen();
88
89
		if ( null === $screen ) {
90
			return;
91
		}
92
93
		if ( 'pronamic_gateway' !== $screen->id ) {
94
			return;
95
		}
96
97
		$host = wp_parse_url( home_url( '/' ), PHP_URL_HOST );
98
99
		if ( is_array( $host ) ) {
100
			return;
101
		}
102
103
		if ( '.test' !== substr( $host, -5 ) ) {
104
			return;
105
		}
106
107
		$post_id = get_the_ID();
108
109
		if ( empty( $post_id ) ) {
110
			return;
111
		}
112
113
		$gateway_id = get_post_meta( $post_id, '_pronamic_gateway_id', true );
114
115
		if ( 'rabobank-omnikassa-2' !== $gateway_id ) {
116
			return;
117
		}
118
119
		$class   = 'notice notice-error';
120
		$message = sprintf(
121
			/* translators: 1: Pronamic Pay, 2: Documentation link, 3: <code>.test</code> */
122
			__( '%1$s — <a href="%2$s">OmniKassa 2 does not accept payments from %3$s environments</a>.', 'pronamic_ideal' ),
123
			sprintf(
124
				'<strong>%s</strong>',
125
				__( 'Pronamic Pay', 'pronamic_ideal' )
126
			),
127
			'https://github.com/wp-pay-gateways/omnikassa-2/tree/develop/documentation#merchantreturnurl-is-not-a-valid-web-address',
128
			'<code>.test</code>'
129
		);
130
131
		printf(
132
			'<div class="%1$s"><p>%2$s</p></div>',
133
			esc_attr( $class ),
134
			wp_kses(
135
				$message,
136
				array(
137
					'a'      => array(
138
						'href' => true,
139
					),
140
					'code'   => array(),
141
					'strong' => array(),
142
				)
143
			)
144
		);
145
	}
146
147
	/**
148
	 * Get settings fields.
149
	 *
150
	 * @return array
151
	 */
152
	public function get_settings_fields() {
153
		$fields = array();
154
155
		// Refresh Token.
156
		$fields[] = array(
157
			'section'  => 'general',
158
			'filter'   => FILTER_SANITIZE_STRING,
159
			'meta_key' => '_pronamic_gateway_omnikassa_2_refresh_token',
160
			'title'    => _x( 'Refresh Token', 'omnikassa', 'pronamic_ideal' ),
161
			'type'     => 'textarea',
162
			'classes'  => array( 'code' ),
163
		);
164
165
		// Signing Key.
166
		$fields[] = array(
167
			'section'  => 'general',
168
			'filter'   => FILTER_SANITIZE_STRING,
169
			'meta_key' => '_pronamic_gateway_omnikassa_2_signing_key',
170
			'title'    => _x( 'Signing Key', 'omnikassa', 'pronamic_ideal' ),
171
			'type'     => 'text',
172
			'classes'  => array( 'large-text', 'code' ),
173
		);
174
175
		// Purchase ID.
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: Parameter */
185
				__( 'The OmniKassa %s parameter.', 'pronamic_ideal' ),
186
				sprintf( '<code>%s</code>', 'orderId' )
187
			),
188
			'description' => sprintf(
189
				'%s %s<br />%s',
190
				__( 'Available tags:', 'pronamic_ideal' ),
191
				sprintf(
192
					'<code>%s</code> <code>%s</code>',
193
					'{order_id}',
194
					'{payment_id}'
195
				),
196
				sprintf(
197
					/* translators: %s: {payment_id} */
198
					__( 'Default: <code>%s</code>', 'pronamic_ideal' ),
199
					'{payment_id}'
200
				)
201
			),
202
		);
203
204
		// Webhook.
205
		$fields[] = array(
206
			'section'  => 'feedback',
207
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
208
			'type'     => 'text',
209
			'classes'  => array( 'large-text', 'code' ),
210
			'value'    => add_query_arg( 'omnikassa2_webhook', '', home_url( '/' ) ),
211
			'readonly' => true,
212
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
213
		);
214
215
		return $fields;
216
	}
217
218
	/**
219
	 * Get configuration by post ID.
220
	 *
221
	 * @param string $post_id Post ID.
222
	 * @return Config
223
	 */
224
	public function get_config( $post_id ) {
225
		$config = new Config();
226
227
		$config->post_id                  = intval( $post_id );
228
		$config->mode                     = $this->get_meta( $post_id, 'mode' );
229
		$config->refresh_token            = $this->get_meta( $post_id, 'omnikassa_2_refresh_token' );
230
		$config->signing_key              = $this->get_meta( $post_id, 'omnikassa_2_signing_key' );
231
		$config->access_token             = $this->get_meta( $post_id, 'omnikassa_2_access_token' );
232
		$config->access_token_valid_until = $this->get_meta( $post_id, 'omnikassa_2_access_token_valid_until' );
233
		$config->order_id                 = $this->get_meta( $post_id, 'omnikassa_2_order_id' );
234
235
		return $config;
236
	}
237
238
	/**
239
	 * Delete access token meta for the specified post ID.
240
	 *
241
	 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/post.php#L3724-L3736
242
	 * @link https://codex.wordpress.org/Function_Reference/delete_post_meta
243
	 *
244
	 * @param int $post_id Post ID.
245
	 */
246
	public static function delete_access_token_meta( $post_id ) {
247
		delete_post_meta( $post_id, '_pronamic_gateway_omnikassa_2_access_token' );
248
		delete_post_meta( $post_id, '_pronamic_gateway_omnikassa_2_access_token_valid_until' );
249
	}
250
251
	/**
252
	 * Get gateway.
253
	 *
254
	 * @param int $post_id Post ID.
255
	 * @return Gateway
256
	 */
257
	public function get_gateway( $post_id ) {
258
		return new Gateway( $this->get_config( $post_id ) );
259
	}
260
}
261