Failed Conditions
Push — develop ( ea6df3...593e4f )
by Remco
04:34
created

src/Integration.php (1 issue)

Labels
Severity
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<string, 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
		new Install( $this );
91
92
		/**
93
		 * Admin
94
		 */
95 7
		if ( \is_admin() ) {
96
			new Admin();
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
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
			new CLI();
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
	 * @return void
131
	 */
132 7
	private function register_tables() {
133 7
		global $wpdb;
134
135
		/**
136
		 * Tables.
137
		 */
138 7
		$wpdb->pronamic_pay_mollie_organizations  = $wpdb->base_prefix . 'pronamic_pay_mollie_organizations';
139 7
		$wpdb->pronamic_pay_mollie_profiles       = $wpdb->base_prefix . 'pronamic_pay_mollie_profiles';
140 7
		$wpdb->pronamic_pay_mollie_customers      = $wpdb->base_prefix . 'pronamic_pay_mollie_customers';
141 7
		$wpdb->pronamic_pay_mollie_customer_users = $wpdb->base_prefix . 'pronamic_pay_mollie_customer_users';
142 7
	}
143
144
	/**
145
	 * Get settings fields.
146
	 *
147
	 * @return array<int, array<string, array<int, string>|int|string|true>>
148
	 */
149 1
	public function get_settings_fields() {
150 1
		$fields = array();
151
152
		// API Key.
153 1
		$fields[] = array(
154 1
			'section'  => 'general',
155 1
			'filter'   => FILTER_SANITIZE_STRING,
156 1
			'meta_key' => '_pronamic_gateway_mollie_api_key',
157 1
			'title'    => _x( 'API Key', 'mollie', 'pronamic_ideal' ),
158 1
			'type'     => 'text',
159
			'classes'  => array( 'regular-text', 'code' ),
160 1
			'tooltip'  => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ),
161
		);
162
163
		// Due date days.
164 1
		$fields[] = array(
165 1
			'section'     => 'advanced',
166
			'filter'      => \FILTER_SANITIZE_NUMBER_INT,
167 1
			'meta_key'    => '_pronamic_gateway_mollie_due_date_days',
168 1
			'title'       => _x( 'Due date days', 'mollie', 'pronamic_ideal' ),
169 1
			'type'        => 'number',
170 1
			'min'         => 1,
171 1
			'max'         => 100,
172
			'classes'     => array( 'regular-text' ),
173 1
			'tooltip'     => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ),
174 1
			'description' => sprintf(
175
				/* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */
176 1
				__( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ),
177 1
				sprintf( '<code>%s</code>', '1' ),
178 1
				sprintf( '<code>%s</code>', '100' ),
179 1
				sprintf( '<code>%s</code>', '12' )
180
			),
181
		);
182
183
		// Webhook.
184 1
		$fields[] = array(
185 1
			'section'  => 'feedback',
186 1
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
187 1
			'type'     => 'text',
188
			'classes'  => array( 'large-text', 'code' ),
189 1
			'value'    => rest_url( self::REST_ROUTE_NAMESPACE . '/webhook' ),
190
			'readonly' => true,
191 1
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
192
		);
193
194 1
		return $fields;
195
	}
196
197
	/**
198
	 * Save post.
199
	 *
200
	 * @link https://developer.wordpress.org/reference/functions/get_post_meta/
201
	 * @param int $post_id Post ID.
202
	 * @return void
203
	 */
204
	public function save_post( $post_id ) {
205
		$api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true );
206
207
		if ( ! is_string( $api_key ) ) {
208
			return;
209
		}
210
211
		$api_key_prefix = substr( $api_key, 0, 4 );
212
213
		switch ( $api_key_prefix ) {
214
			case 'live':
215
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE );
216
217
				return;
218
			case 'test':
219
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST );
220
221
				return;
222
		}
223
	}
224
225
	/**
226
	 * Payment provider URL.
227
	 *
228
	 * @param string|null $url     Payment provider URL.
229
	 * @param Payment     $payment Payment.
230
	 * @return string|null
231
	 */
232 1
	public function payment_provider_url( $url, Payment $payment ) {
233 1
		$transaction_id = $payment->get_transaction_id();
234
235 1
		if ( null === $transaction_id ) {
236
			return $url;
237
		}
238
239 1
		return sprintf(
240 1
			'https://www.mollie.com/dashboard/payments/%s',
241
			$transaction_id
242
		);
243
	}
244
	/**
245
	 * Get configuration by post ID.
246
	 *
247
	 * @param int $post_id Post ID.
248
	 * @return Config
249
	 */
250 2
	public function get_config( $post_id ) {
251 2
		$config = new Config();
252
253 2
		$config->id            = intval( $post_id );
254 2
		$config->api_key       = $this->get_meta( $post_id, 'mollie_api_key' );
255 2
		$config->mode          = $this->get_meta( $post_id, 'mode' );
256 2
		$config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' );
257
258 2
		return $config;
259
	}
260
261
	/**
262
	 * Get gateway.
263
	 *
264
	 * @param int $post_id Post ID.
265
	 * @return Gateway
266
	 */
267 1
	public function get_gateway( $post_id ) {
268 1
		return new Gateway( $this->get_config( $post_id ) );
269
	}
270
271
	/**
272
	 * Next payment delivery date.
273
	 *
274
	 * @param \DateTime $next_payment_delivery_date Next payment delivery date.
275
	 * @param Payment   $payment                    Payment.
276
	 * @return \DateTime
277
	 */
278 10
	public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) {
279 10
		$config_id = $payment->get_config_id();
280
281 10
		if ( null === $config_id ) {
282
			return $next_payment_delivery_date;
283
		}
284
285
		// Check gateway.
286 10
		$gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true );
287
288 10
		if ( 'mollie' !== $gateway_id ) {
289 10
			return $next_payment_delivery_date;
290
		}
291
292
		// Check direct debit payment method.
293
		$method = $payment->get_method();
294
295
		if ( null === $method ) {
296
			return $next_payment_delivery_date;
297
		}
298
299
		if ( ! PaymentMethods::is_direct_debit_method( $method ) ) {
300
			return $next_payment_delivery_date;
301
		}
302
303
		// Check subscription.
304
		$subscription = $payment->get_subscription();
305
306
		if ( null === $subscription ) {
307
			return $next_payment_delivery_date;
308
		}
309
310
		// Base delivery date on next payment date.
311
		$next_payment_date = $subscription->get_next_payment_date();
312
313
		if ( null === $next_payment_date ) {
314
			return $next_payment_delivery_date;
315
		}
316
317
		$next_payment_delivery_date = clone $next_payment_date;
318
319
		// Textual representation of the day of the week, Sunday through Saturday.
320
		$day_of_week = $next_payment_delivery_date->format( 'l' );
321
322
		/*
323
		 * Subtract days from next payment date for earlier delivery.
324
		 *
325
		 * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out-
326
		 * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times-
327
		 */
328
		switch ( $day_of_week ) {
329
			case 'Monday':
330
				$next_payment_delivery_date->modify( '-3 days' );
331
332
				break;
333
			case 'Saturday':
334
				$next_payment_delivery_date->modify( '-2 days' );
335
336
				break;
337
			case 'Sunday':
338
				$next_payment_delivery_date->modify( '-3 days' );
339
340
				break;
341
			default:
342
				$next_payment_delivery_date->modify( '-1 day' );
343
344
				break;
345
		}
346
347
		$next_payment_delivery_date->setTime( 0, 0, 0 );
348
349
		return $next_payment_delivery_date;
350
	}
351
}
352