wp-pay-gateways /
mollie
| 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\Payments\PaymentStatus; |
||
| 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.0.8 |
||
| 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( \strval( $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 | 38 | } |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get issuers |
||
| 79 | * |
||
| 80 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
| 81 | */ |
||
| 82 | 2 | public function get_issuers() { |
|
| 83 | 2 | $groups = array(); |
|
| 84 | |||
| 85 | 2 | $result = $this->client->get_issuers(); |
|
| 86 | |||
| 87 | 2 | if ( ! $result ) { |
|
|
0 ignored issues
–
show
|
|||
| 88 | 2 | $this->error = $this->client->get_error(); |
|
| 89 | |||
| 90 | 2 | return $groups; |
|
| 91 | } |
||
| 92 | |||
| 93 | $groups[] = array( |
||
| 94 | 'options' => $result, |
||
| 95 | ); |
||
| 96 | |||
| 97 | return $groups; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get available payment methods. |
||
| 102 | * |
||
| 103 | * @see Core_Gateway::get_available_payment_methods() |
||
| 104 | */ |
||
| 105 | 1 | public function get_available_payment_methods() { |
|
| 106 | 1 | $payment_methods = array(); |
|
| 107 | |||
| 108 | // Set sequence types to get payment methods for. |
||
| 109 | 1 | $sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST ); |
|
| 110 | |||
| 111 | 1 | $results = array(); |
|
| 112 | |||
| 113 | 1 | foreach ( $sequence_types as $sequence_type ) { |
|
| 114 | // Get active payment methods for Mollie account. |
||
| 115 | 1 | $result = $this->client->get_payment_methods( $sequence_type ); |
|
| 116 | |||
| 117 | 1 | if ( ! $result ) { |
|
|
0 ignored issues
–
show
The expression
$result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 118 | 1 | $this->error = $this->client->get_error(); |
|
| 119 | |||
| 120 | 1 | break; |
|
| 121 | } |
||
| 122 | |||
| 123 | if ( Sequence::FIRST === $sequence_type ) { |
||
| 124 | foreach ( $result as $method => $title ) { |
||
| 125 | unset( $result[ $method ] ); |
||
| 126 | |||
| 127 | // Get WordPress payment method for direct debit method. |
||
| 128 | $method = Methods::transform_gateway_method( $method ); |
||
| 129 | $payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true ); |
||
| 130 | |||
| 131 | if ( $payment_method ) { |
||
| 132 | $results[ $payment_method ] = $title; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | if ( is_array( $result ) ) { |
||
| 138 | $results = array_merge( $results, $result ); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | // Transform to WordPress payment methods. |
||
| 143 | 1 | foreach ( $results as $method => $title ) { |
|
| 144 | if ( PaymentMethods::is_recurring_method( $method ) ) { |
||
| 145 | $payment_method = $method; |
||
| 146 | } else { |
||
| 147 | $payment_method = Methods::transform_gateway_method( $method ); |
||
| 148 | } |
||
| 149 | |||
| 150 | if ( $payment_method ) { |
||
| 151 | $payment_methods[] = $payment_method; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | 1 | $payment_methods = array_unique( $payment_methods ); |
|
| 156 | |||
| 157 | 1 | return $payment_methods; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get supported payment methods |
||
| 162 | * |
||
| 163 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||
| 164 | */ |
||
| 165 | 2 | public function get_supported_payment_methods() { |
|
| 166 | return array( |
||
| 167 | 2 | PaymentMethods::BANCONTACT, |
|
| 168 | PaymentMethods::BANK_TRANSFER, |
||
| 169 | PaymentMethods::BELFIUS, |
||
| 170 | PaymentMethods::CREDIT_CARD, |
||
| 171 | PaymentMethods::DIRECT_DEBIT, |
||
| 172 | PaymentMethods::DIRECT_DEBIT_BANCONTACT, |
||
| 173 | PaymentMethods::DIRECT_DEBIT_IDEAL, |
||
| 174 | PaymentMethods::DIRECT_DEBIT_SOFORT, |
||
| 175 | PaymentMethods::EPS, |
||
| 176 | PaymentMethods::GIROPAY, |
||
| 177 | PaymentMethods::IDEAL, |
||
| 178 | PaymentMethods::KBC, |
||
| 179 | PaymentMethods::PAYPAL, |
||
| 180 | PaymentMethods::SOFORT, |
||
| 181 | ); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get webhook URL for Mollie. |
||
| 186 | * |
||
| 187 | * @return string|null |
||
| 188 | */ |
||
| 189 | 4 | public function get_webhook_url() { |
|
| 190 | 4 | $url = home_url( '/' ); |
|
| 191 | |||
| 192 | 4 | $host = wp_parse_url( $url, PHP_URL_HOST ); |
|
| 193 | |||
| 194 | 4 | if ( is_array( $host ) ) { |
|
| 195 | // Parsing failure. |
||
| 196 | $host = ''; |
||
| 197 | } |
||
| 198 | |||
| 199 | 4 | if ( 'localhost' === $host ) { |
|
| 200 | // Mollie doesn't allow localhost. |
||
| 201 | 1 | return null; |
|
| 202 | 3 | } elseif ( '.dev' === substr( $host, -4 ) ) { |
|
| 203 | // Mollie doesn't allow the .dev TLD. |
||
| 204 | 1 | return null; |
|
| 205 | 2 | } elseif ( '.local' === substr( $host, -6 ) ) { |
|
| 206 | // Mollie doesn't allow the .local TLD. |
||
| 207 | 1 | return null; |
|
| 208 | 1 | } elseif ( '.test' === substr( $host, -5 ) ) { |
|
| 209 | // Mollie doesn't allow the .test TLD. |
||
| 210 | return null; |
||
| 211 | } |
||
| 212 | |||
| 213 | 1 | $url = add_query_arg( 'mollie_webhook', '', $url ); |
|
| 214 | |||
| 215 | 1 | return $url; |
|
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Start |
||
| 220 | * |
||
| 221 | * @see Pronamic_WP_Pay_Gateway::start() |
||
| 222 | * |
||
| 223 | * @param Payment $payment Payment. |
||
| 224 | */ |
||
| 225 | public function start( Payment $payment ) { |
||
| 226 | $request = new PaymentRequest(); |
||
| 227 | $request->amount = AmountTransformer::transform( $payment->get_total_amount() ); |
||
| 228 | $request->description = \strval( $payment->get_description() ); |
||
| 229 | $request->redirect_url = $payment->get_return_url(); |
||
| 230 | $request->webhook_url = $this->get_webhook_url(); |
||
| 231 | |||
| 232 | // Locale. |
||
| 233 | if ( null !== $payment->get_customer() ) { |
||
| 234 | $request->locale = LocaleHelper::transform( $payment->get_customer()->get_locale() ); |
||
| 235 | } |
||
| 236 | |||
| 237 | // Customer ID. |
||
| 238 | $customer_id = $this->get_customer_id_for_payment( $payment ); |
||
| 239 | |||
| 240 | if ( is_string( $customer_id ) && ! empty( $customer_id ) ) { |
||
| 241 | $request->customer_id = $customer_id; |
||
| 242 | } |
||
| 243 | |||
| 244 | // Payment method. |
||
| 245 | $payment_method = $payment->get_method(); |
||
| 246 | |||
| 247 | // Recurring payment method. |
||
| 248 | $is_recurring_method = ( $payment->get_subscription() && PaymentMethods::is_recurring_method( $payment_method ) ); |
||
| 249 | |||
| 250 | if ( false === $is_recurring_method ) { |
||
| 251 | // Always use 'direct debit mandate via iDEAL/Bancontact/Sofort' payment methods as recurring method. |
||
| 252 | $is_recurring_method = PaymentMethods::is_direct_debit_method( $payment_method ); |
||
| 253 | } |
||
| 254 | |||
| 255 | if ( $is_recurring_method ) { |
||
| 256 | $request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST; |
||
| 257 | |||
| 258 | if ( Sequence::FIRST === $request->sequence_type ) { |
||
| 259 | $payment_method = PaymentMethods::get_first_payment_method( $payment_method ); |
||
| 260 | } |
||
| 261 | |||
| 262 | if ( Sequence::RECURRING === $request->sequence_type ) { |
||
| 263 | $payment->set_action_url( $payment->get_return_url() ); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||
| 268 | $request->method = Methods::transform( $payment_method, $payment_method ); |
||
| 269 | |||
| 270 | // Issuer. |
||
| 271 | if ( Methods::IDEAL === $request->method ) { |
||
| 272 | $request->issuer = $payment->get_issuer(); |
||
| 273 | } |
||
| 274 | |||
| 275 | // Due date. |
||
| 276 | try { |
||
| 277 | $due_date = new DateTime( sprintf( '+%s days', $this->config->due_date_days ) ); |
||
| 278 | } catch ( \Exception $e ) { |
||
| 279 | $due_date = null; |
||
| 280 | } |
||
| 281 | |||
| 282 | $request->set_due_date( $due_date ); |
||
| 283 | |||
| 284 | // Create payment. |
||
| 285 | $result = $this->client->create_payment( $request ); |
||
| 286 | |||
| 287 | // Set transaction ID. |
||
| 288 | if ( isset( $result->id ) ) { |
||
| 289 | $payment->set_transaction_id( $result->id ); |
||
| 290 | } |
||
| 291 | |||
| 292 | // Set status. |
||
| 293 | if ( isset( $result->status ) ) { |
||
| 294 | $payment->set_status( Statuses::transform( $result->status ) ); |
||
| 295 | } |
||
| 296 | |||
| 297 | // Set transfer reference. |
||
| 298 | if ( isset( $result->details ) ) { |
||
| 299 | if ( isset( $result->details->transferReference ) ) { |
||
| 300 | $payment->set_meta( 'mollie_transfer_reference', $result->details->transferReference ); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | // Set action URL. |
||
| 305 | if ( isset( $result->_links ) ) { |
||
| 306 | if ( isset( $result->_links->checkout->href ) ) { |
||
| 307 | $payment->set_action_url( $result->_links->checkout->href ); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Update status of the specified payment |
||
| 314 | * |
||
| 315 | * @param Payment $payment Payment. |
||
| 316 | * |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function update_status( Payment $payment ) { |
||
| 320 | $transaction_id = $payment->get_transaction_id(); |
||
| 321 | |||
| 322 | if ( null === $transaction_id ) { |
||
| 323 | return; |
||
| 324 | } |
||
| 325 | |||
| 326 | $mollie_payment = $this->client->get_payment( $transaction_id ); |
||
| 327 | |||
| 328 | if ( isset( $mollie_payment->status ) ) { |
||
| 329 | $payment->set_status( Statuses::transform( $mollie_payment->status ) ); |
||
| 330 | } |
||
| 331 | |||
| 332 | if ( isset( $mollie_payment->details ) ) { |
||
| 333 | $details = $mollie_payment->details; |
||
| 334 | |||
| 335 | /* |
||
| 336 | * @codingStandardsIgnoreStart |
||
| 337 | * |
||
| 338 | * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
||
| 339 | */ |
||
| 340 | if ( isset( $details->consumerName ) ) { |
||
| 341 | $payment->set_consumer_name( $details->consumerName ); |
||
| 342 | } |
||
| 343 | |||
| 344 | if ( isset( $details->cardHolder ) ) { |
||
| 345 | $payment->set_consumer_name( $details->cardHolder ); |
||
| 346 | } |
||
| 347 | |||
| 348 | if ( isset( $details->consumerAccount ) ) { |
||
| 349 | $payment->set_consumer_iban( $details->consumerAccount ); |
||
| 350 | } |
||
| 351 | |||
| 352 | if ( isset( $details->consumerBic ) ) { |
||
| 353 | $payment->set_consumer_bic( $details->consumerBic ); |
||
| 354 | } |
||
| 355 | // @codingStandardsIgnoreEnd |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get Mollie customer ID for payment. |
||
| 361 | * |
||
| 362 | * @param Payment $payment Payment. |
||
| 363 | * |
||
| 364 | * @return bool|string |
||
| 365 | */ |
||
| 366 | public function get_customer_id_for_payment( Payment $payment ) { |
||
| 367 | // Get WordPress user ID from payment customer. |
||
| 368 | 11 | $user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() ); |
|
| 369 | |||
| 370 | 11 | // Get Mollie customer ID from user meta. |
|
| 371 | $customer_id = $this->get_customer_id_by_wp_user_id( $user_id ); |
||
| 372 | |||
| 373 | 11 | $subscription = $payment->get_subscription(); |
|
| 374 | |||
| 375 | 11 | // Get customer ID from subscription meta. |
|
| 376 | if ( $subscription ) { |
||
| 377 | $subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||
| 378 | 11 | ||
| 379 | 11 | // Try to get (legacy) customer ID from first payment. |
|
| 380 | if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) { |
||
| 381 | $first_payment = $subscription->get_first_payment(); |
||
| 382 | 11 | ||
| 383 | 7 | $subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' ); |
|
| 384 | } |
||
| 385 | 7 | ||
| 386 | if ( ! empty( $subscription_customer_id ) ) { |
||
| 387 | $customer_id = $subscription_customer_id; |
||
| 388 | 11 | } |
|
| 389 | 5 | } |
|
| 390 | |||
| 391 | // Create new customer if the customer does not exist at Mollie. |
||
| 392 | if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) { |
||
| 393 | $customer_name = null; |
||
| 394 | 11 | ||
| 395 | if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) { |
||
| 396 | $customer_name = strval( $payment->get_customer()->get_name() ); |
||
| 397 | } |
||
| 398 | |||
| 399 | $customer_id = $this->client->create_customer( $payment->get_email(), $customer_name ); |
||
| 400 | |||
| 401 | $this->update_wp_user_customer_id( $user_id, $customer_id ); |
||
| 402 | } |
||
| 403 | |||
| 404 | // Store customer ID in subscription meta. |
||
| 405 | if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) { |
||
| 406 | $subscription->set_meta( 'mollie_customer_id', $customer_id ); |
||
| 407 | 11 | } |
|
| 408 | |||
| 409 | // Copy customer ID from subscription to user meta. |
||
| 410 | $this->copy_customer_id_to_wp_user( $payment ); |
||
| 411 | |||
| 412 | 11 | return $customer_id; |
|
| 413 | } |
||
| 414 | 11 | ||
| 415 | /** |
||
| 416 | * Get Mollie customer ID by the specified WordPress user ID. |
||
| 417 | * |
||
| 418 | * @param int $user_id WordPress user ID. |
||
| 419 | * |
||
| 420 | * @return string|bool |
||
| 421 | */ |
||
| 422 | public function get_customer_id_by_wp_user_id( $user_id ) { |
||
| 423 | if ( empty( $user_id ) ) { |
||
| 424 | 28 | return false; |
|
| 425 | 28 | } |
|
| 426 | 11 | ||
| 427 | return get_user_meta( $user_id, $this->meta_key_customer_id, true ); |
||
| 428 | } |
||
| 429 | 17 | ||
| 430 | /** |
||
| 431 | * Update Mollie customer ID meta for WordPress user. |
||
| 432 | * |
||
| 433 | * @param int $user_id WordPress user ID. |
||
| 434 | * @param string $customer_id Mollie Customer ID. |
||
| 435 | * |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | private function update_wp_user_customer_id( $user_id, $customer_id ) { |
||
| 439 | if ( empty( $user_id ) || is_bool( $user_id ) ) { |
||
| 440 | return false; |
||
| 441 | } |
||
| 442 | |||
| 443 | if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) { |
||
| 444 | return false; |
||
| 445 | } |
||
| 446 | |||
| 447 | update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id ); |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Copy Mollie customer ID from subscription meta to WordPress user meta. |
||
| 452 | * |
||
| 453 | * @param Payment $payment Payment. |
||
| 454 | * |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | public function copy_customer_id_to_wp_user( Payment $payment ) { |
||
| 458 | if ( $this->config->id !== $payment->config_id ) { |
||
| 459 | 28 | return; |
|
| 460 | 28 | } |
|
| 461 | 1 | ||
| 462 | $subscription = $payment->get_subscription(); |
||
| 463 | |||
| 464 | 27 | if ( ! $subscription || empty( $subscription->user_id ) ) { |
|
| 465 | return; |
||
| 466 | 27 | } |
|
| 467 | 27 | ||
| 468 | // Get customer ID from subscription meta. |
||
| 469 | $customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||
| 470 | |||
| 471 | $user_customer_id = $this->get_customer_id_by_wp_user_id( $subscription->user_id ); |
||
| 472 | |||
| 473 | if ( empty( $user_customer_id ) ) { |
||
| 474 | // Set customer ID as user meta. |
||
| 475 | $this->update_wp_user_customer_id( $subscription->user_id, $customer_id ); |
||
| 476 | } |
||
| 477 | } |
||
| 478 | } |
||
| 479 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.