1 | <?php |
||||||
2 | /** |
||||||
3 | * Mollie gateway. |
||||||
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 DateInterval; |
||||||
14 | use Pronamic\WordPress\DateTime\DateTime; |
||||||
15 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||||||
16 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||||||
17 | use Pronamic\WordPress\Pay\Core\Recurring as Core_Recurring; |
||||||
18 | use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses; |
||||||
19 | use Pronamic\WordPress\Pay\Payments\Payment; |
||||||
20 | |||||||
21 | /** |
||||||
22 | * Title: Mollie |
||||||
23 | * Description: |
||||||
24 | * Copyright: 2005-2019 Pronamic |
||||||
25 | * Company: Pronamic |
||||||
26 | * |
||||||
27 | * @author Remco Tolsma |
||||||
28 | * @version 2.1.0 |
||||||
29 | * @since 1.1.0 |
||||||
30 | */ |
||||||
31 | class Gateway extends Core_Gateway { |
||||||
32 | /** |
||||||
33 | * Client. |
||||||
34 | * |
||||||
35 | * @var Client |
||||||
36 | */ |
||||||
37 | protected $client; |
||||||
38 | |||||||
39 | /** |
||||||
40 | * Meta key for customer ID. |
||||||
41 | * |
||||||
42 | * @var string |
||||||
43 | */ |
||||||
44 | private $meta_key_customer_id = '_pronamic_pay_mollie_customer_id'; |
||||||
45 | |||||||
46 | /** |
||||||
47 | * Constructs and initializes an Mollie gateway |
||||||
48 | * |
||||||
49 | * @param Config $config Config. |
||||||
50 | */ |
||||||
51 | 38 | public function __construct( Config $config ) { |
|||||
52 | 38 | parent::__construct( $config ); |
|||||
53 | |||||||
54 | 38 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
|||||
55 | |||||||
56 | // Supported features. |
||||||
57 | 38 | $this->supports = array( |
|||||
58 | 'payment_status_request', |
||||||
59 | 'recurring_direct_debit', |
||||||
60 | 'recurring_credit_card', |
||||||
61 | 'recurring', |
||||||
62 | ); |
||||||
63 | |||||||
64 | // Client. |
||||||
65 | 38 | $this->client = new Client( $config->api_key ); |
|||||
66 | 38 | $this->client->set_mode( $config->mode ); |
|||||
67 | |||||||
68 | // Mollie customer ID meta key. |
||||||
69 | 38 | if ( self::MODE_TEST === $config->mode ) { |
|||||
70 | 37 | $this->meta_key_customer_id = '_pronamic_pay_mollie_customer_id_test'; |
|||||
71 | } |
||||||
72 | |||||||
73 | // Actions. |
||||||
74 | 38 | add_action( 'pronamic_payment_status_update', array( $this, 'copy_customer_id_to_wp_user' ), 99, 1 ); |
|||||
75 | |||||||
76 | 38 | add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', array( $this, 'next_payment_delivery_date' ), 10, 2 ); |
|||||
77 | 38 | } |
|||||
78 | |||||||
79 | /** |
||||||
80 | * Get issuers |
||||||
81 | * |
||||||
82 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||||||
83 | */ |
||||||
84 | 2 | public function get_issuers() { |
|||||
85 | 2 | $groups = array( |
|||||
86 | array( |
||||||
87 | 2 | 'options' => $this->client->get_issuers(), |
|||||
88 | ), |
||||||
89 | 2 | ); |
|||||
90 | 2 | ||||||
91 | return $groups; |
||||||
92 | 2 | } |
|||||
93 | |||||||
94 | /** |
||||||
95 | * Get available payment methods. |
||||||
96 | * |
||||||
97 | * @see Core_Gateway::get_available_payment_methods() |
||||||
98 | */ |
||||||
99 | public function get_available_payment_methods() { |
||||||
100 | $payment_methods = array(); |
||||||
101 | |||||||
102 | // Set sequence types to get payment methods for. |
||||||
103 | $sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST ); |
||||||
104 | |||||||
105 | $results = array(); |
||||||
106 | |||||||
107 | 1 | foreach ( $sequence_types as $sequence_type ) { |
|||||
108 | 1 | // Get active payment methods for Mollie account. |
|||||
109 | $result = $this->client->get_payment_methods( $sequence_type ); |
||||||
110 | |||||||
111 | 1 | if ( Sequence::FIRST === $sequence_type ) { |
|||||
112 | foreach ( $result as $method => $title ) { |
||||||
113 | 1 | unset( $result[ $method ] ); |
|||||
114 | |||||||
115 | 1 | // Get WordPress payment method for direct debit method. |
|||||
116 | $method = Methods::transform_gateway_method( $method ); |
||||||
117 | 1 | $payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true ); |
|||||
118 | |||||||
119 | 1 | if ( $payment_method ) { |
|||||
120 | 1 | $results[ $payment_method ] = $title; |
|||||
121 | } |
||||||
122 | 1 | } |
|||||
123 | } |
||||||
124 | |||||||
125 | $results = array_merge( $results, $result ); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
126 | } |
||||||
127 | |||||||
128 | // Transform to WordPress payment methods. |
||||||
129 | foreach ( $results as $method => $title ) { |
||||||
130 | if ( PaymentMethods::is_recurring_method( $method ) ) { |
||||||
131 | $payment_method = $method; |
||||||
132 | } else { |
||||||
133 | $payment_method = Methods::transform_gateway_method( $method ); |
||||||
134 | } |
||||||
135 | |||||||
136 | if ( $payment_method ) { |
||||||
137 | $payment_methods[] = $payment_method; |
||||||
138 | } |
||||||
139 | } |
||||||
140 | |||||||
141 | $payment_methods = array_unique( $payment_methods ); |
||||||
142 | |||||||
143 | 1 | return $payment_methods; |
|||||
144 | } |
||||||
145 | |||||||
146 | /** |
||||||
147 | * Get supported payment methods |
||||||
148 | * |
||||||
149 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||||||
150 | */ |
||||||
151 | public function get_supported_payment_methods() { |
||||||
152 | return array( |
||||||
153 | PaymentMethods::BANCONTACT, |
||||||
154 | PaymentMethods::BANK_TRANSFER, |
||||||
155 | 1 | PaymentMethods::BELFIUS, |
|||||
156 | PaymentMethods::BITCOIN, |
||||||
157 | 1 | PaymentMethods::CREDIT_CARD, |
|||||
158 | PaymentMethods::DIRECT_DEBIT, |
||||||
159 | PaymentMethods::DIRECT_DEBIT_BANCONTACT, |
||||||
160 | PaymentMethods::DIRECT_DEBIT_IDEAL, |
||||||
161 | PaymentMethods::DIRECT_DEBIT_SOFORT, |
||||||
162 | PaymentMethods::EPS, |
||||||
163 | PaymentMethods::GIROPAY, |
||||||
164 | PaymentMethods::IDEAL, |
||||||
165 | 2 | PaymentMethods::KBC, |
|||||
166 | PaymentMethods::PAYPAL, |
||||||
167 | 2 | PaymentMethods::SOFORT, |
|||||
168 | ); |
||||||
169 | } |
||||||
170 | |||||||
171 | /** |
||||||
172 | * Get webhook URL for Mollie. |
||||||
173 | * |
||||||
174 | * @return string |
||||||
175 | */ |
||||||
176 | public function get_webhook_url() { |
||||||
177 | $url = home_url( '/' ); |
||||||
178 | |||||||
179 | $host = wp_parse_url( $url, PHP_URL_HOST ); |
||||||
180 | |||||||
181 | if ( is_array( $host ) ) { |
||||||
182 | // Parsing failure. |
||||||
183 | $host = ''; |
||||||
184 | } |
||||||
185 | |||||||
186 | if ( 'localhost' === $host ) { |
||||||
187 | // Mollie doesn't allow localhost. |
||||||
188 | return null; |
||||||
189 | } elseif ( '.dev' === substr( $host, -4 ) ) { |
||||||
190 | 4 | // Mollie doesn't allow the .dev TLD. |
|||||
191 | 4 | return null; |
|||||
192 | } elseif ( '.local' === substr( $host, -6 ) ) { |
||||||
193 | 4 | // Mollie doesn't allow the .local TLD. |
|||||
194 | return null; |
||||||
195 | 4 | } elseif ( '.test' === substr( $host, -5 ) ) { |
|||||
196 | // Mollie doesn't allow the .test TLD. |
||||||
197 | return null; |
||||||
198 | } |
||||||
199 | |||||||
200 | 4 | $url = add_query_arg( 'mollie_webhook', '', $url ); |
|||||
201 | |||||||
202 | 1 | return $url; |
|||||
203 | 3 | } |
|||||
204 | |||||||
205 | 1 | /** |
|||||
206 | 2 | * Start |
|||||
207 | * |
||||||
208 | 1 | * @see Pronamic_WP_Pay_Gateway::start() |
|||||
209 | 1 | * |
|||||
210 | * @param Payment $payment Payment. |
||||||
211 | */ |
||||||
212 | public function start( Payment $payment ) { |
||||||
213 | $request = new PaymentRequest(); |
||||||
214 | 1 | $request->amount = AmountTransformer::transform( $payment->get_total_amount() ); |
|||||
215 | $request->description = $payment->get_description(); |
||||||
216 | 1 | $request->redirect_url = $payment->get_return_url(); |
|||||
217 | $request->webhook_url = $this->get_webhook_url(); |
||||||
218 | |||||||
219 | // Locale. |
||||||
220 | if ( null !== $payment->get_customer() ) { |
||||||
221 | $request->locale = LocaleHelper::transform( $payment->get_customer()->get_locale() ); |
||||||
222 | } |
||||||
223 | |||||||
224 | // Customer ID. |
||||||
225 | $customer_id = $this->get_customer_id_for_payment( $payment ); |
||||||
226 | |||||||
227 | if ( is_string( $customer_id ) && ! empty( $customer_id ) ) { |
||||||
228 | $request->customer_id = $customer_id; |
||||||
229 | } |
||||||
230 | |||||||
231 | // Payment method. |
||||||
232 | $payment_method = $payment->get_method(); |
||||||
233 | |||||||
234 | // Subscription. |
||||||
235 | $subscription = $payment->get_subscription(); |
||||||
236 | |||||||
237 | if ( $subscription && PaymentMethods::is_recurring_method( $payment_method ) ) { |
||||||
238 | $request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST; |
||||||
239 | |||||||
240 | if ( Sequence::FIRST === $request->sequence_type ) { |
||||||
241 | $payment_method = PaymentMethods::get_first_payment_method( $payment_method ); |
||||||
242 | } |
||||||
243 | |||||||
244 | if ( Sequence::RECURRING === $request->sequence_type ) { |
||||||
245 | $payment->set_action_url( $payment->get_return_url() ); |
||||||
246 | } |
||||||
247 | } |
||||||
248 | |||||||
249 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||||||
250 | $request->method = Methods::transform( $payment_method, $payment_method ); |
||||||
251 | |||||||
252 | // Issuer. |
||||||
253 | if ( Methods::IDEAL === $request->method ) { |
||||||
254 | $request->issuer = $payment->get_issuer(); |
||||||
255 | } |
||||||
256 | |||||||
257 | // Create payment. |
||||||
258 | $result = $this->client->create_payment( $request ); |
||||||
259 | |||||||
260 | // Set transaction ID. |
||||||
261 | if ( isset( $result->id ) ) { |
||||||
262 | $payment->set_transaction_id( $result->id ); |
||||||
263 | } |
||||||
264 | |||||||
265 | // Set status. |
||||||
266 | if ( isset( $result->status ) ) { |
||||||
267 | $payment->set_status( Statuses::transform( $result->status ) ); |
||||||
268 | } |
||||||
269 | |||||||
270 | // Set action URL. |
||||||
271 | if ( isset( $result->_links->checkout->href ) ) { |
||||||
272 | $payment->set_action_url( $result->_links->checkout->href ); |
||||||
273 | } |
||||||
274 | } |
||||||
275 | |||||||
276 | /** |
||||||
277 | * Update status of the specified payment |
||||||
278 | * |
||||||
279 | * @param Payment $payment Payment. |
||||||
280 | * |
||||||
281 | * @return void |
||||||
282 | */ |
||||||
283 | public function update_status( Payment $payment ) { |
||||||
284 | $mollie_payment = $this->client->get_payment( $payment->get_transaction_id() ); |
||||||
285 | |||||||
286 | if ( ! $mollie_payment ) { |
||||||
287 | $payment->set_status( Core_Statuses::FAILURE ); |
||||||
288 | |||||||
289 | return; |
||||||
290 | } |
||||||
291 | |||||||
292 | $payment->set_status( Statuses::transform( $mollie_payment->status ) ); |
||||||
293 | |||||||
294 | if ( isset( $mollie_payment->details ) ) { |
||||||
295 | $details = $mollie_payment->details; |
||||||
296 | |||||||
297 | /* |
||||||
298 | * @codingStandardsIgnoreStart |
||||||
299 | * |
||||||
300 | * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
||||||
301 | */ |
||||||
302 | if ( isset( $details->consumerName ) ) { |
||||||
303 | $payment->set_consumer_name( $details->consumerName ); |
||||||
304 | } |
||||||
305 | |||||||
306 | if ( isset( $details->cardHolder ) ) { |
||||||
307 | $payment->set_consumer_name( $details->cardHolder ); |
||||||
308 | } |
||||||
309 | |||||||
310 | if ( isset( $details->consumerAccount ) ) { |
||||||
311 | $payment->set_consumer_iban( $details->consumerAccount ); |
||||||
312 | } |
||||||
313 | |||||||
314 | if ( isset( $details->consumerBic ) ) { |
||||||
315 | $payment->set_consumer_bic( $details->consumerBic ); |
||||||
316 | } |
||||||
317 | // @codingStandardsIgnoreEnd |
||||||
318 | } |
||||||
319 | } |
||||||
320 | |||||||
321 | /** |
||||||
322 | * Get Mollie customer ID for payment. |
||||||
323 | * |
||||||
324 | * @param Payment $payment Payment. |
||||||
325 | * |
||||||
326 | * @return bool|string |
||||||
327 | */ |
||||||
328 | public function get_customer_id_for_payment( Payment $payment ) { |
||||||
329 | // Get WordPress user ID from payment customer. |
||||||
330 | $user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() ); |
||||||
331 | |||||||
332 | // Get Mollie customer ID from user meta. |
||||||
333 | $customer_id = $this->get_customer_id_by_wp_user_id( $user_id ); |
||||||
334 | |||||||
335 | $subscription = $payment->get_subscription(); |
||||||
336 | |||||||
337 | // Get customer ID from subscription meta. |
||||||
338 | if ( $subscription ) { |
||||||
339 | $subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||||||
340 | |||||||
341 | // Try to get (legacy) customer ID from first payment. |
||||||
342 | if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) { |
||||||
343 | $first_payment = $subscription->get_first_payment(); |
||||||
344 | |||||||
345 | $subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' ); |
||||||
346 | } |
||||||
347 | |||||||
348 | if ( ! empty( $subscription_customer_id ) ) { |
||||||
349 | $customer_id = $subscription_customer_id; |
||||||
350 | 11 | } |
|||||
351 | } |
||||||
352 | 11 | ||||||
353 | // Create new customer if the customer does not exist at Mollie. |
||||||
354 | if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) { |
||||||
355 | 11 | $customer_name = null; |
|||||
356 | |||||||
357 | 11 | if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) { |
|||||
358 | $customer_name = strval( $payment->get_customer()->get_name() ); |
||||||
359 | } |
||||||
360 | 11 | ||||||
361 | 11 | $customer_id = $this->client->create_customer( $payment->get_email(), $customer_name ); |
|||||
362 | |||||||
363 | $this->update_wp_user_customer_id( $user_id, $customer_id ); |
||||||
364 | 11 | } |
|||||
365 | 7 | ||||||
366 | // Store customer ID in subscription meta. |
||||||
367 | 7 | if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) { |
|||||
368 | $subscription->set_meta( 'mollie_customer_id', $customer_id ); |
||||||
369 | } |
||||||
370 | 11 | ||||||
371 | 5 | // Copy customer ID from subscription to user meta. |
|||||
372 | $this->copy_customer_id_to_wp_user( $payment ); |
||||||
373 | |||||||
374 | return $customer_id; |
||||||
375 | } |
||||||
376 | 11 | ||||||
377 | /** |
||||||
378 | * Get Mollie customer ID by the specified WordPress user ID. |
||||||
379 | * |
||||||
380 | * @param int $user_id WordPress user ID. |
||||||
381 | * |
||||||
382 | * @return string|bool |
||||||
383 | */ |
||||||
384 | public function get_customer_id_by_wp_user_id( $user_id ) { |
||||||
385 | if ( empty( $user_id ) ) { |
||||||
386 | return false; |
||||||
387 | } |
||||||
388 | |||||||
389 | 11 | return get_user_meta( $user_id, $this->meta_key_customer_id, true ); |
|||||
390 | } |
||||||
391 | |||||||
392 | /** |
||||||
393 | * Update Mollie customer ID meta for WordPress user. |
||||||
394 | 11 | * |
|||||
395 | * @param int $user_id WordPress user ID. |
||||||
396 | 11 | * @param string $customer_id Mollie Customer ID. |
|||||
397 | * |
||||||
398 | * @return bool |
||||||
399 | */ |
||||||
400 | private function update_wp_user_customer_id( $user_id, $customer_id ) { |
||||||
401 | if ( empty( $user_id ) || is_bool( $user_id ) ) { |
||||||
402 | return false; |
||||||
403 | } |
||||||
404 | |||||||
405 | if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) { |
||||||
406 | 28 | return false; |
|||||
407 | 28 | } |
|||||
408 | 11 | ||||||
409 | update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id ); |
||||||
410 | } |
||||||
411 | 17 | ||||||
412 | /** |
||||||
413 | * Copy Mollie customer ID from subscription meta to WordPress user meta. |
||||||
414 | * |
||||||
415 | * @param Payment $payment Payment. |
||||||
416 | * |
||||||
417 | * @return void |
||||||
418 | */ |
||||||
419 | public function copy_customer_id_to_wp_user( Payment $payment ) { |
||||||
420 | if ( $this->config->id !== $payment->config_id ) { |
||||||
421 | return; |
||||||
422 | } |
||||||
423 | |||||||
424 | $subscription = $payment->get_subscription(); |
||||||
425 | |||||||
426 | if ( ! $subscription || empty( $subscription->user_id ) ) { |
||||||
427 | return; |
||||||
428 | } |
||||||
429 | |||||||
430 | // Get customer ID from subscription meta. |
||||||
431 | $customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||||||
432 | |||||||
433 | $user_customer_id = $this->get_customer_id_by_wp_user_id( $subscription->user_id ); |
||||||
0 ignored issues
–
show
It seems like
$subscription->user_id can also be of type string ; however, parameter $user_id of Pronamic\WordPress\Pay\G...omer_id_by_wp_user_id() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
434 | |||||||
435 | if ( empty( $user_customer_id ) ) { |
||||||
436 | // Set customer ID as user meta. |
||||||
437 | $this->update_wp_user_customer_id( $subscription->user_id, $customer_id ); |
||||||
0 ignored issues
–
show
It seems like
$subscription->user_id can also be of type string ; however, parameter $user_id of Pronamic\WordPress\Pay\G...e_wp_user_customer_id() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
$customer_id can also be of type false ; however, parameter $customer_id of Pronamic\WordPress\Pay\G...e_wp_user_customer_id() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
438 | } |
||||||
439 | } |
||||||
440 | |||||||
441 | 28 | /** |
|||||
442 | 28 | * Next payment delivery date. |
|||||
443 | 1 | * |
|||||
444 | * @param \DateTime $next_payment_delivery_date Next payment delivery date. |
||||||
445 | * @param Payment $payment Payment. |
||||||
446 | 27 | * |
|||||
447 | * @return \DateTime |
||||||
448 | 27 | */ |
|||||
449 | 27 | public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) { |
|||||
450 | // Check gateway. |
||||||
451 | $gateway_id = get_post_meta( $payment->get_config_id(), '_pronamic_gateway_id', true ); |
||||||
452 | |||||||
453 | if ( 'mollie' !== $gateway_id ) { |
||||||
454 | return $next_payment_delivery_date; |
||||||
455 | } |
||||||
456 | |||||||
457 | // Check direct debit payment method. |
||||||
458 | if ( ! PaymentMethods::is_direct_debit_method( $payment->get_method() ) ) { |
||||||
459 | return $next_payment_delivery_date; |
||||||
460 | } |
||||||
461 | |||||||
462 | // Textual representation of the day of the week, Sunday through Saturday. |
||||||
463 | $day_of_week = $next_payment_delivery_date->format( 'l' ); |
||||||
464 | |||||||
465 | switch ( $day_of_week ) { |
||||||
466 | case 'Monday': |
||||||
467 | $next_payment_delivery_date->sub( new DateInterval( 'P3D' ) ); |
||||||
468 | break; |
||||||
469 | |||||||
470 | case 'Saturday': |
||||||
471 | 11 | $next_payment_delivery_date->sub( new DateInterval( 'P2D' ) ); |
|||||
472 | break; |
||||||
473 | 11 | ||||||
474 | case 'Sunday': |
||||||
475 | 11 | $next_payment_delivery_date->sub( new DateInterval( 'P3D' ) ); |
|||||
476 | 11 | break; |
|||||
477 | |||||||
478 | default: |
||||||
479 | $next_payment_delivery_date->sub( new DateInterval( 'P1D' ) ); |
||||||
480 | break; |
||||||
481 | } |
||||||
482 | |||||||
483 | $next_payment_delivery_date->setTime( 0, 0, 0 ); |
||||||
484 | |||||||
485 | return $next_payment_delivery_date; |
||||||
486 | } |
||||||
487 | } |
||||||
488 |