Test Failed
Push — develop ( 90cd3b...e7eb71 )
by Reüel
05:09
created

Integration::scheduled_payment_start()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 10.1554

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 26
ccs 3
cts 11
cp 0.2727
crap 10.1554
rs 9.7666
1
<?php
2
/**
3
 * Mollie integration.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 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\DateTime\DateTime;
14
use Pronamic\WordPress\Pay\Core\PaymentMethods;
15
use Pronamic\WordPress\Pay\AbstractGatewayIntegration;
16
use Pronamic\WordPress\Pay\Payments\Payment;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Pronamic\WordPress\Pay\Gateways\Mollie\Payment. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Pronamic\WordPress\Pay\Subscriptions\Subscription as CoreSubscription;
18
19
/**
20
 * Title: Mollie integration
21
 * Description:
22
 * Copyright: 2005-2021 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 initialize Mollie integration.
46
	 *
47
	 * @param array<string, array> $args Arguments.
48
	 */
49 7
	public function __construct( $args = array() ) {
50 7
		$args = wp_parse_args(
51 7
			$args,
52
			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 7
				'provider'               => 'mollie',
60
				'supports'               => array(
61
					'payment_status_request',
62
					'recurring_direct_debit',
63
					'recurring_credit_card',
64
					'recurring',
65
					'refunds',
66
					'webhook',
67
					'webhook_log',
68
					'webhook_no_config',
69
				),
70 7
				'version_option_name'    => 'pronamic_pay_mollie_version',
71 7
				'db_version_option_name' => 'pronamic_pay_mollie_db_version',
72
			)
73
		);
74
75 7
		parent::__construct( $args );
76
77
		// Filters.
78 7
		$function = array( $this, 'next_payment_delivery_date' );
79
80 7
		if ( ! \has_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function ) ) {
81 7
			\add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function, 10, 2 );
82
		}
83
84 7
		add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 );
85
86
		// Actions.
87 7
		$function = array( $this, 'scheduled_payment_start' );
88
89
		if ( ! \has_action( 'pronamic_pay_mollie_payment_start', $function ) ) {
90
			\add_action( 'pronamic_pay_mollie_payment_start', $function, 10, 1 );
91
		}
92 7
93
		// Tables.
94
		$this->register_tables();
95
96
		/**
97 7
		 * Install.
98
		 */
99
		new Install( $this );
100
101
		/**
102
		 * Admin
103
		 */
104
		if ( \is_admin() ) {
105
			new Admin();
106 7
		}
107
108
		/**
109 7
		 * CLI.
110
		 *
111
		 * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-woocommerce.php#L453-L455
112
		 */
113
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
114
			new CLI();
115
		}
116
	}
117
118
	/**
119
	 * Setup gateway integration.
120
	 *
121
	 * @return void
122
	 */
123
	public function setup() {
124
		// Check if dependencies are met and integration is active.
125
		if ( ! $this->is_active() ) {
126
			return;
127
		}
128
129
		// Webhook controller.
130
		$webhook_controller = new WebhookController();
131
132
		$webhook_controller->setup();
133
	}
134 7
135 7
	/**
136
	 * Register tables.
137
	 *
138
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L894-L937
139
	 * @return void
140 7
	 */
141 7
	private function register_tables() {
142 7
		global $wpdb;
143 7
144 7
		/**
145
		 * Tables.
146
		 */
147
		$wpdb->pronamic_pay_mollie_organizations  = $wpdb->base_prefix . 'pronamic_pay_mollie_organizations';
148
		$wpdb->pronamic_pay_mollie_profiles       = $wpdb->base_prefix . 'pronamic_pay_mollie_profiles';
149
		$wpdb->pronamic_pay_mollie_customers      = $wpdb->base_prefix . 'pronamic_pay_mollie_customers';
150
		$wpdb->pronamic_pay_mollie_customer_users = $wpdb->base_prefix . 'pronamic_pay_mollie_customer_users';
151 1
	}
152 1
153
	/**
154
	 * Get settings fields.
155 1
	 *
156 1
	 * @return array<int, array<string, callable|int|string|bool|array<int|string,int|string>>>
157 1
	 */
158 1
	public function get_settings_fields() {
159 1
		$fields = array();
160 1
161
		// API Key.
162 1
		$fields[] = array(
163
			'section'  => 'general',
164
			'filter'   => FILTER_SANITIZE_STRING,
165
			'meta_key' => '_pronamic_gateway_mollie_api_key',
166 1
			'title'    => _x( 'API Key', 'mollie', 'pronamic_ideal' ),
167 1
			'type'     => 'text',
168
			'classes'  => array( 'regular-text', 'code' ),
169 1
			'tooltip'  => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ),
170 1
		);
171 1
172 1
		// Due date days.
173 1
		$fields[] = array(
174
			'section'     => 'advanced',
175 1
			'filter'      => \FILTER_SANITIZE_NUMBER_INT,
176 1
			'meta_key'    => '_pronamic_gateway_mollie_due_date_days',
177
			'title'       => _x( 'Due date days', 'mollie', 'pronamic_ideal' ),
178 1
			'type'        => 'number',
179 1
			'min'         => 1,
180 1
			'max'         => 100,
181 1
			'classes'     => array( 'regular-text' ),
182
			'tooltip'     => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ),
183
			'description' => sprintf(
184
				/* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */
185
				__( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ),
186 1
				sprintf( '<code>%s</code>', '1' ),
187 1
				sprintf( '<code>%s</code>', '100' ),
188 1
				sprintf( '<code>%s</code>', '12' )
189 1
			),
190
		);
191 1
192
		// Webhook.
193 1
		$fields[] = array(
194
			'section'  => 'feedback',
195
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
196 1
			'type'     => 'text',
197
			'classes'  => array( 'large-text', 'code' ),
198
			'value'    => rest_url( self::REST_ROUTE_NAMESPACE . '/webhook' ),
199
			'readonly' => true,
200
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
201
		);
202
203
		return $fields;
204
	}
205
206
	/**
207
	 * Save post.
208
	 *
209
	 * @link https://developer.wordpress.org/reference/functions/get_post_meta/
210
	 * @param int $post_id Post ID.
211
	 * @return void
212
	 */
213
	public function save_post( $post_id ) {
214
		$api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true );
215
216
		if ( ! is_string( $api_key ) ) {
217
			return;
218
		}
219
220
		$api_key_prefix = substr( $api_key, 0, 4 );
221
222
		switch ( $api_key_prefix ) {
223
			case 'live':
224
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE );
225
226
				return;
227
			case 'test':
228
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST );
229
230
				return;
231
		}
232
	}
233
234 1
	/**
235 1
	 * Payment provider URL.
236
	 *
237 1
	 * @param string|null $url     Payment provider URL.
238
	 * @param Payment     $payment Payment.
239
	 * @return string|null
240
	 */
241 1
	public function payment_provider_url( $url, Payment $payment ) {
242 1
		$transaction_id = $payment->get_transaction_id();
243
244
		if ( null === $transaction_id ) {
245
			return $url;
246
		}
247
248
		return sprintf(
249
			'https://www.mollie.com/dashboard/payments/%s',
250
			$transaction_id
251
		);
252
	}
253 2
254 2
	/**
255
	 * Get configuration by post ID.
256 2
	 *
257 2
	 * @param int $post_id Post ID.
258 2
	 * @return Config
259 2
	 */
260
	public function get_config( $post_id ) {
261 2
		$config = new Config();
262
263
		$config->id            = intval( $post_id );
264
		$config->api_key       = $this->get_meta( $post_id, 'mollie_api_key' );
265
		$config->mode          = $this->get_meta( $post_id, 'mode' );
266
		$config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' );
267
268
		return $config;
269
	}
270 1
271 1
	/**
272
	 * Get gateway.
273
	 *
274
	 * @param int $post_id Post ID.
275
	 * @return Gateway
276
	 */
277
	public function get_gateway( $post_id ) {
278
		return new Gateway( $this->get_config( $post_id ) );
279
	}
280
281 10
	/**
282 10
	 * Start scheduled payment.
283
	 *
284 10
	 * @param int $payment_id Payment ID.
285
	 * @return void
286
	 */
287
	public function scheduled_payment_start( $payment_id ) {
288
		// Check payment.
289 10
		$payment = \get_pronamic_payment( $payment_id );
290
291 10
		if ( null === $payment ) {
292 10
			return;
293
		}
294
295
		// Check gateway.
296
		$gateway = $payment->get_gateway();
0 ignored issues
show
Bug introduced by
The method get_gateway() does not exist on Pronamic\WordPress\Pay\Payments\Payment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

296
		/** @scrutinizer ignore-call */ 
297
  $gateway = $payment->get_gateway();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
297
298
		if ( null === $gateway ) {
299
			return;
300
		}
301
302
		// Start payment.
303
		try {
304
			$gateway->start( $payment );
305
		} catch ( \Exception $e ) {
306
			\as_schedule_single_action(
0 ignored issues
show
Bug introduced by
The function as_schedule_single_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

306
			/** @scrutinizer ignore-call */ 
307
   \as_schedule_single_action(
Loading history...
307
				\time() + 60,
308
				'pronamic_pay_mollie_payment_start',
309
				array(
310
					'payment_id' => $payment_id,
311
				),
312
				'pronamic-pay-mollie'
313
			);
314
		}
315
	}
316
317
	/**
318
	 * Next payment delivery date.
319
	 *
320
	 * @param DateTime         $next_payment_delivery_date Next payment delivery date.
321
	 * @param CoreSubscription $subscription               Subscription.
322
	 * @return DateTime
323
	 */
324
	public function next_payment_delivery_date( DateTime $next_payment_delivery_date, CoreSubscription $subscription ) {
325
		$config_id = $subscription->get_config_id();
326
327
		if ( null === $config_id ) {
328
			return $next_payment_delivery_date;
329
		}
330
331
		// Check gateway.
332
		$gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true );
333
334
		if ( 'mollie' !== $gateway_id ) {
335
			return $next_payment_delivery_date;
336
		}
337
338
		// Check direct debit payment method.
339
		$payment_method = $subscription->get_payment_method();
0 ignored issues
show
Bug introduced by
The method get_payment_method() does not exist on Pronamic\WordPress\Pay\Subscriptions\Subscription. Did you maybe mean get_payments()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

339
		/** @scrutinizer ignore-call */ 
340
  $payment_method = $subscription->get_payment_method();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
340
341
		if ( null === $payment_method ) {
342
			return $next_payment_delivery_date;
343
		}
344
345
		if ( ! PaymentMethods::is_direct_debit_method( $payment_method ) ) {
346
			return $next_payment_delivery_date;
347
		}
348
349
		// Base delivery date on next payment date.
350
		$next_payment_date = $subscription->get_next_payment_date();
351
352
		if ( null === $next_payment_date ) {
353
			return $next_payment_delivery_date;
354
		}
355
356
		$next_payment_delivery_date = clone $next_payment_date;
357
358
		// Textual representation of the day of the week, Sunday through Saturday.
359
		$day_of_week = $next_payment_delivery_date->format( 'l' );
360
361
		/*
362
		 * Subtract days from next payment date for earlier delivery.
363
		 *
364
		 * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out-
365
		 * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times-
366
		 */
367
		switch ( $day_of_week ) {
368
			case 'Monday':
369
				$next_payment_delivery_date->modify( '-3 days' );
370
371
				break;
372
			case 'Saturday':
373
				$next_payment_delivery_date->modify( '-2 days' );
374
375
				break;
376
			case 'Sunday':
377
				$next_payment_delivery_date->modify( '-3 days' );
378
379
				break;
380
			default:
381
				$next_payment_delivery_date->modify( '-1 day' );
382
383
				break;
384
		}
385
386
		$next_payment_delivery_date->setTime( 0, 0, 0 );
387
388
		return $next_payment_delivery_date;
389
	}
390
}
391