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 | 'version' => '3.0.0', |
||
55 | 'url' => 'http://www.mollie.com/en/', |
||
56 | 'product_url' => \__( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' ), |
||
57 | 7 | 'dashboard_url' => 'https://www.mollie.com/dashboard/', |
|
58 | 'provider' => 'mollie', |
||
59 | 'supports' => array( |
||
60 | 7 | 'payment_status_request', |
|
61 | 'recurring_direct_debit', |
||
62 | 7 | 'recurring_credit_card', |
|
63 | 7 | 'recurring', |
|
64 | 'webhook', |
||
65 | 'webhook_log', |
||
66 | 7 | 'webhook_no_config', |
|
67 | ), |
||
68 | 'version_option_name' => 'pronamic_pay_mollie_version', |
||
69 | 'db_version_option_name' => 'pronamic_pay_mollie_db_version', |
||
70 | ) |
||
71 | ); |
||
72 | |||
73 | parent::__construct( $args ); |
||
74 | |||
75 | // Filters. |
||
76 | $function = array( $this, 'next_payment_delivery_date' ); |
||
77 | |||
78 | if ( ! \has_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function ) ) { |
||
79 | 7 | \add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function, 10, 2 ); |
|
80 | } |
||
81 | 7 | ||
82 | 7 | add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 ); |
|
83 | |||
84 | // Tables. |
||
85 | 7 | $this->register_tables(); |
|
86 | 7 | ||
87 | /** |
||
88 | * Install. |
||
89 | */ |
||
90 | $this->install = new Install( $this ); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
91 | |||
92 | /** |
||
93 | 1 | * Admin |
|
94 | 1 | */ |
|
95 | if ( \is_admin() ) { |
||
96 | $this->admin = new Admin(); |
||
0 ignored issues
–
show
|
|||
97 | 1 | } |
|
98 | 1 | ||
99 | 1 | /** |
|
100 | 1 | * CLI. |
|
101 | 1 | * |
|
102 | 1 | * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-woocommerce.php#L453-L455 |
|
103 | */ |
||
104 | 1 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
0 ignored issues
–
show
|
|||
105 | $this->cli = new CLI(); |
||
0 ignored issues
–
show
|
|||
106 | } |
||
107 | } |
||
108 | 1 | ||
109 | 1 | /** |
|
110 | * Plugins loaded. |
||
111 | 1 | * |
|
112 | 1 | * @return void |
|
113 | 1 | */ |
|
114 | 1 | public function plugins_loaded() { |
|
115 | 1 | if ( ! $this->get_dependencies()->are_met() ) { |
|
116 | return; |
||
117 | 1 | } |
|
118 | 1 | ||
119 | // Webhook controller. |
||
120 | 1 | $webhook_controller = new WebhookController(); |
|
121 | 1 | ||
122 | 1 | $webhook_controller->setup(); |
|
123 | 1 | } |
|
124 | |||
125 | /** |
||
126 | * Register tables. |
||
127 | * |
||
128 | 1 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L894-L937 |
|
129 | 1 | */ |
|
130 | 1 | private function register_tables() { |
|
131 | 1 | global $wpdb; |
|
132 | |||
133 | 1 | /** |
|
134 | * Tables. |
||
135 | 1 | */ |
|
136 | $wpdb->pronamic_pay_mollie_organizations = $wpdb->base_prefix . 'pronamic_pay_mollie_organizations'; |
||
137 | $wpdb->pronamic_pay_mollie_profiles = $wpdb->base_prefix . 'pronamic_pay_mollie_profiles'; |
||
138 | 1 | $wpdb->pronamic_pay_mollie_customers = $wpdb->base_prefix . 'pronamic_pay_mollie_customers'; |
|
139 | $wpdb->pronamic_pay_mollie_customer_users = $wpdb->base_prefix . 'pronamic_pay_mollie_customer_users'; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Get settings fields. |
||
144 | * |
||
145 | * @return array<int, array<string, array<int, string>|int|string|true>> |
||
146 | */ |
||
147 | public function get_settings_fields() { |
||
148 | $fields = array(); |
||
149 | |||
150 | // API Key. |
||
151 | $fields[] = array( |
||
152 | 'section' => 'general', |
||
153 | 'filter' => FILTER_SANITIZE_STRING, |
||
154 | 'meta_key' => '_pronamic_gateway_mollie_api_key', |
||
155 | 'title' => _x( 'API Key', 'mollie', 'pronamic_ideal' ), |
||
156 | 'type' => 'text', |
||
157 | 'classes' => array( 'regular-text', 'code' ), |
||
158 | 'tooltip' => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ), |
||
159 | ); |
||
160 | |||
161 | // Due date days. |
||
162 | $fields[] = array( |
||
163 | 'section' => 'advanced', |
||
164 | 'filter' => \FILTER_SANITIZE_NUMBER_INT, |
||
165 | 'meta_key' => '_pronamic_gateway_mollie_due_date_days', |
||
166 | 'title' => _x( 'Due date days', 'mollie', 'pronamic_ideal' ), |
||
167 | 'type' => 'number', |
||
168 | 'min' => 1, |
||
169 | 'max' => 100, |
||
170 | 'classes' => array( 'regular-text' ), |
||
171 | 'tooltip' => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ), |
||
172 | 'description' => sprintf( |
||
173 | /* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */ |
||
174 | __( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ), |
||
175 | sprintf( '<code>%s</code>', '1' ), |
||
176 | sprintf( '<code>%s</code>', '100' ), |
||
177 | sprintf( '<code>%s</code>', '12' ) |
||
178 | ), |
||
179 | ); |
||
180 | |||
181 | // Webhook. |
||
182 | $fields[] = array( |
||
183 | 'section' => 'feedback', |
||
184 | 'title' => __( 'Webhook URL', 'pronamic_ideal' ), |
||
185 | 'type' => 'text', |
||
186 | 'classes' => array( 'large-text', 'code' ), |
||
187 | 'value' => rest_url( self::REST_ROUTE_NAMESPACE . '/webhook' ), |
||
188 | 1 | 'readonly' => true, |
|
189 | 1 | 'tooltip' => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ), |
|
190 | ); |
||
191 | 1 | ||
192 | return $fields; |
||
193 | } |
||
194 | |||
195 | 1 | /** |
|
196 | 1 | * Save post. |
|
197 | * |
||
198 | * @link https://developer.wordpress.org/reference/functions/get_post_meta/ |
||
199 | * @param int $post_id Post ID. |
||
200 | * @return void |
||
201 | */ |
||
202 | public function save_post( $post_id ) { |
||
203 | $api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true ); |
||
204 | |||
205 | if ( ! is_string( $api_key ) ) { |
||
206 | 2 | return; |
|
207 | 2 | } |
|
208 | |||
209 | 2 | $api_key_prefix = substr( $api_key, 0, 4 ); |
|
210 | 2 | ||
211 | 2 | switch ( $api_key_prefix ) { |
|
212 | 2 | case 'live': |
|
213 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE ); |
||
214 | 2 | ||
215 | return; |
||
216 | case 'test': |
||
217 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST ); |
||
218 | |||
219 | return; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | 1 | /** |
|
224 | 1 | * Payment provider URL. |
|
225 | * |
||
226 | * @param string|null $url Payment provider URL. |
||
227 | * @param Payment $payment Payment. |
||
228 | * @return string|null |
||
229 | */ |
||
230 | public function payment_provider_url( $url, Payment $payment ) { |
||
231 | $transaction_id = $payment->get_transaction_id(); |
||
232 | |||
233 | if ( null === $transaction_id ) { |
||
234 | return $url; |
||
235 | } |
||
236 | |||
237 | return sprintf( |
||
238 | 'https://www.mollie.com/dashboard/payments/%s', |
||
239 | $transaction_id |
||
240 | ); |
||
241 | } |
||
242 | /** |
||
243 | * Get configuration by post ID. |
||
244 | * |
||
245 | * @param int $post_id Post ID. |
||
246 | * @return Config |
||
247 | */ |
||
248 | public function get_config( $post_id ) { |
||
249 | $config = new Config(); |
||
250 | |||
251 | $config->id = intval( $post_id ); |
||
252 | $config->api_key = $this->get_meta( $post_id, 'mollie_api_key' ); |
||
253 | $config->mode = $this->get_meta( $post_id, 'mode' ); |
||
254 | $config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' ); |
||
255 | |||
256 | return $config; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get gateway. |
||
261 | * |
||
262 | * @param int $post_id Post ID. |
||
263 | * @return Gateway |
||
264 | */ |
||
265 | public function get_gateway( $post_id ) { |
||
266 | return new Gateway( $this->get_config( $post_id ) ); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Next payment delivery date. |
||
271 | * |
||
272 | * @param \DateTime $next_payment_delivery_date Next payment delivery date. |
||
273 | * @param Payment $payment Payment. |
||
274 | * @return \DateTime |
||
275 | */ |
||
276 | public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) { |
||
277 | $config_id = $payment->get_config_id(); |
||
278 | |||
279 | if ( null === $config_id ) { |
||
280 | return $next_payment_delivery_date; |
||
281 | } |
||
282 | |||
283 | // Check gateway. |
||
284 | $gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true ); |
||
285 | |||
286 | if ( 'mollie' !== $gateway_id ) { |
||
287 | return $next_payment_delivery_date; |
||
288 | } |
||
289 | |||
290 | // Check direct debit payment method. |
||
291 | $method = $payment->get_method(); |
||
292 | |||
293 | if ( null === $method ) { |
||
294 | return $next_payment_delivery_date; |
||
295 | } |
||
296 | |||
297 | if ( ! PaymentMethods::is_direct_debit_method( $method ) ) { |
||
298 | return $next_payment_delivery_date; |
||
299 | } |
||
300 | |||
301 | // Check subscription. |
||
302 | $subscription = $payment->get_subscription(); |
||
303 | |||
304 | if ( null === $subscription ) { |
||
305 | return $next_payment_delivery_date; |
||
306 | } |
||
307 | |||
308 | // Base delivery date on next payment date. |
||
309 | $next_payment_date = $subscription->get_next_payment_date(); |
||
310 | |||
311 | if ( null === $next_payment_date ) { |
||
312 | return $next_payment_delivery_date; |
||
313 | } |
||
314 | |||
315 | $next_payment_delivery_date = clone $next_payment_date; |
||
316 | |||
317 | // Textual representation of the day of the week, Sunday through Saturday. |
||
318 | $day_of_week = $next_payment_delivery_date->format( 'l' ); |
||
319 | |||
320 | /* |
||
321 | * Subtract days from next payment date for earlier delivery. |
||
322 | * |
||
323 | * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out- |
||
324 | * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times- |
||
325 | */ |
||
326 | switch ( $day_of_week ) { |
||
327 | case 'Monday': |
||
328 | $next_payment_delivery_date->modify( '-3 days' ); |
||
329 | |||
330 | break; |
||
331 | case 'Saturday': |
||
332 | $next_payment_delivery_date->modify( '-2 days' ); |
||
333 | |||
334 | break; |
||
335 | case 'Sunday': |
||
336 | $next_payment_delivery_date->modify( '-3 days' ); |
||
337 | |||
338 | break; |
||
339 | default: |
||
340 | $next_payment_delivery_date->modify( '-1 day' ); |
||
341 | |||
342 | break; |
||
343 | } |
||
344 | |||
345 | $next_payment_delivery_date->setTime( 0, 0, 0 ); |
||
346 | |||
347 | return $next_payment_delivery_date; |
||
348 | } |
||
349 | } |
||
350 |