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 | ||||||
77 | 38 | /** |
|||||
78 | * Get issuers |
||||||
79 | * |
||||||
80 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||||||
81 | */ |
||||||
82 | public function get_issuers() { |
||||||
83 | $groups = array( |
||||||
84 | 2 | array( |
|||||
85 | 2 | 'options' => $this->client->get_issuers(), |
|||||
86 | ), |
||||||
87 | 2 | ); |
|||||
88 | |||||||
89 | 2 | return $groups; |
|||||
90 | 2 | } |
|||||
91 | |||||||
92 | 2 | /** |
|||||
93 | * Get available payment methods. |
||||||
94 | * |
||||||
95 | * @see Core_Gateway::get_available_payment_methods() |
||||||
96 | */ |
||||||
97 | public function get_available_payment_methods() { |
||||||
98 | $payment_methods = array(); |
||||||
99 | |||||||
100 | // Set sequence types to get payment methods for. |
||||||
101 | $sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST ); |
||||||
102 | |||||||
103 | $results = array(); |
||||||
104 | |||||||
105 | foreach ( $sequence_types as $sequence_type ) { |
||||||
106 | // Get active payment methods for Mollie account. |
||||||
107 | 1 | $result = $this->client->get_payment_methods( $sequence_type ); |
|||||
108 | 1 | ||||||
109 | if ( Sequence::FIRST === $sequence_type ) { |
||||||
110 | foreach ( $result as $method => $title ) { |
||||||
111 | 1 | unset( $result[ $method ] ); |
|||||
112 | |||||||
113 | 1 | // Get WordPress payment method for direct debit method. |
|||||
114 | $method = Methods::transform_gateway_method( $method ); |
||||||
115 | 1 | $payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true ); |
|||||
116 | |||||||
117 | 1 | if ( $payment_method ) { |
|||||
118 | $results[ $payment_method ] = $title; |
||||||
119 | 1 | } |
|||||
120 | 1 | } |
|||||
121 | } |
||||||
122 | 1 | ||||||
123 | if ( is_array( $result ) ) { |
||||||
124 | $results = array_merge( $results, $result ); |
||||||
125 | } |
||||||
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 | // Recurring payment method. |
||||||
235 | $is_recurring_method = ( $payment->get_subscription() && PaymentMethods::is_recurring_method( $payment_method ) ); |
||||||
236 | |||||||
237 | if ( false === $is_recurring_method ) { |
||||||
238 | // Always use 'direct debit mandate via iDEAL/Bancontact/Sofort' payment methods as recurring method. |
||||||
239 | $is_recurring_method = PaymentMethods::is_direct_debit_method( $payment_method ); |
||||||
240 | } |
||||||
241 | |||||||
242 | if ( $is_recurring_method ) { |
||||||
243 | $request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST; |
||||||
244 | |||||||
245 | if ( Sequence::FIRST === $request->sequence_type ) { |
||||||
246 | $payment_method = PaymentMethods::get_first_payment_method( $payment_method ); |
||||||
247 | } |
||||||
248 | |||||||
249 | if ( Sequence::RECURRING === $request->sequence_type ) { |
||||||
250 | $payment->set_action_url( $payment->get_return_url() ); |
||||||
251 | } |
||||||
252 | } |
||||||
253 | |||||||
254 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||||||
255 | $request->method = Methods::transform( $payment_method, $payment_method ); |
||||||
256 | |||||||
257 | // Issuer. |
||||||
258 | if ( Methods::IDEAL === $request->method ) { |
||||||
259 | $request->issuer = $payment->get_issuer(); |
||||||
260 | } |
||||||
261 | |||||||
262 | // Create payment. |
||||||
263 | $result = $this->client->create_payment( $request ); |
||||||
264 | |||||||
265 | // Set transaction ID. |
||||||
266 | if ( isset( $result->id ) ) { |
||||||
267 | $payment->set_transaction_id( $result->id ); |
||||||
268 | } |
||||||
269 | |||||||
270 | // Set status. |
||||||
271 | if ( isset( $result->status ) ) { |
||||||
272 | $payment->set_status( Statuses::transform( $result->status ) ); |
||||||
273 | } |
||||||
274 | |||||||
275 | // Set action URL. |
||||||
276 | if ( isset( $result->_links->checkout->href ) ) { |
||||||
277 | $payment->set_action_url( $result->_links->checkout->href ); |
||||||
278 | } |
||||||
279 | } |
||||||
280 | |||||||
281 | /** |
||||||
282 | * Update status of the specified payment |
||||||
283 | * |
||||||
284 | * @param Payment $payment Payment. |
||||||
285 | * |
||||||
286 | * @return void |
||||||
287 | */ |
||||||
288 | public function update_status( Payment $payment ) { |
||||||
289 | $mollie_payment = $this->client->get_payment( $payment->get_transaction_id() ); |
||||||
290 | |||||||
291 | if ( ! $mollie_payment ) { |
||||||
292 | $payment->set_status( Core_Statuses::FAILURE ); |
||||||
293 | |||||||
294 | return; |
||||||
295 | } |
||||||
296 | |||||||
297 | $payment->set_status( Statuses::transform( $mollie_payment->status ) ); |
||||||
298 | |||||||
299 | if ( isset( $mollie_payment->details ) ) { |
||||||
300 | $details = $mollie_payment->details; |
||||||
301 | |||||||
302 | /* |
||||||
303 | * @codingStandardsIgnoreStart |
||||||
304 | * |
||||||
305 | * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
||||||
306 | */ |
||||||
307 | if ( isset( $details->consumerName ) ) { |
||||||
308 | $payment->set_consumer_name( $details->consumerName ); |
||||||
309 | } |
||||||
310 | |||||||
311 | if ( isset( $details->cardHolder ) ) { |
||||||
312 | $payment->set_consumer_name( $details->cardHolder ); |
||||||
313 | } |
||||||
314 | |||||||
315 | if ( isset( $details->consumerAccount ) ) { |
||||||
316 | $payment->set_consumer_iban( $details->consumerAccount ); |
||||||
317 | } |
||||||
318 | |||||||
319 | if ( isset( $details->consumerBic ) ) { |
||||||
320 | $payment->set_consumer_bic( $details->consumerBic ); |
||||||
321 | } |
||||||
322 | // @codingStandardsIgnoreEnd |
||||||
323 | } |
||||||
324 | } |
||||||
325 | |||||||
326 | /** |
||||||
327 | * Get Mollie customer ID for payment. |
||||||
328 | * |
||||||
329 | * @param Payment $payment Payment. |
||||||
330 | * |
||||||
331 | * @return bool|string |
||||||
332 | */ |
||||||
333 | public function get_customer_id_for_payment( Payment $payment ) { |
||||||
334 | // Get WordPress user ID from payment customer. |
||||||
335 | $user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() ); |
||||||
336 | |||||||
337 | // Get Mollie customer ID from user meta. |
||||||
338 | $customer_id = $this->get_customer_id_by_wp_user_id( $user_id ); |
||||||
339 | |||||||
340 | $subscription = $payment->get_subscription(); |
||||||
341 | |||||||
342 | // Get customer ID from subscription meta. |
||||||
343 | if ( $subscription ) { |
||||||
344 | $subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||||||
345 | |||||||
346 | // Try to get (legacy) customer ID from first payment. |
||||||
347 | if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) { |
||||||
348 | $first_payment = $subscription->get_first_payment(); |
||||||
349 | |||||||
350 | 11 | $subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' ); |
|||||
351 | } |
||||||
352 | 11 | ||||||
353 | if ( ! empty( $subscription_customer_id ) ) { |
||||||
354 | $customer_id = $subscription_customer_id; |
||||||
355 | 11 | } |
|||||
356 | } |
||||||
357 | 11 | ||||||
358 | // Create new customer if the customer does not exist at Mollie. |
||||||
359 | if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) { |
||||||
360 | 11 | $customer_name = null; |
|||||
361 | 11 | ||||||
362 | if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) { |
||||||
363 | $customer_name = strval( $payment->get_customer()->get_name() ); |
||||||
364 | 11 | } |
|||||
365 | 7 | ||||||
366 | $customer_id = $this->client->create_customer( $payment->get_email(), $customer_name ); |
||||||
367 | 7 | ||||||
368 | $this->update_wp_user_customer_id( $user_id, $customer_id ); |
||||||
369 | } |
||||||
370 | 11 | ||||||
371 | 5 | // Store customer ID in subscription meta. |
|||||
372 | if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) { |
||||||
373 | $subscription->set_meta( 'mollie_customer_id', $customer_id ); |
||||||
374 | } |
||||||
375 | |||||||
376 | 11 | // Copy customer ID from subscription to user meta. |
|||||
377 | $this->copy_customer_id_to_wp_user( $payment ); |
||||||
378 | |||||||
379 | return $customer_id; |
||||||
380 | } |
||||||
381 | |||||||
382 | /** |
||||||
383 | * Get Mollie customer ID by the specified WordPress user ID. |
||||||
384 | * |
||||||
385 | * @param int $user_id WordPress user ID. |
||||||
386 | * |
||||||
387 | * @return string|bool |
||||||
388 | */ |
||||||
389 | 11 | public function get_customer_id_by_wp_user_id( $user_id ) { |
|||||
390 | if ( empty( $user_id ) ) { |
||||||
391 | return false; |
||||||
392 | } |
||||||
393 | |||||||
394 | 11 | return get_user_meta( $user_id, $this->meta_key_customer_id, true ); |
|||||
395 | } |
||||||
396 | 11 | ||||||
397 | /** |
||||||
398 | * Update Mollie customer ID meta for WordPress user. |
||||||
399 | * |
||||||
400 | * @param int $user_id WordPress user ID. |
||||||
401 | * @param string $customer_id Mollie Customer ID. |
||||||
402 | * |
||||||
403 | * @return bool |
||||||
404 | */ |
||||||
405 | private function update_wp_user_customer_id( $user_id, $customer_id ) { |
||||||
406 | 28 | if ( empty( $user_id ) || is_bool( $user_id ) ) { |
|||||
407 | 28 | return false; |
|||||
408 | 11 | } |
|||||
409 | |||||||
410 | if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) { |
||||||
411 | 17 | return false; |
|||||
412 | } |
||||||
413 | |||||||
414 | update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id ); |
||||||
415 | } |
||||||
416 | |||||||
417 | /** |
||||||
418 | * Copy Mollie customer ID from subscription meta to WordPress user meta. |
||||||
419 | * |
||||||
420 | * @param Payment $payment Payment. |
||||||
421 | * |
||||||
422 | * @return void |
||||||
423 | */ |
||||||
424 | public function copy_customer_id_to_wp_user( Payment $payment ) { |
||||||
425 | if ( $this->config->id !== $payment->config_id ) { |
||||||
426 | return; |
||||||
427 | } |
||||||
428 | |||||||
429 | $subscription = $payment->get_subscription(); |
||||||
430 | |||||||
431 | if ( ! $subscription || empty( $subscription->user_id ) ) { |
||||||
432 | return; |
||||||
433 | } |
||||||
434 | |||||||
435 | // Get customer ID from subscription meta. |
||||||
436 | $customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||||||
437 | |||||||
438 | $user_customer_id = $this->get_customer_id_by_wp_user_id( $subscription->user_id ); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
439 | |||||||
440 | if ( empty( $user_customer_id ) ) { |
||||||
441 | 28 | // Set customer ID as user meta. |
|||||
442 | 28 | $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
![]() |
|||||||
443 | 1 | } |
|||||
444 | } |
||||||
445 | } |
||||||
446 |