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