Failed Conditions
Push — develop ( d9e064...07ea3a )
by Reüel
04:43
created

Integration::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Mollie integration.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use Pronamic\WordPress\Pay\Core\PaymentMethods;
14
use Pronamic\WordPress\Pay\AbstractGatewayIntegration;
15
use Pronamic\WordPress\Pay\Payments\Payment;
16
use WP_User;
17
18
/**
19
 * Title: Mollie integration
20
 * Description:
21
 * Copyright: 2005-2020 Pronamic
22
 * Company: Pronamic
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.9
26
 * @since   1.0.0
27
 */
28
class Integration extends AbstractGatewayIntegration {
29
	/**
30
	 * REST route namespace.
31
	 *
32
	 * @var string
33
	 */
34
	const REST_ROUTE_NAMESPACE = 'pronamic-pay/mollie/v1';
35
36
	/**
37
	 * Register URL.
38
	 *
39
	 * @var string
40
	 */
41
	public $register_url;
42
43
	/**
44
	 * Construct and intialize Mollie integration.
45
	 *
46
	 * @param array $args Arguments.
47
	 */
48 7
	public function __construct( $args = array() ) {
49 7
		$args = wp_parse_args(
50 7
			$args,
51
			array(
52 7
				'id'                     => 'mollie',
53 7
				'name'                   => 'Mollie',
54 7
				'version'                => '2.1.0',
55 7
				'url'                    => 'http://www.mollie.com/en/',
56 7
				'product_url'            => \__( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' ),
57 7
				'dashboard_url'          => 'https://www.mollie.com/dashboard/',
58 7
				'provider'               => 'mollie',
59
				'supports'               => array(
60
					'payment_status_request',
61
					'recurring_direct_debit',
62
					'recurring_credit_card',
63
					'recurring',
64
					'webhook',
65
					'webhook_log',
66
					'webhook_no_config',
67
				),
68 7
				'version_option_name'    => 'pronamic_pay_mollie_version',
69 7
				'db_version_option_name' => 'pronamic_pay_mollie_db_version',
70
			)
71
		);
72
73 7
		parent::__construct( $args );
74
75
		// Filters.
76 7
		$function = array( $this, 'next_payment_delivery_date' );
77
78 7
		if ( ! \has_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function ) ) {
79 7
			\add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function, 10, 2 );
80
		}
81
82 7
		add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 );
83
84
		// Tables.
85 7
		$this->register_tables();
86
87
		/**
88
		 * Install.
89
		 */
90 7
		$this->install = new Install( $this );
0 ignored issues
show
Bug Best Practice introduced by
The property install does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
91
92
		/**
93
		 * Admin
94
		 */
95 7
		if ( \is_admin() ) {
96
			$this->admin = new Admin();
0 ignored issues
show
Bug Best Practice introduced by
The property admin does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
97
		}
98
99
		/**
100
		 * CLI.
101
		 *
102
		 * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-woocommerce.php#L453-L455
103
		 */
104 7
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
0 ignored issues
show
Bug introduced by
The constant Pronamic\WordPress\Pay\Gateways\Mollie\WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
105
			$this->cli = new CLI();
0 ignored issues
show
Bug Best Practice introduced by
The property cli does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
106
		}
107 7
	}
108
109
	/**
110
	 * Setup gateway integration.
111
	 *
112
	 * @return void
113
	 */
114
	public function setup() {
115
		// Check if dependencies are met and integration is active.
116
		if ( ! $this->is_active() ) {
117
			return;
118
		}
119
120
		// Webhook controller.
121
		$webhook_controller = new WebhookController();
122
123
		$webhook_controller->setup();
124
	}
125
126
	/**
127
	 * Register tables.
128
	 *
129
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L894-L937
130
	 */
131 7
	private function register_tables() {
132 7
		global $wpdb;
133
134
		/**
135
		 * Tables.
136
		 */
137 7
		$wpdb->pronamic_pay_mollie_organizations  = $wpdb->base_prefix . 'pronamic_pay_mollie_organizations';
138 7
		$wpdb->pronamic_pay_mollie_profiles       = $wpdb->base_prefix . 'pronamic_pay_mollie_profiles';
139 7
		$wpdb->pronamic_pay_mollie_customers      = $wpdb->base_prefix . 'pronamic_pay_mollie_customers';
140 7
		$wpdb->pronamic_pay_mollie_customer_users = $wpdb->base_prefix . 'pronamic_pay_mollie_customer_users';
141 7
	}
142
143
	/**
144
	 * Get settings fields.
145
	 *
146
	 * @return array<int, array<string, array<int, string>|int|string|true>>
147
	 */
148 1
	public function get_settings_fields() {
149 1
		$fields = array();
150
151
		// API Key.
152 1
		$fields[] = array(
153 1
			'section'  => 'general',
154 1
			'filter'   => FILTER_SANITIZE_STRING,
155 1
			'meta_key' => '_pronamic_gateway_mollie_api_key',
156 1
			'title'    => _x( 'API Key', 'mollie', 'pronamic_ideal' ),
157 1
			'type'     => 'text',
158
			'classes'  => array( 'regular-text', 'code' ),
159 1
			'tooltip'  => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ),
160
		);
161
162
		// Due date days.
163 1
		$fields[] = array(
164 1
			'section'     => 'advanced',
165
			'filter'      => \FILTER_SANITIZE_NUMBER_INT,
166 1
			'meta_key'    => '_pronamic_gateway_mollie_due_date_days',
167 1
			'title'       => _x( 'Due date days', 'mollie', 'pronamic_ideal' ),
168 1
			'type'        => 'number',
169 1
			'min'         => 1,
170 1
			'max'         => 100,
171
			'classes'     => array( 'regular-text' ),
172 1
			'tooltip'     => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ),
173 1
			'description' => sprintf(
174
				/* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */
175 1
				__( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ),
176 1
				sprintf( '<code>%s</code>', '1' ),
177 1
				sprintf( '<code>%s</code>', '100' ),
178 1
				sprintf( '<code>%s</code>', '12' )
179
			),
180
		);
181
182
		// Webhook.
183 1
		$fields[] = array(
184 1
			'section'  => 'feedback',
185 1
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
186 1
			'type'     => 'text',
187
			'classes'  => array( 'large-text', 'code' ),
188 1
			'value'    => rest_url( self::REST_ROUTE_NAMESPACE . '/webhook' ),
189
			'readonly' => true,
190 1
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
191
		);
192
193 1
		return $fields;
194
	}
195
196
	/**
197
	 * Save post.
198
	 *
199
	 * @link https://developer.wordpress.org/reference/functions/get_post_meta/
200
	 * @param int $post_id Post ID.
201
	 * @return void
202
	 */
203
	public function save_post( $post_id ) {
204
		$api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true );
205
206
		if ( ! is_string( $api_key ) ) {
207
			return;
208
		}
209
210
		$api_key_prefix = substr( $api_key, 0, 4 );
211
212
		switch ( $api_key_prefix ) {
213
			case 'live':
214
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE );
215
216
				return;
217
			case 'test':
218
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST );
219
220
				return;
221
		}
222
	}
223
224
	/**
225
	 * Payment provider URL.
226
	 *
227
	 * @param string|null $url     Payment provider URL.
228
	 * @param Payment     $payment Payment.
229
	 * @return string|null
230
	 */
231 1
	public function payment_provider_url( $url, Payment $payment ) {
232 1
		$transaction_id = $payment->get_transaction_id();
233
234 1
		if ( null === $transaction_id ) {
235
			return $url;
236
		}
237
238 1
		return sprintf(
239 1
			'https://www.mollie.com/dashboard/payments/%s',
240
			$transaction_id
241
		);
242
	}
243
	/**
244
	 * Get configuration by post ID.
245
	 *
246
	 * @param int $post_id Post ID.
247
	 * @return Config
248
	 */
249 2
	public function get_config( $post_id ) {
250 2
		$config = new Config();
251
252 2
		$config->id            = intval( $post_id );
253 2
		$config->api_key       = $this->get_meta( $post_id, 'mollie_api_key' );
254 2
		$config->mode          = $this->get_meta( $post_id, 'mode' );
255 2
		$config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' );
256
257 2
		return $config;
258
	}
259
260
	/**
261
	 * Get gateway.
262
	 *
263
	 * @param int $post_id Post ID.
264
	 * @return Gateway
265
	 */
266 1
	public function get_gateway( $post_id ) {
267 1
		return new Gateway( $this->get_config( $post_id ) );
268
	}
269
270
	/**
271
	 * Next payment delivery date.
272
	 *
273
	 * @param \DateTime $next_payment_delivery_date Next payment delivery date.
274
	 * @param Payment   $payment                    Payment.
275
	 * @return \DateTime
276
	 */
277 10
	public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) {
278 10
		$config_id = $payment->get_config_id();
279
280 10
		if ( null === $config_id ) {
281
			return $next_payment_delivery_date;
282
		}
283
284
		// Check gateway.
285 10
		$gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true );
286
287 10
		if ( 'mollie' !== $gateway_id ) {
288 10
			return $next_payment_delivery_date;
289
		}
290
291
		// Check direct debit payment method.
292
		$method = $payment->get_method();
293
294
		if ( null === $method ) {
295
			return $next_payment_delivery_date;
296
		}
297
298
		if ( ! PaymentMethods::is_direct_debit_method( $method ) ) {
299
			return $next_payment_delivery_date;
300
		}
301
302
		// Check subscription.
303
		$subscription = $payment->get_subscription();
304
305
		if ( null === $subscription ) {
306
			return $next_payment_delivery_date;
307
		}
308
309
		// Base delivery date on next payment date.
310
		$next_payment_date = $subscription->get_next_payment_date();
311
312
		if ( null === $next_payment_date ) {
313
			return $next_payment_delivery_date;
314
		}
315
316
		$next_payment_delivery_date = clone $next_payment_date;
317
318
		// Textual representation of the day of the week, Sunday through Saturday.
319
		$day_of_week = $next_payment_delivery_date->format( 'l' );
320
321
		/*
322
		 * Subtract days from next payment date for earlier delivery.
323
		 *
324
		 * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out-
325
		 * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times-
326
		 */
327
		switch ( $day_of_week ) {
328
			case 'Monday':
329
				$next_payment_delivery_date->modify( '-3 days' );
330
331
				break;
332
			case 'Saturday':
333
				$next_payment_delivery_date->modify( '-2 days' );
334
335
				break;
336
			case 'Sunday':
337
				$next_payment_delivery_date->modify( '-3 days' );
338
339
				break;
340
			default:
341
				$next_payment_delivery_date->modify( '-1 day' );
342
343
				break;
344
		}
345
346
		$next_payment_delivery_date->setTime( 0, 0, 0 );
347
348
		return $next_payment_delivery_date;
349
	}
350
}
351