Passed
Push — master ( 63237f...7c0386 )
by Reüel
06:18 queued 10s
created

Integration   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 275
Duplicated Lines 0 %

Test Coverage

Coverage 52.59%

Importance

Changes 15
Bugs 0 Features 0
Metric Value
eloc 120
c 15
b 0
f 0
dl 0
loc 275
ccs 61
cts 116
cp 0.5259
rs 10
wmc 25

8 Methods

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