Test Failed
Push — develop ( a631f3...d09f54 )
by Remco
04:21
created

src/Integration.php (3 issues)

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;
0 ignored issues
show
The type Pronamic\WordPress\Pay\AbstractGatewayIntegration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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