Test Failed
Push — develop ( 172541...029f0f )
by Reüel
03:40
created

src/Integration.php (1 issue)

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