Test Failed
Push — develop ( 9b8657...429647 )
by Reüel
05:35
created

Integration::user_profile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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.8
26
 * @since   1.0.0
27
 */
28
class Integration extends AbstractIntegration {
29
	/**
30
	 * Register URL.
31
	 *
32 7
	 * @var string
33 7
	 */
34 7
	public $register_url;
35 7
36 7
	/**
37 7
	 * Construct and intialize Mollie integration.
38 7
	 */
39 7
	public function __construct() {
40 7
		$this->id            = 'mollie';
41
		$this->name          = 'Mollie';
42
		$this->url           = 'http://www.mollie.com/en/';
43
		$this->product_url   = __( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' );
44
		$this->dashboard_url = 'https://www.mollie.com/dashboard/';
45
		$this->register_url  = 'https://www.mollie.com/nl/signup/665327';
46
		$this->provider      = 'mollie';
47
		$this->supports      = array(
48 7
			'payment_status_request',
49
			'webhook',
50 7
			'webhook_log',
51 7
			'webhook_no_config',
52
		);
53
54 7
		$this->set_manual_url( __( 'https://www.pronamic.eu/support/how-to-connect-mollie-with-wordpress-via-pronamic-pay/', 'pronamic_ideal' ) );
0 ignored issues
show
Bug introduced by
The method set_manual_url() does not exist on Pronamic\WordPress\Pay\Gateways\Mollie\Integration. ( Ignorable by Annotation )

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

54
		$this->/** @scrutinizer ignore-call */ 
55
         set_manual_url( __( 'https://www.pronamic.eu/support/how-to-connect-mollie-with-wordpress-via-pronamic-pay/', 'pronamic_ideal' ) );

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