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
|
|||||||
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 | * @throws \Exception Throws exception after four failed attempts. |
||||||
287 | */ |
||||||
288 | public function scheduled_payment_start( $payment_id ) { |
||||||
289 | 10 | // Check payment. |
|||||
290 | $payment = \get_pronamic_payment( $payment_id ); |
||||||
291 | 10 | ||||||
292 | 10 | if ( null === $payment ) { |
|||||
293 | return; |
||||||
294 | } |
||||||
295 | |||||||
296 | // Check gateway. |
||||||
297 | $gateway = $payment->get_gateway(); |
||||||
0 ignored issues
–
show
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
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. ![]() |
|||||||
298 | |||||||
299 | if ( null === $gateway ) { |
||||||
300 | return; |
||||||
301 | } |
||||||
302 | |||||||
303 | // Attempt. |
||||||
304 | $attempt = (int) $payment->get_meta( 'mollie_create_payment_attempt' ); |
||||||
305 | |||||||
306 | if ( $attempt > 4 ) { |
||||||
307 | throw new \Exception( \sprintf( 'Could not create Mollie payment for %s after %s attempts.', $payment_id, $attempt ) ); |
||||||
308 | } |
||||||
309 | |||||||
310 | // Start payment. |
||||||
311 | $gateway->start( $payment ); |
||||||
312 | |||||||
313 | $payment->save(); |
||||||
314 | } |
||||||
315 | |||||||
316 | /** |
||||||
317 | * Next payment delivery date. |
||||||
318 | * |
||||||
319 | * @param DateTime $next_payment_delivery_date Next payment delivery date. |
||||||
320 | * @param CoreSubscription $subscription Subscription. |
||||||
321 | * @return DateTime |
||||||
322 | */ |
||||||
323 | public function next_payment_delivery_date( DateTime $next_payment_delivery_date, CoreSubscription $subscription ) { |
||||||
324 | $config_id = $subscription->get_config_id(); |
||||||
325 | |||||||
326 | if ( null === $config_id ) { |
||||||
327 | return $next_payment_delivery_date; |
||||||
328 | } |
||||||
329 | |||||||
330 | // Check gateway. |
||||||
331 | $gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true ); |
||||||
332 | |||||||
333 | if ( 'mollie' !== $gateway_id ) { |
||||||
334 | return $next_payment_delivery_date; |
||||||
335 | } |
||||||
336 | |||||||
337 | // Check direct debit payment method. |
||||||
338 | $payment_method = $subscription->get_payment_method(); |
||||||
0 ignored issues
–
show
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
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. ![]() |
|||||||
339 | |||||||
340 | if ( null === $payment_method ) { |
||||||
341 | return $next_payment_delivery_date; |
||||||
342 | } |
||||||
343 | |||||||
344 | if ( ! PaymentMethods::is_direct_debit_method( $payment_method ) ) { |
||||||
345 | return $next_payment_delivery_date; |
||||||
346 | } |
||||||
347 | |||||||
348 | // Base delivery date on next payment date. |
||||||
349 | $next_payment_date = $subscription->get_next_payment_date(); |
||||||
350 | |||||||
351 | if ( null === $next_payment_date ) { |
||||||
352 | return $next_payment_delivery_date; |
||||||
353 | } |
||||||
354 | |||||||
355 | $next_payment_delivery_date = clone $next_payment_date; |
||||||
356 | |||||||
357 | // Textual representation of the day of the week, Sunday through Saturday. |
||||||
358 | $day_of_week = $next_payment_delivery_date->format( 'l' ); |
||||||
359 | |||||||
360 | /* |
||||||
361 | * Subtract days from next payment date for earlier delivery. |
||||||
362 | * |
||||||
363 | * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out- |
||||||
364 | * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times- |
||||||
365 | */ |
||||||
366 | switch ( $day_of_week ) { |
||||||
367 | case 'Monday': |
||||||
368 | $next_payment_delivery_date->modify( '-3 days' ); |
||||||
369 | |||||||
370 | break; |
||||||
371 | case 'Saturday': |
||||||
372 | $next_payment_delivery_date->modify( '-2 days' ); |
||||||
373 | |||||||
374 | break; |
||||||
375 | case 'Sunday': |
||||||
376 | $next_payment_delivery_date->modify( '-3 days' ); |
||||||
377 | |||||||
378 | break; |
||||||
379 | default: |
||||||
380 | $next_payment_delivery_date->modify( '-1 day' ); |
||||||
381 | |||||||
382 | break; |
||||||
383 | } |
||||||
384 | |||||||
385 | $next_payment_delivery_date->setTime( 0, 0, 0 ); |
||||||
386 | |||||||
387 | return $next_payment_delivery_date; |
||||||
388 | } |
||||||
389 | } |
||||||
390 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: