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; |
||
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 | * REST route namespace. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | const REST_ROUTE_NAMESPACE = 'pronamic-pay/mollie/v1'; |
||
35 | |||
36 | /** |
||
37 | * Register URL. |
||
38 | * |
||
39 | 7 | * @var string |
|
40 | 7 | */ |
|
41 | 7 | public $register_url; |
|
42 | 7 | ||
43 | 7 | /** |
|
44 | 7 | * Construct and intialize Mollie integration. |
|
45 | 7 | * |
|
46 | 7 | * @param array $args Arguments. |
|
47 | 7 | */ |
|
48 | public function __construct( $args = array() ) { |
||
49 | $args = wp_parse_args( |
||
50 | $args, |
||
51 | array( |
||
52 | 'id' => 'mollie', |
||
53 | 'name' => 'Mollie', |
||
54 | 'url' => 'http://www.mollie.com/en/', |
||
55 | 'product_url' => \__( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' ), |
||
56 | 'dashboard_url' => 'https://www.mollie.com/dashboard/', |
||
57 | 7 | 'provider' => 'mollie', |
|
58 | 'supports' => array( |
||
59 | 'payment_status_request', |
||
60 | 7 | 'recurring_direct_debit', |
|
61 | 'recurring_credit_card', |
||
62 | 7 | 'recurring', |
|
63 | 7 | 'webhook', |
|
64 | 'webhook_log', |
||
65 | 'webhook_no_config', |
||
66 | 7 | ), |
|
67 | ) |
||
68 | ); |
||
69 | |||
70 | parent::__construct( $args ); |
||
71 | |||
72 | // Filters. |
||
73 | $function = array( $this, 'next_payment_delivery_date' ); |
||
74 | |||
75 | if ( ! \has_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function ) ) { |
||
76 | \add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function, 10, 2 ); |
||
77 | } |
||
78 | |||
79 | 7 | add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 ); |
|
80 | |||
81 | 7 | // Tables. |
|
82 | 7 | $this->register_tables(); |
|
83 | |||
84 | // Upgrades. |
||
85 | 7 | $upgrades = $this->get_upgrades(); |
|
86 | 7 | ||
87 | $upgrades->add( new Upgrade300() ); |
||
88 | |||
89 | /** |
||
90 | * Admin |
||
91 | */ |
||
92 | if ( \is_admin() ) { |
||
93 | 1 | $this->admin = new Admin(); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
94 | 1 | } |
|
95 | |||
96 | /** |
||
97 | 1 | * CLI. |
|
98 | 1 | * |
|
99 | 1 | * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-woocommerce.php#L453-L455 |
|
100 | 1 | */ |
|
101 | 1 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
0 ignored issues
–
show
|
|||
102 | 1 | $this->cli = new CLI(); |
|
0 ignored issues
–
show
|
|||
103 | } |
||
104 | 1 | } |
|
105 | |||
106 | /** |
||
107 | * Plugins loaded. |
||
108 | 1 | * |
|
109 | 1 | * @return void |
|
110 | */ |
||
111 | 1 | public function plugins_loaded() { |
|
112 | 1 | if ( ! $this->get_dependencies()->are_met() ) { |
|
113 | 1 | return; |
|
114 | 1 | } |
|
115 | 1 | ||
116 | // Webhook controller. |
||
117 | 1 | $webhook_controller = new WebhookController(); |
|
118 | 1 | ||
119 | $webhook_controller->setup(); |
||
120 | 1 | } |
|
121 | 1 | ||
122 | 1 | /** |
|
123 | 1 | * Register tables. |
|
124 | * |
||
125 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L894-L937 |
||
126 | */ |
||
127 | private function register_tables() { |
||
128 | 1 | global $wpdb; |
|
129 | 1 | ||
130 | 1 | /** |
|
131 | 1 | * Tables. |
|
132 | */ |
||
133 | 1 | $wpdb->pronamic_pay_mollie_organizations = $wpdb->base_prefix . 'pronamic_pay_mollie_organizations'; |
|
134 | $wpdb->pronamic_pay_mollie_profiles = $wpdb->base_prefix . 'pronamic_pay_mollie_profiles'; |
||
135 | 1 | $wpdb->pronamic_pay_mollie_customers = $wpdb->base_prefix . 'pronamic_pay_mollie_customers'; |
|
136 | $wpdb->pronamic_pay_mollie_customer_users = $wpdb->base_prefix . 'pronamic_pay_mollie_customer_users'; |
||
137 | } |
||
138 | 1 | ||
139 | /** |
||
140 | * Get settings fields. |
||
141 | * |
||
142 | * @return array<int, array<string, array<int, string>|int|string|true>> |
||
143 | */ |
||
144 | public function get_settings_fields() { |
||
145 | $fields = array(); |
||
146 | |||
147 | // API Key. |
||
148 | $fields[] = array( |
||
149 | 'section' => 'general', |
||
150 | 'filter' => FILTER_SANITIZE_STRING, |
||
151 | 'meta_key' => '_pronamic_gateway_mollie_api_key', |
||
152 | 'title' => _x( 'API Key', 'mollie', 'pronamic_ideal' ), |
||
153 | 'type' => 'text', |
||
154 | 'classes' => array( 'regular-text', 'code' ), |
||
155 | 'tooltip' => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ), |
||
156 | ); |
||
157 | |||
158 | // Due date days. |
||
159 | $fields[] = array( |
||
160 | 'section' => 'advanced', |
||
161 | 'filter' => \FILTER_SANITIZE_NUMBER_INT, |
||
162 | 'meta_key' => '_pronamic_gateway_mollie_due_date_days', |
||
163 | 'title' => _x( 'Due date days', 'mollie', 'pronamic_ideal' ), |
||
164 | 'type' => 'number', |
||
165 | 'min' => 1, |
||
166 | 'max' => 100, |
||
167 | 'classes' => array( 'regular-text' ), |
||
168 | 'tooltip' => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ), |
||
169 | 'description' => sprintf( |
||
170 | /* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */ |
||
171 | __( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ), |
||
172 | sprintf( '<code>%s</code>', '1' ), |
||
173 | sprintf( '<code>%s</code>', '100' ), |
||
174 | sprintf( '<code>%s</code>', '12' ) |
||
175 | ), |
||
176 | ); |
||
177 | |||
178 | // Webhook. |
||
179 | $fields[] = array( |
||
180 | 'section' => 'feedback', |
||
181 | 'title' => __( 'Webhook URL', 'pronamic_ideal' ), |
||
182 | 'type' => 'text', |
||
183 | 'classes' => array( 'large-text', 'code' ), |
||
184 | 'value' => rest_url( self::REST_ROUTE_NAMESPACE . '/webhook' ), |
||
185 | 'readonly' => true, |
||
186 | 'tooltip' => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ), |
||
187 | ); |
||
188 | 1 | ||
189 | 1 | return $fields; |
|
190 | } |
||
191 | 1 | ||
192 | /** |
||
193 | * Save post. |
||
194 | * |
||
195 | 1 | * @link https://developer.wordpress.org/reference/functions/get_post_meta/ |
|
196 | 1 | * @param int $post_id Post ID. |
|
197 | * @return void |
||
198 | */ |
||
199 | public function save_post( $post_id ) { |
||
200 | $api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true ); |
||
201 | |||
202 | if ( ! is_string( $api_key ) ) { |
||
203 | return; |
||
204 | } |
||
205 | |||
206 | 2 | $api_key_prefix = substr( $api_key, 0, 4 ); |
|
207 | 2 | ||
208 | switch ( $api_key_prefix ) { |
||
209 | 2 | case 'live': |
|
210 | 2 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE ); |
|
211 | 2 | ||
212 | 2 | return; |
|
213 | case 'test': |
||
214 | 2 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST ); |
|
215 | |||
216 | return; |
||
217 | } |
||
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 |